Opened 4 years ago
Closed 4 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 , 4 years ago
| Has patch: | set |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
comment:2 by , 4 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 4 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Thanks for the report.