Skip to content

Commit 46e8b05

Browse files
committed
Handle non-utf8 bytes
1 parent 9659b1f commit 46e8b05

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

sentry_sdk/integrations/redis/utils.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,23 @@ def _get_safe_command(name, args):
4444
return command
4545

4646

47+
def _safe_decode(key):
48+
# type: (Any) -> str
49+
if isinstance(key, bytes):
50+
try:
51+
return key.decode()
52+
except UnicodeDecodeError:
53+
return ""
54+
55+
return key
56+
57+
4758
def _key_as_string(key):
4859
# type: (Any) -> str
4960
if isinstance(key, (dict, list, tuple)):
50-
key = ", ".join(x.decode() if isinstance(x, bytes) else x for x in key)
61+
key = ", ".join(_safe_decode(x) for x in key)
5162
elif isinstance(key, bytes):
52-
key = key.decode()
63+
key = _safe_decode(key)
5364
elif key is None:
5465
key = ""
5566
else:

tests/integrations/django/test_cache_module.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import pytest
21
import os
32
import random
3+
import uuid
44

5+
import pytest
56
from django import VERSION as DJANGO_VERSION
6-
77
from werkzeug.test import Client
88

99
try:
@@ -358,6 +358,7 @@ def test_cache_spans_templatetag(
358358
("get", None, None, ""),
359359
("get", [], {}, ""),
360360
("get", ["bla", "blub", "foo"], {}, "bla"),
361+
("get", [uuid.uuid4().bytes], {}, ""),
361362
(
362363
"get_many",
363364
[["bla1", "bla2", "bla3"], "blub", "foo"],

tests/integrations/redis/test_redis_cache_module.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import uuid
2+
13
import pytest
24

35
import fakeredis
@@ -230,6 +232,18 @@ def test_cache_data(sentry_init, capture_events):
230232
{"key": [b"bla", "blub", "foo"]},
231233
(b"bla", "blub", "foo"),
232234
),
235+
(
236+
"not-important",
237+
None,
238+
{"key": b"\x00c\x0f\xeaC\xe1L\x1c\xbff\xcb\xcc\xc1\xed\xc6\t"},
239+
(b"\x00c\x0f\xeaC\xe1L\x1c\xbff\xcb\xcc\xc1\xed\xc6\t",),
240+
),
241+
(
242+
"get",
243+
[b"\x00c\x0f\xeaC\xe1L\x1c\xbff\xcb\xcc\xc1\xed\xc6\t"],
244+
None,
245+
(b"\x00c\x0f\xeaC\xe1L\x1c\xbff\xcb\xcc\xc1\xed\xc6\t",),
246+
),
233247
],
234248
)
235249
def test_get_safe_key(method_name, args, kwargs, expected_key):
@@ -251,6 +265,7 @@ def test_get_safe_key(method_name, args, kwargs, expected_key):
251265
"bla",
252266
),
253267
(["bla", "blub", "foo"], "bla, blub, foo"),
268+
([uuid.uuid4().bytes], ""),
254269
],
255270
)
256271
def test_key_as_string(key, expected_key):

0 commit comments

Comments
 (0)