Skip to content

Commit 5bf360a

Browse files
committed
Compress only tokens that start with dot
1 parent 2609c83 commit 5bf360a

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
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
77+
78+
payload_raw = token_raw.split(".")[0]
7379

74-
data = data.split(".")[0]
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)
86+
87+
return json.loads(payload_data)
8388

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

mergin/test/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test_login(mc):
165165

166166
with pytest.raises(LoginError, match="Invalid username or password"):
167167
mc.login("foo", "bar")
168-
168+
169169
valid_token_dot = "Bearer .eJxNi0kKgDAMAL8iubqQRuuSkz-RojkEWi0uIIh_Fz15G2aYC45N1kEnYJN9PLsgwOCijl5l3iEDCU793_VyuhC9FOMS3n5GXd-JkGyObU6UmI6pZoNFZRtbUorIiHA_KFshoA.abc.def"
170170
decoded_value = decode_token_data(valid_token_dot)
171171

0 commit comments

Comments
 (0)