summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2014-08-08 10:20:08 -0400
committerTim Graham <timograham@gmail.com>2014-08-20 14:39:40 -0400
commit0d8d30b7ddfe83ab03120f4560c7aa153f4d0ed1 (patch)
tree36523aca0c14222c8b30241e2e0a8fe3a8af511c /django
parent28e765810df46a3f28ff4785491e9973593382fd (diff)
Fixed #23157 -- Removed O(n) algorithm when uploading duplicate file names.
This is a security fix. Disclosure following shortly.
Diffstat (limited to 'django')
-rw-r--r--django/core/files/storage.py11
1 files changed, 5 insertions, 6 deletions
diff --git a/django/core/files/storage.py b/django/core/files/storage.py
index a5cded72ac..cff2d06f1a 100644
--- a/django/core/files/storage.py
+++ b/django/core/files/storage.py
@@ -1,12 +1,12 @@
import os
import errno
-import itertools
from datetime import datetime
from django.conf import settings
from django.core.exceptions import SuspiciousFileOperation
from django.core.files import locks, File
from django.core.files.move import file_move_safe
+from django.utils.crypto import get_random_string
from django.utils.encoding import force_text, filepath_to_uri
from django.utils.functional import LazyObject
from django.utils.module_loading import import_string
@@ -69,13 +69,12 @@ class Storage(object):
"""
dir_name, file_name = os.path.split(name)
file_root, file_ext = os.path.splitext(file_name)
- # If the filename already exists, add an underscore and a number (before
- # the file extension, if one exists) to the filename until the generated
- # filename doesn't exist.
- count = itertools.count(1)
+ # If the filename already exists, add an underscore and a random 7
+ # character alphanumeric string (before the file extension, if one
+ # exists) to the filename until the generated filename doesn't exist.
while self.exists(name):
# file_ext includes the dot.
- name = os.path.join(dir_name, "%s_%s%s" % (file_root, next(count), file_ext))
+ name = os.path.join(dir_name, "%s_%s%s" % (file_root, get_random_string(7), file_ext))
return name