Ticket #32643: 32643.diff

File 32643.diff, 2.5 KB (added by Florian Apolloner, 4 years ago)
  • django/contrib/messages/storage/cookie.py

    diff --git a/django/contrib/messages/storage/cookie.py b/django/contrib/messages/storage/cookie.py
    index 30689dde3b..17bed82057 100644
    a b  
     1import binascii
    12import json
    23
    34from django.conf import settings
    class CookieStorage(BaseStorage):  
    182183            # with:
    183184            #   decoded = None.
    184185            decoded = self._legacy_decode(data)
    185         except json.JSONDecodeError:
     186        except (binascii.Error, json.JSONDecodeError):
    186187            decoded = self.signer.unsign(data)
    187188
    188189        if decoded:
  • tests/messages_tests/test_cookie.py

    diff --git a/tests/messages_tests/test_cookie.py b/tests/messages_tests/test_cookie.py
    index 9f82ce93e9..191ae6eba6 100644
    a b  
     1import binascii
    12import json
    23import random
    34
    from django.contrib.messages.storage.base import Message  
    78from django.contrib.messages.storage.cookie import (
    89    CookieStorage, MessageDecoder, MessageEncoder,
    910)
    10 from django.core.signing import get_cookie_signer
     11from django.core.signing import get_cookie_signer, b64_decode
    1112from django.test import SimpleTestCase, override_settings
    1213from django.test.utils import ignore_warnings
    1314from django.utils.crypto import get_random_string
    class CookieTests(BaseTests, SimpleTestCase):  
    205206        decoded_messages = storage._decode(encoded_messages)
    206207        self.assertEqual(messages, decoded_messages)
    207208
     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
    208226    @ignore_warnings(category=RemovedInDjango40Warning)
    209227    def test_default_hashing_algorithm(self):
    210228        messages = Message(constants.DEBUG, ['this', 'that'])
Back to Top