-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentry-scrub.ts
More file actions
44 lines (35 loc) · 969 Bytes
/
sentry-scrub.ts
File metadata and controls
44 lines (35 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const SENSITIVE_KEY_PATTERN =
/(token|password|secret|authorization|cookie|api[-_]?key|session|bearer)/i
const REDACTED_VALUE = "[REDACTED]"
function scrubValue(value: unknown, seen: WeakSet<object>): unknown {
if (value === null || value === undefined) {
return value
}
if (Array.isArray(value)) {
if (seen.has(value)) {
return value
}
seen.add(value)
return value.map((item) => scrubValue(item, seen))
}
if (typeof value !== "object") {
return value
}
if (seen.has(value)) {
return value
}
seen.add(value)
const record = value as Record<string, unknown>
for (const [key, nested] of Object.entries(record)) {
if (SENSITIVE_KEY_PATTERN.test(key)) {
record[key] = REDACTED_VALUE
continue
}
record[key] = scrubValue(nested, seen)
}
return record
}
export function sentryBeforeSend<T>(event: T): T {
const seen = new WeakSet<object>()
return scrubValue(event, seen) as T
}