@benchcutlogic/edge-kit 0.0.0-stage → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +172 -2
- package/dist/analytics.d.ts +41 -0
- package/dist/analytics.js +212 -0
- package/dist/body.d.ts +55 -0
- package/dist/body.js +177 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/scrub.d.ts +128 -0
- package/dist/scrub.js +636 -0
- package/dist/ssrf.d.ts +165 -0
- package/dist/ssrf.js +643 -0
- package/dist/sveltekit.d.ts +60 -0
- package/dist/sveltekit.js +149 -0
- package/dist/tenant.d.ts +44 -0
- package/dist/tenant.js +43 -0
- package/package.json +87 -3
package/dist/scrub.js
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
export const DEFAULT_SECRET_KEYS = [
|
|
2
|
+
'authorization',
|
|
3
|
+
'proxy-authorization',
|
|
4
|
+
'cookie',
|
|
5
|
+
'set-cookie',
|
|
6
|
+
'token',
|
|
7
|
+
'secret',
|
|
8
|
+
'api_key',
|
|
9
|
+
'apikey',
|
|
10
|
+
'api-key',
|
|
11
|
+
'x-api-key',
|
|
12
|
+
'password',
|
|
13
|
+
'passwd',
|
|
14
|
+
'pwd',
|
|
15
|
+
'code',
|
|
16
|
+
'state',
|
|
17
|
+
'access_token',
|
|
18
|
+
'access-token',
|
|
19
|
+
'refresh_token',
|
|
20
|
+
'refresh-token',
|
|
21
|
+
'id_token',
|
|
22
|
+
'id-token',
|
|
23
|
+
'client_secret',
|
|
24
|
+
'client-secret',
|
|
25
|
+
'private_key',
|
|
26
|
+
'private-key'
|
|
27
|
+
];
|
|
28
|
+
const DEFAULT_REPLACEMENT = '[REDACTED]';
|
|
29
|
+
const DEFAULT_MAX_DEPTH = 12;
|
|
30
|
+
const SECRET_KEY_SUBSTRINGS = [
|
|
31
|
+
'secret',
|
|
32
|
+
'password',
|
|
33
|
+
'passwd',
|
|
34
|
+
'token',
|
|
35
|
+
'apikey',
|
|
36
|
+
'api_key',
|
|
37
|
+
'api-key',
|
|
38
|
+
'authorization',
|
|
39
|
+
'cookie',
|
|
40
|
+
'credential',
|
|
41
|
+
'private_key',
|
|
42
|
+
'private-key'
|
|
43
|
+
];
|
|
44
|
+
const QUERY_PII_KEYS = new Set(['email', 'e-mail', 'phone', 'ssn', 'signature', 'sig', 'jwt']);
|
|
45
|
+
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
46
|
+
const NUMERIC_ID_REGEX = /^\d+$/;
|
|
47
|
+
const LONG_TOKEN_REGEX = /^(?=.*\d)[A-Za-z0-9_-]{20,}$|^[0-9a-f]{16,}$/i;
|
|
48
|
+
const EMAIL_SEGMENT_REGEX = /^[^/\s@]+@[^/\s@]+\.[^/\s@]+$/;
|
|
49
|
+
function buildSecretKeySet(config) {
|
|
50
|
+
const set = new Set();
|
|
51
|
+
for (const key of DEFAULT_SECRET_KEYS) {
|
|
52
|
+
set.add(key.toLowerCase());
|
|
53
|
+
}
|
|
54
|
+
if (config?.secretKeys) {
|
|
55
|
+
for (const key of config.secretKeys) {
|
|
56
|
+
set.add(key.toLowerCase());
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return set;
|
|
60
|
+
}
|
|
61
|
+
function isSecretKey(key, secretKeySet) {
|
|
62
|
+
const lower = key.toLowerCase();
|
|
63
|
+
if (secretKeySet.has(lower))
|
|
64
|
+
return true;
|
|
65
|
+
const normalized = lower.replace(/-/g, '_');
|
|
66
|
+
if (secretKeySet.has(normalized))
|
|
67
|
+
return true;
|
|
68
|
+
for (const sub of SECRET_KEY_SUBSTRINGS) {
|
|
69
|
+
if (lower.includes(sub))
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
function escapeRegExp(str) {
|
|
75
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
76
|
+
}
|
|
77
|
+
function buildKeyValueRegex(secretKeySet) {
|
|
78
|
+
const keys = new Set([...secretKeySet, ...QUERY_PII_KEYS]);
|
|
79
|
+
const escaped = [...keys].sort((a, b) => b.length - a.length).map(escapeRegExp);
|
|
80
|
+
return new RegExp(`([?&\\s,;]|^)(${escaped.join('|')})=([^&\\s#"'<>]+)`, 'gi');
|
|
81
|
+
}
|
|
82
|
+
function buildHeaderLineRegex(secretKeySet) {
|
|
83
|
+
const headerKeys = ['authorization', 'proxy-authorization', 'cookie', 'set-cookie', 'x-api-key'];
|
|
84
|
+
for (const k of secretKeySet) {
|
|
85
|
+
if (!headerKeys.includes(k))
|
|
86
|
+
headerKeys.push(k);
|
|
87
|
+
}
|
|
88
|
+
const escaped = headerKeys.sort((a, b) => b.length - a.length).map(escapeRegExp);
|
|
89
|
+
return new RegExp(`((?:^|[\\r\\n])\\s*(?:${escaped.join('|')})\\s*:\\s*)([^\\r\\n]+)`, 'gi');
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Collapses dynamic path segments (UUIDs, numeric IDs, long tokens, emails)
|
|
93
|
+
* and strips query strings and hash fragments from a URL or pathname.
|
|
94
|
+
*/
|
|
95
|
+
export function templatePath(input, config) {
|
|
96
|
+
const trimmed = input.trim();
|
|
97
|
+
if (trimmed.length === 0)
|
|
98
|
+
return trimmed;
|
|
99
|
+
let prefix = '';
|
|
100
|
+
let pathname = trimmed;
|
|
101
|
+
if (/^https?:\/\//i.test(trimmed)) {
|
|
102
|
+
try {
|
|
103
|
+
const parsed = new URL(trimmed);
|
|
104
|
+
prefix = parsed.origin;
|
|
105
|
+
pathname = parsed.pathname;
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// Fall back to string stripping below
|
|
109
|
+
pathname = trimmed.split(/[?#]/, 1)[0] ?? trimmed;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
pathname = trimmed.split(/[?#]/, 1)[0] ?? trimmed;
|
|
114
|
+
}
|
|
115
|
+
const templatedSegments = pathname.split('/').map((segment) => {
|
|
116
|
+
if (segment.length === 0)
|
|
117
|
+
return segment;
|
|
118
|
+
let decoded = segment;
|
|
119
|
+
try {
|
|
120
|
+
decoded = decodeURIComponent(segment);
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
// Keep raw segment if invalid percent-encoding
|
|
124
|
+
}
|
|
125
|
+
if (UUID_REGEX.test(decoded))
|
|
126
|
+
return ':id';
|
|
127
|
+
if (NUMERIC_ID_REGEX.test(decoded))
|
|
128
|
+
return ':id';
|
|
129
|
+
if (EMAIL_SEGMENT_REGEX.test(decoded))
|
|
130
|
+
return ':email';
|
|
131
|
+
if (LONG_TOKEN_REGEX.test(decoded))
|
|
132
|
+
return ':id';
|
|
133
|
+
return segment;
|
|
134
|
+
});
|
|
135
|
+
let result = `${prefix}${templatedSegments.join('/')}`;
|
|
136
|
+
if (config?.pathPatterns) {
|
|
137
|
+
for (const rule of config.pathPatterns) {
|
|
138
|
+
result = result.replace(rule.pattern, rule.replacement);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
const NOT_VALID_JSON_SUFFIX = 'is not valid json';
|
|
144
|
+
const JSON_PARSE_PREFIX = 'json.parse:';
|
|
145
|
+
function scrubNotValidJsonSnippets(input, replacement) {
|
|
146
|
+
const lower = input.toLowerCase();
|
|
147
|
+
if (!lower.includes(NOT_VALID_JSON_SUFFIX)) {
|
|
148
|
+
return input;
|
|
149
|
+
}
|
|
150
|
+
let out = '';
|
|
151
|
+
let cursor = 0;
|
|
152
|
+
while (cursor < input.length) {
|
|
153
|
+
const suffixIdx = lower.indexOf(NOT_VALID_JSON_SUFFIX, cursor);
|
|
154
|
+
if (suffixIdx === -1) {
|
|
155
|
+
out += input.slice(cursor);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
// Walk back over whitespace immediately preceding "is not valid JSON"
|
|
159
|
+
let quoteEnd = suffixIdx - 1;
|
|
160
|
+
while (quoteEnd >= cursor && /\s/.test(input[quoteEnd])) {
|
|
161
|
+
quoteEnd -= 1;
|
|
162
|
+
}
|
|
163
|
+
const quoteChar = quoteEnd >= cursor ? input[quoteEnd] : undefined;
|
|
164
|
+
if (quoteChar === '"' || quoteChar === "'") {
|
|
165
|
+
// Prefer the quote immediately after "Unexpected token ..., " when present;
|
|
166
|
+
// otherwise take the earliest matching quote in the current segment.
|
|
167
|
+
let openQuote = -1;
|
|
168
|
+
const unexpectedIdx = lower.lastIndexOf('unexpected', quoteEnd);
|
|
169
|
+
if (unexpectedIdx >= cursor) {
|
|
170
|
+
const commaIdx = input.indexOf(',', unexpectedIdx);
|
|
171
|
+
if (commaIdx !== -1 && commaIdx < quoteEnd) {
|
|
172
|
+
let afterComma = commaIdx + 1;
|
|
173
|
+
while (afterComma < quoteEnd && /\s/.test(input[afterComma])) {
|
|
174
|
+
afterComma += 1;
|
|
175
|
+
}
|
|
176
|
+
if (input[afterComma] === quoteChar) {
|
|
177
|
+
openQuote = afterComma;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (openQuote === -1 && quoteChar === '"') {
|
|
182
|
+
const firstQuote = input.indexOf('"', cursor);
|
|
183
|
+
if (firstQuote !== -1 && firstQuote < quoteEnd) {
|
|
184
|
+
openQuote = firstQuote;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (openQuote !== -1 && openQuote < quoteEnd) {
|
|
188
|
+
out += input.slice(cursor, openQuote + 1);
|
|
189
|
+
out += replacement;
|
|
190
|
+
out += quoteChar;
|
|
191
|
+
out += input.slice(quoteEnd + 1, suffixIdx + NOT_VALID_JSON_SUFFIX.length);
|
|
192
|
+
cursor = suffixIdx + NOT_VALID_JSON_SUFFIX.length;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
out += input.slice(cursor, suffixIdx + NOT_VALID_JSON_SUFFIX.length);
|
|
197
|
+
cursor = suffixIdx + NOT_VALID_JSON_SUFFIX.length;
|
|
198
|
+
}
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
function scrubFirefoxJsonParseSnippets(input, replacement) {
|
|
202
|
+
const lower = input.toLowerCase();
|
|
203
|
+
if (!lower.includes(JSON_PARSE_PREFIX)) {
|
|
204
|
+
return input;
|
|
205
|
+
}
|
|
206
|
+
let out = '';
|
|
207
|
+
let cursor = 0;
|
|
208
|
+
while (cursor < input.length) {
|
|
209
|
+
const prefixIdx = lower.indexOf(JSON_PARSE_PREFIX, cursor);
|
|
210
|
+
if (prefixIdx === -1) {
|
|
211
|
+
out += input.slice(cursor);
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
const nextPrefixIdx = lower.indexOf(JSON_PARSE_PREFIX, prefixIdx + JSON_PARSE_PREFIX.length);
|
|
215
|
+
const segmentEnd = nextPrefixIdx === -1 ? input.length : nextPrefixIdx;
|
|
216
|
+
const unexpectedIdx = lower.indexOf('unexpected', prefixIdx + JSON_PARSE_PREFIX.length);
|
|
217
|
+
if (unexpectedIdx !== -1 && unexpectedIdx < segmentEnd) {
|
|
218
|
+
const colonIdx = input.indexOf(':', unexpectedIdx);
|
|
219
|
+
if (colonIdx !== -1 && colonIdx < segmentEnd) {
|
|
220
|
+
let openQuote = colonIdx + 1;
|
|
221
|
+
while (openQuote < segmentEnd && /\s/.test(input[openQuote])) {
|
|
222
|
+
openQuote += 1;
|
|
223
|
+
}
|
|
224
|
+
if (openQuote < segmentEnd && input[openQuote] === '"') {
|
|
225
|
+
const closeQuote = input.lastIndexOf('"', segmentEnd - 1);
|
|
226
|
+
if (closeQuote > openQuote) {
|
|
227
|
+
out += input.slice(cursor, openQuote + 1);
|
|
228
|
+
out += replacement;
|
|
229
|
+
out += '"';
|
|
230
|
+
out += input.slice(closeQuote + 1, segmentEnd);
|
|
231
|
+
cursor = segmentEnd;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
out += input.slice(cursor, segmentEnd);
|
|
238
|
+
cursor = segmentEnd;
|
|
239
|
+
}
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Scrubs Bearer/Basic tokens, secret query parameters, secret header lines,
|
|
244
|
+
* and multiline JSON `SyntaxError` snippets from a string.
|
|
245
|
+
*/
|
|
246
|
+
export function scrubString(value, config) {
|
|
247
|
+
const replacement = config?.replacement ?? DEFAULT_REPLACEMENT;
|
|
248
|
+
const secretKeySet = buildSecretKeySet(config);
|
|
249
|
+
let result = value;
|
|
250
|
+
// 1. Scrub multiline JSON.parse / SyntaxError raw payload snippets in linear time
|
|
251
|
+
result = scrubNotValidJsonSnippets(result, replacement);
|
|
252
|
+
result = scrubFirefoxJsonParseSnippets(result, replacement);
|
|
253
|
+
// 2. Scrub Bearer / Basic credentials
|
|
254
|
+
result = result.replace(/\b(Bearer|Basic)\s+[A-Za-z0-9\-._~+/]+=*/gi, `$1 ${replacement}`);
|
|
255
|
+
// 3. Scrub secret query parameters and key=value pairs
|
|
256
|
+
const kvRegex = buildKeyValueRegex(secretKeySet);
|
|
257
|
+
result = result.replace(kvRegex, `$1$2=${replacement}`);
|
|
258
|
+
// 4. Scrub embedded secret header lines (e.g. "Authorization: ...", "Cookie: ...")
|
|
259
|
+
const headerRegex = buildHeaderLineRegex(secretKeySet);
|
|
260
|
+
result = result.replace(headerRegex, `$1${replacement}`);
|
|
261
|
+
return result;
|
|
262
|
+
}
|
|
263
|
+
function scrubValueInternal(value, secretKeySet, replacement, maxDepth, depth, seen, config) {
|
|
264
|
+
if (value === null || value === undefined)
|
|
265
|
+
return value;
|
|
266
|
+
if (typeof value === 'string') {
|
|
267
|
+
return scrubString(value, config);
|
|
268
|
+
}
|
|
269
|
+
if (typeof value !== 'object') {
|
|
270
|
+
return value;
|
|
271
|
+
}
|
|
272
|
+
if (depth >= maxDepth) {
|
|
273
|
+
return '[MAX_DEPTH]';
|
|
274
|
+
}
|
|
275
|
+
if (seen.has(value)) {
|
|
276
|
+
return '[CIRCULAR]';
|
|
277
|
+
}
|
|
278
|
+
if (value instanceof Date) {
|
|
279
|
+
return new Date(value.getTime());
|
|
280
|
+
}
|
|
281
|
+
if (value instanceof RegExp) {
|
|
282
|
+
return new RegExp(value.source, value.flags);
|
|
283
|
+
}
|
|
284
|
+
if (value instanceof Error) {
|
|
285
|
+
return scrubErrorInternal(value, secretKeySet, replacement, maxDepth, depth, seen, config);
|
|
286
|
+
}
|
|
287
|
+
seen.add(value);
|
|
288
|
+
try {
|
|
289
|
+
if (Array.isArray(value)) {
|
|
290
|
+
return value.map((item) => scrubValueInternal(item, secretKeySet, replacement, maxDepth, depth + 1, seen, config));
|
|
291
|
+
}
|
|
292
|
+
if (value instanceof Map) {
|
|
293
|
+
const out = new Map();
|
|
294
|
+
for (const [k, v] of value.entries()) {
|
|
295
|
+
if (typeof k === 'string' && isSecretKey(k, secretKeySet)) {
|
|
296
|
+
out.set(k, replacement);
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
out.set(k, scrubValueInternal(v, secretKeySet, replacement, maxDepth, depth + 1, seen, config));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return out;
|
|
303
|
+
}
|
|
304
|
+
if (value instanceof Set) {
|
|
305
|
+
const out = new Set();
|
|
306
|
+
for (const item of value.values()) {
|
|
307
|
+
out.add(scrubValueInternal(item, secretKeySet, replacement, maxDepth, depth + 1, seen, config));
|
|
308
|
+
}
|
|
309
|
+
return out;
|
|
310
|
+
}
|
|
311
|
+
const inputRecord = value;
|
|
312
|
+
const output = {};
|
|
313
|
+
for (const [key, entry] of Object.entries(inputRecord)) {
|
|
314
|
+
if (isSecretKey(key, secretKeySet)) {
|
|
315
|
+
output[key] = replacement;
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
output[key] = scrubValueInternal(entry, secretKeySet, replacement, maxDepth, depth + 1, seen, config);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return output;
|
|
322
|
+
}
|
|
323
|
+
finally {
|
|
324
|
+
seen.delete(value);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
function scrubErrorInternal(error, secretKeySet, replacement, maxDepth, depth, seen, config) {
|
|
328
|
+
seen.add(error);
|
|
329
|
+
try {
|
|
330
|
+
const scrubbed = new Error(scrubString(error.message, config));
|
|
331
|
+
scrubbed.name = error.name;
|
|
332
|
+
if (error.stack !== undefined) {
|
|
333
|
+
scrubbed.stack = scrubString(error.stack, config);
|
|
334
|
+
}
|
|
335
|
+
const hasCause = 'cause' in error && error.cause !== undefined;
|
|
336
|
+
if (hasCause) {
|
|
337
|
+
scrubbed.cause = scrubValueInternal(error.cause, secretKeySet, replacement, maxDepth, depth + 1, seen, config);
|
|
338
|
+
}
|
|
339
|
+
for (const [key, val] of Object.entries(error)) {
|
|
340
|
+
if (key === 'cause' && hasCause)
|
|
341
|
+
continue;
|
|
342
|
+
if (isSecretKey(key, secretKeySet)) {
|
|
343
|
+
scrubbed[key] = replacement;
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
scrubbed[key] = scrubValueInternal(val, secretKeySet, replacement, maxDepth, depth + 1, seen, config);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return scrubbed;
|
|
350
|
+
}
|
|
351
|
+
finally {
|
|
352
|
+
seen.delete(error);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Recursively clones and redacts secret keys, tokens, and multiline JSON
|
|
357
|
+
* syntax snippets from an arbitrary object graph without mutating the input.
|
|
358
|
+
*/
|
|
359
|
+
export function scrubObject(value, config) {
|
|
360
|
+
const secretKeySet = buildSecretKeySet(config);
|
|
361
|
+
const replacement = config?.replacement ?? DEFAULT_REPLACEMENT;
|
|
362
|
+
const maxDepth = config?.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
363
|
+
return scrubValueInternal(value, secretKeySet, replacement, maxDepth, 0, new WeakSet(), config);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Produces a scrubbed `Error` copy with redacted message, stack trace, cause,
|
|
367
|
+
* and enumerable custom properties without mutating the input error.
|
|
368
|
+
*/
|
|
369
|
+
export function scrubError(error, config) {
|
|
370
|
+
const replacement = config?.replacement ?? DEFAULT_REPLACEMENT;
|
|
371
|
+
const secretKeySet = buildSecretKeySet(config);
|
|
372
|
+
const maxDepth = config?.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
373
|
+
if (error instanceof Error) {
|
|
374
|
+
return scrubErrorInternal(error, secretKeySet, replacement, maxDepth, 0, new WeakSet(), config);
|
|
375
|
+
}
|
|
376
|
+
return new Error(scrubString(String(error), config));
|
|
377
|
+
}
|
|
378
|
+
function stripUrlQueryAndHash(rawUrl) {
|
|
379
|
+
try {
|
|
380
|
+
const parsed = new URL(rawUrl);
|
|
381
|
+
parsed.username = '';
|
|
382
|
+
parsed.password = '';
|
|
383
|
+
parsed.search = '';
|
|
384
|
+
parsed.hash = '';
|
|
385
|
+
return parsed.toString();
|
|
386
|
+
}
|
|
387
|
+
catch {
|
|
388
|
+
return rawUrl.split(/[?#]/, 1)[0] ?? rawUrl;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function scrubSerializedRequestData(raw, config) {
|
|
392
|
+
const replacement = config?.replacement ?? DEFAULT_REPLACEMENT;
|
|
393
|
+
try {
|
|
394
|
+
const parsed = JSON.parse(raw);
|
|
395
|
+
return JSON.stringify(scrubObject(parsed, config));
|
|
396
|
+
}
|
|
397
|
+
catch {
|
|
398
|
+
return replacement;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Builds a Sentry `beforeSend` hook that scrubs secret headers, cookies,
|
|
403
|
+
* query strings, Bearer/Basic tokens, multiline `SyntaxError` JSON snippets,
|
|
404
|
+
* and dynamic URL segments.
|
|
405
|
+
*/
|
|
406
|
+
export function createSentryBeforeSend(config) {
|
|
407
|
+
const replacement = config?.replacement ?? DEFAULT_REPLACEMENT;
|
|
408
|
+
return (input) => {
|
|
409
|
+
// Cast: the scrubber writes plain strings into fields that every Sentry
|
|
410
|
+
// event type declares as optional strings or unknown, so the mutable
|
|
411
|
+
// `SentryEvent` view is a widening of `E`, and the result keeps `E`'s shape.
|
|
412
|
+
const event = input;
|
|
413
|
+
const scrubbed = scrubObject(event, config);
|
|
414
|
+
if (typeof scrubbed.message === 'string') {
|
|
415
|
+
scrubbed.message = scrubString(scrubbed.message, config);
|
|
416
|
+
}
|
|
417
|
+
if (scrubbed.request !== undefined && typeof scrubbed.request === 'object') {
|
|
418
|
+
if (typeof scrubbed.request.url === 'string') {
|
|
419
|
+
scrubbed.request.url = templatePath(stripUrlQueryAndHash(scrubbed.request.url), config);
|
|
420
|
+
}
|
|
421
|
+
if (scrubbed.request.query_string !== undefined) {
|
|
422
|
+
delete scrubbed.request.query_string;
|
|
423
|
+
}
|
|
424
|
+
if (scrubbed.request.cookies !== undefined) {
|
|
425
|
+
scrubbed.request.cookies = replacement;
|
|
426
|
+
}
|
|
427
|
+
if (event.request !== undefined &&
|
|
428
|
+
typeof event.request === 'object' &&
|
|
429
|
+
typeof event.request.data === 'string') {
|
|
430
|
+
scrubbed.request.data = scrubSerializedRequestData(event.request.data, config);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (scrubbed.exception?.values !== undefined) {
|
|
434
|
+
for (const ex of scrubbed.exception.values) {
|
|
435
|
+
if (typeof ex.value === 'string') {
|
|
436
|
+
ex.value = scrubString(ex.value, config);
|
|
437
|
+
}
|
|
438
|
+
if (ex.stacktrace?.frames !== undefined) {
|
|
439
|
+
for (const frame of ex.stacktrace.frames) {
|
|
440
|
+
if (typeof frame.filename === 'string') {
|
|
441
|
+
frame.filename = stripUrlQueryAndHash(frame.filename);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
if (scrubbed.breadcrumbs !== undefined) {
|
|
448
|
+
for (const crumb of scrubbed.breadcrumbs) {
|
|
449
|
+
if (typeof crumb.message === 'string') {
|
|
450
|
+
crumb.message = scrubString(crumb.message, config);
|
|
451
|
+
}
|
|
452
|
+
if (crumb.data !== undefined && typeof crumb.data === 'object') {
|
|
453
|
+
for (const urlField of ['url', 'from', 'to', 'href']) {
|
|
454
|
+
const rawVal = crumb.data[urlField];
|
|
455
|
+
if (typeof rawVal === 'string') {
|
|
456
|
+
crumb.data[urlField] = templatePath(stripUrlQueryAndHash(rawVal), config);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// Cast: `scrubbed` is a copy of `input` with string fields redacted or removed.
|
|
463
|
+
return scrubbed;
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
class ScrubberInvariantError extends Error {
|
|
467
|
+
constructor(message) {
|
|
468
|
+
super(message);
|
|
469
|
+
this.name = 'AssertionError';
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Verifies that a Sentry `beforeSend` scrubber upholds all core redaction invariants:
|
|
474
|
+
* - Bearer/Basic tokens in messages & exceptions
|
|
475
|
+
* - Secret query parameters (`?token=abc`, `?code=...`, `?state=...`)
|
|
476
|
+
* - Multiline JSON `SyntaxError` payload snippets
|
|
477
|
+
* - Secret HTTP headers (`Authorization`, `Cookie`, `Set-Cookie`, `X-Api-Key`)
|
|
478
|
+
* - Query-string PII (`?email=...`) in request URLs, query strings, and breadcrumbs
|
|
479
|
+
*
|
|
480
|
+
* Throws a descriptive `AssertionError` if any invariant is violated.
|
|
481
|
+
*/
|
|
482
|
+
export function assertScrubberInvariants(beforeSend) {
|
|
483
|
+
const sentinels = [
|
|
484
|
+
{
|
|
485
|
+
label: 'Bearer token in message and exception',
|
|
486
|
+
secret: 'SENTINEL.BEARER.CREDENTIAL_1',
|
|
487
|
+
event: {
|
|
488
|
+
message: 'Upstream failed with Bearer SENTINEL.BEARER.CREDENTIAL_1',
|
|
489
|
+
exception: {
|
|
490
|
+
values: [
|
|
491
|
+
{
|
|
492
|
+
type: 'Error',
|
|
493
|
+
value: 'Auth header was Basic SENTINEL_BASIC_CREDENTIAL_1'
|
|
494
|
+
}
|
|
495
|
+
]
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
{
|
|
500
|
+
label: 'Basic token in exception',
|
|
501
|
+
secret: 'SENTINEL_BASIC_CREDENTIAL_1',
|
|
502
|
+
event: {
|
|
503
|
+
exception: {
|
|
504
|
+
values: [
|
|
505
|
+
{
|
|
506
|
+
type: 'Error',
|
|
507
|
+
value: 'Auth header was Basic SENTINEL_BASIC_CREDENTIAL_1'
|
|
508
|
+
}
|
|
509
|
+
]
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
{
|
|
514
|
+
label: 'Query param ?token=abc in message and URL',
|
|
515
|
+
secret: 'abc_super_secret_token_val',
|
|
516
|
+
event: {
|
|
517
|
+
message: 'Callback failed for https://app.example.com/cb?token=abc_super_secret_token_val',
|
|
518
|
+
request: {
|
|
519
|
+
url: 'https://app.example.com/cb?token=abc_super_secret_token_val&code=oauth_code_val&state=csrf_state_val',
|
|
520
|
+
query_string: 'token=abc_super_secret_token_val&email=victim@example.com'
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
},
|
|
524
|
+
{
|
|
525
|
+
label: 'Multiline JSON SyntaxError snippet',
|
|
526
|
+
secret: 'sentinel_multiline_api_key_1',
|
|
527
|
+
event: {
|
|
528
|
+
exception: {
|
|
529
|
+
values: [
|
|
530
|
+
{
|
|
531
|
+
type: 'SyntaxError',
|
|
532
|
+
value: 'Unexpected token \'x\', "{\n "api_key": "sentinel_multiline_api_key_1",\n "ssn": "123-45-6789"\n}" is not valid JSON'
|
|
533
|
+
}
|
|
534
|
+
]
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
},
|
|
538
|
+
{
|
|
539
|
+
label: 'Multiline JSON SyntaxError PII field',
|
|
540
|
+
secret: '123-45-6789',
|
|
541
|
+
event: {
|
|
542
|
+
exception: {
|
|
543
|
+
values: [
|
|
544
|
+
{
|
|
545
|
+
type: 'SyntaxError',
|
|
546
|
+
value: 'Unexpected token \'x\', "{\n "api_key": "sentinel_multiline_api_key_1",\n "ssn": "123-45-6789"\n}" is not valid JSON'
|
|
547
|
+
}
|
|
548
|
+
]
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
},
|
|
552
|
+
{
|
|
553
|
+
label: 'Secret headers (Authorization, Cookie, Set-Cookie, X-Api-Key)',
|
|
554
|
+
secret: 'header_secret_cookie_session_42',
|
|
555
|
+
event: {
|
|
556
|
+
request: {
|
|
557
|
+
url: 'https://app.example.com/api/profile',
|
|
558
|
+
headers: {
|
|
559
|
+
Authorization: 'Bearer header_bearer_secret_42',
|
|
560
|
+
Cookie: 'session=header_secret_cookie_session_42',
|
|
561
|
+
'Set-Cookie': 'sid=set_cookie_secret_42; HttpOnly',
|
|
562
|
+
'X-Api-Key': 'x_api_key_secret_42'
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
},
|
|
567
|
+
{
|
|
568
|
+
label: 'X-Api-Key header',
|
|
569
|
+
secret: 'x_api_key_secret_42',
|
|
570
|
+
event: {
|
|
571
|
+
request: {
|
|
572
|
+
headers: {
|
|
573
|
+
'X-Api-Key': 'x_api_key_secret_42'
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
},
|
|
578
|
+
{
|
|
579
|
+
label: 'Query-string PII in request URL, query_string, and breadcrumbs',
|
|
580
|
+
secret: 'victim.pii@example.com',
|
|
581
|
+
event: {
|
|
582
|
+
request: {
|
|
583
|
+
url: 'https://app.example.com/reset?email=victim.pii@example.com&token=reset_tok_1',
|
|
584
|
+
query_string: 'email=victim.pii@example.com'
|
|
585
|
+
},
|
|
586
|
+
breadcrumbs: [
|
|
587
|
+
{
|
|
588
|
+
category: 'navigation',
|
|
589
|
+
data: {
|
|
590
|
+
from: 'https://app.example.com/login?email=victim.pii@example.com',
|
|
591
|
+
to: 'https://app.example.com/reset?email=victim.pii@example.com'
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
]
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
{
|
|
598
|
+
label: 'Secret fields in extra and request data',
|
|
599
|
+
secret: 'nested_refresh_token_secret_77',
|
|
600
|
+
event: {
|
|
601
|
+
extra: {
|
|
602
|
+
access_token: 'nested_access_token_secret_77',
|
|
603
|
+
refresh_token: 'nested_refresh_token_secret_77',
|
|
604
|
+
password: 'nested_password_secret_77',
|
|
605
|
+
code: 'nested_oauth_code_77',
|
|
606
|
+
state: 'nested_oauth_state_77'
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
},
|
|
610
|
+
{
|
|
611
|
+
label: 'Serialized JSON request.data secret fields',
|
|
612
|
+
secret: 'hunter2_serialized_pw',
|
|
613
|
+
event: {
|
|
614
|
+
request: {
|
|
615
|
+
data: JSON.stringify({
|
|
616
|
+
username: 'alice',
|
|
617
|
+
password: 'hunter2_serialized_pw',
|
|
618
|
+
access_token: 'serialized_access_token_88'
|
|
619
|
+
})
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
];
|
|
624
|
+
for (const testCase of sentinels) {
|
|
625
|
+
// Cast: each sentinel is a plain JSON Sentry event built from the fields
|
|
626
|
+
// `SentryEventLike` declares, which is what a `beforeSend` hook receives.
|
|
627
|
+
const result = beforeSend(testCase.event, {});
|
|
628
|
+
if (result === null || result === undefined) {
|
|
629
|
+
throw new ScrubberInvariantError(`Scrubber invariant failed (${testCase.label}): beforeSend returned ${String(result)} instead of a scrubbed event`);
|
|
630
|
+
}
|
|
631
|
+
const serialized = JSON.stringify(result);
|
|
632
|
+
if (serialized.includes(testCase.secret)) {
|
|
633
|
+
throw new ScrubberInvariantError(`Scrubber invariant failed (${testCase.label}): sentinel secret leaked into scrubbed event output`);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|