2012-04-30 01:58:00 +08:00
|
|
|
import json
|
|
|
|
|
2009-12-10 00:57:23 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.messages.storage.base import BaseStorage, Message
|
2011-01-25 04:35:46 +08:00
|
|
|
from django.http import SimpleCookie
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.crypto import constant_time_compare, salted_hmac
|
|
|
|
from django.utils.safestring import SafeData, mark_safe
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MessageEncoder(json.JSONEncoder):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Compactly serialize instances of the ``Message`` class as JSON.
|
2009-12-10 00:57:23 +08:00
|
|
|
"""
|
|
|
|
message_key = '__json_message'
|
|
|
|
|
|
|
|
def default(self, obj):
|
|
|
|
if isinstance(obj, Message):
|
2012-12-21 17:01:03 +08:00
|
|
|
# Using 0/1 here instead of False/True to produce more compact json
|
|
|
|
is_safedata = 1 if isinstance(obj.message, SafeData) else 0
|
|
|
|
message = [self.message_key, is_safedata, obj.level, obj.message]
|
2009-12-10 00:57:23 +08:00
|
|
|
if obj.extra_tags:
|
|
|
|
message.append(obj.extra_tags)
|
|
|
|
return message
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().default(obj)
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MessageDecoder(json.JSONDecoder):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Decode JSON that includes serialized ``Message`` instances.
|
2009-12-10 00:57:23 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def process_messages(self, obj):
|
|
|
|
if isinstance(obj, list) and obj:
|
|
|
|
if obj[0] == MessageEncoder.message_key:
|
2014-04-15 23:52:30 +08:00
|
|
|
if len(obj) == 3:
|
|
|
|
# Compatibility with previously-encoded messages
|
|
|
|
return Message(*obj[1:])
|
2012-12-21 17:01:03 +08:00
|
|
|
if obj[1]:
|
|
|
|
obj[3] = mark_safe(obj[3])
|
|
|
|
return Message(*obj[2:])
|
2009-12-10 00:57:23 +08:00
|
|
|
return [self.process_messages(item) for item in obj]
|
|
|
|
if isinstance(obj, dict):
|
2014-12-07 05:00:09 +08:00
|
|
|
return {key: self.process_messages(value)
|
2017-01-07 19:11:46 +08:00
|
|
|
for key, value in obj.items()}
|
2009-12-10 00:57:23 +08:00
|
|
|
return obj
|
|
|
|
|
|
|
|
def decode(self, s, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
decoded = super().decode(s, **kwargs)
|
2009-12-10 00:57:23 +08:00
|
|
|
return self.process_messages(decoded)
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2009-12-10 00:57:23 +08:00
|
|
|
class CookieStorage(BaseStorage):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Store messages in a cookie.
|
2009-12-10 00:57:23 +08:00
|
|
|
"""
|
|
|
|
cookie_name = 'messages'
|
2012-09-08 07:23:16 +08:00
|
|
|
# uwsgi's default configuration enforces a maximum size of 4kb for all the
|
|
|
|
# HTTP headers. In order to leave some room for other cookies and headers,
|
|
|
|
# restrict the session cookie to 1/2 of 4kb. See #18781.
|
|
|
|
max_cookie_size = 2048
|
2009-12-10 00:57:23 +08:00
|
|
|
not_finished = '__messagesnotfinished__'
|
|
|
|
|
|
|
|
def _get(self, *args, **kwargs):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Retrieve a list of messages from the messages cookie. If the
|
2009-12-10 00:57:23 +08:00
|
|
|
not_finished sentinel value is found at the end of the message list,
|
|
|
|
remove it and return a result indicating that not all messages were
|
|
|
|
retrieved by this storage.
|
|
|
|
"""
|
|
|
|
data = self.request.COOKIES.get(self.cookie_name)
|
|
|
|
messages = self._decode(data)
|
|
|
|
all_retrieved = not (messages and messages[-1] == self.not_finished)
|
|
|
|
if messages and not all_retrieved:
|
|
|
|
# remove the sentinel value
|
|
|
|
messages.pop()
|
|
|
|
return messages, all_retrieved
|
|
|
|
|
|
|
|
def _update_cookie(self, encoded_data, response):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Either set the cookie with the encoded data if there is any data to
|
|
|
|
store, or delete the cookie.
|
2009-12-10 00:57:23 +08:00
|
|
|
"""
|
|
|
|
if encoded_data:
|
2016-03-29 06:33:29 +08:00
|
|
|
response.set_cookie(
|
|
|
|
self.cookie_name, encoded_data,
|
2013-08-27 03:34:29 +08:00
|
|
|
domain=settings.SESSION_COOKIE_DOMAIN,
|
|
|
|
secure=settings.SESSION_COOKIE_SECURE or None,
|
2016-03-29 06:33:29 +08:00
|
|
|
httponly=settings.SESSION_COOKIE_HTTPONLY or None,
|
|
|
|
)
|
2009-12-10 00:57:23 +08:00
|
|
|
else:
|
2016-03-29 06:33:29 +08:00
|
|
|
response.delete_cookie(self.cookie_name, domain=settings.SESSION_COOKIE_DOMAIN)
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
def _store(self, messages, response, remove_oldest=True, *args, **kwargs):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Store the messages to a cookie and return a list of any messages which
|
2009-12-10 00:57:23 +08:00
|
|
|
could not be stored.
|
|
|
|
|
2017-01-25 04:31:57 +08:00
|
|
|
If the encoded data is larger than ``max_cookie_size``, remove
|
2009-12-10 00:57:23 +08:00
|
|
|
messages until the data fits (these are the messages which are
|
|
|
|
returned), and add the not_finished sentinel value to indicate as much.
|
|
|
|
"""
|
|
|
|
unstored_messages = []
|
|
|
|
encoded_data = self._encode(messages)
|
|
|
|
if self.max_cookie_size:
|
2011-01-25 04:35:46 +08:00
|
|
|
# data is going to be stored eventually by SimpleCookie, which
|
2014-04-27 01:18:45 +08:00
|
|
|
# adds its own overhead, which we must account for.
|
2013-11-03 05:02:56 +08:00
|
|
|
cookie = SimpleCookie() # create outside the loop
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2010-01-24 07:56:04 +08:00
|
|
|
def stored_length(val):
|
2010-01-26 02:14:30 +08:00
|
|
|
return len(cookie.value_encode(val)[1])
|
2010-01-24 07:56:04 +08:00
|
|
|
|
|
|
|
while encoded_data and stored_length(encoded_data) > self.max_cookie_size:
|
2009-12-10 00:57:23 +08:00
|
|
|
if remove_oldest:
|
|
|
|
unstored_messages.append(messages.pop(0))
|
|
|
|
else:
|
|
|
|
unstored_messages.insert(0, messages.pop())
|
|
|
|
encoded_data = self._encode(messages + [self.not_finished],
|
|
|
|
encode_empty=unstored_messages)
|
|
|
|
self._update_cookie(encoded_data, response)
|
|
|
|
return unstored_messages
|
|
|
|
|
|
|
|
def _hash(self, value):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Create an HMAC/SHA1 hash based on the value and the project setting's
|
2009-12-10 00:57:23 +08:00
|
|
|
SECRET_KEY, modified to make it unique for the present purpose.
|
|
|
|
"""
|
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
|
|
|
key_salt = 'django.contrib.messages'
|
|
|
|
return salted_hmac(key_salt, value).hexdigest()
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
def _encode(self, messages, encode_empty=False):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return an encoded version of the messages list which can be stored as
|
2009-12-10 00:57:23 +08:00
|
|
|
plain text.
|
|
|
|
|
|
|
|
Since the data will be retrieved from the client-side, the encoded data
|
|
|
|
also contains a hash to ensure that the data was not tampered with.
|
|
|
|
"""
|
|
|
|
if messages or encode_empty:
|
|
|
|
encoder = MessageEncoder(separators=(',', ':'))
|
|
|
|
value = encoder.encode(messages)
|
|
|
|
return '%s$%s' % (self._hash(value), value)
|
|
|
|
|
|
|
|
def _decode(self, data):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Safely decode an encoded text stream back into a list of messages.
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
If the encoded text stream contained an invalid hash or was in an
|
2017-01-25 04:31:57 +08:00
|
|
|
invalid format, return None.
|
2009-12-10 00:57:23 +08:00
|
|
|
"""
|
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
bits = data.split('$', 1)
|
|
|
|
if len(bits) == 2:
|
|
|
|
hash, value = bits
|
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
|
|
|
if constant_time_compare(hash, self._hash(value)):
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2009-12-10 00:57:23 +08:00
|
|
|
# If we get here (and the JSON decode works), everything is
|
|
|
|
# good. In any other case, drop back and return None.
|
|
|
|
return json.loads(value, cls=MessageDecoder)
|
2017-09-07 20:16:21 +08:00
|
|
|
except ValueError:
|
|
|
|
pass
|
2009-12-10 00:57:23 +08:00
|
|
|
# Mark the data as used (so it gets removed) since something was wrong
|
|
|
|
# with the data.
|
|
|
|
self.used = True
|
|
|
|
return None
|