Skip to content

Commit 9445305

Browse files
fix(scrubber): Match hyphenated header keys against the denylist
Several default denylist entries use WSGI environ spelling (x_csrftoken, x_api_key, set_cookie), while request headers reach the scrubber hyphenated ("X-Csrftoken") or lowercase with hyphens ("x-api-key"), so those entries never matched. Fold hyphens and underscores together on both sides of the comparison. x_forwarded_for was listed in DEFAULT_DENYLIST as well as in DEFAULT_PII_DENYLIST, which would now scrub it even with send_default_pii=True, so drop it from the first list. Also leave a value alone when it is already an AnnotatedValue, so a header emptied by _filter_headers keeps its original annotation.
1 parent 26ef571 commit 9445305

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎sentry_sdk/scrubber.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"sessionid",
3232
# wsgi
3333
"x_csrftoken",
34-
"x_forwarded_for",
3534
"set_cookie",
3635
"cookie",
3736
"authorization",
@@ -60,6 +59,10 @@
6059
]
6160

6261

62+
def _normalize_key(key: str) -> str:
63+
return key.lower().replace("-", "_")
64+
65+
6366
class EventScrubber:
6467
def __init__(
6568
self,
@@ -84,7 +87,10 @@ def __init__(
8487
)
8588
self.denylist += pii_denylist
8689

87-
self.denylist = [x.lower() for x in self.denylist]
90+
# Header keys reach the scrubber hyphenated ("X-Api-Key"), while several
91+
# entries use WSGI environ spelling ("x_api_key"), so compare both sides
92+
# with hyphens and underscores folded together.
93+
self.denylist = [_normalize_key(x) for x in self.denylist]
8894
self.recursive = recursive
8995

9096
def scrub_list(self, lst: object) -> None:
@@ -114,8 +120,10 @@ def scrub_dict(self, d: object) -> None:
114120
for k, v in d.items():
115121
# The cast is needed because mypy is not smart enough to figure out that k must be a
116122
# string after the isinstance check.
117-
if isinstance(k, str) and k.lower() in self.denylist:
118-
d[k] = AnnotatedValue.substituted_because_contains_sensitive_data()
123+
if isinstance(k, str) and _normalize_key(k) in self.denylist:
124+
# Keep a value that was already removed and annotated earlier.
125+
if not isinstance(v, AnnotatedValue):
126+
d[k] = AnnotatedValue.substituted_because_contains_sensitive_data()
119127
elif self.recursive:
120128
self.scrub_dict(v) # no-op unless v is a dict
121129
self.scrub_list(v) # no-op unless v is a list

‎tests/test_scrubber.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22
import sys
33

4+
import pytest
5+
46
from sentry_sdk import capture_event, capture_exception, start_span, start_transaction
57
from sentry_sdk.scrubber import EventScrubber
68
from sentry_sdk.utils import event_from_exception
@@ -336,3 +338,43 @@ def test_recursive_scrubber_does_not_override_original(sentry_init, capture_even
336338
(frame,) = frames
337339
assert data["csrf"] == "secret"
338340
assert frame["vars"]["data"]["csrf"] == "[Filtered]"
341+
342+
343+
@pytest.mark.parametrize("send_default_pii", [False, True])
344+
def test_hyphenated_header_scrubbing(sentry_init, capture_events, send_default_pii):
345+
"""Header keys are hyphenated, while denylist entries such as x_csrftoken
346+
and x_api_key use underscores. Both spellings must match."""
347+
sentry_init(send_default_pii=send_default_pii)
348+
events = capture_events()
349+
350+
capture_event(
351+
{
352+
"request": {
353+
"headers": {
354+
"X-Csrftoken": "secret",
355+
"x-api-key": "secret",
356+
"Set-Cookie": "secret",
357+
"Origin": "google.com",
358+
},
359+
}
360+
}
361+
)
362+
363+
(event,) = events
364+
assert event["request"]["headers"] == {
365+
"X-Csrftoken": "[Filtered]",
366+
"x-api-key": "[Filtered]",
367+
"Set-Cookie": "[Filtered]",
368+
"Origin": "google.com",
369+
}
370+
371+
372+
def test_custom_denylist_matches_either_spelling(sentry_init, capture_events):
373+
sentry_init(event_scrubber=EventScrubber(denylist=["x-custom-token"]))
374+
events = capture_events()
375+
376+
capture_event({"extra": {"x_custom_token": "secret", "X-Custom-Token": "secret"}})
377+
378+
(event,) = events
379+
assert event["extra"]["x_custom_token"] == "[Filtered]"
380+
assert event["extra"]["X-Custom-Token"] == "[Filtered]"

0 commit comments

Comments
 (0)