Skip to content

Commit

Permalink
Fix incorrect str/bytes type of session data. (#645)
Browse files Browse the repository at this point in the history
Works with:
- MySQL column type `TEXT` and `VARBINARY`
- PostgreSQL column type `TEXT` and `bytea`
  • Loading branch information
iredmail committed Jun 9, 2020
1 parent ee8a3f5 commit 8e789a7
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion web/session.py
Expand Up @@ -239,6 +239,9 @@ def encode(self, session_dict):

def decode(self, session_data):
"""decodes the data to get back the session dict """
if isinstance(session_data, str):
session_data = session_data.encode()

pickled = decodebytes(session_data)
return pickle.loads(pickled)

Expand Down Expand Up @@ -344,7 +347,10 @@ def __getitem__(self, key):
return self.decode(s.data)

def __setitem__(self, key, value):
pickled = self.encode(value)
# Remove the leading `b` of bytes object (`b"..."`), otherwise encoded
# value is invalid base64 format.
pickled = self.encode(value).decode()

now = datetime.datetime.now()
if key in self:
self.db.update(
Expand Down

0 comments on commit 8e789a7

Please sign in to comment.