| 1 |
import binascii |
| 2 |
import json |
| 3 |
|
| 4 |
from django.conf import settings |
| 5 |
from django.contrib.messages.storage.base import BaseStorage, Message |
| 6 |
from django.core import signing |
| 7 |
from django.http import SimpleCookie |
| 8 |
from django.utils.safestring import SafeData, mark_safe |
| 9 |
|
| 10 |
|
| 11 |
class MessageEncoder(json.JSONEncoder): |
| 12 |
""" |
| 13 |
Compactly serialize instances of the ``Message`` class as JSON. |
| 14 |
""" |
| 15 |
|
| 16 |
message_key = "__json_message" |
| 17 |
|
| 18 |
def default(self, obj): |
| 19 |
if isinstance(obj, Message): |
| 20 |
# Using 0/1 here instead of False/True to produce more compact json |
| 21 |
is_safedata = 1 if isinstance(obj.message, SafeData) else 0 |
| 22 |
message = [self.message_key, is_safedata, obj.level, obj.message] |
| 23 |
if obj.extra_tags is not None: |
| 24 |
message.append(obj.extra_tags) |
| 25 |
return message |
| 26 |
return super().default(obj) |
| 27 |
|
| 28 |
|
| 29 |
class MessageDecoder(json.JSONDecoder): |
| 30 |
""" |
| 31 |
Decode JSON that includes serialized ``Message`` instances. |
| 32 |
""" |
| 33 |
|
| 34 |
def process_messages(self, obj): |
| 35 |
if isinstance(obj, list) and obj: |
| 36 |
if obj[0] == MessageEncoder.message_key: |
| 37 |
if obj[1]: |
| 38 |
obj[3] = mark_safe(obj[3]) |
| 39 |
return Message(*obj[2:]) |
| 40 |
return [self.process_messages(item) for item in obj] |
| 41 |
if isinstance(obj, dict): |
| 42 |
return {key: self.process_messages(value) for key, value in obj.items()} |
| 43 |
return obj |
| 44 |
|
| 45 |
def decode(self, s, **kwargs): |
| 46 |
decoded = super().decode(s, **kwargs) |
| 47 |
return self.process_messages(decoded) |
| 48 |
|
| 49 |
|
| 50 |
class MessagePartSerializer: |
| 51 |
def dumps(self, obj): |
| 52 |
return [ |
| 53 |
json.dumps( |
| 54 |
o, |
| 55 |
separators=(",", ":"), |
| 56 |
cls=MessageEncoder, |
| 57 |
) |
| 58 |
for o in obj |
| 59 |
] |
| 60 |
|
| 61 |
|
| 62 |
class MessagePartGatherSerializer: |
| 63 |
def dumps(self, obj): |
| 64 |
""" |
| 65 |
The parameter is an already serialized list of Message objects. No need |
| 66 |
to serialize it again, only join the list together and encode it. |
| 67 |
""" |
| 68 |
return ("[" + ",".join(obj) + "]").encode("latin-1") |
| 69 |
|
| 70 |
|
| 71 |
class MessageSerializer: |
| 72 |
def loads(self, data): |
| 73 |
return json.loads(data.decode("latin-1"), cls=MessageDecoder) |
| 74 |
|
| 75 |
|
| 76 |
class CookieStorage(BaseStorage): |
| 77 |
""" |
| 78 |
Store messages in a cookie. |
| 79 |
""" |
| 80 |
|
| 81 |
cookie_name = "messages" |
| 82 |
# uwsgi's default configuration enforces a maximum size of 4kb for all the |
| 83 |
# HTTP headers. In order to leave some room for other cookies and headers, |
| 84 |
# restrict the session cookie to 1/2 of 4kb. See #18781. |
| 85 |
max_cookie_size = 2048 |
| 86 |
not_finished = "__messagesnotfinished__" |
| 87 |
not_finished_json = json.dumps("__messagesnotfinished__") |
| 88 |
key_salt = "django.contrib.messages" |
| 89 |
|
| 90 |
def __init__(self, *args, **kwargs): |
| 91 |
super().__init__(*args, **kwargs) |
| 92 |
self.signer = signing.get_cookie_signer(salt=self.key_salt) |
| 93 |
|
| 94 |
def _get(self, *args, **kwargs): |
| 95 |
""" |
| 96 |
Retrieve a list of messages from the messages cookie. If the |
| 97 |
not_finished sentinel value is found at the end of the message list, |
| 98 |
remove it and return a result indicating that not all messages were |
| 99 |
retrieved by this storage. |
| 100 |
""" |
| 101 |
data = self.request.COOKIES.get(self.cookie_name) |
| 102 |
messages = self._decode(data) |
| 103 |
all_retrieved = not (messages and messages[-1] == self.not_finished) |
| 104 |
if messages and not all_retrieved: |
| 105 |
# remove the sentinel value |
| 106 |
messages.pop() |
| 107 |
return messages, all_retrieved |
| 108 |
|
| 109 |
def _update_cookie(self, encoded_data, response): |
| 110 |
""" |
| 111 |
Either set the cookie with the encoded data if there is any data to |
| 112 |
store, or delete the cookie. |
| 113 |
""" |
| 114 |
if encoded_data: |
| 115 |
response.set_cookie( |
| 116 |
self.cookie_name, |
| 117 |
encoded_data, |
| 118 |
domain=settings.SESSION_COOKIE_DOMAIN, |
| 119 |
secure=settings.SESSION_COOKIE_SECURE or None, |
| 120 |
httponly=settings.SESSION_COOKIE_HTTPONLY or None, |
| 121 |
samesite=settings.SESSION_COOKIE_SAMESITE, |
| 122 |
) |
| 123 |
else: |
| 124 |
response.delete_cookie( |
| 125 |
self.cookie_name, |
| 126 |
domain=settings.SESSION_COOKIE_DOMAIN, |
| 127 |
samesite=settings.SESSION_COOKIE_SAMESITE, |
| 128 |
) |
| 129 |
|
| 130 |
def _store(self, messages, response, remove_oldest=True, *args, **kwargs): |
| 131 |
""" |
| 132 |
Store the messages to a cookie and return a list of any messages which |
| 133 |
could not be stored. |
| 134 |
|
| 135 |
If the encoded data is larger than ``max_cookie_size``, remove |
| 136 |
messages until the data fits (these are the messages which are |
| 137 |
returned), and add the not_finished sentinel value to indicate as much. |
| 138 |
""" |
| 139 |
unstored_messages = [] |
| 140 |
serialized_messages = MessagePartSerializer().dumps(messages) |
| 141 |
encoded_data = self._encode_parts(serialized_messages) |
| 142 |
if self.max_cookie_size: |
| 143 |
# data is going to be stored eventually by SimpleCookie, which |
| 144 |
# adds its own overhead, which we must account for. |
| 145 |
cookie = SimpleCookie() # create outside the loop |
| 146 |
|
| 147 |
def is_too_large_for_cookie(data): |
| 148 |
return data and len(cookie.value_encode(data)[1]) > self.max_cookie_size |
| 149 |
|
| 150 |
def compute_msg(some_serialized_msg): |
| 151 |
return self._encode_parts( |
| 152 |
some_serialized_msg + [self.not_finished_json], |
| 153 |
encode_empty=True, |
| 154 |
) |
| 155 |
|
| 156 |
if is_too_large_for_cookie(encoded_data): |
| 157 |
if remove_oldest: |
| 158 |
idx = bisect_keep_right( |
| 159 |
serialized_messages, |
| 160 |
fn=lambda m: is_too_large_for_cookie(compute_msg(m)), |
| 161 |
) |
| 162 |
unstored_messages = messages[:idx] |
| 163 |
encoded_data = compute_msg(serialized_messages[idx:]) |
| 164 |
else: |
| 165 |
idx = bisect_keep_left( |
| 166 |
serialized_messages, |
| 167 |
fn=lambda m: is_too_large_for_cookie(compute_msg(m)), |
| 168 |
) |
| 169 |
unstored_messages = messages[idx:] |
| 170 |
encoded_data = compute_msg(serialized_messages[:idx]) |
| 171 |
|
| 172 |
self._update_cookie(encoded_data, response) |
| 173 |
return unstored_messages |
| 174 |
|
| 175 |
def _encode_parts(self, messages, encode_empty=False): |
| 176 |
""" |
| 177 |
Return an encoded version of the serialized messages list which can be |
| 178 |
stored as plain text. |
| 179 |
|
| 180 |
Since the data will be retrieved from the client-side, the encoded data |
| 181 |
also contains a hash to ensure that the data was not tampered with. |
| 182 |
""" |
| 183 |
if messages or encode_empty: |
| 184 |
return self.signer.sign_object( |
| 185 |
messages, serializer=MessagePartGatherSerializer, compress=True |
| 186 |
) |
| 187 |
|
| 188 |
def _encode(self, messages, encode_empty=False): |
| 189 |
""" |
| 190 |
Return an encoded version of the messages list which can be stored as |
| 191 |
plain text. |
| 192 |
|
| 193 |
Proxies MessagePartSerializer.dumps and _encoded_parts. |
| 194 |
""" |
| 195 |
serialized_messages = MessagePartSerializer().dumps(messages) |
| 196 |
return self._encode_parts(serialized_messages, encode_empty=encode_empty) |
| 197 |
|
| 198 |
def _decode(self, data): |
| 199 |
""" |
| 200 |
Safely decode an encoded text stream back into a list of messages. |
| 201 |
|
| 202 |
If the encoded text stream contained an invalid hash or was in an |
| 203 |
invalid format, return None. |
| 204 |
""" |
| 205 |
if not data: |
| 206 |
return None |
| 207 |
try: |
| 208 |
return self.signer.unsign_object(data, serializer=MessageSerializer) |
| 209 |
except (signing.BadSignature, binascii.Error, json.JSONDecodeError): |
| 210 |
pass |
| 211 |
# Mark the data as used (so it gets removed) since something was wrong |
| 212 |
# with the data. |
| 213 |
self.used = True |
| 214 |
return None |
| 215 |
|
| 216 |
|
| 217 |
def bisect_keep_left(a, fn): |
| 218 |
""" |
| 219 |
Find the index of the first element from the start of the array that |
| 220 |
verifies the given condition. |
| 221 |
The function is applied from the start of the array to the pivot. |
| 222 |
""" |
| 223 |
lo = 0 |
| 224 |
hi = len(a) |
| 225 |
while lo < hi: |
| 226 |
mid = (lo + hi) // 2 |
| 227 |
if fn(a[: mid + 1]): |
| 228 |
hi = mid |
| 229 |
else: |
| 230 |
lo = mid + 1 |
| 231 |
return lo |
| 232 |
|
| 233 |
|
| 234 |
def bisect_keep_right(a, fn): |
| 235 |
""" |
| 236 |
Find the index of the first element from the end of the array that verifies |
| 237 |
the given condition. |
| 238 |
The function is applied from the pivot to the end of array. |
| 239 |
""" |
| 240 |
lo = 0 |
| 241 |
hi = len(a) |
| 242 |
while lo < hi: |
| 243 |
mid = (lo + hi) // 2 |
| 244 |
if fn(a[mid:]): |
| 245 |
lo = mid + 1 |
| 246 |
else: |
| 247 |
hi = mid |
| 248 |
return lo |