When the key does not exist in Redis, the get() returns None and this None gets passed as a string to decode(). In Django 3 decode() silently detected the error and returned an empty session, Django 4 additionally emits a log that the session data is corrupt.
Approach
Handle case where get in Redis session backend returns None.
This code is now in-line with what django.contrib.sessions.backend.cache does.
Purpose
When the key does not exist in Redis, the
get()
returnsNone
and thisNone
gets passed as a string todecode()
. In Django 3decode()
silently detected the error and returned an empty session, Django 4 additionally emits a log that the session data is corrupt.Approach
Handle case where
get
in Redis session backend returnsNone
. This code is now in-line with whatdjango.contrib.sessions.backend.cache
does.Where should the reviewer start?
decode()
fails: https://github.com/django/django/blob/3.2.19/django/contrib/sessions/backends/base.py#L159decode()
fails (fallback to_legacy_decode()
is removed): https://github.com/django/django/blob/4.2.2/django/contrib/sessions/backends/base.py#L108Quality assurance
I have a test case but since the test suite is not running on latest Python due to
nose
being unmaintained it I didn't add it in this PR.I have also been running this patched version in a production environment for a few weeks.