summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2021-03-16 10:19:00 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-04-06 08:25:24 +0200
commitcca0d98118cccf9ae0c6dcf2d6c57fc50469fbf0 (patch)
tree4ce9e5d999d73ed3a51d28a16d2c288a43a89e60 /django
parent6eb01cb0521a09003c42a3ab3ad7503ecede36be (diff)
[3.1.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.py13
1 files changed, 8 insertions, 5 deletions
diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py
index b3472f7be2..2351055e3a 100644
--- a/django/http/multipartparser.py
+++ b/django/http/multipartparser.py
@@ -209,9 +209,8 @@ class MultiPartParser:
# This is a file, use the handler...
file_name = disposition.get('filename')
if file_name:
- file_name = os.path.basename(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
@@ -299,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.