summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2012-11-29 16:36:43 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2012-11-29 16:38:09 +0100
commit83df1f3b571667f3d625c95af745c6ff687ef78e (patch)
tree5c15dde5a4ff7ef6a28fcaf1c88916178056545b
parentfe55052cca019371de712fde9daef6c1cc518a8a (diff)
[1.5.x] Fixed #19356 -- Increased session key entropy.
Backport of d913a8b from master.
-rw-r--r--django/contrib/sessions/backends/base.py11
-rw-r--r--django/contrib/sessions/backends/file.py6
2 files changed, 8 insertions, 9 deletions
diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py
index ff8ab7f677..f79a264500 100644
--- a/django/contrib/sessions/backends/base.py
+++ b/django/contrib/sessions/backends/base.py
@@ -6,6 +6,7 @@ try:
from django.utils.six.moves import cPickle as pickle
except ImportError:
import pickle
+import string
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
@@ -15,6 +16,10 @@ from django.utils.crypto import salted_hmac
from django.utils import timezone
from django.utils.encoding import force_bytes
+# session_key should not be case sensitive because some backends can store it
+# on case insensitive file systems.
+VALID_KEY_CHARS = string.ascii_lowercase + string.digits
+
class CreateError(Exception):
"""
Used internally as a consistent exception type to catch from save (see the
@@ -132,12 +137,8 @@ class SessionBase(object):
def _get_new_session_key(self):
"Returns session key that isn't being used."
- # Todo: move to 0-9a-z charset in 1.5
- hex_chars = '1234567890abcdef'
- # session_key should not be case sensitive because some backends
- # can store it on case insensitive file systems.
while True:
- session_key = get_random_string(32, hex_chars)
+ session_key = get_random_string(32, VALID_KEY_CHARS)
if not self.exists(session_key):
break
return session_key
diff --git a/django/contrib/sessions/backends/file.py b/django/contrib/sessions/backends/file.py
index 401c90cf74..7d933c678a 100644
--- a/django/contrib/sessions/backends/file.py
+++ b/django/contrib/sessions/backends/file.py
@@ -4,7 +4,7 @@ import os
import tempfile
from django.conf import settings
-from django.contrib.sessions.backends.base import SessionBase, CreateError
+from django.contrib.sessions.backends.base import SessionBase, CreateError, VALID_KEY_CHARS
from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
from django.utils import timezone
@@ -36,8 +36,6 @@ class SessionStore(SessionBase):
cls._storage_path = storage_path
return storage_path
- VALID_KEY_CHARS = set("abcdef0123456789")
-
def _key_to_file(self, session_key=None):
"""
Get the file associated with this session key.
@@ -48,7 +46,7 @@ class SessionStore(SessionBase):
# Make sure we're not vulnerable to directory traversal. Session keys
# should always be md5s, so they should never contain directory
# components.
- if not set(session_key).issubset(self.VALID_KEY_CHARS):
+ if not set(session_key).issubset(set(VALID_KEY_CHARS)):
raise SuspiciousOperation(
"Invalid characters in session key")