@agent-inspect/redact 6.31.37 → 6.31.38
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/dist/index.cjs +765 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +104 -0
- package/dist/index.d.ts +104 -0
- package/dist/index.mjs +750 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +1 -1
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
|
|
3
|
+
// packages/redact/src/index.ts
|
|
4
|
+
|
|
5
|
+
// packages/redact/src/sensitive-key.ts
|
|
6
|
+
function keyHasExplicitSeparator(value) {
|
|
7
|
+
return /[_\-.]/.test(value);
|
|
8
|
+
}
|
|
9
|
+
function normalizeSensitiveKey(value) {
|
|
10
|
+
return value.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "");
|
|
11
|
+
}
|
|
12
|
+
var NON_CREDENTIAL_TOKEN_CONFIG_KEYS = new Set(
|
|
13
|
+
[
|
|
14
|
+
"tokens",
|
|
15
|
+
"max_tokens",
|
|
16
|
+
"min_tokens",
|
|
17
|
+
"ls_max_tokens",
|
|
18
|
+
"token_count",
|
|
19
|
+
"token_limit",
|
|
20
|
+
"token_budget",
|
|
21
|
+
"input_tokens",
|
|
22
|
+
"output_tokens",
|
|
23
|
+
"total_tokens",
|
|
24
|
+
"cached_tokens",
|
|
25
|
+
"prompt_tokens",
|
|
26
|
+
"completion_tokens"
|
|
27
|
+
].map(normalizeSensitiveKey)
|
|
28
|
+
);
|
|
29
|
+
function isTokenCredentialKey(normalized) {
|
|
30
|
+
if (NON_CREDENTIAL_TOKEN_CONFIG_KEYS.has(normalized)) return false;
|
|
31
|
+
if (normalized === "token") return true;
|
|
32
|
+
if (normalized.endsWith("tokens")) return false;
|
|
33
|
+
return normalized.endsWith("token");
|
|
34
|
+
}
|
|
35
|
+
function isCredentialSensitiveKey(key, sensitiveKeys) {
|
|
36
|
+
if (!key) return false;
|
|
37
|
+
const normalized = normalizeSensitiveKey(key);
|
|
38
|
+
if (!normalized) return false;
|
|
39
|
+
if (NON_CREDENTIAL_TOKEN_CONFIG_KEYS.has(normalized)) return false;
|
|
40
|
+
const allowPrefixCompound = keyHasExplicitSeparator(key);
|
|
41
|
+
for (const sensitive of sensitiveKeys) {
|
|
42
|
+
const s = normalizeSensitiveKey(sensitive);
|
|
43
|
+
if (!s) continue;
|
|
44
|
+
if (s === "token") {
|
|
45
|
+
if (isTokenCredentialKey(normalized)) return true;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (normalized === s) return true;
|
|
49
|
+
if (normalized.endsWith(`_${s}`)) return true;
|
|
50
|
+
if (allowPrefixCompound && normalized.startsWith(`${s}_`)) return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// packages/redact/src/key-value-secret.ts
|
|
56
|
+
var KEY_VALUE_SECRET_PATTERN_SOURCE = String.raw`\b(?:api[_-]?key|internal[_-]?token|access[_-]?token|auth[_-]?token|password|secret|token)=(?!\[(?:REDACTED|HASH:(?:[0-9a-f]{8}|unknown))\](?![^\s"'\\&#?]))([^\s"'\\]{8,})`;
|
|
57
|
+
var KEY_VALUE_SECRET_PATTERN = new RegExp(
|
|
58
|
+
KEY_VALUE_SECRET_PATTERN_SOURCE,
|
|
59
|
+
"i"
|
|
60
|
+
);
|
|
61
|
+
function valueContainsKeyValueSecret(value) {
|
|
62
|
+
KEY_VALUE_SECRET_PATTERN.lastIndex = 0;
|
|
63
|
+
return KEY_VALUE_SECRET_PATTERN.test(value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// packages/redact/src/url-redaction.ts
|
|
67
|
+
var HTTP_URL = /^https?:\/\/[^\s]+$/i;
|
|
68
|
+
var CONNECTION_PROTOCOLS = /* @__PURE__ */ new Set([
|
|
69
|
+
"postgres:",
|
|
70
|
+
"postgresql:",
|
|
71
|
+
"mysql:",
|
|
72
|
+
"mariadb:",
|
|
73
|
+
"mongodb:",
|
|
74
|
+
"mongodb+srv:",
|
|
75
|
+
"redis:",
|
|
76
|
+
"rediss:",
|
|
77
|
+
"amqp:",
|
|
78
|
+
"amqps:"
|
|
79
|
+
]);
|
|
80
|
+
var CONNECTION_URI = /^(?:postgres|postgresql|mysql|mariadb|mongodb(?:\+srv)?|rediss?|amqps?):\/\/[^\s]+$/i;
|
|
81
|
+
var CONNECTION_USERINFO = /^(postgres|postgresql|mysql|mariadb|mongodb(?:\+srv)?|rediss?|amqps?):\/\/([^@/?#]+)@(.+)$/i;
|
|
82
|
+
var CONNECTION_HAS_USERINFO = /^(?:postgres|postgresql|mysql|mariadb|mongodb(?:\+srv)?|rediss?|amqps?):\/\/[^@/?#]+@/i;
|
|
83
|
+
var UUID_SEGMENT = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
84
|
+
var HEX_SEGMENT = /^[0-9a-f]{12,}$/i;
|
|
85
|
+
var NUMERIC_SEGMENT = /^\d{5,}$/;
|
|
86
|
+
var OPAQUE_SEGMENT = /^[A-Za-z0-9_-]{20,}$/;
|
|
87
|
+
function looksLikeHttpUrl(value) {
|
|
88
|
+
return HTTP_URL.test(value.trim());
|
|
89
|
+
}
|
|
90
|
+
function looksLikeConnectionUri(value) {
|
|
91
|
+
return CONNECTION_URI.test(value.trim());
|
|
92
|
+
}
|
|
93
|
+
function looksLikeUrlForRedaction(value) {
|
|
94
|
+
return looksLikeHttpUrl(value) || looksLikeConnectionUri(value);
|
|
95
|
+
}
|
|
96
|
+
function parseRedactableUrl(value) {
|
|
97
|
+
try {
|
|
98
|
+
const url = new URL(value.trim());
|
|
99
|
+
if (url.protocol === "http:" || url.protocol === "https:") return url;
|
|
100
|
+
if (CONNECTION_PROTOCOLS.has(url.protocol)) return url;
|
|
101
|
+
return void 0;
|
|
102
|
+
} catch {
|
|
103
|
+
return void 0;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function redactConnectionUserinfoFallback(value, replacement) {
|
|
107
|
+
const trimmed = value.trim();
|
|
108
|
+
const match = CONNECTION_USERINFO.exec(trimmed);
|
|
109
|
+
if (match) {
|
|
110
|
+
const [, scheme, , rest] = match;
|
|
111
|
+
if (scheme && rest && rest.length > 0) {
|
|
112
|
+
return {
|
|
113
|
+
value: `${scheme}://${rest}`,
|
|
114
|
+
credentialRedacted: true,
|
|
115
|
+
identifiersMasked: false
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (looksLikeConnectionUri(trimmed) && CONNECTION_HAS_USERINFO.test(trimmed)) {
|
|
120
|
+
return {
|
|
121
|
+
value: replacement,
|
|
122
|
+
credentialRedacted: true,
|
|
123
|
+
identifiersMasked: false
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return void 0;
|
|
127
|
+
}
|
|
128
|
+
function isHttpProtocol(protocol) {
|
|
129
|
+
return protocol === "http:" || protocol === "https:";
|
|
130
|
+
}
|
|
131
|
+
function isIdentifierSegment(segment) {
|
|
132
|
+
if (UUID_SEGMENT.test(segment)) return true;
|
|
133
|
+
if (HEX_SEGMENT.test(segment)) return true;
|
|
134
|
+
if (NUMERIC_SEGMENT.test(segment)) return true;
|
|
135
|
+
return OPAQUE_SEGMENT.test(segment) && /\d/.test(segment) && /[A-Za-z]/.test(segment);
|
|
136
|
+
}
|
|
137
|
+
function decodeParam(value) {
|
|
138
|
+
try {
|
|
139
|
+
return decodeURIComponent(value.replace(/\+/g, " "));
|
|
140
|
+
} catch {
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function redactParams(raw, options, replacement) {
|
|
145
|
+
let redacted = false;
|
|
146
|
+
const parts = raw.split("&").map((part) => {
|
|
147
|
+
if (part.length === 0) return part;
|
|
148
|
+
const separator = part.indexOf("=");
|
|
149
|
+
if (separator < 0) return part;
|
|
150
|
+
const rawKey = part.slice(0, separator);
|
|
151
|
+
const rawValue = part.slice(separator + 1);
|
|
152
|
+
const key = decodeParam(rawKey);
|
|
153
|
+
const value = decodeParam(rawValue);
|
|
154
|
+
if (isCredentialSensitiveKey(key, options.sensitiveKeys) || options.isSensitiveValue(value)) {
|
|
155
|
+
redacted = true;
|
|
156
|
+
return `${rawKey}=${replacement}`;
|
|
157
|
+
}
|
|
158
|
+
return part;
|
|
159
|
+
});
|
|
160
|
+
return { value: parts.join("&"), redacted };
|
|
161
|
+
}
|
|
162
|
+
function redactUrlString(value, options) {
|
|
163
|
+
const unchanged = {
|
|
164
|
+
value,
|
|
165
|
+
credentialRedacted: false,
|
|
166
|
+
identifiersMasked: false
|
|
167
|
+
};
|
|
168
|
+
if (!looksLikeUrlForRedaction(value)) return unchanged;
|
|
169
|
+
const replacement = options.replacement ?? "[REDACTED]";
|
|
170
|
+
const url = parseRedactableUrl(value);
|
|
171
|
+
if (!url) {
|
|
172
|
+
return redactConnectionUserinfoFallback(value, replacement) ?? unchanged;
|
|
173
|
+
}
|
|
174
|
+
const idPlaceholder = options.idPlaceholder ?? "[id]";
|
|
175
|
+
const http = isHttpProtocol(url.protocol);
|
|
176
|
+
let credentialRedacted = url.username.length > 0 || url.password.length > 0;
|
|
177
|
+
let identifiersMasked = false;
|
|
178
|
+
let pathname = url.pathname;
|
|
179
|
+
if (http && options.maskPathIds === true) {
|
|
180
|
+
pathname = url.pathname.split("/").map((segment) => {
|
|
181
|
+
if (segment.length === 0 || !isIdentifierSegment(decodeParam(segment))) {
|
|
182
|
+
return segment;
|
|
183
|
+
}
|
|
184
|
+
identifiersMasked = true;
|
|
185
|
+
return idPlaceholder;
|
|
186
|
+
}).join("/");
|
|
187
|
+
}
|
|
188
|
+
let search = "";
|
|
189
|
+
if (url.search.length > 1) {
|
|
190
|
+
const params = redactParams(url.search.slice(1), options, replacement);
|
|
191
|
+
if (params.redacted) credentialRedacted = true;
|
|
192
|
+
search = `?${params.value}`;
|
|
193
|
+
} else if (url.search.length === 1) {
|
|
194
|
+
search = "?";
|
|
195
|
+
}
|
|
196
|
+
let hash = url.hash;
|
|
197
|
+
if (url.hash.includes("=")) {
|
|
198
|
+
const params = redactParams(url.hash.slice(1), options, replacement);
|
|
199
|
+
if (params.redacted) credentialRedacted = true;
|
|
200
|
+
hash = `#${params.value}`;
|
|
201
|
+
}
|
|
202
|
+
if (!credentialRedacted && !identifiersMasked) return unchanged;
|
|
203
|
+
return {
|
|
204
|
+
value: `${url.protocol}//${url.host}${pathname}${search}${hash}`,
|
|
205
|
+
credentialRedacted,
|
|
206
|
+
identifiersMasked
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// packages/redact/src/index.ts
|
|
211
|
+
var DEFAULT_REDACT_KEYS = [
|
|
212
|
+
"authorization",
|
|
213
|
+
"cookie",
|
|
214
|
+
"token",
|
|
215
|
+
"apiKey",
|
|
216
|
+
"password",
|
|
217
|
+
"secret",
|
|
218
|
+
"email"
|
|
219
|
+
];
|
|
220
|
+
var SHARE_PROFILE_EXTRA_KEYS = [
|
|
221
|
+
"userEmail",
|
|
222
|
+
"customerEmail",
|
|
223
|
+
"phone",
|
|
224
|
+
"phoneNumber",
|
|
225
|
+
"address",
|
|
226
|
+
"ip",
|
|
227
|
+
"ipAddress",
|
|
228
|
+
"sessionId",
|
|
229
|
+
"requestId",
|
|
230
|
+
"correlationId",
|
|
231
|
+
"decisionId",
|
|
232
|
+
"groupId",
|
|
233
|
+
"customerId",
|
|
234
|
+
"userId",
|
|
235
|
+
"accountId",
|
|
236
|
+
"tenantId",
|
|
237
|
+
"orgId",
|
|
238
|
+
"organizationId",
|
|
239
|
+
"traceId",
|
|
240
|
+
"spanId",
|
|
241
|
+
"parentSpanId",
|
|
242
|
+
"currentTask",
|
|
243
|
+
"task",
|
|
244
|
+
"userInput",
|
|
245
|
+
"requestText",
|
|
246
|
+
"conversationText"
|
|
247
|
+
];
|
|
248
|
+
var STRICT_PROFILE_EXTRA_KEYS = [
|
|
249
|
+
"prompt",
|
|
250
|
+
"completion",
|
|
251
|
+
"input",
|
|
252
|
+
"output",
|
|
253
|
+
"inputPreview",
|
|
254
|
+
"outputPreview",
|
|
255
|
+
"message",
|
|
256
|
+
"messages",
|
|
257
|
+
"transcript",
|
|
258
|
+
"context",
|
|
259
|
+
"document",
|
|
260
|
+
"documents",
|
|
261
|
+
"chunk",
|
|
262
|
+
"chunks",
|
|
263
|
+
"retrieval",
|
|
264
|
+
"query",
|
|
265
|
+
"stack",
|
|
266
|
+
"errorStack",
|
|
267
|
+
"stackTrace"
|
|
268
|
+
];
|
|
269
|
+
function isRecord(value) {
|
|
270
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
271
|
+
}
|
|
272
|
+
function toKey(key) {
|
|
273
|
+
return key.toLowerCase();
|
|
274
|
+
}
|
|
275
|
+
function findCompiledKeyRule(key, rules) {
|
|
276
|
+
const exact = toKey(key);
|
|
277
|
+
const direct = rules.find((candidate) => candidate.key === exact);
|
|
278
|
+
if (direct) return direct;
|
|
279
|
+
return rules.find(
|
|
280
|
+
(candidate) => isCredentialSensitiveKey(key, [candidate.raw ?? candidate.key])
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
function stableHash(value) {
|
|
284
|
+
const hash = crypto.createHash("sha256").update(value, "utf8").digest("hex");
|
|
285
|
+
return hash.slice(0, 8);
|
|
286
|
+
}
|
|
287
|
+
function stringifyScalar(value) {
|
|
288
|
+
if (typeof value === "string") return value;
|
|
289
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
290
|
+
return String(value);
|
|
291
|
+
}
|
|
292
|
+
return void 0;
|
|
293
|
+
}
|
|
294
|
+
function patternDetector(options) {
|
|
295
|
+
return {
|
|
296
|
+
id: options.id,
|
|
297
|
+
severity: options.severity ?? "warning",
|
|
298
|
+
matchKind: "value",
|
|
299
|
+
detect(input) {
|
|
300
|
+
if (typeof input.value !== "string") return [];
|
|
301
|
+
options.pattern.lastIndex = 0;
|
|
302
|
+
return options.pattern.test(input.value) ? [{ action: "replace", severity: options.severity ?? "warning", matchKind: "value" }] : [];
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
function digitsOnly(value) {
|
|
307
|
+
return value.replace(/\D/g, "");
|
|
308
|
+
}
|
|
309
|
+
function passesLuhn(value) {
|
|
310
|
+
const digits = digitsOnly(value);
|
|
311
|
+
if (digits.length < 13 || digits.length > 19) return false;
|
|
312
|
+
let sum = 0;
|
|
313
|
+
let double = false;
|
|
314
|
+
for (let i = digits.length - 1; i >= 0; i -= 1) {
|
|
315
|
+
let digit = Number(digits[i]);
|
|
316
|
+
if (double) {
|
|
317
|
+
digit *= 2;
|
|
318
|
+
if (digit > 9) digit -= 9;
|
|
319
|
+
}
|
|
320
|
+
sum += digit;
|
|
321
|
+
double = !double;
|
|
322
|
+
}
|
|
323
|
+
return sum % 10 === 0;
|
|
324
|
+
}
|
|
325
|
+
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
326
|
+
var EPOCH_MS_RE = /^1[0-9]{12}$/;
|
|
327
|
+
var EMAIL_RE = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi;
|
|
328
|
+
function pathSuggestsNonCard(path) {
|
|
329
|
+
const normalized = path.toLowerCase().replace(/[^a-z0-9._]/g, "");
|
|
330
|
+
return /(^|\.)(tokenusage|usage)(\.|$)/.test(normalized) || /(startedat|endedat|durationms|timestamp|createdat|updatedat)(\.|$)/.test(normalized) || /(^|\.)(runid|traceid|spanid|eventid|sessionid|userid|parentid|requestid|correlationid)(\.|$)/.test(
|
|
331
|
+
normalized
|
|
332
|
+
) || /(^|\.)ts(\.|$)/.test(normalized);
|
|
333
|
+
}
|
|
334
|
+
function isUuidLike(value) {
|
|
335
|
+
return UUID_RE.test(value.trim());
|
|
336
|
+
}
|
|
337
|
+
function isPlausibleCardCandidate(candidate, fullValue) {
|
|
338
|
+
const digits = digitsOnly(candidate);
|
|
339
|
+
if (digits.length < 13 || digits.length > 19) return false;
|
|
340
|
+
if (!passesLuhn(candidate)) return false;
|
|
341
|
+
const trimmed = fullValue.trim();
|
|
342
|
+
if (isUuidLike(trimmed)) return false;
|
|
343
|
+
if (EPOCH_MS_RE.test(digits) && digitsOnly(trimmed) === digits && /^\d+$/.test(trimmed)) {
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
const start = fullValue.indexOf(candidate);
|
|
347
|
+
if (start >= 0) {
|
|
348
|
+
const before = fullValue[start - 1];
|
|
349
|
+
const after = fullValue[start + candidate.length];
|
|
350
|
+
if (before && /[A-Za-z0-9_]/.test(before)) return false;
|
|
351
|
+
if (after && /[A-Za-z0-9_]/.test(after)) return false;
|
|
352
|
+
}
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
function looksLikePathOrPackageOrUrl(value) {
|
|
356
|
+
const trimmed = value.trim();
|
|
357
|
+
if (/^(?:[a-z][a-z0-9+.-]*:)?\/\//i.test(trimmed)) return true;
|
|
358
|
+
if (/^webpack:/i.test(trimmed)) return true;
|
|
359
|
+
if (/^@[A-Za-z0-9_.-]+\/[A-Za-z0-9_.@/-]+$/.test(trimmed)) return true;
|
|
360
|
+
if (/^[A-Za-z]:[\\/]/.test(trimmed) || /^[/\\]/.test(trimmed)) return true;
|
|
361
|
+
if (/[\\/]/.test(trimmed) && !/\s/.test(trimmed)) return true;
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
function emailMatchIsPathAdjacent(value, index, length) {
|
|
365
|
+
const before = value[index - 1];
|
|
366
|
+
const after = value[index + length];
|
|
367
|
+
return before === "/" || before === "\\" || after === "/" || after === "\\";
|
|
368
|
+
}
|
|
369
|
+
function valueContainsEmail(value) {
|
|
370
|
+
if (looksLikePathOrPackageOrUrl(value)) return false;
|
|
371
|
+
EMAIL_RE.lastIndex = 0;
|
|
372
|
+
for (const match of value.matchAll(EMAIL_RE)) {
|
|
373
|
+
const text = match[0] ?? "";
|
|
374
|
+
const index = match.index ?? 0;
|
|
375
|
+
if (emailMatchIsPathAdjacent(value, index, text.length)) continue;
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
var credentialDetectors = [
|
|
381
|
+
patternDetector({
|
|
382
|
+
id: "value.authorizationHeader",
|
|
383
|
+
pattern: /^(?:basic|bearer|digest|apikey)\s+[a-z0-9._~+/=-]+$/i,
|
|
384
|
+
severity: "error"
|
|
385
|
+
}),
|
|
386
|
+
patternDetector({
|
|
387
|
+
id: "value.bearerToken",
|
|
388
|
+
pattern: /\bbearer\s+[a-z0-9._~+/=-]{12,}\b/i,
|
|
389
|
+
severity: "error"
|
|
390
|
+
}),
|
|
391
|
+
patternDetector({
|
|
392
|
+
id: "value.cookie",
|
|
393
|
+
pattern: /\b[a-z0-9_.-]+=[^;\s]+(?:;\s*[a-z0-9_.-]+=[^;\s]+)+/i,
|
|
394
|
+
severity: "error"
|
|
395
|
+
}),
|
|
396
|
+
patternDetector({
|
|
397
|
+
id: "value.jwt",
|
|
398
|
+
pattern: /\beyJ[a-zA-Z0-9_-]{8,}\.[a-zA-Z0-9_-]{8,}\.[a-zA-Z0-9_-]{8,}\b/,
|
|
399
|
+
severity: "error"
|
|
400
|
+
}),
|
|
401
|
+
patternDetector({
|
|
402
|
+
id: "value.providerApiKey",
|
|
403
|
+
pattern: /\b(?:sk-(?:proj-)?[a-zA-Z0-9_-]{16,}|sk-ant-[a-zA-Z0-9_-]{16,}|AIza[0-9A-Za-z_-]{20,})\b/,
|
|
404
|
+
severity: "error"
|
|
405
|
+
}),
|
|
406
|
+
// Stripe secret/restricted keys (underscore form). Keep in sync with
|
|
407
|
+
// packages/core/src/safety/credential-value-patterns.ts + checks DEFAULT_SECRET_PATTERNS (#520/#528).
|
|
408
|
+
// Non-alnum lookarounds so underscore-adjacent forms (acct_sk_live_…, …_eu) match.
|
|
409
|
+
patternDetector({
|
|
410
|
+
id: "value.stripeSecretKey",
|
|
411
|
+
pattern: /(?<![A-Za-z0-9])(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{16,}(?![A-Za-z0-9])/,
|
|
412
|
+
severity: "error"
|
|
413
|
+
}),
|
|
414
|
+
patternDetector({
|
|
415
|
+
id: "value.slackToken",
|
|
416
|
+
pattern: /\bxox[abposr]-[A-Za-z0-9-]{10,}\b/,
|
|
417
|
+
severity: "error"
|
|
418
|
+
}),
|
|
419
|
+
patternDetector({
|
|
420
|
+
id: "value.githubToken",
|
|
421
|
+
pattern: /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{20,}\b/,
|
|
422
|
+
severity: "error"
|
|
423
|
+
}),
|
|
424
|
+
patternDetector({
|
|
425
|
+
id: "value.awsAccessKey",
|
|
426
|
+
pattern: /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/,
|
|
427
|
+
severity: "error"
|
|
428
|
+
}),
|
|
429
|
+
patternDetector({
|
|
430
|
+
id: "value.privateKey",
|
|
431
|
+
pattern: /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*-----END [A-Z ]*PRIVATE KEY-----/,
|
|
432
|
+
severity: "error"
|
|
433
|
+
}),
|
|
434
|
+
{
|
|
435
|
+
id: "value.keyValueSecret",
|
|
436
|
+
severity: "error",
|
|
437
|
+
matchKind: "value",
|
|
438
|
+
detect(input) {
|
|
439
|
+
if (typeof input.value !== "string") return [];
|
|
440
|
+
return valueContainsKeyValueSecret(input.value) ? [{ action: "replace", severity: "error", matchKind: "value" }] : [];
|
|
441
|
+
}
|
|
442
|
+
},
|
|
443
|
+
{
|
|
444
|
+
id: "value.creditCard",
|
|
445
|
+
severity: "error",
|
|
446
|
+
matchKind: "value",
|
|
447
|
+
detect(input) {
|
|
448
|
+
if (typeof input.value !== "string") return [];
|
|
449
|
+
if (pathSuggestsNonCard(input.path)) return [];
|
|
450
|
+
if (isUuidLike(input.value)) return [];
|
|
451
|
+
const candidatePattern = /(?:\d[ -]?){13,19}/g;
|
|
452
|
+
for (const match of input.value.matchAll(candidatePattern)) {
|
|
453
|
+
const candidate = match[0] ?? "";
|
|
454
|
+
if (isPlausibleCardCandidate(candidate, input.value)) {
|
|
455
|
+
return [{ action: "replace", severity: "error", matchKind: "value" }];
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return [];
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
];
|
|
462
|
+
var identifierDetectors = [
|
|
463
|
+
{
|
|
464
|
+
id: "value.email",
|
|
465
|
+
severity: "warning",
|
|
466
|
+
matchKind: "value",
|
|
467
|
+
detect(input) {
|
|
468
|
+
if (typeof input.value !== "string") return [];
|
|
469
|
+
return valueContainsEmail(input.value) ? [{ action: "replace", severity: "warning", matchKind: "value" }] : [];
|
|
470
|
+
}
|
|
471
|
+
},
|
|
472
|
+
patternDetector({
|
|
473
|
+
id: "value.phone",
|
|
474
|
+
pattern: /\b(?:\+\d{1,3}[\s.-]?)?(?:\(?\d{3}\)?[\s.-])\d{3}[\s.-]\d{4}\b/
|
|
475
|
+
}),
|
|
476
|
+
patternDetector({
|
|
477
|
+
id: "value.ipv4",
|
|
478
|
+
pattern: /\b(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)\b/
|
|
479
|
+
}),
|
|
480
|
+
patternDetector({
|
|
481
|
+
id: "value.ipv6",
|
|
482
|
+
pattern: /\b(?:[0-9a-f]{1,4}:){2,7}[0-9a-f]{1,4}\b/i
|
|
483
|
+
})
|
|
484
|
+
];
|
|
485
|
+
function builtInDetectorsForProfile(profile) {
|
|
486
|
+
if (profile === "local") return credentialDetectors;
|
|
487
|
+
return [...credentialDetectors, ...identifierDetectors];
|
|
488
|
+
}
|
|
489
|
+
function compileRules(rules, extraKeys) {
|
|
490
|
+
const out = /* @__PURE__ */ new Map();
|
|
491
|
+
const set = (rule) => {
|
|
492
|
+
const key = toKey(rule.key);
|
|
493
|
+
out.set(key, { ...rule, key, raw: rule.raw ?? rule.key });
|
|
494
|
+
};
|
|
495
|
+
for (const key of DEFAULT_REDACT_KEYS) {
|
|
496
|
+
set({ key, strategy: "full" });
|
|
497
|
+
}
|
|
498
|
+
for (const key of extraKeys ?? []) {
|
|
499
|
+
if (typeof key === "string" && key.length > 0) {
|
|
500
|
+
set({ key, strategy: "full" });
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
for (const rule of rules ?? []) {
|
|
504
|
+
if (typeof rule === "string") {
|
|
505
|
+
set({ key: rule, strategy: "full" });
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
if (rule.strategy === "full") set({ key: rule.key, strategy: "full" });
|
|
509
|
+
if (rule.strategy === "hash") set({ key: rule.key, strategy: "hash" });
|
|
510
|
+
if (rule.strategy === "prefix") {
|
|
511
|
+
set({
|
|
512
|
+
key: rule.key,
|
|
513
|
+
strategy: "prefix",
|
|
514
|
+
keep: typeof rule.keep === "number" ? rule.keep : 8
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
return [...out.values()];
|
|
519
|
+
}
|
|
520
|
+
function actionForRule(rule) {
|
|
521
|
+
if (rule.strategy === "full") return "replace";
|
|
522
|
+
return rule.strategy;
|
|
523
|
+
}
|
|
524
|
+
function isCompleteRedactionMarker(value, replacement = "[REDACTED]") {
|
|
525
|
+
if (typeof value !== "string") return false;
|
|
526
|
+
const trimmed = value.trim();
|
|
527
|
+
if (trimmed.length === 0) return false;
|
|
528
|
+
if (trimmed === replacement) return true;
|
|
529
|
+
if (trimmed === "[HASH:unknown]") return true;
|
|
530
|
+
if (/^\[HASH:[0-9a-f]{8}\]$/.test(trimmed)) return true;
|
|
531
|
+
return false;
|
|
532
|
+
}
|
|
533
|
+
function applyRule(rule, value, replacement) {
|
|
534
|
+
if (rule.strategy === "full") return replacement;
|
|
535
|
+
const asString = stringifyScalar(value);
|
|
536
|
+
if (rule.strategy === "prefix") {
|
|
537
|
+
if (asString === void 0) return replacement;
|
|
538
|
+
const keep = Math.max(0, Math.floor(rule.keep));
|
|
539
|
+
return asString.length <= keep ? `${asString}\u2026` : `${asString.slice(0, keep)}\u2026`;
|
|
540
|
+
}
|
|
541
|
+
if (rule.strategy === "hash") {
|
|
542
|
+
if (asString === void 0) return "[HASH:unknown]";
|
|
543
|
+
return `[HASH:${stableHash(asString)}]`;
|
|
544
|
+
}
|
|
545
|
+
return value;
|
|
546
|
+
}
|
|
547
|
+
function childPath(path, key) {
|
|
548
|
+
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key)) {
|
|
549
|
+
return path ? `${path}.${key}` : key;
|
|
550
|
+
}
|
|
551
|
+
return `${path || "$"}[${JSON.stringify(key)}]`;
|
|
552
|
+
}
|
|
553
|
+
function indexPath(path, index) {
|
|
554
|
+
return `${path || "$"}[${index}]`;
|
|
555
|
+
}
|
|
556
|
+
function makeFinding(path, detector, action, matchKind, severity = "warning", preview) {
|
|
557
|
+
return preview === void 0 ? { path, detector, action, severity, matchKind } : { path, detector, action, severity, matchKind, preview };
|
|
558
|
+
}
|
|
559
|
+
function createRedactionProfile(profile = "local") {
|
|
560
|
+
switch (profile) {
|
|
561
|
+
case "local":
|
|
562
|
+
return { profile: "local", extraKeys: [] };
|
|
563
|
+
case "share":
|
|
564
|
+
return {
|
|
565
|
+
profile: "share",
|
|
566
|
+
extraKeys: SHARE_PROFILE_EXTRA_KEYS,
|
|
567
|
+
maxMetadataValueLengthCap: 500,
|
|
568
|
+
maxPreviewLengthCap: 200
|
|
569
|
+
};
|
|
570
|
+
case "strict":
|
|
571
|
+
return {
|
|
572
|
+
profile: "strict",
|
|
573
|
+
extraKeys: [...SHARE_PROFILE_EXTRA_KEYS, ...STRICT_PROFILE_EXTRA_KEYS],
|
|
574
|
+
maxMetadataValueLengthCap: 200,
|
|
575
|
+
maxPreviewLengthCap: 80
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
var Redactor = class {
|
|
580
|
+
#rules;
|
|
581
|
+
#sensitiveKeys;
|
|
582
|
+
#detectors;
|
|
583
|
+
#profile;
|
|
584
|
+
#replacement;
|
|
585
|
+
#maxDepth;
|
|
586
|
+
#collectFindings;
|
|
587
|
+
constructor(options) {
|
|
588
|
+
const resolved = createRedactionProfile(options?.profile ?? "local");
|
|
589
|
+
this.#profile = resolved.profile;
|
|
590
|
+
this.#rules = compileRules(options?.rules, [
|
|
591
|
+
...resolved.extraKeys,
|
|
592
|
+
...options?.extraKeys ?? []
|
|
593
|
+
]);
|
|
594
|
+
this.#sensitiveKeys = this.#rules.map((rule) => rule.raw ?? rule.key);
|
|
595
|
+
this.#detectors = [
|
|
596
|
+
...builtInDetectorsForProfile(this.#profile),
|
|
597
|
+
...options?.detectors ?? []
|
|
598
|
+
];
|
|
599
|
+
this.#replacement = options?.replacement ?? "[REDACTED]";
|
|
600
|
+
this.#maxDepth = options?.maxDepth ?? 32;
|
|
601
|
+
this.#collectFindings = options?.collectFindings ?? true;
|
|
602
|
+
}
|
|
603
|
+
redactValue(key, value) {
|
|
604
|
+
return this.#redactValue(value, key, key, 0, {
|
|
605
|
+
findings: [],
|
|
606
|
+
seen: /* @__PURE__ */ new WeakMap()
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
redactRecord(record) {
|
|
610
|
+
return this.redact(record).value;
|
|
611
|
+
}
|
|
612
|
+
redact(value) {
|
|
613
|
+
const state = {
|
|
614
|
+
findings: [],
|
|
615
|
+
seen: /* @__PURE__ */ new WeakMap()
|
|
616
|
+
};
|
|
617
|
+
const redacted = this.#redactValue(value, void 0, "$", 0, state);
|
|
618
|
+
return {
|
|
619
|
+
value: redacted,
|
|
620
|
+
findings: state.findings,
|
|
621
|
+
redacted: state.findings.some((finding) => finding.action !== "keep"),
|
|
622
|
+
profile: this.#profile
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
#recordFinding(state, finding) {
|
|
626
|
+
if (this.#collectFindings) state.findings.push(finding);
|
|
627
|
+
}
|
|
628
|
+
#detect(value, key, path) {
|
|
629
|
+
for (const detector of this.#detectors) {
|
|
630
|
+
for (const detection of detector.detect({ path, key, value })) {
|
|
631
|
+
if ((detection.action ?? "replace") !== "keep") return { detector, detection };
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
return void 0;
|
|
635
|
+
}
|
|
636
|
+
#redactValue(value, key, path, depth, state) {
|
|
637
|
+
if (depth > this.#maxDepth) {
|
|
638
|
+
this.#recordFinding(
|
|
639
|
+
state,
|
|
640
|
+
makeFinding(path, "structure.maxDepth", "truncate", "value", "warning")
|
|
641
|
+
);
|
|
642
|
+
return "[Truncated]";
|
|
643
|
+
}
|
|
644
|
+
if (key !== void 0) {
|
|
645
|
+
const rule = findCompiledKeyRule(key, this.#rules);
|
|
646
|
+
if (rule) {
|
|
647
|
+
if (isCompleteRedactionMarker(value, this.#replacement)) {
|
|
648
|
+
return value;
|
|
649
|
+
}
|
|
650
|
+
this.#recordFinding(
|
|
651
|
+
state,
|
|
652
|
+
makeFinding(path, `key.${rule.key}`, actionForRule(rule), "key", "warning")
|
|
653
|
+
);
|
|
654
|
+
return applyRule(rule, value, this.#replacement);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
if (typeof value === "string" && looksLikeUrlForRedaction(value)) {
|
|
658
|
+
const url = redactUrlString(value, {
|
|
659
|
+
sensitiveKeys: this.#sensitiveKeys,
|
|
660
|
+
isSensitiveValue: (candidate) => this.#detect(candidate, key, path) !== void 0,
|
|
661
|
+
replacement: this.#replacement,
|
|
662
|
+
maskPathIds: this.#profile !== "local"
|
|
663
|
+
});
|
|
664
|
+
if (url.value !== value) {
|
|
665
|
+
this.#recordFinding(
|
|
666
|
+
state,
|
|
667
|
+
makeFinding(
|
|
668
|
+
path,
|
|
669
|
+
url.credentialRedacted ? "value.urlCredential" : "value.urlIdentifier",
|
|
670
|
+
"replace",
|
|
671
|
+
"value",
|
|
672
|
+
url.credentialRedacted ? "error" : "warning"
|
|
673
|
+
)
|
|
674
|
+
);
|
|
675
|
+
const residual = this.#detect(
|
|
676
|
+
url.value.split(this.#replacement).join(""),
|
|
677
|
+
key,
|
|
678
|
+
path
|
|
679
|
+
);
|
|
680
|
+
if (residual === void 0) return url.value;
|
|
681
|
+
this.#recordFinding(
|
|
682
|
+
state,
|
|
683
|
+
makeFinding(
|
|
684
|
+
path,
|
|
685
|
+
residual.detector.id,
|
|
686
|
+
"replace",
|
|
687
|
+
residual.detection.matchKind ?? residual.detector.matchKind ?? "custom",
|
|
688
|
+
residual.detection.severity ?? residual.detector.severity ?? "warning"
|
|
689
|
+
)
|
|
690
|
+
);
|
|
691
|
+
return this.#replacement;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
for (const detector of this.#detectors) {
|
|
695
|
+
const detections = detector.detect({ path, key, value });
|
|
696
|
+
for (const detection of detections) {
|
|
697
|
+
const action = detection.action ?? "replace";
|
|
698
|
+
this.#recordFinding(
|
|
699
|
+
state,
|
|
700
|
+
makeFinding(
|
|
701
|
+
path,
|
|
702
|
+
detector.id,
|
|
703
|
+
action,
|
|
704
|
+
detection.matchKind ?? detector.matchKind ?? "custom",
|
|
705
|
+
detection.severity ?? detector.severity ?? "warning",
|
|
706
|
+
detection.preview
|
|
707
|
+
)
|
|
708
|
+
);
|
|
709
|
+
if (action !== "keep") {
|
|
710
|
+
return detection.replacement ?? this.#replacement;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
if (Array.isArray(value)) {
|
|
715
|
+
if (state.seen.has(value)) return state.seen.get(value);
|
|
716
|
+
const out = [];
|
|
717
|
+
state.seen.set(value, out);
|
|
718
|
+
value.forEach((item, index) => {
|
|
719
|
+
out[index] = this.#redactValue(item, void 0, indexPath(path, index), depth + 1, state);
|
|
720
|
+
});
|
|
721
|
+
return out;
|
|
722
|
+
}
|
|
723
|
+
if (isRecord(value)) {
|
|
724
|
+
if (state.seen.has(value)) return state.seen.get(value);
|
|
725
|
+
const out = {};
|
|
726
|
+
state.seen.set(value, out);
|
|
727
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
728
|
+
out[entryKey] = this.#redactValue(
|
|
729
|
+
entryValue,
|
|
730
|
+
entryKey,
|
|
731
|
+
childPath(path === "$" ? "" : path, entryKey),
|
|
732
|
+
depth + 1,
|
|
733
|
+
state
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
return out;
|
|
737
|
+
}
|
|
738
|
+
return value;
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
function createRedactor(options) {
|
|
742
|
+
return new Redactor(options);
|
|
743
|
+
}
|
|
744
|
+
function redact(value, options) {
|
|
745
|
+
return createRedactor(options).redact(value);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
export { DEFAULT_REDACT_KEYS, KEY_VALUE_SECRET_PATTERN, KEY_VALUE_SECRET_PATTERN_SOURCE, Redactor, SHARE_PROFILE_EXTRA_KEYS, STRICT_PROFILE_EXTRA_KEYS, createRedactionProfile, createRedactor, redact, valueContainsKeyValueSecret };
|
|
749
|
+
//# sourceMappingURL=index.mjs.map
|
|
750
|
+
//# sourceMappingURL=index.mjs.map
|