Skip to content

Commit 459d124

Browse files
committed
fix cookie specific sensitive terms
1 parent 9e0d7ae commit 459d124

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎packages/core/src/utils/data-collection/filterCookies.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CollectBehavior } from '../../types/datacollection';
22
import { parseCookie } from '../cookie';
3-
import { FILTERED_VALUE as FILTERED } from './filtering-snippets';
3+
import { FILTERED_VALUE as FILTERED, SENSITIVE_COOKIE_NAME_SNIPPETS } from './filtering-snippets';
44
import { filterKeyValueData } from './filterKeyValueData';
55

66
/**
@@ -21,7 +21,7 @@ export function filterCookies(cookieString: string, behavior: CollectBehavior):
2121
return {};
2222
}
2323

24-
return filterKeyValueData(parsed, behavior);
24+
return filterKeyValueData(parsed, behavior, SENSITIVE_COOKIE_NAME_SNIPPETS);
2525
} catch {
2626
return FILTERED;
2727
}

‎packages/core/src/utils/data-collection/filterKeyValueData.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
import type { CollectBehavior } from '../../types/datacollection';
22
import { FILTERED_VALUE as FILTERED, SENSITIVE_KEY_SNIPPETS } from './filtering-snippets';
33

4-
function isSensitiveKey(key: string): boolean {
5-
const lower = key.toLowerCase();
6-
return SENSITIVE_KEY_SNIPPETS.some(snippet => lower.includes(snippet));
4+
function isSensitiveKey(lower: string, denySnippets: string[]): boolean {
5+
return denySnippets.some(snippet => lower.includes(snippet));
76
}
87

98
/**
109
* Filters a key-value record according to a `CollectBehavior`.
1110
*
1211
* Key names are always preserved. Values are either kept, replaced with
1312
* `[Filtered]`, or the entire record is dropped (off mode).
13+
*
14+
* @param additionalDenyTerms - Additional sensitive snippets to check beyond the built-in denylist (e.g. cookie-specific terms).
1415
*/
15-
export function filterKeyValueData(data: Record<string, string>, behavior: CollectBehavior): Record<string, string> {
16+
export function filterKeyValueData(
17+
data: Record<string, string>,
18+
behavior: CollectBehavior,
19+
additionalDenyTerms?: string[],
20+
): Record<string, string> {
1621
if (behavior === false) {
1722
return {};
1823
}
1924

25+
const denySnippets =
26+
additionalDenyTerms != null ? [...SENSITIVE_KEY_SNIPPETS, ...additionalDenyTerms] : SENSITIVE_KEY_SNIPPETS;
2027
const result: Record<string, string> = {};
2128

2229
if (behavior === true) {
2330
for (const key of Object.keys(data)) {
24-
result[key] = isSensitiveKey(key) ? FILTERED : data[key]!;
31+
result[key] = isSensitiveKey(key.toLowerCase(), denySnippets) ? FILTERED : data[key]!;
2532
}
2633
return result;
2734
}
@@ -30,7 +37,7 @@ export function filterKeyValueData(data: Record<string, string>, behavior: Colle
3037
const lowerTerms = behavior.deny.map(t => t.toLowerCase());
3138
for (const key of Object.keys(data)) {
3239
const lower = key.toLowerCase();
33-
const isDenied = isSensitiveKey(key) || lowerTerms.some(term => lower.includes(term));
40+
const isDenied = isSensitiveKey(lower, denySnippets) || lowerTerms.some(term => lower.includes(term));
3441
result[key] = isDenied ? FILTERED : data[key]!;
3542
}
3643
return result;
@@ -39,10 +46,10 @@ export function filterKeyValueData(data: Record<string, string>, behavior: Colle
3946
// allowList mode
4047
const lowerTerms = behavior.allow.map(t => t.toLowerCase());
4148
for (const key of Object.keys(data)) {
42-
if (isSensitiveKey(key)) {
49+
const lower = key.toLowerCase();
50+
if (isSensitiveKey(lower, denySnippets)) {
4351
result[key] = FILTERED;
4452
} else {
45-
const lower = key.toLowerCase();
4653
const isAllowed = lowerTerms.some(term => lower.includes(term));
4754
result[key] = isAllowed ? data[key]! : FILTERED;
4855
}

‎packages/core/test/lib/utils/data-collection/filterCookies.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ describe('filterCookies', () => {
2727
color: 'blue',
2828
});
2929
});
30+
31+
it('filters cookie-specific sensitive names', () => {
32+
const result = filterCookies('theme=dark; connect.sid=abc; remember_me=xyz; __secure-token=secret', true);
33+
34+
expect(result).toEqual({
35+
theme: 'dark',
36+
'connect.sid': '[Filtered]', // matches ".sid"
37+
remember_me: '[Filtered]', // matches "remember"
38+
'__secure-token': '[Filtered]', // matches "__secure-" and "token"
39+
});
40+
});
3041
});
3142

3243
describe('denyList mode ({ deny: [...] })', () => {

0 commit comments

Comments
 (0)