diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-03-16 10:19:00 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-04-06 08:33:16 +0200 |
| commit | e7fba62248f604c76da4f23dcf1db4a57b0808ea (patch) | |
| tree | 3275a467085c0a34b82592da37332bc556728bf6 /django | |
| parent | 232d5f61e6afd9cd6f10a47ddb4375f86818717e (diff) | |
[3.0.x] Fixed CVE-2021-28658 -- Fixed potential directory-traversal via uploaded files.
Thanks Claude Paroz for the initial patch.
Thanks Dennis Brinkrolf for the report.
Backport of d4d800ca1addc4141e03c5440a849bb64d1582cd from main.
Diffstat (limited to 'django')
| -rw-r--r-- | django/http/multipartparser.py | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py index fd8fce8b4d..db1b5ce8b9 100644 --- a/django/http/multipartparser.py +++ b/django/http/multipartparser.py @@ -9,6 +9,7 @@ import binascii import cgi import collections import html +import os from urllib.parse import unquote from django.conf import settings @@ -209,7 +210,7 @@ class MultiPartParser: file_name = disposition.get('filename') if file_name: file_name = force_str(file_name, encoding, errors='replace') - file_name = self.IE_sanitize(html.unescape(file_name)) + file_name = self.sanitize_file_name(file_name) if not file_name: continue @@ -297,9 +298,13 @@ class MultiPartParser: self._files.appendlist(force_str(old_field_name, self._encoding, errors='replace'), file_obj) break - def IE_sanitize(self, filename): - """Cleanup filename from Internet Explorer full paths.""" - return filename and filename[filename.rfind("\\") + 1:].strip() + def sanitize_file_name(self, file_name): + file_name = html.unescape(file_name) + # Cleanup Windows-style path separators. + file_name = file_name[file_name.rfind('\\') + 1:].strip() + return os.path.basename(file_name) + + IE_sanitize = sanitize_file_name def _close_files(self): # Free up all file handles. |
