Skip to content

Commit a45ac33

Browse files
s1gr1dchargome
andauthored
ref(http): Use shared snippets for filtering headers and cookies (#20970)
First merge: #20967 --------- Co-authored-by: Charly Gomez <charly.gomez@sentry.io>
1 parent 410b4ee commit a45ac33

3 files changed

Lines changed: 69 additions & 64 deletions

File tree

dev-packages/cloudflare-integration-tests/suites/public-api/startSpan-streamed/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ it('sends a streamed span envelope with correct spans for a manually started spa
226226
},
227227
'http.request.header.cf_connecting_ip': {
228228
type: 'string',
229-
value: expect.stringMatching(/^(::1|127\.0\.0\.1)$/),
229+
value: '[Filtered]',
230230
},
231231
'http.request.header.host': {
232232
type: 'string',
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,62 @@
11
export const PII_HEADER_SNIPPETS = ['forwarded', '-ip', 'remote-', 'via', '-user'];
2+
3+
export const SENSITIVE_KEY_SNIPPETS = [
4+
'auth',
5+
'token',
6+
'secret',
7+
'session', // for the user_session cookie
8+
'password',
9+
'passwd',
10+
'pwd',
11+
'key',
12+
'jwt',
13+
'bearer',
14+
'sso',
15+
'saml',
16+
'csrf',
17+
'xsrf',
18+
'credentials',
19+
'sid',
20+
'identity',
21+
// Always treat cookie headers as sensitive in case individual key-value cookie pairs cannot properly be extracted
22+
'set-cookie',
23+
'cookie',
24+
];
25+
26+
/**
27+
* Extra substrings matched only against individual Cookie / Set-Cookie **names** (not header names),
28+
* so we can cover common session secrets that do not match {@link SENSITIVE_KEY_SNIPPETS}
29+
* (e.g. `connect.sid` does not contain `session`) without false positives on arbitrary HTTP headers.
30+
*
31+
* Cookie names are checked with the same `includes()` list as headers plus these entries; omit redundant
32+
* cookie-only snippets that are already implied by a header match (e.g. `oauth` → `auth`, `id_token` → `token`,
33+
* `next-auth` → `auth`).
34+
*/
35+
export const SENSITIVE_COOKIE_NAME_SNIPPETS = [
36+
// Express / Connect default session cookie
37+
'.sid',
38+
// Opaque session ids (PHPSESSID, ASPSESSIONID*, BIGipServer*, *sessid*, …)
39+
'sessid',
40+
// Laravel etc. "remember me" tokens
41+
'remember',
42+
// OIDC / OAuth auxiliary (`oauth*` covered by header snippet `auth`)
43+
'oidc',
44+
'pkce',
45+
'nonce',
46+
// RFC 6265bis high-security cookie name prefixes
47+
'__secure-',
48+
'__host-',
49+
// Load balancer / CDN sticky-session cookies (opaque routing tokens)
50+
'awsalb',
51+
'awselb',
52+
'akamai',
53+
// BaaS / IdP session cookies (names often omit "session")
54+
'__stripe',
55+
'cognito',
56+
'firebase',
57+
'supabase',
58+
'sb-',
59+
// Step-up / MFA cookies
60+
'mfa',
61+
'2fa',
62+
];

packages/core/src/utils/request.ts

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import type { RequestEventData } from '../types/request';
66
import type { WebFetchHeaders, WebFetchRequest } from '../types/webfetchapi';
77
import { debug } from './debug-logger';
88
import { safeUnref } from './timer';
9+
import {
10+
PII_HEADER_SNIPPETS,
11+
SENSITIVE_COOKIE_NAME_SNIPPETS,
12+
SENSITIVE_KEY_SNIPPETS,
13+
} from '../utils/data-collection/filtering-snippets';
914

1015
/**
1116
* Maximum size of incoming HTTP request bodies attached to events.
@@ -258,67 +263,6 @@ function getAbsoluteUrl({
258263
return undefined;
259264
}
260265

261-
const SENSITIVE_HEADER_SNIPPETS = [
262-
'auth',
263-
'token',
264-
'secret',
265-
'session', // for the user_session cookie
266-
'password',
267-
'passwd',
268-
'pwd',
269-
'key',
270-
'jwt',
271-
'bearer',
272-
'sso',
273-
'saml',
274-
'csrf',
275-
'xsrf',
276-
'credentials',
277-
// Always treat cookie headers as sensitive in case individual key-value cookie pairs cannot properly be extracted
278-
'set-cookie',
279-
'cookie',
280-
];
281-
282-
/**
283-
* Extra substrings matched only against individual Cookie / Set-Cookie **names** (not header names),
284-
* so we can cover common session secrets that do not match {@link SENSITIVE_HEADER_SNIPPETS}
285-
* (e.g. `connect.sid` does not contain `session`) without false positives on arbitrary HTTP headers.
286-
*
287-
* Cookie names are checked with the same `includes()` list as headers plus these entries; omit redundant
288-
* cookie-only snippets that are already implied by a header match (e.g. `oauth` → `auth`, `id_token` → `token`,
289-
* `next-auth` → `auth`).
290-
*/
291-
const SENSITIVE_COOKIE_NAME_SNIPPETS = [
292-
// Express / Connect default session cookie
293-
'.sid',
294-
// Opaque session ids (PHPSESSID, ASPSESSIONID*, BIGipServer*, *sessid*, …)
295-
'sessid',
296-
// Laravel etc. "remember me" tokens
297-
'remember',
298-
// OIDC / OAuth auxiliary (`oauth*` covered by header snippet `auth`)
299-
'oidc',
300-
'pkce',
301-
'nonce',
302-
// RFC 6265bis high-security cookie name prefixes
303-
'__secure-',
304-
'__host-',
305-
// Load balancer / CDN sticky-session cookies (opaque routing tokens)
306-
'awsalb',
307-
'awselb',
308-
'akamai',
309-
// BaaS / IdP session cookies (names often omit "session")
310-
'__stripe',
311-
'cognito',
312-
'firebase',
313-
'supabase',
314-
'sb-',
315-
// Step-up / MFA cookies
316-
'mfa',
317-
'2fa',
318-
];
319-
320-
const PII_HEADER_SNIPPETS = ['x-forwarded-', '-user'];
321-
322266
/**
323267
* Converts incoming HTTP request or response headers to OpenTelemetry span attributes following semantic conventions.
324268
* Header names are converted to the format: http.<request|response>.header.<key>
@@ -434,8 +378,8 @@ function handleHttpHeader(
434378
isCookieSubKey: boolean = false,
435379
): string | undefined {
436380
const snippetsForSensitivity = isCookieSubKey
437-
? [...SENSITIVE_HEADER_SNIPPETS, ...SENSITIVE_COOKIE_NAME_SNIPPETS]
438-
: SENSITIVE_HEADER_SNIPPETS;
381+
? [...SENSITIVE_KEY_SNIPPETS, ...SENSITIVE_COOKIE_NAME_SNIPPETS]
382+
: SENSITIVE_KEY_SNIPPETS;
439383

440384
const isSensitive = sendPii
441385
? snippetsForSensitivity.some(snippet => lowerCasedKey.includes(snippet))

0 commit comments

Comments
 (0)