Skip to content

Commit

Permalink
this fixes another type of issue, fwiw it wasn't working beforehand e…
Browse files Browse the repository at this point in the history
…ither
  • Loading branch information
dmarticus committed Aug 7, 2024
1 parent 4457361 commit 5183042
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 11 additions & 3 deletions posthog/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,17 +487,25 @@ def test_base64_decode(self):
no_padding = "SGVsbG8sIFdvcmxkIQ"
self.assertEqual(base64_decode(no_padding), simple_string)

# Test with real URL encoded data
# Several tests with real URL encoded data
# from: https://posthog.sentry.io/issues/5680826999/
encoded_data = b"data=eyJ0b2tlbiI6InBoY191eEl4QmhLQ2NVZll0d1NoTmhlRVMyNTJBak45b0pYNzZmcElybTV3cWpmIiwiZGlzdGluY3RfaWQiOiIwMTkxMjliNi1kNTQwLTczZjUtYjY3YS1kODI3MTEzOWFmYTYiLCJncm91cHMiOnt9fQ%3D%3D"

encoded_data = "eyJ0b2tlbiI6InBoY191eEl4QmhLQ2NVZll0d1NoTmhlRVMyNTJBak45b0pYNzZmcElybTV3cWpmIiwiZGlzdGluY3RfaWQiOiIwMTkxMjliNi1kNTQwLTczZjUtYjY3YS1kODI3MTEzOWFmYTYiLCJncm91cHMiOnt9fQ%3D%3D"
decoded = base64_decode(encoded_data)
decoded_json = json.loads(decoded)

self.assertEqual(decoded_json["token"], "phc_uxIxBhKCcUfYtwShNheES252AjN9oJX76fpIrm5wqjf")
self.assertEqual(decoded_json["distinct_id"], "019129b6-d540-73f5-b67a-d8271139afa6")
self.assertEqual(decoded_json["groups"], {})

# from: https://posthog.sentry.io/issues/5680826999/events/2ec7bfd975594873a84ce8153388c6e2/
encoded_data = b"eyJ0b2tlbiI6InBoY19JN3hJY09idHNrcDFWc2FFY0pPdEhycThrWGxrdVg3bGpwdnFWaDNJQ0Z6IiwiZGlzdGluY3RfaWQiOiIwMTkxMmU3Ny1hMjYwLTc5NWMtYjBmYy1lOWE4NzI5MWViNzAiLCJncm91cHMiOnt9fQ%3D%3D"
decoded = base64_decode(encoded_data)
decoded_json = json.loads(decoded)

self.assertEqual(decoded_json["token"], "phc_I7xIcObtskp1VsaEcJOtHrq8kXlkuX7ljpvqVh3ICFz")
self.assertEqual(decoded_json["distinct_id"], "01912e77-a260-795c-b0fc-e9a87291eb70")
self.assertEqual(decoded_json["groups"], {})


class TestFlatten(TestCase):
def test_flatten_lots_of_depth(self):
Expand Down
4 changes: 4 additions & 0 deletions posthog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,13 @@ def base64_decode(data):
data = data.encode("ascii")

# Check if the data is URL-encoded
# See
if data.startswith(b"data="):
data = unquote(data.decode("ascii")).split("=", 1)[1]
data = data.encode("ascii")
else:
# If it's not starting with 'data=', it might be just URL-encoded,
data = unquote(data.decode("ascii")).encode("ascii")

# Remove any whitespace and add padding if necessary
data = data.replace(b" ", b"")
Expand Down

0 comments on commit 5183042

Please sign in to comment.