Skip to content

fix(scrubber): Match hyphenated header keys against the denylist - #8305

Draft
ratomirjovanovic wants to merge 2 commits into
getsentry:masterfrom
ratomirjovanovic:fix/scrubber-header-keys
Draft

ratomirjovanovic wants to merge 2 commits into
getsentry:masterfrom
ratomirjovanovic:fix/scrubber-header-keys

Conversation

@ratomirjovanovic

@ratomirjovanovic ratomirjovanovic commented Nov 17, 2026 •

Copy link
Copy Markdown

Description

Several entries in DEFAULT_DENYLIST use WSGI environ spelling (x_csrftoken, x_api_key, set_cookie), but request headers reach EventScrubber hyphenated (X-Csrftoken) or lowercase with hyphens (x-api-key), so those entries never matched a header. scrub_dict now folds hyphens and underscores together on both sides of the comparison, so a custom denylist=["x-api-key"] matches either spelling as well.

Follow-ups this needs, as discussed in the issue:

  • x_forwarded_for was in DEFAULT_DENYLIST and in DEFAULT_PII_DENYLIST. With matching fixed it would be scrubbed even with send_default_pii=True, so it is removed from the first list and stays PII-only.
  • With send_default_pii=False, the PII list now also matches headers: X-Forwarded-For, X-Real-Ip and remote-addr (added by ASGI test clients) show up as [Filtered]. I updated the WSGI and ASGI tests that assumed they were never matched.

Tests added in tests/test_scrubber.py (hyphenated headers with send_default_pii on and off, custom denylist with either spelling). The new tests fail on master and pass with this change.

Verified locally: tests/test_scrubber.py and the wsgi, asgi and flask integration suites (314 passed), plus ruff check, ruff format --check and mypy on the changed files. I could not run aws_lambda tests locally; the one that failed in CI already expects [Filtered], so it should pass again.

Issues

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.

@sentrivana sentrivana left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ratomirjovanovic, requesting two small changes, otherwise looks good to me.

Edited to add: Please check the failing tests, too.

Comment thread sentry_sdk/scrubber.py Outdated
Comment on lines +62 to +63
def _normalize_key(key: str) -> str:
return key.lower().replace("-", "_")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a private method of the EventScrubber please

Comment thread sentry_sdk/scrubber.py Outdated
Comment on lines +90 to +92
# 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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.

It's pretty self-explanatory without the comment, let's remove it

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.
@ratomirjovanovic

Copy link
Copy Markdown
Author

Pushed both changes. The CI failures came from the new matching also hitting X-Forwarded-For and remote-addr when PII is off, so I updated those test expectations.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EventScrubber's header entries in DEFAULT_DENYLIST never match request headers

2 participants