Skip to content

Commit 23d5d18

Browse files
committed
Compress only tokens that start with dot
1 parent 2609c83 commit 23d5d18

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

mergin/client.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,26 @@ def decode_token_data(token):
6666
if not token.startswith(token_prefix):
6767
raise TokenError(f"Token doesn't start with 'Bearer ': {token}")
6868
try:
69-
data = token[len(token_prefix) :]
70-
71-
if data.startswith('.'):
72-
data = data.lstrip(".")
69+
token_raw = token[len(token_prefix) :]
70+
is_compressed = False
71+
72+
# compressed tokens start with dot,
73+
# see https://github.com/pallets/itsdangerous/blob/main/src/itsdangerous/url_safe.py#L55
74+
if token_raw.startswith('.'):
75+
token_raw = token_raw.lstrip(".")
76+
is_compressed = True
7377

74-
data = data.split(".")[0]
78+
payload_raw = token_raw.split(".")[0]
79+
7580
# add proper base64 padding
76-
data += "=" * (-len(data) % 4)
77-
data = base64.urlsafe_b64decode(data)
81+
payload_raw += "=" * (-len(payload_raw) % 4)
82+
payload_data = base64.urlsafe_b64decode(payload_raw)
7883

79-
try:
80-
data = zlib.decompress(data)
81-
except zlib.error as e:
82-
print("There was an issue during decompression, continuing without it, error:", e)
84+
if is_compressed:
85+
payload_data = zlib.decompress(payload_data)
8386

84-
return json.loads(data)
87+
return json.loads(payload_data)
88+
8589
except (IndexError, TypeError, ValueError, zlib.error):
8690
raise TokenError(f"Invalid token data: {token}")
8791

0 commit comments

Comments
 (0)