Skip to content

Commit

Permalink
Prevent upward path traversals when serving protected media (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsalvino committed Sep 29, 2021
1 parent acc37d2 commit 06006ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions coderedcms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ def serve_protected_file(request, path):
"""
Function that serves protected files uploaded from forms.
"""
fullpath = os.path.join(cr_settings['PROTECTED_MEDIA_ROOT'], path)
if os.path.isfile(fullpath):
# Fully resolve all provided paths.
mediapath = os.path.abspath(cr_settings['PROTECTED_MEDIA_ROOT'])
fullpath = os.path.abspath(os.path.join(mediapath, path))

# Path must be a sub-path of the PROTECTED_MEDIA_ROOT, and exist.
if fullpath.startswith(mediapath) and os.path.isfile(fullpath):
mimetype, encoding = mimetypes.guess_type(fullpath)
with open(fullpath, 'rb') as f:
response = HttpResponse(f.read(), content_type=mimetype)
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/v0.22.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ Bug fixes
* Form submission emails were broken on custom form pages which override
``get_form_fields()`` and do not return fields as classes inheriting
``AbstractFormField`` (Bug was introduced in 0.22.2).

* Prevent upward path traversals outside of ``CODERED_PROTECTED_MEDIA_ROOT``
when serving protected media files. This only applies to logged in users,
anonymous users do not have permission to access this URL.

0 comments on commit 06006ce

Please sign in to comment.