8000 self-protect against broken Mapping implementations · getsentry/sentry-python@db41349 · GitHub
[go: up one dir, main page]

Skip to content

Commit db41349

Browse files
committed
self-protect against broken Mapping implementations
1 parent 7a1ba2f commit db41349

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

sentry_sdk/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,16 @@ def object_to_json(obj, remaining_depth=4, memo=None):
372372
]
373373

374374
if isinstance(obj, Mapping):
375+
try:
376+
items = list(obj.items())
377+
except Exception:
378+
return u"<broken items>"
379+
375380
return {
376381
safe_str(k): object_to_json(
377382
v, remaining_depth=remaining_depth - 1, memo=memo
378383
)
379-
for k, v in list(obj.items())
384+
for k, v in items
380385
}
381386

382387
return safe_repr(obj)

tests/test_client.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@
1818
)
1919
from sentry_sdk.hub import HubMeta
2020
from sentry_sdk.transport import Transport
21-
from sentry_sdk._compat import reraise, text_type
21+
from sentry_sdk._compat import reraise, text_type, PY2
2222
from sentry_sdk.utils import HAS_CHAINED_EXCEPTIONS
2323

24+
if PY2:
25+
# Importing ABCs from collections is deprecated, and will stop working in 3.8
26+
# https://github.com/python/cpython/blob/master/Lib/collections/__init__.py#L49
27+
from collections import Mapping
28+
else:
29+
# New in 3.3
30+
# https://docs.python.org/3/library/collections.abc.html
31+
from collections.abc import Mapping
32+
2433

2534
class EventCaptured(Exception):
2635
pass
@@ -404,3 +413,30 @@ def test_chained_exceptions(sentry_init, capture_events):
404413
event, = events
405414

406415
assert len(event["exception"]["values"]) == 2
416+
417+
418+
def test_broken_mapping(sentry_init, capture_events):
419+
sentry_init()
420+
events = capture_events()
421+
422+
class C(Mapping):
423+
def broken(self, *args, **kwargs):
424+
raise Exception("broken")
425+
< 939B /td>
426+
__getitem__ = broken
427+
__setitem__ = broken
428+
__delitem__ = broken
429+
__iter__ = broken
430+
__len__ = broken
431+
432+
try:
433+
a = C() # noqa
434+
1 / 0
435+
except Exception:
436+
capture_exception()
437+
438+
event, = events
439+
assert (
440+
event["exception"]["values"][0]["stacktrace"]["frames"][0]["vars"]["a"]
441+
== "<broken items>"
442+
)

0 commit comments

Comments
 (0)
0