From 9445305964db81565fe8d92256b9a22aa0cf80fe Mon Sep 17 00:00:00 2001 From: Ratomir Jovanovic Date: Tue, 17 Nov 2026 10:58:12 +0100 Subject: [PATCH 1/2] 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]" From 5decde5fc3668a6ae5dbd8e8b7fc0cf349c520d6 Mon Sep 17 00:00:00 2001 From: Ratomir Jovanovic Date: Tue, 17 Nov 2026 18:08:18 +0100 Subject: [PATCH 2/2] ref(scrubber): Address review and update affected tests Make the key normalizer a private method of EventScrubber, drop the redundant comment and the AnnotatedValue skip. Update the WSGI and ASGI tests whose expectations assumed that header names such as X-Forwarded-For and remote-addr were never matched by the denylist. --- sentry_sdk/scrubber.py | 19 +++++++------------ tests/integrations/asgi/test_asgi.py | 2 +- tests/integrations/wsgi/test_wsgi.py | 15 +++------------ 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/sentry_sdk/scrubber.py b/sentry_sdk/scrubber.py index ae46c2d6c7..f6bd994bcb 100644 --- a/sentry_sdk/scrubber.py +++ b/sentry_sdk/scrubber.py @@ -59,10 +59,6 @@ ] -def _normalize_key(key: str) -> str: - return key.lower().replace("-", "_") - - class EventScrubber: def __init__( self, @@ -87,12 +83,13 @@ def __init__( ) self.denylist += pii_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.denylist = [self._normalize_key(x) for x in self.denylist] self.recursive = recursive + @staticmethod + def _normalize_key(key: str) -> str: + return key.lower().replace("-", "_") + def scrub_list(self, lst: object) -> None: """ If a list is passed to this method, the method recursively searches the list and any @@ -120,10 +117,8 @@ 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 _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() + if isinstance(k, str) and self._normalize_key(k) in self.denylist: + 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/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index cae3a24cb4..b9098db966 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -262,7 +262,7 @@ async def test_capture_transaction( assert transaction_event["request"] == { "headers": { "host": "localhost", - "remote-addr": "127.0.0.1", + "remote-addr": "127.0.0.1" if should_send_pii else "[Filtered]", "user-agent": "ASGI-Test-Client", }, "method": "GET", diff --git a/tests/integrations/wsgi/test_wsgi.py b/tests/integrations/wsgi/test_wsgi.py index 2cda3a1b98..53e29fb7e6 100644 --- a/tests/integrations/wsgi/test_wsgi.py +++ b/tests/integrations/wsgi/test_wsgi.py @@ -873,12 +873,9 @@ def test_request_headers_legacy_no_pii_redacts_sensitive( the legacy ``send_default_pii`` behaviour. When PII is disabled, headers in ``SENSITIVE_HEADERS`` are replaced with an ``AnnotatedValue`` (the default ``use_annotated_value=True`` on the event-processor call site), which - serializes to an emptied value plus a ``_meta`` annotation. Non-sensitive + serializes to an emptied value. The ``EventScrubber`` also scrubs these + headers when PII is disabled, so they end up as ``[Filtered]``. Non-sensitive headers pass through untouched. - - ``X-Forwarded-For`` is used because it is in ``SENSITIVE_HEADERS`` but is - not scrubbed by the default ``EventScrubber``, so the substitution we are - asserting on can only come from ``_filter_headers``. """ sentry_init(send_default_pii=False) app = SentryWsgiMiddleware(crashing_app) @@ -896,15 +893,9 @@ def test_request_headers_legacy_no_pii_redacts_sensitive( (event,) = events - assert event["request"]["headers"]["X-Forwarded-For"] == "" + assert event["request"]["headers"]["X-Forwarded-For"] == "[Filtered]" assert event["request"]["headers"]["X-Custom-Header"] == "passthrough" - # The emptied value is accompanied by a `_meta` annotation marking it as - # removed, confirming the substitution came from the AnnotatedValue path. - assert event["_meta"]["request"]["headers"]["X-Forwarded-For"] == { - "": {"rem": [["!config", "x"]]} - } - def test_request_headers_data_collection_off_collects_no_headers( sentry_init, crashing_app, capture_events