-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(core): Filter collected HTTP bodies and redact browser GraphQL document literals #24178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
s1gr1d
wants to merge
9
commits into
develop
Choose a base branch
from
sig/http-body-filtering
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c943d35
fix(core): Filter collected HTTP bodies and redact browser GraphQL do…
s1gr1d 9c9af4a
Merge branch 'develop' into sig/http-body-filtering
s1gr1d d553f79
additional fixes and tests
s1gr1d 2216e26
add trcp and trimming
s1gr1d 4911a6c
simplify scrubbing
s1gr1d 253015b
Merge branch 'develop' into sig/http-body-filtering
s1gr1d 7eca651
fix
s1gr1d e93cb39
fix test
s1gr1d 4190fe4
form body additions
s1gr1d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { isPlainObject } from '../is'; | ||
| import { FILTERED_VALUE } from './filtering-snippets'; | ||
| import { shouldFilterDataKey } from './filterKeyValueData'; | ||
| import { filterQueryParams } from './filterQueryParams'; | ||
|
|
||
| /** One `&`-separated form segment: empty, a bare key, or `key=value`. */ | ||
| const FORM_SEGMENT_RE = /^(?:[^=]+(?:=.*)?)?$/; | ||
|
|
||
| /** | ||
| * A form body is `&`-separated `key=value` pairs, the only non-JSON shape whose keys the denylist | ||
| * can check. Valueless keys, empty segments, and a trailing `&` are tolerated — a too-strict gate | ||
| * would let a body like `password=secret&` skip the filter and ship raw. At least one `=` is | ||
| * required so prose is never rewritten as a pseudo-form. | ||
| */ | ||
| function isFormBody(body: string): boolean { | ||
| return body.includes('=') && body.split('&').every(segment => FORM_SEGMENT_RE.test(segment)); | ||
| } | ||
|
|
||
| /** | ||
| * Scrubs the values of known-sensitive keys in an HTTP body the SDK collected itself, before it | ||
| * becomes `request.data` or `http.request.body.data`. | ||
| * | ||
| * Only values the SDK can attribute to a sensitive key are replaced. Everything else passes | ||
| * through unchanged: Relay scrubs server-side anyway and cannot tell an SDK-filtered value from a | ||
| * literal one, so client-side filtering beyond known-sensitive keys only destroys data. | ||
| */ | ||
| export function filterCollectedHttpBody(body: unknown): unknown { | ||
| if (typeof body === 'string') { | ||
| return filterCollectedHttpBodyString(body); | ||
| } | ||
|
|
||
| return isPlainObject(body) || Array.isArray(body) ? filterBodyValue(body) : body; | ||
| } | ||
|
|
||
| /** | ||
| * String-only variant of {@link filterCollectedHttpBody}. Capture sites call this before they | ||
| * truncate, because a truncated JSON body no longer parses and would pass through unfiltered. | ||
| */ | ||
| export function filterCollectedHttpBodyString(body: string): string { | ||
| if (!body) { | ||
| return body; | ||
| } | ||
|
|
||
| try { | ||
| const json: unknown = JSON.parse(body); | ||
| if (typeof json === 'object' && json !== null) { | ||
| return JSON.stringify(filterBodyValue(json)); | ||
| } | ||
| } catch { | ||
| // Not JSON. The form-encoded attempt below runs instead. | ||
| } | ||
|
|
||
| if (isFormBody(body)) { | ||
| // The query-param filter keeps the body's original encoding byte-for-byte. | ||
| return filterQueryParams(body, true) ?? body; | ||
| } | ||
|
|
||
| return body; | ||
| } | ||
|
|
||
| function filterBodyValue(value: unknown): unknown { | ||
| if (Array.isArray(value)) { | ||
| return value.map(filterBodyValue); | ||
| } | ||
|
|
||
| if (!isPlainObject(value)) { | ||
| return value; | ||
| } | ||
|
|
||
| // `Object.fromEntries` instead of assigning `result[key]`, so user-controlled keys like | ||
| // `__proto__` never hit a computed property write (CodeQL js/remote-property-injection). | ||
| return Object.fromEntries( | ||
| Object.entries(value).map(([key, nested]) => [ | ||
| key, | ||
| shouldFilterDataKey(key, true) ? FILTERED_VALUE : filterBodyValue(nested), | ||
| ]), | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The
isFormBody()function misidentifies XML with attributes as form-encoded, causingfilterQueryParams()to corrupt the XML body when sensitive attribute names are present.Severity: HIGH
Suggested Fix
Before applying form-body filtering, check the request's
Content-Typeheader to ensure it is actuallyapplication/x-www-form-urlencoded. Alternatively, make the regex inisFormBody()stricter to avoid matching XML structures.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.