From 9445305964db81565fe8d92256b9a22aa0cf80fe Mon Sep 17 00:00:00 2001 From: Ratomir Jovanovic Date: Tue, 17 Nov 2026 10:58:12 +0100 Subject: [PATCH] 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. --- sentry_sdk/scrubber.py | 16 ++++++++++++---- tests/test_scrubber.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/scrubber.py b/sentry_sdk/scrubber.py index 5f9a3e5df6..ae46c2d6c7 100644 --- a/sentry_sdk/scrubber.py +++ b/sentry_sdk/scrubber.py @@ -31,7 +31,6 @@ "sessionid", # wsgi "x_csrftoken", - "x_forwarded_for", "set_cookie", "cookie", "authorization", @@ -60,6 +59,10 @@ ] +def _normalize_key(key: str) -> str: + return key.lower().replace("-", "_") + + class EventScrubber: def __init__( self, @@ -84,7 +87,10 @@ def __init__( ) self.denylist += pii_denylist - self.denylist = [x.lower() for x in self.denylist] + # Header keys reach the scrubber hyphenated ("X-Api-Key"), while several + # entries use WSGI environ spelling ("x_api_key"), so compare both sides + # with hyphens and underscores folded together. + self.denylist = [_normalize_key(x) for x in self.denylist] self.recursive = recursive def scrub_list(self, lst: object) -> None: @@ -114,8 +120,10 @@ def scrub_dict(self, d: object) -> None: for k, v in d.items(): # The cast is needed because mypy is not smart enough to figure out that k must be a # string after the isinstance check. - if isinstance(k, str) and k.lower() in self.denylist: - d[k] = AnnotatedValue.substituted_because_contains_sensitive_data() + if isinstance(k, str) and _normalize_key(k) in self.denylist: + # Keep a value that was already removed and annotated earlier. + if not isinstance(v, AnnotatedValue): + d[k] = AnnotatedValue.substituted_because_contains_sensitive_data() elif self.recursive: self.scrub_dict(v) # no-op unless v is a dict self.scrub_list(v) # no-op unless v is a list diff --git a/tests/test_scrubber.py b/tests/test_scrubber.py index dd8c3fa45a..83092db181 100644 --- a/tests/test_scrubber.py +++ b/tests/test_scrubber.py @@ -1,6 +1,8 @@ import logging import sys +import pytest + from sentry_sdk import capture_event, capture_exception, start_span, start_transaction from sentry_sdk.scrubber import EventScrubber from sentry_sdk.utils import event_from_exception @@ -336,3 +338,43 @@ def test_recursive_scrubber_does_not_override_original(sentry_init, capture_even (frame,) = frames assert data["csrf"] == "secret" assert frame["vars"]["data"]["csrf"] == "[Filtered]" + + +@pytest.mark.parametrize("send_default_pii", [False, True]) +def test_hyphenated_header_scrubbing(sentry_init, capture_events, send_default_pii): + """Header keys are hyphenated, while denylist entries such as x_csrftoken + and x_api_key use underscores. Both spellings must match.""" + sentry_init(send_default_pii=send_default_pii) + events = capture_events() + + capture_event( + { + "request": { + "headers": { + "X-Csrftoken": "secret", + "x-api-key": "secret", + "Set-Cookie": "secret", + "Origin": "google.com", + }, + } + } + ) + + (event,) = events + assert event["request"]["headers"] == { + "X-Csrftoken": "[Filtered]", + "x-api-key": "[Filtered]", + "Set-Cookie": "[Filtered]", + "Origin": "google.com", + } + + +def test_custom_denylist_matches_either_spelling(sentry_init, capture_events): + sentry_init(event_scrubber=EventScrubber(denylist=["x-custom-token"])) + events = capture_events() + + capture_event({"extra": {"x_custom_token": "secret", "X-Custom-Token": "secret"}}) + + (event,) = events + assert event["extra"]["x_custom_token"] == "[Filtered]" + assert event["extra"]["X-Custom-Token"] == "[Filtered]"