diff --git a/django/contrib/messages/storage/cookie.py b/django/contrib/messages/storage/cookie.py
index 30689dde3b..17bed82057 100644
a
|
b
|
|
| 1 | import binascii |
1 | 2 | import json |
2 | 3 | |
3 | 4 | from django.conf import settings |
… |
… |
class CookieStorage(BaseStorage):
|
182 | 183 | # with: |
183 | 184 | # decoded = None. |
184 | 185 | decoded = self._legacy_decode(data) |
185 | | except json.JSONDecodeError: |
| 186 | except (binascii.Error, json.JSONDecodeError): |
186 | 187 | decoded = self.signer.unsign(data) |
187 | 188 | |
188 | 189 | if decoded: |
diff --git a/tests/messages_tests/test_cookie.py b/tests/messages_tests/test_cookie.py
index 9f82ce93e9..191ae6eba6 100644
a
|
b
|
|
| 1 | import binascii |
1 | 2 | import json |
2 | 3 | import random |
3 | 4 | |
… |
… |
from django.contrib.messages.storage.base import Message
|
7 | 8 | from django.contrib.messages.storage.cookie import ( |
8 | 9 | CookieStorage, MessageDecoder, MessageEncoder, |
9 | 10 | ) |
10 | | from django.core.signing import get_cookie_signer |
| 11 | from django.core.signing import get_cookie_signer, b64_decode |
11 | 12 | from django.test import SimpleTestCase, override_settings |
12 | 13 | from django.test.utils import ignore_warnings |
13 | 14 | from django.utils.crypto import get_random_string |
… |
… |
class CookieTests(BaseTests, SimpleTestCase):
|
205 | 206 | decoded_messages = storage._decode(encoded_messages) |
206 | 207 | self.assertEqual(messages, decoded_messages) |
207 | 208 | |
| 209 | def test_legacy_encode_decode2(self): |
| 210 | # RemovedInDjango41Warning: pre-Django 3.2 encoded messages will be |
| 211 | # invalid. |
| 212 | storage = self.storage_class(self.get_request()) |
| 213 | expected_texts = ['Successfully signed in as admin@example.org'] |
| 214 | messages = [Message(0, msg) for msg in expected_texts] |
| 215 | # Encode/decode a message using the pre-Django 3.2 format. |
| 216 | encoder = MessageEncoder() |
| 217 | value = encoder.encode(messages) |
| 218 | with self.assertRaises(binascii.Error): |
| 219 | b64_decode(value.encode()) |
| 220 | signer = get_cookie_signer(salt=storage.key_salt) |
| 221 | encoded_messages = signer.sign(value) |
| 222 | decoded_messages = storage._decode(encoded_messages) |
| 223 | self.assertEqual(expected_texts, list(map(lambda x: x.message, decoded_messages))) |
| 224 | |
| 225 | |
208 | 226 | @ignore_warnings(category=RemovedInDjango40Warning) |
209 | 227 | def test_default_hashing_algorithm(self): |
210 | 228 | messages = Message(constants.DEBUG, ['this', 'that']) |