Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible removal of directories with path traversal #201

Merged
merged 5 commits into from
Feb 5, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check if folder to delete is subpath of data directory
  • Loading branch information
sqrtroot committed Feb 4, 2021
commit 687bd4a947d7c1d90288ae3022cc6ee8e03889e0
16 changes: 15 additions & 1 deletion archivy/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ def get_data_dir():
"""Returns the directory where dataobjs are stored"""
return Path(current_app.config['USER_DIR']) / "data"

def is_relative_to(a, b):
try:
a.relative_to(b)
return True
except ValueError:
return False

def is_sub_datadir(path: Path):
#Not implemented in python < 3.8
#return path.resolve().is_relative_to(get_data_dir().resolve())
return is_relative_to(path.resolve(), get_data_dir().resolve())

class Directory:
"""Tree like file-structure used to build file navigation in Archiv"""
Expand Down Expand Up @@ -195,8 +206,11 @@ def create_dir(name):

def delete_dir(name):
"""Deletes dir of given name"""
delete_dir = get_data_dir() / name
if not is_sub_datadir(delete_dir):
return False
try:
rmtree(get_data_dir() / name.strip("/"))
rmtree(delete_dir)
return True
except FileNotFoundError:
return False
Expand Down