@debugbundle/sdk-react-native 1.3.0 → 2.0.0
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/CHANGELOG.md +13 -2
- package/DebugBundleReactNative.podspec +2 -2
- package/README.md +3 -3
- package/android/build.gradle +3 -3
- package/android/src/main/java/com/debugbundle/reactnative/DebugBundleReactNativeModule.java +2 -2
- package/app.plugin.js +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +42 -4
- package/dist/privacy-keys.d.ts +3 -0
- package/dist/privacy-keys.d.ts.map +1 -0
- package/dist/privacy-keys.js +35 -0
- package/dist/privacy-telemetry.d.ts +19 -0
- package/dist/privacy-telemetry.d.ts.map +1 -0
- package/dist/privacy-telemetry.js +164 -0
- package/dist/privacy-text.d.ts +3 -0
- package/dist/privacy-text.d.ts.map +1 -0
- package/dist/privacy-text.js +85 -0
- package/dist/redaction.d.ts.map +1 -1
- package/dist/redaction.js +29 -45
- package/ios/DebugBundleReactNative.swift +2 -2
- package/package.json +4 -4
- package/src/client.ts +42 -4
- package/src/privacy-keys.ts +39 -0
- package/src/privacy-telemetry.ts +168 -0
- package/src/privacy-text.ts +86 -0
- package/src/redaction.ts +26 -46
- package/android/src/test/java/com/debugbundle/reactnative/DebugBundleReactNativeModuleTest.java +0 -474
|
@@ -4,7 +4,7 @@ import DebugBundle
|
|
|
4
4
|
|
|
5
5
|
@objc(DebugBundleReactNative)
|
|
6
6
|
final class DebugBundleReactNative: NSObject {
|
|
7
|
-
private var currentSDKVersion = "
|
|
7
|
+
private var currentSDKVersion = "2.0.0"
|
|
8
8
|
private var currentService = "react-native-app"
|
|
9
9
|
private var currentEnvironment = "production"
|
|
10
10
|
|
|
@@ -14,7 +14,7 @@ final class DebugBundleReactNative: NSObject {
|
|
|
14
14
|
resolver resolve: @escaping RCTPromiseResolveBlock,
|
|
15
15
|
rejecter reject: @escaping RCTPromiseRejectBlock
|
|
16
16
|
) {
|
|
17
|
-
currentSDKVersion = config["sdkVersion"] as? String ?? "
|
|
17
|
+
currentSDKVersion = config["sdkVersion"] as? String ?? "2.0.0"
|
|
18
18
|
currentService = config["service"] as? String ?? "react-native-app"
|
|
19
19
|
currentEnvironment = config["environment"] as? String ?? "production"
|
|
20
20
|
let sdkConfig = DebugBundleConfig(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@debugbundle/sdk-react-native",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "React Native SDK for DebugBundle mobile event capture.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"files": [
|
|
43
43
|
"android/build.gradle",
|
|
44
|
-
"android/src",
|
|
44
|
+
"android/src/main",
|
|
45
45
|
"ios",
|
|
46
46
|
"src",
|
|
47
47
|
"dist",
|
|
@@ -104,9 +104,9 @@
|
|
|
104
104
|
},
|
|
105
105
|
"devDependencies": {
|
|
106
106
|
"@types/react": "^19.1.2",
|
|
107
|
-
"@vitest/coverage-v8": "^4.1.
|
|
107
|
+
"@vitest/coverage-v8": "^4.1.11",
|
|
108
108
|
"react": "^19.2.4",
|
|
109
109
|
"typescript": "^6.0.2",
|
|
110
|
-
"vitest": "^4.1.
|
|
110
|
+
"vitest": "^4.1.11"
|
|
111
111
|
}
|
|
112
112
|
}
|
package/src/client.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getNativeModule, degradedNativeState, safeNativeCall } from "./native.js";
|
|
2
2
|
import { applyBeforeSend } from "./before-send.js";
|
|
3
3
|
import { defaultRedactFields, DEFAULT_HEADER_ALLOWLIST, sanitizeHeaders, sanitizeValue } from "./redaction.js";
|
|
4
|
+
import { sanitizeTelemetry } from "./privacy-telemetry.js";
|
|
4
5
|
import type {
|
|
5
6
|
DebugBundleCaptureContext,
|
|
6
7
|
DebugBundleClient,
|
|
@@ -18,7 +19,7 @@ import type {
|
|
|
18
19
|
} from "./types.js";
|
|
19
20
|
|
|
20
21
|
const SDK_NAME = "@debugbundle/sdk-react-native" as const;
|
|
21
|
-
const DEFAULT_SDK_VERSION = "
|
|
22
|
+
const DEFAULT_SDK_VERSION = "2.0.0";
|
|
22
23
|
const LOG_LEVELS: Record<DebugBundleLogLevel, number> = {
|
|
23
24
|
debug: 10,
|
|
24
25
|
info: 20,
|
|
@@ -27,6 +28,39 @@ const LOG_LEVELS: Record<DebugBundleLogLevel, number> = {
|
|
|
27
28
|
critical: 50
|
|
28
29
|
};
|
|
29
30
|
|
|
31
|
+
function protectEventFields(
|
|
32
|
+
event: DebugBundleEventEnvelope,
|
|
33
|
+
additionalKeys: string[]
|
|
34
|
+
): DebugBundleEventEnvelope | null {
|
|
35
|
+
// Preserve typed identities only when their scalar values pass the mandatory policy.
|
|
36
|
+
for (const value of [event.schema_version, event.sdk_name, event.sdk_version,
|
|
37
|
+
...Object.values(event.correlation ?? {})]) {
|
|
38
|
+
if (typeof value !== "string") continue;
|
|
39
|
+
const checked = sanitizeTelemetry(value, { additionalKeys });
|
|
40
|
+
if (!checked.ok || checked.value !== value) return null;
|
|
41
|
+
}
|
|
42
|
+
const result = sanitizeTelemetry({
|
|
43
|
+
service: event.service,
|
|
44
|
+
payload: event.payload,
|
|
45
|
+
...(event.context ? { context: event.context } : {})
|
|
46
|
+
}, { additionalKeys });
|
|
47
|
+
if (!result.ok || !result.value || typeof result.value !== "object" || Array.isArray(result.value)) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const safeFields = result.value as Record<string, unknown>;
|
|
51
|
+
if (!safeFields.service || !safeFields.payload ||
|
|
52
|
+
typeof safeFields.service !== "object" || Array.isArray(safeFields.service) ||
|
|
53
|
+
typeof safeFields.payload !== "object" || Array.isArray(safeFields.payload)) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
...event,
|
|
58
|
+
service: safeFields.service as DebugBundleEventEnvelope["service"],
|
|
59
|
+
payload: safeFields.payload as Record<string, unknown>,
|
|
60
|
+
...(event.context ? { context: safeFields.context as Record<string, unknown> } : {})
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
30
64
|
export class DebugBundleReactNativeClient implements DebugBundleClient {
|
|
31
65
|
private config: ResolvedDebugBundleConfig;
|
|
32
66
|
private nativeModule: NativeDebugBundleModule | null = null;
|
|
@@ -268,18 +302,22 @@ export class DebugBundleReactNativeClient implements DebugBundleClient {
|
|
|
268
302
|
? { ...payload, device: canonicalDevice(this.nativeState.device, this.config) }
|
|
269
303
|
: payload
|
|
270
304
|
};
|
|
271
|
-
const
|
|
305
|
+
const initial = protectEventFields(authoredEvent, this.config.redactFields);
|
|
306
|
+
if (!initial) return;
|
|
307
|
+
const event = applyBeforeSend(initial, this.config.beforeSend);
|
|
272
308
|
if (!event || !localPolicyAllows) {
|
|
273
309
|
return;
|
|
274
310
|
}
|
|
311
|
+
const safeEvent = protectEventFields(event, this.config.redactFields);
|
|
312
|
+
if (!safeEvent) return;
|
|
275
313
|
if (!this.nativeModule) {
|
|
276
314
|
this.nativeState = degradedNativeState("native_module_unavailable");
|
|
277
315
|
return;
|
|
278
316
|
}
|
|
279
317
|
if (this.nativeModule.enqueueCanonicalEvent) {
|
|
280
|
-
void safeNativeCall(() => this.nativeModule!.enqueueCanonicalEvent!(
|
|
318
|
+
void safeNativeCall(() => this.nativeModule!.enqueueCanonicalEvent!(safeEvent), undefined);
|
|
281
319
|
} else {
|
|
282
|
-
void safeNativeCall(() => this.nativeModule!.enqueueEvent(toLegacyNativeEvent(
|
|
320
|
+
void safeNativeCall(() => this.nativeModule!.enqueueEvent(toLegacyNativeEvent(safeEvent)), undefined);
|
|
283
321
|
}
|
|
284
322
|
}
|
|
285
323
|
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export const DEFAULT_SENSITIVE_KEYS = [
|
|
2
|
+
"password", "secret", "token", "api_key", "apikey", "access_token", "refresh_token",
|
|
3
|
+
"private_key", "authorization", "bearer", "cookie", "session_id", "passwd", "ssn",
|
|
4
|
+
"credit_card", "card_number", "cvv", "cvc", "pin", "expiry", "phone", "otp",
|
|
5
|
+
"verification_code"
|
|
6
|
+
] as const;
|
|
7
|
+
|
|
8
|
+
function canonicalizeSensitiveKey(value: string): string {
|
|
9
|
+
return value.toLowerCase().replace(/[^a-z0-9]+/g, "");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function splitKeyIntoSegments(key: string): string[] {
|
|
13
|
+
return key
|
|
14
|
+
.trim()
|
|
15
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
16
|
+
.toLowerCase()
|
|
17
|
+
.split(/[^a-z0-9]+/)
|
|
18
|
+
.filter((segment) => segment.length > 0);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isSensitiveKey(key: string, sensitiveKeys: readonly string[]): boolean {
|
|
22
|
+
const normalized = key.trim().toLowerCase();
|
|
23
|
+
if (sensitiveKeys.includes(normalized)) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const canonicalSensitiveKeys = new Set(sensitiveKeys.map(canonicalizeSensitiveKey));
|
|
28
|
+
const segments = splitKeyIntoSegments(key);
|
|
29
|
+
for (let start = 0; start < segments.length; start += 1) {
|
|
30
|
+
let combined = "";
|
|
31
|
+
for (let end = start; end < segments.length; end += 1) {
|
|
32
|
+
combined += segments[end];
|
|
33
|
+
if (canonicalSensitiveKeys.has(combined)) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };
|
|
2
|
+
import { DEFAULT_SENSITIVE_KEYS, isSensitiveKey } from "./privacy-keys.js";
|
|
3
|
+
import { scrubCredentialText } from "./privacy-text.js";
|
|
4
|
+
|
|
5
|
+
const REDACTED = "[REDACTED]";
|
|
6
|
+
const MAX_DEPTH = 16;
|
|
7
|
+
const MAX_NODES = 4096;
|
|
8
|
+
const MAX_ENTRIES = 256;
|
|
9
|
+
const MAX_STRING_BYTES = 16 * 1024;
|
|
10
|
+
const MAX_TOTAL_BYTES = 256 * 1024;
|
|
11
|
+
|
|
12
|
+
export interface TelemetrySanitizationOptions {
|
|
13
|
+
additionalKeys?: readonly string[];
|
|
14
|
+
maxTotalBytes?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type TelemetrySanitizationResult =
|
|
18
|
+
| { ok: true; value: JsonValue; redactionCount: number }
|
|
19
|
+
| { ok: false; reason: "unsafe_input" | "budget_exceeded" };
|
|
20
|
+
|
|
21
|
+
interface WorkState {
|
|
22
|
+
nodes: number;
|
|
23
|
+
textBytes: number;
|
|
24
|
+
redactionCount: number;
|
|
25
|
+
seen: WeakSet<object>;
|
|
26
|
+
keys: string[];
|
|
27
|
+
extra: readonly string[];
|
|
28
|
+
maxTotalBytes: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
class BudgetExceeded extends Error {}
|
|
32
|
+
|
|
33
|
+
function byteLength(value: string): number {
|
|
34
|
+
let bytes = 0;
|
|
35
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
36
|
+
const code = value.charCodeAt(index);
|
|
37
|
+
if (code < 0x80) bytes += 1;
|
|
38
|
+
else if (code < 0x800) bytes += 2;
|
|
39
|
+
else if (code >= 0xd800 && code <= 0xdbff && index + 1 < value.length &&
|
|
40
|
+
value.charCodeAt(index + 1) >= 0xdc00 && value.charCodeAt(index + 1) <= 0xdfff) {
|
|
41
|
+
bytes += 4;
|
|
42
|
+
index += 1;
|
|
43
|
+
} else bytes += 3;
|
|
44
|
+
}
|
|
45
|
+
return bytes;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function mask(state: WorkState): string {
|
|
49
|
+
state.redactionCount += 1;
|
|
50
|
+
return REDACTED;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function sanitizeString(value: string, state: WorkState, allowStructured: boolean): JsonValue {
|
|
54
|
+
if (value.length > MAX_STRING_BYTES) return mask(state);
|
|
55
|
+
const length = byteLength(value);
|
|
56
|
+
if (length > MAX_STRING_BYTES) return mask(state);
|
|
57
|
+
state.textBytes += length;
|
|
58
|
+
if (state.textBytes > state.maxTotalBytes) throw new BudgetExceeded();
|
|
59
|
+
|
|
60
|
+
if (allowStructured && (value.startsWith("{") || value.startsWith("["))) {
|
|
61
|
+
try {
|
|
62
|
+
const parsed: unknown = JSON.parse(value);
|
|
63
|
+
if (typeof parsed === "object" && parsed !== null) {
|
|
64
|
+
const nested = sanitizeValue(parsed, state, 0, false);
|
|
65
|
+
const encoded = JSON.stringify(nested);
|
|
66
|
+
if (encoded !== value) state.redactionCount += 1;
|
|
67
|
+
return encoded;
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (error instanceof BudgetExceeded) throw error;
|
|
71
|
+
// Malformed structured content with a recognizable sensitive label is withheld below.
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const result = scrubCredentialText(value, state.extra);
|
|
76
|
+
if (result !== value) state.redactionCount += 1;
|
|
77
|
+
if ((value.startsWith("{") || value.startsWith("[")) && result === value &&
|
|
78
|
+
/(?:password|token|secret|authorization|cookie)["']?\s*[:=]/i.test(value)) {
|
|
79
|
+
return mask(state);
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function sanitizeValue(value: unknown, state: WorkState, depth: number, allowStructured: boolean): JsonValue {
|
|
85
|
+
state.nodes += 1;
|
|
86
|
+
if (state.nodes > MAX_NODES) throw new BudgetExceeded();
|
|
87
|
+
if (depth > MAX_DEPTH) return mask(state);
|
|
88
|
+
if (typeof value === "string") return sanitizeString(value, state, allowStructured);
|
|
89
|
+
if (value === null || typeof value === "boolean") return value;
|
|
90
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
91
|
+
if (typeof value !== "object") throw new TypeError("unsafe_input");
|
|
92
|
+
|
|
93
|
+
if (state.seen.has(value)) return "[Circular]";
|
|
94
|
+
state.seen.add(value);
|
|
95
|
+
try {
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
if (value.length > MAX_ENTRIES) return mask(state);
|
|
98
|
+
return Array.from({ length: value.length }, (_, index) => {
|
|
99
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, String(index));
|
|
100
|
+
if (descriptor === undefined || !("value" in descriptor)) throw new TypeError("unsafe_input");
|
|
101
|
+
return sanitizeValue(descriptor.value, state, depth + 1, allowStructured);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) {
|
|
105
|
+
throw new TypeError("unsafe_input");
|
|
106
|
+
}
|
|
107
|
+
const keys = Object.keys(value);
|
|
108
|
+
if (keys.length > MAX_ENTRIES) return mask(state);
|
|
109
|
+
const result: Record<string, JsonValue> = {};
|
|
110
|
+
for (const key of keys) {
|
|
111
|
+
if (key.length > 128) {
|
|
112
|
+
state.redactionCount += 1;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
state.textBytes += byteLength(key);
|
|
116
|
+
if (state.textBytes > state.maxTotalBytes) throw new BudgetExceeded();
|
|
117
|
+
const safeKey = scrubCredentialText(key, state.extra);
|
|
118
|
+
if (safeKey !== key) {
|
|
119
|
+
state.redactionCount += 1;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
123
|
+
if (descriptor === undefined || !("value" in descriptor)) throw new TypeError("unsafe_input");
|
|
124
|
+
const safeValue = isSensitiveKey(key, state.keys)
|
|
125
|
+
? mask(state)
|
|
126
|
+
: sanitizeValue(descriptor.value, state, depth + 1, allowStructured);
|
|
127
|
+
Object.defineProperty(result, key, {
|
|
128
|
+
value: safeValue,
|
|
129
|
+
enumerable: true,
|
|
130
|
+
configurable: true,
|
|
131
|
+
writable: true
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
} finally {
|
|
136
|
+
state.seen.delete(value);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Mandatory telemetry policy; unlike the legacy `redact` helper, custom keys are additive. */
|
|
141
|
+
export function sanitizeTelemetry(value: unknown, options: TelemetrySanitizationOptions = {}): TelemetrySanitizationResult {
|
|
142
|
+
try {
|
|
143
|
+
const extra = options.additionalKeys ?? [];
|
|
144
|
+
if (extra.length > 128 || extra.some((key) => typeof key !== "string" || key.length > 64 || !key.trim())) {
|
|
145
|
+
return { ok: false, reason: "unsafe_input" };
|
|
146
|
+
}
|
|
147
|
+
const maxTotalBytes = options.maxTotalBytes ?? MAX_TOTAL_BYTES;
|
|
148
|
+
if (!Number.isInteger(maxTotalBytes) || maxTotalBytes < 1 || maxTotalBytes > 512 * 1024) {
|
|
149
|
+
return { ok: false, reason: "unsafe_input" };
|
|
150
|
+
}
|
|
151
|
+
const state: WorkState = {
|
|
152
|
+
nodes: 0,
|
|
153
|
+
textBytes: 0,
|
|
154
|
+
redactionCount: 0,
|
|
155
|
+
seen: new WeakSet<object>(),
|
|
156
|
+
keys: [...DEFAULT_SENSITIVE_KEYS, ...extra],
|
|
157
|
+
extra,
|
|
158
|
+
maxTotalBytes
|
|
159
|
+
};
|
|
160
|
+
const sanitized = sanitizeValue(value, state, 0, true);
|
|
161
|
+
if (byteLength(JSON.stringify(sanitized)) > maxTotalBytes) {
|
|
162
|
+
return { ok: false, reason: "budget_exceeded" };
|
|
163
|
+
}
|
|
164
|
+
return { ok: true, value: sanitized, redactionCount: state.redactionCount };
|
|
165
|
+
} catch (error) {
|
|
166
|
+
return { ok: false, reason: error instanceof BudgetExceeded ? "budget_exceeded" : "unsafe_input" };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { DEFAULT_SENSITIVE_KEYS, isSensitiveKey } from "./privacy-keys.js";
|
|
2
|
+
|
|
3
|
+
const REDACTED = "[REDACTED]";
|
|
4
|
+
const BUILTIN_LABELS = [...DEFAULT_SENSITIVE_KEYS, "client_secret", "x_api_key",
|
|
5
|
+
"accessToken", "refreshToken", "privateKey", "clientSecret"];
|
|
6
|
+
const ASSIGNMENT = new RegExp(
|
|
7
|
+
`\\b(${BUILTIN_LABELS.join("|")})\\b(["']?\\s*[:=]\\s*)(?:"[^"]*"|'[^']*'|[^\\s&,;]+)`,
|
|
8
|
+
"gi"
|
|
9
|
+
);
|
|
10
|
+
const HEADER = /\b(Authorization|Proxy-Authorization|Cookie|Set-Cookie)\s*:\s*[^\r\n]*/gi;
|
|
11
|
+
const PEM = /-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/gi;
|
|
12
|
+
const PEM_COMPLETE = new RegExp(PEM.source, "i");
|
|
13
|
+
const PEM_START = /-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/i;
|
|
14
|
+
const DEBUGBUNDLE_TOKEN = /\bdbundle_(?:proj|mem|probe|agent)_[A-Za-z0-9_-]+\b/g;
|
|
15
|
+
const BEARER = /\b(Bearer|Basic)\s+[A-Za-z0-9._~+\/-]{6,}/gi;
|
|
16
|
+
const URL_TEXT = /\bhttps?:\/\/[^\s<>"']+/gi;
|
|
17
|
+
// Do not treat numeric substrings inside UUIDs, hashes, or identifier slugs as cards.
|
|
18
|
+
const CARD = /(?<![A-Za-z0-9_-])(?:\d[ -]?){12,18}\d(?![A-Za-z0-9_-])/g;
|
|
19
|
+
|
|
20
|
+
function isValidCard(value: string): boolean {
|
|
21
|
+
const digits = value.replace(/[^0-9]/g, "");
|
|
22
|
+
if (digits.length < 13 || digits.length > 19 || /^(\d)\1+$/.test(digits)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
let sum = 0;
|
|
26
|
+
for (let index = digits.length - 1; index >= 0; index -= 1) {
|
|
27
|
+
let digit = Number(digits[index]);
|
|
28
|
+
if ((digits.length - index) % 2 === 0) {
|
|
29
|
+
digit *= 2;
|
|
30
|
+
if (digit > 9) digit -= 9;
|
|
31
|
+
}
|
|
32
|
+
sum += digit;
|
|
33
|
+
}
|
|
34
|
+
return sum % 10 === 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function scrubUrl(raw: string, keys: readonly string[]): string {
|
|
38
|
+
try {
|
|
39
|
+
const url = new URL(raw);
|
|
40
|
+
if (url.username || url.password) {
|
|
41
|
+
url.username = "REDACTED";
|
|
42
|
+
url.password = "";
|
|
43
|
+
}
|
|
44
|
+
const nextQuery = new URLSearchParams();
|
|
45
|
+
for (const [key, value] of url.searchParams) {
|
|
46
|
+
if (key.length > 128 || scrubCredentialText(key, keys, false) !== key) continue;
|
|
47
|
+
nextQuery.append(key, isSensitiveKey(key, keys) || scrubCredentialText(value, keys, false) !== value ? REDACTED : value);
|
|
48
|
+
}
|
|
49
|
+
url.search = nextQuery.toString();
|
|
50
|
+
url.hash = "";
|
|
51
|
+
return url.toString();
|
|
52
|
+
} catch {
|
|
53
|
+
// A malformed URL with a recognizable credential must not be emitted verbatim.
|
|
54
|
+
return REDACTED;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Bounded caller input only. Returns stable replacement text, never the matching secret. */
|
|
59
|
+
export function scrubCredentialText(input: string, additionalKeys: readonly string[], scanUrls = true): string {
|
|
60
|
+
const keys = [...DEFAULT_SENSITIVE_KEYS, ...additionalKeys];
|
|
61
|
+
if (PEM_START.test(input) && !PEM_COMPLETE.test(input)) return REDACTED;
|
|
62
|
+
let output = input;
|
|
63
|
+
if (/(?:password|token|secret|authorization|cookie)%3[ad]/i.test(output)) {
|
|
64
|
+
try {
|
|
65
|
+
output = decodeURIComponent(output);
|
|
66
|
+
} catch {
|
|
67
|
+
return REDACTED;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
output = output.replace(PEM, REDACTED).replace(HEADER, "$1: [REDACTED]");
|
|
71
|
+
output = output.replace(BEARER, "$1 [REDACTED]").replace(DEBUGBUNDLE_TOKEN, REDACTED);
|
|
72
|
+
output = output.replace(ASSIGNMENT, "$1$2[REDACTED]");
|
|
73
|
+
if (additionalKeys.length > 0) {
|
|
74
|
+
for (const key of additionalKeys) {
|
|
75
|
+
const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
76
|
+
const named = new RegExp(`\\b(${escaped})\\b(["']?\\s*[:=]\\s*)(?:"[^"]*"|'[^']*'|[^\\s&,;]+)`, "gi");
|
|
77
|
+
output = output.replace(named, "$1$2[REDACTED]");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
output = output.replace(CARD, (candidate) => isValidCard(candidate) ? REDACTED : candidate);
|
|
81
|
+
if (!scanUrls) return output;
|
|
82
|
+
return output.replace(URL_TEXT, (matched) => {
|
|
83
|
+
const trailing = matched.match(/[).,;]+$/)?.[0] ?? "";
|
|
84
|
+
return scrubUrl(matched.slice(0, matched.length - trailing.length), keys) + trailing;
|
|
85
|
+
});
|
|
86
|
+
}
|
package/src/redaction.ts
CHANGED
|
@@ -1,26 +1,8 @@
|
|
|
1
|
+
import { DEFAULT_SENSITIVE_KEYS, isSensitiveKey as privacySensitiveKey } from "./privacy-keys.js";
|
|
2
|
+
import { sanitizeTelemetry } from "./privacy-telemetry.js";
|
|
3
|
+
|
|
1
4
|
const DEFAULT_REDACT_FIELDS = [
|
|
2
|
-
|
|
3
|
-
"secret",
|
|
4
|
-
"token",
|
|
5
|
-
"api_key",
|
|
6
|
-
"apikey",
|
|
7
|
-
"access_token",
|
|
8
|
-
"refresh_token",
|
|
9
|
-
"private_key",
|
|
10
|
-
"passwd",
|
|
11
|
-
"card_number",
|
|
12
|
-
"cvv",
|
|
13
|
-
"cvc",
|
|
14
|
-
"pin",
|
|
15
|
-
"expiry",
|
|
16
|
-
"phone",
|
|
17
|
-
"bearer",
|
|
18
|
-
"session_id",
|
|
19
|
-
"otp",
|
|
20
|
-
"verification_code",
|
|
21
|
-
"authorization",
|
|
22
|
-
"cookie",
|
|
23
|
-
"ssn"
|
|
5
|
+
...DEFAULT_SENSITIVE_KEYS
|
|
24
6
|
];
|
|
25
7
|
|
|
26
8
|
export const DEFAULT_HEADER_ALLOWLIST = [
|
|
@@ -46,8 +28,15 @@ export function defaultRedactFields(): string[] {
|
|
|
46
28
|
}
|
|
47
29
|
|
|
48
30
|
export function sanitizeValue(value: unknown, options: SanitizeOptions = {}): unknown {
|
|
49
|
-
|
|
50
|
-
|
|
31
|
+
try {
|
|
32
|
+
const normalized = normalizeOptions(options);
|
|
33
|
+
const visited = new WeakSet<object>();
|
|
34
|
+
const prepared = sanitize(value, normalized, 0, visited, undefined);
|
|
35
|
+
const protectedValue = sanitizeTelemetry(prepared, { additionalKeys: options.redactFields ?? [] });
|
|
36
|
+
return protectedValue.ok ? protectedValue.value : "[REDACTED]";
|
|
37
|
+
} catch {
|
|
38
|
+
return "[REDACTED]";
|
|
39
|
+
}
|
|
51
40
|
}
|
|
52
41
|
|
|
53
42
|
export function sanitizeHeaders(
|
|
@@ -71,7 +60,7 @@ export function sanitizeHeaders(
|
|
|
71
60
|
|
|
72
61
|
function normalizeOptions(options: SanitizeOptions): Required<SanitizeOptions> {
|
|
73
62
|
return {
|
|
74
|
-
redactFields: options.redactFields ??
|
|
63
|
+
redactFields: [...DEFAULT_REDACT_FIELDS, ...(options.redactFields ?? [])],
|
|
75
64
|
maxDepth: options.maxDepth ?? 8,
|
|
76
65
|
maxStringLength: options.maxStringLength ?? 2048,
|
|
77
66
|
maxArrayLength: options.maxArrayLength ?? 50,
|
|
@@ -87,13 +76,13 @@ function sanitize(
|
|
|
87
76
|
key: string | undefined
|
|
88
77
|
): unknown {
|
|
89
78
|
if (key && isSensitiveKey(key, options.redactFields)) {
|
|
90
|
-
return "[
|
|
79
|
+
return "[REDACTED]";
|
|
91
80
|
}
|
|
92
81
|
if (value === null || value === undefined) {
|
|
93
82
|
return value ?? null;
|
|
94
83
|
}
|
|
95
84
|
if (typeof value === "string") {
|
|
96
|
-
return truncate(value, options.maxStringLength);
|
|
85
|
+
return truncate(value, options.maxStringLength, options.redactFields);
|
|
97
86
|
}
|
|
98
87
|
if (typeof value === "number" || typeof value === "boolean") {
|
|
99
88
|
return value;
|
|
@@ -136,30 +125,21 @@ function sanitizeError(
|
|
|
136
125
|
visited: WeakSet<object>
|
|
137
126
|
): Record<string, unknown> {
|
|
138
127
|
return {
|
|
139
|
-
name: truncate(error.name || "Error", options.maxStringLength),
|
|
140
|
-
message: truncate(error.message || "", options.maxStringLength),
|
|
141
|
-
stack: typeof error.stack === "string" ? truncate(error.stack, options.maxStringLength * 4) : null,
|
|
128
|
+
name: truncate(error.name || "Error", options.maxStringLength, options.redactFields),
|
|
129
|
+
message: truncate(error.message || "", options.maxStringLength, options.redactFields),
|
|
130
|
+
stack: typeof error.stack === "string" ? truncate(error.stack, options.maxStringLength * 4, options.redactFields) : null,
|
|
142
131
|
cause: "cause" in error ? sanitize(error.cause, options, depth + 1, visited, "cause") : null
|
|
143
132
|
};
|
|
144
133
|
}
|
|
145
134
|
|
|
146
|
-
function truncate(value: string, maxLength: number): string {
|
|
147
|
-
if (value.length
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
135
|
+
function truncate(value: string, maxLength: number, fields: string[]): string {
|
|
136
|
+
if (value.length > 16 * 1024) return "[REDACTED]";
|
|
137
|
+
const safe = sanitizeTelemetry(value, { additionalKeys: fields });
|
|
138
|
+
if (!safe.ok || typeof safe.value !== "string") return "[REDACTED]";
|
|
139
|
+
if (safe.value.length <= maxLength) return safe.value;
|
|
140
|
+
return `${safe.value.slice(0, maxLength)}...[Truncated]`;
|
|
151
141
|
}
|
|
152
142
|
|
|
153
143
|
function isSensitiveKey(key: string, sensitiveFields: string[]): boolean {
|
|
154
|
-
|
|
155
|
-
const sensitive = new Set(sensitiveFields.flatMap(splitKey));
|
|
156
|
-
return segments.some((segment) => sensitive.has(segment));
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function splitKey(key: string): string[] {
|
|
160
|
-
return key
|
|
161
|
-
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
162
|
-
.split(/[^a-zA-Z0-9]+/)
|
|
163
|
-
.map((segment) => segment.toLowerCase())
|
|
164
|
-
.filter(Boolean);
|
|
144
|
+
return privacySensitiveKey(key, [...DEFAULT_SENSITIVE_KEYS, ...sensitiveFields]);
|
|
165
145
|
}
|