Skip to content

Commit

Permalink
Add function is_valid_upload_path to fix crafted filepaths to escape …
Browse files Browse the repository at this point in the history
…the base directory. Close #2.
  • Loading branch information
sc0tfree committed Feb 19, 2020
1 parent ed0f911 commit 1fe14fb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions updog/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.serving import run_simple

from updog.utils.path import is_valid_subpath, get_parent_directory, process_files
from updog.utils.path import is_valid_subpath, is_valid_upload_path, get_parent_directory, process_files
from updog.utils.output import error, info, warn, success
from updog import version as VERSION

Expand Down Expand Up @@ -129,7 +129,7 @@ def upload():

path = request.form['path']
# Prevent file upload to paths outside of base directory
if not is_valid_subpath(path, base_directory) or path == '':
if not is_valid_upload_path(path, base_directory):
return redirect(request.referrer)

for file in request.files.getlist('file'):
Expand Down
7 changes: 7 additions & 0 deletions updog/utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ def is_valid_subpath(relative_directory, base_directory):
return os.path.commonprefix([base_directory, in_question]) == base_directory


def is_valid_upload_path(path, base_directory):
if path == '':
return False
in_question = os.path.abspath(path)
return os.path.commonprefix([base_directory, in_question]) == base_directory


def get_relative_path(file_path, base_directory):
return file_path.split(os.path.commonprefix([base_directory, file_path]))[1][1:]

Expand Down

0 comments on commit 1fe14fb

Please sign in to comment.