Skip to content

Commit

Permalink
Vendor the PickleSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Mar 7, 2024
1 parent 9cfd877 commit 4986ee8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/nav/django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
'django.contrib.messages.middleware.MessageMiddleware',
)

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
SESSION_SERIALIZER = 'nav.web.session_serializer.PickleSerializer'
SESSION_ENGINE = 'django.contrib.sessions.backends.db'
SESSION_COOKIE_AGE = int(_webfront_config.get('sessions', {}).get('timeout', 3600))
SESSION_COOKIE_NAME = 'nav_sessionid'
Expand Down
24 changes: 24 additions & 0 deletions python/nav/web/session_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pickle

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured


class PickleSerializer:
"""
Simple wrapper around pickle to be used in signing.dumps()/loads() and
cache backends.
"""

def __init__(self, protocol=None):
if settings.SESSION_ENGINE == 'django.contrib.sessions.backends.signed_cookies':
raise ImproperlyConfigured(
"PickleSerializer cannot be used with signed_cookies SESSION_ENGINE"
)
self.protocol = pickle.HIGHEST_PROTOCOL if protocol is None else protocol

def dumps(self, obj):
return pickle.dumps(obj, self.protocol)

def loads(self, data):
return pickle.loads(data)

0 comments on commit 4986ee8

Please sign in to comment.