Skip to content

Commit

Permalink
[1.1.X] Fixed a security issue in the file session backend. Disclosur…
Browse files Browse the repository at this point in the history
…e and new release forthcoming.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@15469 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
alex committed Feb 9, 2011
1 parent 408c5c8 commit 570a32a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions django/contrib/sessions/backends/file.py
Expand Up @@ -26,6 +26,8 @@ def __init__(self, session_key=None):
self.file_prefix = settings.SESSION_COOKIE_NAME self.file_prefix = settings.SESSION_COOKIE_NAME
super(SessionStore, self).__init__(session_key) super(SessionStore, self).__init__(session_key)


VALID_KEY_CHARS = set("abcdef0123456789")

def _key_to_file(self, session_key=None): def _key_to_file(self, session_key=None):
""" """
Get the file associated with this session key. Get the file associated with this session key.
Expand All @@ -36,9 +38,9 @@ def _key_to_file(self, session_key=None):
# Make sure we're not vulnerable to directory traversal. Session keys # Make sure we're not vulnerable to directory traversal. Session keys
# should always be md5s, so they should never contain directory # should always be md5s, so they should never contain directory
# components. # components.
if os.path.sep in session_key: if not set(session_key).issubset(self.VALID_KEY_CHARS):
raise SuspiciousOperation( raise SuspiciousOperation(
"Invalid characters (directory components) in session key") "Invalid characters in session key")


return os.path.join(self.storage_path, self.file_prefix + session_key) return os.path.join(self.storage_path, self.file_prefix + session_key)


Expand Down
11 changes: 11 additions & 0 deletions django/contrib/sessions/tests.py
Expand Up @@ -129,6 +129,17 @@
>>> file_session = FileSession(file_session.session_key) >>> file_session = FileSession(file_session.session_key)
>>> file_session.save() >>> file_session.save()
# Ensure we don't allow directory traversal
>>> FileSession("a/b/c").load()
Traceback (innermost last):
...
SuspiciousOperation: Invalid characters in session key
>>> FileSession("a\\b\\c").load()
Traceback (innermost last):
...
SuspiciousOperation: Invalid characters in session key
# Make sure the file backend checks for a good storage dir # Make sure the file backend checks for a good storage dir
>>> settings.SESSION_FILE_PATH = "/if/this/directory/exists/you/have/a/weird/computer" >>> settings.SESSION_FILE_PATH = "/if/this/directory/exists/you/have/a/weird/computer"
>>> FileSession() >>> FileSession()
Expand Down

0 comments on commit 570a32a

Please sign in to comment.