diff --git a/sentry_sdk/scrubber.py b/sentry_sdk/scrubber.py index 5f9a3e5df6..f6bd994bcb 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", @@ -84,9 +83,13 @@ def __init__( ) self.denylist += pii_denylist - self.denylist = [x.lower() 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 @@ -114,7 +117,7 @@ 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: + 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 diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index f15a7ec36a..129aff3a51 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -260,7 +260,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 b57517383c..edd833fd39 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 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]"