diff options
author | Florian Apolloner <florian@apolloner.eu> | 2021-04-14 18:23:44 +0200 |
---|---|---|
committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2021-05-04 08:44:42 +0200 |
commit | 0b79eb36915d178aef5c6a7bbce71b1e76d376d3 (patch) | |
tree | ceb3f3df98ca1ee553f793121b6e43dc67ee2607 /django/http/multipartparser.py | |
parent | 8de4ca74ba49b3f97a252e2b9d385cb2e70c442c (diff) | |
download | django-0b79eb36915d178aef5c6a7bbce71b1e76d376d3.tar.gz |
Fixed CVE-2021-31542 -- Tightened path & file name sanitation in file uploads.
Diffstat (limited to 'django/http/multipartparser.py')
-rw-r--r-- | django/http/multipartparser.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py index 180a533bb6..f464caa1b4 100644 --- a/django/http/multipartparser.py +++ b/django/http/multipartparser.py @@ -9,7 +9,6 @@ import binascii import cgi import collections import html -import os from urllib.parse import unquote from django.conf import settings @@ -306,10 +305,25 @@ class MultiPartParser: break def sanitize_file_name(self, file_name): + """ + Sanitize the filename of an upload. + + Remove all possible path separators, even though that might remove more + than actually required by the target system. Filenames that could + potentially cause problems (current/parent dir) are also discarded. + + It should be noted that this function could still return a "filepath" + like "C:some_file.txt" which is handled later on by the storage layer. + So while this function does sanitize filenames to some extent, the + resulting filename should still be considered as untrusted user input. + """ 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) + file_name = file_name.rsplit('/')[-1] + file_name = file_name.rsplit('\\')[-1] + + if file_name in {'', '.', '..'}: + return None + return file_name IE_sanitize = sanitize_file_name |