Skip to content

Commit 61c19f1

Browse files
committed
Add serialize_str & unserialize_str
1 parent 3f6ba6a commit 61c19f1

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ lint:
44
pyright .
55

66
test:
7-
pytest -v --cov=encryptedcookie --cov-report=html
7+
pytest -v --cov=encryptedcookie --cov-report=html --cov-report=term

encryptedcookie/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ def serialize(
6565

6666
return string
6767

68+
def serialize_str(
69+
self, data: dict, expires: float | int | timedelta | None = None
70+
) -> str:
71+
string = self.serialize(data, expires)
72+
if self.quote_base64:
73+
return string.decode('ascii')
74+
else:
75+
return string.hex()
76+
6877
@classmethod
6978
def loads(cls, data: bytes) -> dict:
7079
return json.loads(data.decode())
@@ -90,7 +99,7 @@ def unserialize(self, string: bytes) -> dict:
9099
if self.quote_base64:
91100
try:
92101
string = base64.b64decode(string)
93-
except Exception:
102+
except ValueError:
94103
pass
95104

96105
payload = self.decrypt(string)
@@ -109,6 +118,16 @@ def unserialize(self, string: bytes) -> dict:
109118

110119
return data
111120

121+
def unserialize_str(self, string: str) -> dict:
122+
if self.quote_base64:
123+
data = string.encode()
124+
else:
125+
try:
126+
data = bytes.fromhex(string)
127+
except ValueError:
128+
data = string.encode()
129+
return self.unserialize(data)
130+
112131

113132
class SecureEncryptedCookie(EncryptedCookie):
114133
def encrypt(self, data: bytes) -> bytes:

tests.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def test_serialize_unserialize(self):
5353
r = cookie.unserialize(r)
5454
assert r == case
5555

56+
def test_serialize_str_unserialize_str(self):
57+
for Cookie in [self.Cookie, self.RawCookie]:
58+
cookie = Cookie(b'my little key')
59+
for case in [{'a': 'b'}, {'a': 'próba'}, {'próba': '123'}]:
60+
r = cookie.serialize_str(case)
61+
assert isinstance(r, str)
62+
63+
r = cookie.unserialize_str(r)
64+
assert r == case
65+
5666
def test_unserialize_binary(self):
5767
"""
5868
Test unserialize compatibility with existing binary data.
@@ -104,6 +114,31 @@ def test_fail_when_corrupted(self):
104114
r = cookie.unserialize(r[:20] + r[21:])
105115
assert not r
106116

117+
def test_fail_incorrect_str(self):
118+
cookie = self.Cookie(b'my little key')
119+
raw = self.RawCookie(b'my little key').serialize({'a': 'próba'})
120+
121+
# Send somthing which can't be base64 decoded
122+
r = cookie.unserialize_str(raw.hex())
123+
assert r == {}
124+
125+
# Send somthing not ascii at all
126+
r = cookie.unserialize_str(raw.decode('latin-1'))
127+
assert r == {}
128+
129+
def test_fail_incorrect_raw_str(self):
130+
b64 = self.Cookie(b'my little key').serialize({'a': 'próba'})
131+
raw_cookie = self.RawCookie(b'my little key')
132+
133+
# Send somthing which can't be hex decoded
134+
r = raw_cookie.unserialize_str(b64.decode())
135+
assert r == {}
136+
137+
# Send somthing not ascii at all
138+
raw = raw_cookie.serialize({'a': 'próba'})
139+
r = raw_cookie.unserialize_str(raw.decode('latin-1'))
140+
assert r == {}
141+
107142
def test_compression_and_decompression(self):
108143
key = b'my little key'
109144
case = {'a': 'próba'}

0 commit comments

Comments
 (0)