Skip to content

refactor: Add recursive scrubbing to EventScrubber with tests #2755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions sentry_sdk/scrubber.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,36 @@


class EventScrubber(object):
def __init__(self, denylist=None):
# type: (Optional[List[str]]) -> None
def __init__(self, denylist=None, recursive=False):
# type: (Optional[List[str]], bool) -> None
self.denylist = DEFAULT_DENYLIST if denylist is None else denylist
self.denylist = [x.lower() for x in self.denylist]
self.recursive = recursive

def scrub_list(self, lst):
# type: (List[Any]) -> None
if not isinstance(lst, list):
return

for v in lst:
if isinstance(v, dict):
self.scrub_dict(v)
elif isinstance(v, list):
self.scrub_list(v)

def scrub_dict(self, d):
# type: (Dict[str, Any]) -> None
if not isinstance(d, dict):
return

for k in d.keys():
for k, v in d.items():
if isinstance(k, string_types) and k.lower() in self.denylist:
d[k] = AnnotatedValue.substituted_because_contains_sensitive_data()
elif self.recursive:
if isinstance(v, dict):
self.scrub_dict(v)
elif isinstance(v, list):
self.scrub_list(v)

def scrub_request(self, event):
# type: (Event) -> None
Expand Down
15 changes: 15 additions & 0 deletions tests/test_scrubber.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,18 @@ def test_scrubbing_doesnt_affect_local_vars(sentry_init, capture_events):
(frame,) = frames
assert frame["vars"]["password"] == "[Filtered]"
assert password == "cat123"


def test_recursive_event_scrubber(sentry_init, capture_events):
sentry_init(event_scrubber=EventScrubber(recursive=True))
events = capture_events()
complex_structure = {
"deep": {
"deeper": [{"deepest": {"password": "my_darkest_secret"}}],
},
}

capture_event({"extra": complex_structure})

(event,) = events
assert event["extra"]["deep"]["deeper"][0]["deepest"]["password"] == "'[Filtered]'"