2008-08-27 04:19:12 +08:00
|
|
|
try:
|
|
|
|
import cPickle as pickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle
|
2008-09-14 15:04:40 +08:00
|
|
|
|
2011-03-28 09:40:43 +08:00
|
|
|
import hashlib
|
2008-08-27 04:19:12 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.forms import BooleanField
|
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.crypto import salted_hmac
|
|
|
|
|
2008-08-27 04:19:12 +08:00
|
|
|
|
|
|
|
def security_hash(request, form, *args):
|
2008-09-14 15:04:40 +08:00
|
|
|
"""
|
|
|
|
Calculates a security hash for the given Form instance.
|
|
|
|
|
|
|
|
This creates a list of the form field names/values in a deterministic
|
|
|
|
order, pickles the result with the SECRET_KEY setting, then takes an md5
|
|
|
|
hash of that.
|
|
|
|
"""
|
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 warnings
|
|
|
|
warnings.warn("security_hash is deprecated; use form_hmac instead",
|
|
|
|
PendingDeprecationWarning)
|
2009-05-13 05:54:58 +08:00
|
|
|
data = []
|
|
|
|
for bf in form:
|
2009-05-13 06:02:38 +08:00
|
|
|
# Get the value from the form data. If the form allows empty or hasn't
|
|
|
|
# changed then don't call clean() to avoid trigger validation errors.
|
|
|
|
if form.empty_permitted and not form.has_changed():
|
|
|
|
value = bf.data or ''
|
|
|
|
else:
|
|
|
|
value = bf.field.clean(bf.data) or ''
|
2009-05-13 05:54:58 +08:00
|
|
|
if isinstance(value, basestring):
|
|
|
|
value = value.strip()
|
|
|
|
data.append((bf.name, value))
|
2009-05-13 06:02:38 +08:00
|
|
|
|
2008-09-14 15:04:40 +08:00
|
|
|
data.extend(args)
|
|
|
|
data.append(settings.SECRET_KEY)
|
|
|
|
|
2011-03-28 10:26:15 +08:00
|
|
|
# Use HIGHEST_PROTOCOL because it's the most efficient.
|
2008-09-14 15:04:40 +08:00
|
|
|
pickled = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
|
2008-08-27 04:19:12 +08:00
|
|
|
|
2011-03-28 09:40:43 +08:00
|
|
|
return hashlib.md5(pickled).hexdigest()
|
2008-08-27 04:19:12 +08:00
|
|
|
|
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 form_hmac(form):
|
|
|
|
"""
|
|
|
|
Calculates a security hash for the given Form instance.
|
|
|
|
"""
|
|
|
|
data = []
|
|
|
|
for bf in form:
|
|
|
|
# Get the value from the form data. If the form allows empty or hasn't
|
|
|
|
# changed then don't call clean() to avoid trigger validation errors.
|
|
|
|
if form.empty_permitted and not form.has_changed():
|
|
|
|
value = bf.data or ''
|
|
|
|
else:
|
|
|
|
value = bf.field.clean(bf.data) or ''
|
|
|
|
if isinstance(value, basestring):
|
|
|
|
value = value.strip()
|
|
|
|
data.append((bf.name, value))
|
|
|
|
|
|
|
|
pickled = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
|
|
|
|
key_salt = 'django.contrib.formtools'
|
|
|
|
return salted_hmac(key_salt, pickled).hexdigest()
|