[Django] #33458: Messages framework incorrectly serializes/deserializes extra_tags when it's an empty string

16 views
Skip to first unread message

Django

unread,
Jan 21, 2022, 8:49:23 PM1/21/22
to django-...@googlegroups.com
#33458: Messages framework incorrectly serializes/deserializes extra_tags when it's
an empty string
--------------------------------------------+------------------------
Reporter: Tim McCurrach | Owner: nobody
Type: Bug | Status: new
Component: contrib.messages | Version: 4.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
When a message is serialised and then deserialised with any of the built
in storage backends, then `extra_tags==""` is converted to
`extra_tags==None`. This is because `MessageEncoder` checks for the
truthyness of `extra_tags` rather than checking it is not `None`.

=== To replicate this bug ===

{{{
>>> from django.conf import settings
>>> settings.configure() # Just to allow the following import
>>> from django.contrib.messages.storage.base import Message
>>> from django.contrib.messages.storage.cookie import MessageEncoder,
MessageDecoder
>>> original_message = Message(10, "Here is a message", extra_tags="")
>>> encoded_message = MessageEncoder().encode(original_message)
>>> decoded_message = MessageDecoder().decode(encoded_message)
>>> original_message.extra_tags == ""
True
>>> decoded_message.extra_tags is None
True
}}}

=== Effect of the bug in application behaviour ===
This error occurred in the wild with a template tag similar to the
following:
{{{
{% if x not in message.extra_tags %}
}}}
When the message was displayed as part of a redirect, it had been
serialised and deserialized which meant that extra_tags was `None` instead
of the empty string. This caused an error.

It's important to note that this bug affects all of the standard API
(`messages.debug`, `messages.info` etc. all have a default value of
`extra_tags` equal to `""`).

--
Ticket URL: <https://code.djangoproject.com/ticket/33458>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 21, 2022, 9:08:21 PM1/21/22
to django-...@googlegroups.com
#33458: Messages framework incorrectly serializes/deserializes extra_tags when it's
an empty string
-------------------------------------+-------------------------------------
Reporter: Tim McCurrach | Owner: Tim
| McCurrach
Type: Bug | Status: assigned
Component: contrib.messages | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim McCurrach):

* owner: nobody => Tim McCurrach
* status: new => assigned
* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/33458#comment:1>

Django

unread,
Jan 22, 2022, 6:40:46 AM1/22/22
to django-...@googlegroups.com
#33458: Messages framework incorrectly serializes/deserializes extra_tags when it's
an empty string
-------------------------------------+-------------------------------------
Reporter: Tim McCurrach | Owner: Tim
| McCurrach
Type: Bug | Status: assigned
Component: contrib.messages | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Unreviewed => Accepted


Comment:

Thanks for the report.

--
Ticket URL: <https://code.djangoproject.com/ticket/33458#comment:2>

Django

unread,
Jan 24, 2022, 1:06:53 AM1/24/22
to django-...@googlegroups.com
#33458: Messages framework incorrectly serializes/deserializes extra_tags when it's
an empty string
-------------------------------------+-------------------------------------
Reporter: Tim McCurrach | Owner: Tim
| McCurrach
Type: Bug | Status: assigned
Component: contrib.messages | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33458#comment:3>

Django

unread,
Jan 24, 2022, 1:40:31 AM1/24/22
to django-...@googlegroups.com
#33458: Messages framework incorrectly serializes/deserializes extra_tags when it's
an empty string
-------------------------------------+-------------------------------------
Reporter: Tim McCurrach | Owner: Tim
| McCurrach
Type: Bug | Status: closed
Component: contrib.messages | Version: 4.0
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"efb4478e484ae61c5fc23563d4e9df1f8f49df49" efb4478]:
{{{
#!CommitTicketReference repository=""
revision="efb4478e484ae61c5fc23563d4e9df1f8f49df49"
Fixed #33458 -- Fixed encoding of messages with empty string as
extra_tags.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33458#comment:4>

Reply all
Reply to author
Forward
0 new messages