Skip to content

ref: Add internal sensitive-data denylist and key-value filter utility - #5440

Merged
jamescrosswell merged 20 commits into
mainfrom
feat/sensitive-data-filter
Nov 8, 2026
Merged

jamescrosswell merged 20 commits into
mainfrom
feat/sensitive-data-filter

Conversation

@jamescrosswell

@jamescrosswell jamescrosswell commented Jul 22, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #5421

Part of #5420. Was stacked on #5853; that has now merged, so this targets main and stands on its own.

Phase 0 of the DataCollection migration (spec 0.15.0): the internal sensitive-terms denylist and the shared key-value filtering utility that later PRs wire into header, cookie and query-string collection (#5422, #5423, #5424).

SensitiveDataFilter holds the canonical sensitive denylist, cookie-name-only extra terms, the extended GDPR/PII terms (for the SendDefaultPii bridge in #5426), and FilterKeyValueData/FilterValue implementing the spec's semantics: key names always preserved, sensitive values replaced with [Filtered], built-in denylist applied in every mode including allow-list.

It consumes the public KeyValueCollectionBehavior from #5853 rather than defining its own type. An earlier revision of this branch carried an internal KeyValueFilterBehavior/KeyValueFilterMode pair, written when Phase 0 was expected to land before the public option shape. #5853 has since defined that shape with identical modes and semantics, so the internal pair was duplication and has been removed.

Internal only, and nothing is wired up yet, so there is no public API or behavior change.

Cross-SDK alignment

Verified against io.sentry.util.HttpUtils in sentry-java (shipped in 8.58.0), which is the closest analogue to .NET. Three deliberate decisions where the implementations differ:

  1. cookie/set-cookie are not denylist terms. The spec's canonical denylist is exactly 17 terms and excludes them. Java keeps the list canonical and applies an exact equalsIgnoreCase check for Cookie/Set-Cookie inside header filtering only. Treating them as substring denylist terms (as the JS SDK does) bleeds into every category — a query param ?cookie_consent=true, a cookie named cookie_prefs or a header X-Cookie-Debug would all be filtered, which neither the spec nor Java does. The exact-match rule for cookie headers therefore belongs at the header call site, and is scoped to DataCollection phase 0: always redact sensitive header values #5422.

  2. Cookie-name extra terms are kept, as a deliberately more conservative choice than Java. SensitiveCookieNameTerms covers opaque session, sticky-routing and identity-provider cookie names (remember, oidc, pkce, nonce, __secure-, __host-, awsalb, akamai, __stripe, cognito, firebase, supabase, sb-, mfa, 2fa). These are not in the spec and Java has no equivalent, so a cookie such as remember_me is filtered here but not by the Java SDK. Terms already implied by a canonical match were dropped — .sid and sessid both already match sid by substring — so the list contains only terms that actually add coverage.

  3. Query parameter names are decoded before matching, never before storing. DecodeKeyForMatching percent-decodes a key so %74oken=secret is recognised, mirroring Java's URLDecoder use in filterQueryParams. Callers emit the original key and value, which spec 0.11.0 requires: encoded query parameters must be stored exactly as they appear in the URL. Used by DataCollection phase 0: scrub sensitive URL query parameters #5424.

    The decode is wrapped in a catch (UriFormatException) that returns the key undecoded, matching Java's decodeQueryParamName. Uri.UnescapeDataString returns a malformed escape unchanged on modern .NET but throws on .NET Framework; CI only exercises the 4.8 runtime, where it does not throw, so the catch is an intentionally uncovered branch rather than a missing test. It is there because we target net462, 4.6.2–4.7.x is untested, and DataCollection phase 0: scrub sensitive URL query parameters #5424 will feed untrusted query keys through this while enriching an event.

Notes for review

  • Empty and null terms are ignored when matching. "Accept".Contains("") is true in .NET, so a single empty term matches every key: in deny-list mode it filters everything, in allow-list mode it sends everything. The second is the hazard — an apparently restrictive config producing the most permissive outcome.

    KeyValueCollectionBehavior strips null and empty terms in its constructor, and every route reaches that constructor, configuration included: BindableDataCollectionOptions builds through DenyList(...)/AllowList(...). So behavior.Terms cannot carry an empty term, and for that argument this guard is belt-and-braces. Nothing meaningful is discarded either — the two things an empty term could express are already spelled AllowList() and DenyList() with no terms.

    The guard is load-bearing for additionalDenyTerms, which call sites pass in directly and which never passes through that type. That is where its tests now live; the four behavior-level cases they replace duplicated DataCollectionOptionsTests.

    For reference, sentry-java places the equivalent check at match time only — HttpUtils.containsTerm tests term != null && !term.isEmpty() — and its setTerms deliberately does not sanitize, so there the invariant depends on every consumer going through that helper.

  • MatchesAny treats a null term list as "no terms match". behavior.Terms is guaranteed non-null, but additionalDenyTerms is genuinely nullable, so the call site passes it straight to MatchesAny instead of null-checking it twice.

  • FilterKeyValueData returns an ordered list rather than a dictionary, preserving input order and repeated keys. A dictionary silently collapses duplicates, and Set-Cookie legitimately repeats; spec 0.15.0 also now represents cookies as an ordered array of name=value elements.

  • MatchesAny iterates by index rather than foreach, since IReadOnlyList<string> allocates an enumerator where an array doesn't, and this runs per key per event.

#skip-changelog

🤖 Generated with Claude Code

First step of the DataCollection migration (#5420, #5421). Internal only,
nothing wired up yet. Mirrors filterKeyValueData/filtering-snippets from
the JS reference implementation.

Co-Authored-By: Claude Fable 5 <[email protected]>
@codecov

codecov Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.10145% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.73%. Comparing base (eef24b1) to head (33775d4).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...try/Internal/DataCollection/SensitiveDataFilter.cs 97.10% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5440      +/-   ##
==========================================
+ Coverage   76.65%   76.73%   +0.08%     
==========================================
  Files         531      532       +1     
  Lines       19713    19782      +69     
  Branches     3895     3905      +10     
==========================================
+ Hits        15111    15180      +69     
- Misses       3716     3717       +1     
+ Partials      886      885       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

jamescrosswell and others added 7 commits November 2, 2026 17:36
Ignore empty and null terms when matching: an empty term would otherwise
filter every value in deny-list mode and allow every value through in
allow-list mode, which becomes reachable once terms are bound from
configuration.

Trim the denylist to the spec's canonical 17 terms. Treating cookie and
set-cookie as substring terms over-filtered every other category; the
exact header-name rule belongs at the header call site (#5422).

Decode keys before matching so an encoded key such as %74oken is
recognised, while callers still store the raw key and value.

Return an ordered list rather than a dictionary, so repeated headers such
as Set-Cookie are preserved instead of silently collapsed.

Co-Authored-By: Claude Opus 5 <[email protected]>
…ltPii

Adds the option shape from the DataCollection spec as a no-op: nothing reads
it yet. The resolver and SendDefaultPii bridge follow in #5426.

Co-Authored-By: Claude Opus 5 <[email protected]>
Uri.UnescapeDataString never throws on malformed input, it returns the
input unchanged, so the UriFormatException handler could not run.

Off mode now returns [Filtered] from FilterValue instead of being
asserted away, which drops the unreachable default switch arm and makes
the mode's result explicit.

Co-Authored-By: Claude Opus 5 <[email protected]>
Mirrors DataCollectionOptions in BindableSentryOptions so the option can be
set from appsettings.json, following the nested-bindable pattern used by the
platform options. KeyValueCollectionBehavior has no public constructor, so it
binds through a Mode/Terms surrogate.

Co-Authored-By: Claude Opus 5 <[email protected]>
…nable()

Assigning the option is now the opt-in signal, matching sentry-go, sentry-php,
sentry-python and sentry-javascript. Removes Enable() and the internal
bookkeeping it needed to distinguish an unset option from a default one.

Co-Authored-By: Claude Opus 5 <[email protected]>
…filter

The internal KeyValueFilterBehavior/KeyValueFilterMode pair was written
when this phase was expected to land before the public option shape. That
shape now exists with identical modes and semantics, so the internal pair
was duplication and is removed.

MatchesAny tolerates a null term list, because DenyList(null) leaves Terms
null and the SDK must not throw while capturing an event. It also iterates
by index, since IReadOnlyList allocates an enumerator where an array does
not and this runs per key per event.

Co-Authored-By: Claude Opus 5 <[email protected]>
@jamescrosswell
jamescrosswell changed the base branch from main to feat/data-collection-options November 3, 2026 00:33
jamescrosswell and others added 7 commits November 3, 2026 14:03
An explicit null for a params array is legal, so DenyList(null) and
AllowList(null) produced an instance whose Terms was null, which would throw
in any consumer that iterates it. Coalesce in the constructor so both
factories are covered.

Co-Authored-By: Claude Opus 5 <[email protected]>
additionalDenyTerms is nullable, so passing it straight to MatchesAny
replaces a second null check at the call site and keeps the guard
load-bearing now that KeyValueCollectionBehavior.Terms is never null.

Co-Authored-By: Claude Opus 5 <[email protected]>
The bool shorthand code has for cookies, headers and query params has no
configuration equivalent, so a scalar is dropped by the binder. Document the
supported shape on the public type and lock the behavior with a test.

Co-Authored-By: Claude Opus 5 <[email protected]>
Swaps the IncomingResponse/OutgoingResponse doc comments, which described the
wrong direction. Gives KeyValueCollectionBehavior a public constructor and
settable properties so the reflection binder can construct it: on
netstandard2.0 the options are bound directly rather than through
BindableSentryOptions, and any key-value category threw there. Binds
HttpBodies from an array so the spec's empty-array opt-out works instead of
silently collecting every body. Adds coverage for the four ApplyTo lines no
test reached.

Co-Authored-By: Claude Opus 5 <[email protected]>
…in config

A JSON empty array emits a valueless key with no children, which the binder
surfaces as null, so "[]" cannot be told apart from the option being absent
and did not disable body collection. The previous test asserted against a
hand-built list rather than the binder, so it did not catch this.

Co-Authored-By: Claude Opus 5 <[email protected]>
Base automatically changed from feat/data-collection-options to main November 5, 2026 22:28
jamescrosswell and others added 2 commits November 6, 2026 11:30
#5853 was squash-merged, so the branch commits this stack carried were no
longer ancestors of main. Resolve every DataCollection file to main's
version; this branch contributes only SensitiveDataFilter and its tests.

Co-Authored-By: Claude Opus 5 <[email protected]>
KeyValueCollectionBehavior sanitizes its own Terms as of #5853, so the
four behavior-level null/empty-term tests duplicated DataCollectionOptionsTests.
additionalDenyTerms is caller-supplied and unsanitized, so test the guard there.

Co-Authored-By: Claude Opus 5 <[email protected]>
@jamescrosswell jamescrosswell added the skip-changelog Suppress automatic changelog generation via Craft label Nov 5, 2026
@jamescrosswell
jamescrosswell marked this pull request as ready for review November 5, 2026 23:55
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 5, 2026

@ric-oliv ric-oliv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two optional comments only... looking good!

Verified by mutation: with the empty-term guard removed from MatchesAny
both stayed green, while the additionalDenyTerms theories failed. The
behavior type strips the term before it reaches the guard.

Also document that Off requires the caller to drop the key, since
FilterValue returns [Filtered] there only to fail closed.

Co-Authored-By: Claude Opus 5 <[email protected]>
Uri.UnescapeDataString returns the input unchanged for a malformed escape
on modern .NET but throws UriFormatException on .NET Framework. sentry-java
guards the same call in decodeQueryParamName. #5424 feeds untrusted query
keys through here, and the SDK must not throw while enriching an event.

Co-Authored-By: Claude Opus 5 <[email protected]>
@jamescrosswell
jamescrosswell merged commit ccf4820 into main Nov 8, 2026
60 checks passed
@jamescrosswell
jamescrosswell deleted the feat/sensitive-data-filter branch November 8, 2026 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium skip-changelog Suppress automatic changelog generation via Craft

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataCollection phase 0: sensitive-terms denylist and key-value filtering utility

2 participants