Fixed #14445 - Use HMAC and constant-time comparison functions where needed.
All adhoc MAC applications have been updated to use HMAC, using SHA1 to
generate unique keys for each application based on the SECRET_KEY, which is
common practice for this situation. In all cases, backwards compatibility
with existing hashes has been maintained, aiming to phase this out as per
the normal deprecation process. In this way, under most normal
circumstances the old hashes will have expired (e.g. by session expiration
etc.) before they become invalid.
In the case of the messages framework and the cookie backend, which was
already using HMAC, there is the possibility of a backwards incompatibility
if the SECRET_KEY is shorter than the default 50 bytes, but the low
likelihood and low impact meant compatibility code was not worth it.
All known instances where tokens/hashes were compared using simple string
equality, which could potentially open timing based attacks, have also been
fixed using a constant-time comparison function.
There are no known practical attacks against the existing implementations,
so these security improvements will not be backported.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14218 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-15 04:54:30 +08:00
|
|
|
import base64
|
2010-08-05 19:49:58 +08:00
|
|
|
from datetime import datetime, timedelta
|
Fixed #14445 - Use HMAC and constant-time comparison functions where needed.
All adhoc MAC applications have been updated to use HMAC, using SHA1 to
generate unique keys for each application based on the SECRET_KEY, which is
common practice for this situation. In all cases, backwards compatibility
with existing hashes has been maintained, aiming to phase this out as per
the normal deprecation process. In this way, under most normal
circumstances the old hashes will have expired (e.g. by session expiration
etc.) before they become invalid.
In the case of the messages framework and the cookie backend, which was
already using HMAC, there is the possibility of a backwards incompatibility
if the SECRET_KEY is shorter than the default 50 bytes, but the low
likelihood and low impact meant compatibility code was not worth it.
All known instances where tokens/hashes were compared using simple string
equality, which could potentially open timing based attacks, have also been
fixed using a constant-time comparison function.
There are no known practical attacks against the existing implementations,
so these security improvements will not be backported.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14218 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-15 04:54:30 +08:00
|
|
|
import pickle
|
2010-10-11 20:55:17 +08:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
2010-08-05 19:49:58 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.sessions.backends.db import SessionStore as DatabaseSession
|
|
|
|
from django.contrib.sessions.backends.cache import SessionStore as CacheSession
|
|
|
|
from django.contrib.sessions.backends.cached_db import SessionStore as CacheDBSession
|
|
|
|
from django.contrib.sessions.backends.file import SessionStore as FileSession
|
|
|
|
from django.contrib.sessions.backends.base import SessionBase
|
|
|
|
from django.contrib.sessions.models import Session
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.test import TestCase
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.utils import unittest
|
Fixed #14445 - Use HMAC and constant-time comparison functions where needed.
All adhoc MAC applications have been updated to use HMAC, using SHA1 to
generate unique keys for each application based on the SECRET_KEY, which is
common practice for this situation. In all cases, backwards compatibility
with existing hashes has been maintained, aiming to phase this out as per
the normal deprecation process. In this way, under most normal
circumstances the old hashes will have expired (e.g. by session expiration
etc.) before they become invalid.
In the case of the messages framework and the cookie backend, which was
already using HMAC, there is the possibility of a backwards incompatibility
if the SECRET_KEY is shorter than the default 50 bytes, but the low
likelihood and low impact meant compatibility code was not worth it.
All known instances where tokens/hashes were compared using simple string
equality, which could potentially open timing based attacks, have also been
fixed using a constant-time comparison function.
There are no known practical attacks against the existing implementations,
so these security improvements will not be backported.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14218 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-15 04:54:30 +08:00
|
|
|
from django.utils.hashcompat import md5_constructor
|
2010-08-05 19:49:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SessionTestsMixin(object):
|
|
|
|
# This does not inherit from TestCase to avoid any tests being run with this
|
|
|
|
# class, which wouldn't work, and to allow different TestCase subclasses to
|
|
|
|
# be used.
|
|
|
|
|
|
|
|
backend = None # subclasses must specify
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.session = self.backend()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# NB: be careful to delete any sessions created; stale sessions fill up
|
|
|
|
# the /tmp (with some backends) and eventually overwhelm it after lots
|
|
|
|
# of runs (think buildbots)
|
|
|
|
self.session.delete()
|
|
|
|
|
|
|
|
def test_new_session(self):
|
|
|
|
self.assertFalse(self.session.modified)
|
|
|
|
self.assertFalse(self.session.accessed)
|
|
|
|
|
|
|
|
def test_get_empty(self):
|
|
|
|
self.assertEqual(self.session.get('cat'), None)
|
|
|
|
|
|
|
|
def test_store(self):
|
|
|
|
self.session['cat'] = "dog"
|
|
|
|
self.assertTrue(self.session.modified)
|
|
|
|
self.assertEqual(self.session.pop('cat'), 'dog')
|
|
|
|
|
|
|
|
def test_pop(self):
|
|
|
|
self.session['some key'] = 'exists'
|
|
|
|
# Need to reset these to pretend we haven't accessed it:
|
|
|
|
self.accessed = False
|
|
|
|
self.modified = False
|
|
|
|
|
|
|
|
self.assertEqual(self.session.pop('some key'), 'exists')
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
self.assertTrue(self.session.modified)
|
|
|
|
self.assertEqual(self.session.get('some key'), None)
|
|
|
|
|
|
|
|
def test_pop_default(self):
|
|
|
|
self.assertEqual(self.session.pop('some key', 'does not exist'),
|
|
|
|
'does not exist')
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
self.assertFalse(self.session.modified)
|
|
|
|
|
|
|
|
def test_setdefault(self):
|
|
|
|
self.assertEqual(self.session.setdefault('foo', 'bar'), 'bar')
|
|
|
|
self.assertEqual(self.session.setdefault('foo', 'baz'), 'bar')
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
self.assertTrue(self.session.modified)
|
|
|
|
|
|
|
|
def test_update(self):
|
|
|
|
self.session.update({'update key': 1})
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
self.assertTrue(self.session.modified)
|
|
|
|
self.assertEqual(self.session.get('update key', None), 1)
|
|
|
|
|
|
|
|
def test_has_key(self):
|
|
|
|
self.session['some key'] = 1
|
|
|
|
self.session.modified = False
|
|
|
|
self.session.accessed = False
|
|
|
|
self.assertTrue(self.session.has_key('some key'))
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
self.assertFalse(self.session.modified)
|
|
|
|
|
|
|
|
def test_values(self):
|
|
|
|
self.assertEqual(self.session.values(), [])
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
self.session['some key'] = 1
|
|
|
|
self.assertEqual(self.session.values(), [1])
|
|
|
|
|
|
|
|
def test_iterkeys(self):
|
|
|
|
self.session['x'] = 1
|
|
|
|
self.session.modified = False
|
|
|
|
self.session.accessed = False
|
|
|
|
i = self.session.iterkeys()
|
|
|
|
self.assertTrue(hasattr(i, '__iter__'))
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
self.assertFalse(self.session.modified)
|
|
|
|
self.assertEqual(list(i), ['x'])
|
|
|
|
|
|
|
|
def test_iterkeys(self):
|
|
|
|
self.session['x'] = 1
|
|
|
|
self.session.modified = False
|
|
|
|
self.session.accessed = False
|
|
|
|
i = self.session.itervalues()
|
|
|
|
self.assertTrue(hasattr(i, '__iter__'))
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
self.assertFalse(self.session.modified)
|
|
|
|
self.assertEqual(list(i), [1])
|
|
|
|
|
|
|
|
def test_iteritems(self):
|
|
|
|
self.session['x'] = 1
|
|
|
|
self.session.modified = False
|
|
|
|
self.session.accessed = False
|
|
|
|
i = self.session.iteritems()
|
|
|
|
self.assertTrue(hasattr(i, '__iter__'))
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
self.assertFalse(self.session.modified)
|
|
|
|
self.assertEqual(list(i), [('x',1)])
|
|
|
|
|
|
|
|
def test_clear(self):
|
|
|
|
self.session['x'] = 1
|
|
|
|
self.session.modified = False
|
|
|
|
self.session.accessed = False
|
|
|
|
self.assertEqual(self.session.items(), [('x',1)])
|
|
|
|
self.session.clear()
|
|
|
|
self.assertEqual(self.session.items(), [])
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
self.assertTrue(self.session.modified)
|
|
|
|
|
|
|
|
def test_save(self):
|
|
|
|
self.session.save()
|
|
|
|
self.assertTrue(self.session.exists(self.session.session_key))
|
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
self.session.delete(self.session.session_key)
|
|
|
|
self.assertFalse(self.session.exists(self.session.session_key))
|
|
|
|
|
|
|
|
def test_flush(self):
|
|
|
|
self.session['foo'] = 'bar'
|
|
|
|
self.session.save()
|
|
|
|
prev_key = self.session.session_key
|
|
|
|
self.session.flush()
|
|
|
|
self.assertFalse(self.session.exists(prev_key))
|
|
|
|
self.assertNotEqual(self.session.session_key, prev_key)
|
|
|
|
self.assertTrue(self.session.modified)
|
|
|
|
self.assertTrue(self.session.accessed)
|
|
|
|
|
|
|
|
def test_cycle(self):
|
|
|
|
self.session['a'], self.session['b'] = 'c', 'd'
|
|
|
|
self.session.save()
|
|
|
|
prev_key = self.session.session_key
|
|
|
|
prev_data = self.session.items()
|
|
|
|
self.session.cycle_key()
|
|
|
|
self.assertNotEqual(self.session.session_key, prev_key)
|
|
|
|
self.assertEqual(self.session.items(), prev_data)
|
|
|
|
|
|
|
|
def test_invalid_key(self):
|
|
|
|
# Submitting an invalid session key (either by guessing, or if the db has
|
|
|
|
# removed the key) results in a new key being generated.
|
|
|
|
session = self.backend('1')
|
|
|
|
session.save()
|
|
|
|
self.assertNotEqual(session.session_key, '1')
|
|
|
|
self.assertEqual(session.get('cat'), None)
|
|
|
|
session.delete()
|
|
|
|
|
|
|
|
# Custom session expiry
|
|
|
|
def test_default_expiry(self):
|
|
|
|
# A normal session has a max age equal to settings
|
|
|
|
self.assertEqual(self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE)
|
|
|
|
|
|
|
|
# So does a custom session with an idle expiration time of 0 (but it'll
|
|
|
|
# expire at browser close)
|
|
|
|
self.session.set_expiry(0)
|
|
|
|
self.assertEqual(self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE)
|
|
|
|
|
|
|
|
def test_custom_expiry_seconds(self):
|
|
|
|
# Using seconds
|
|
|
|
self.session.set_expiry(10)
|
|
|
|
delta = self.session.get_expiry_date() - datetime.now()
|
|
|
|
self.assertTrue(delta.seconds in (9, 10))
|
|
|
|
|
|
|
|
age = self.session.get_expiry_age()
|
|
|
|
self.assertTrue(age in (9, 10))
|
|
|
|
|
|
|
|
def test_custom_expiry_timedelta(self):
|
|
|
|
# Using timedelta
|
|
|
|
self.session.set_expiry(timedelta(seconds=10))
|
|
|
|
delta = self.session.get_expiry_date() - datetime.now()
|
|
|
|
self.assertTrue(delta.seconds in (9, 10))
|
|
|
|
|
|
|
|
age = self.session.get_expiry_age()
|
|
|
|
self.assertTrue(age in (9, 10))
|
|
|
|
|
|
|
|
def test_custom_expiry_timedelta(self):
|
|
|
|
# Using timedelta
|
|
|
|
self.session.set_expiry(datetime.now() + timedelta(seconds=10))
|
|
|
|
delta = self.session.get_expiry_date() - datetime.now()
|
|
|
|
self.assertTrue(delta.seconds in (9, 10))
|
|
|
|
|
|
|
|
age = self.session.get_expiry_age()
|
|
|
|
self.assertTrue(age in (9, 10))
|
|
|
|
|
|
|
|
def test_custom_expiry_reset(self):
|
|
|
|
self.session.set_expiry(None)
|
|
|
|
self.session.set_expiry(10)
|
|
|
|
self.session.set_expiry(None)
|
|
|
|
self.assertEqual(self.session.get_expiry_age(), settings.SESSION_COOKIE_AGE)
|
|
|
|
|
|
|
|
def test_get_expire_at_browser_close(self):
|
|
|
|
# Tests get_expire_at_browser_close with different settings and different
|
|
|
|
# set_expiry calls
|
|
|
|
try:
|
2010-08-14 20:11:02 +08:00
|
|
|
try:
|
|
|
|
original_expire_at_browser_close = settings.SESSION_EXPIRE_AT_BROWSER_CLOSE
|
|
|
|
settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = False
|
2010-08-05 19:49:58 +08:00
|
|
|
|
2010-08-14 20:11:02 +08:00
|
|
|
self.session.set_expiry(10)
|
|
|
|
self.assertFalse(self.session.get_expire_at_browser_close())
|
2010-08-05 19:49:58 +08:00
|
|
|
|
2010-08-14 20:11:02 +08:00
|
|
|
self.session.set_expiry(0)
|
|
|
|
self.assertTrue(self.session.get_expire_at_browser_close())
|
2010-08-05 19:49:58 +08:00
|
|
|
|
2010-08-14 20:11:02 +08:00
|
|
|
self.session.set_expiry(None)
|
|
|
|
self.assertFalse(self.session.get_expire_at_browser_close())
|
2010-08-05 19:49:58 +08:00
|
|
|
|
2010-08-14 20:11:02 +08:00
|
|
|
settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True
|
2010-08-05 19:49:58 +08:00
|
|
|
|
2010-08-14 20:11:02 +08:00
|
|
|
self.session.set_expiry(10)
|
|
|
|
self.assertFalse(self.session.get_expire_at_browser_close())
|
2010-08-05 19:49:58 +08:00
|
|
|
|
2010-08-14 20:11:02 +08:00
|
|
|
self.session.set_expiry(0)
|
|
|
|
self.assertTrue(self.session.get_expire_at_browser_close())
|
2010-08-05 19:49:58 +08:00
|
|
|
|
2010-08-14 20:11:02 +08:00
|
|
|
self.session.set_expiry(None)
|
|
|
|
self.assertTrue(self.session.get_expire_at_browser_close())
|
2010-08-05 19:49:58 +08:00
|
|
|
|
2010-08-14 20:11:02 +08:00
|
|
|
except:
|
|
|
|
raise
|
2010-08-05 19:49:58 +08:00
|
|
|
finally:
|
|
|
|
settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = original_expire_at_browser_close
|
|
|
|
|
Fixed #14445 - Use HMAC and constant-time comparison functions where needed.
All adhoc MAC applications have been updated to use HMAC, using SHA1 to
generate unique keys for each application based on the SECRET_KEY, which is
common practice for this situation. In all cases, backwards compatibility
with existing hashes has been maintained, aiming to phase this out as per
the normal deprecation process. In this way, under most normal
circumstances the old hashes will have expired (e.g. by session expiration
etc.) before they become invalid.
In the case of the messages framework and the cookie backend, which was
already using HMAC, there is the possibility of a backwards incompatibility
if the SECRET_KEY is shorter than the default 50 bytes, but the low
likelihood and low impact meant compatibility code was not worth it.
All known instances where tokens/hashes were compared using simple string
equality, which could potentially open timing based attacks, have also been
fixed using a constant-time comparison function.
There are no known practical attacks against the existing implementations,
so these security improvements will not be backported.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14218 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-15 04:54:30 +08:00
|
|
|
def test_decode(self):
|
|
|
|
# Ensure we can decode what we encode
|
|
|
|
data = {'a test key': 'a test value'}
|
|
|
|
encoded = self.session.encode(data)
|
|
|
|
self.assertEqual(self.session.decode(encoded), data)
|
|
|
|
|
|
|
|
def test_decode_django12(self):
|
|
|
|
# Ensure we can decode values encoded using Django 1.2
|
|
|
|
# Hard code the Django 1.2 method here:
|
|
|
|
def encode(session_dict):
|
|
|
|
pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
|
|
|
|
pickled_md5 = md5_constructor(pickled + settings.SECRET_KEY).hexdigest()
|
|
|
|
return base64.encodestring(pickled + pickled_md5)
|
|
|
|
|
|
|
|
data = {'a test key': 'a test value'}
|
|
|
|
encoded = encode(data)
|
|
|
|
self.assertEqual(self.session.decode(encoded), data)
|
|
|
|
|
2010-08-05 19:49:58 +08:00
|
|
|
|
|
|
|
class DatabaseSessionTests(SessionTestsMixin, TestCase):
|
|
|
|
|
|
|
|
backend = DatabaseSession
|
|
|
|
|
|
|
|
|
|
|
|
class CacheDBSessionTests(SessionTestsMixin, TestCase):
|
|
|
|
|
|
|
|
backend = CacheDBSession
|
|
|
|
|
|
|
|
# Don't need DB flushing for these tests, so can use unittest.TestCase as base class
|
|
|
|
class FileSessionTests(SessionTestsMixin, unittest.TestCase):
|
|
|
|
|
|
|
|
backend = FileSession
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(FileSessionTests, self).setUp()
|
|
|
|
# Do file session tests in an isolated directory, and kill it after we're done.
|
|
|
|
self.original_session_file_path = settings.SESSION_FILE_PATH
|
|
|
|
self.temp_session_store = settings.SESSION_FILE_PATH = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
settings.SESSION_FILE_PATH = self.original_session_file_path
|
|
|
|
shutil.rmtree(self.temp_session_store)
|
|
|
|
super(FileSessionTests, self).tearDown()
|
|
|
|
|
|
|
|
def test_configuration_check(self):
|
|
|
|
# Make sure the file backend checks for a good storage dir
|
|
|
|
settings.SESSION_FILE_PATH = "/if/this/directory/exists/you/have/a/weird/computer"
|
|
|
|
self.assertRaises(ImproperlyConfigured, self.backend)
|
|
|
|
|
|
|
|
|
|
|
|
class CacheSessionTests(SessionTestsMixin, unittest.TestCase):
|
|
|
|
|
|
|
|
backend = CacheSession
|