Opened 3 years ago

Closed 3 years ago

#33458 closed Bug (fixed)

Messages framework incorrectly serializes/deserializes extra_tags when it's an empty string

Reported by: Tim McCurrach Owned by: Tim McCurrach
Component: contrib.messages Version: 4.0
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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 "").

Change History (4)

comment:1 by Tim McCurrach, 3 years ago

Has patch: set
Owner: changed from nobody to Tim McCurrach
Status: newassigned

comment:2 by Mariusz Felisiak, 3 years ago

Triage Stage: UnreviewedAccepted

Thanks for the report.

comment:3 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In efb4478:

Fixed #33458 -- Fixed encoding of messages with empty string as extra_tags.

Note: See TracTickets for help on using tickets.
Back to Top