21 lines
880 B
TypeScript
21 lines
880 B
TypeScript
export const REDACTED = "[REDACTED]";
|
|
const SENSITIVE_KEY = /passkey|token|cookie|authorization|password|secret|api[-_]?key/i;
|
|
const SENSITIVE_QUERY = /([?&](?:passkey|token|access_token|api_key|apikey|auth|sign)=)[^&#\s"'<>]*/gi;
|
|
export function redactString(s: string): string {
|
|
return s.replace(SENSITIVE_QUERY, `$1${REDACTED}`);
|
|
}
|
|
export function redact(value: unknown, depth = 0): unknown {
|
|
if (depth > 8) return value;
|
|
if (typeof value === "string") return redactString(value);
|
|
if (Array.isArray(value)) return value.map((v) => redact(v, depth + 1));
|
|
if (value && typeof value === "object") {
|
|
const out: Record<string, unknown> = {};
|
|
for (const [k, v] of Object.entries(value)) {
|
|
if (SENSITIVE_KEY.test(k) && typeof v !== "number" && v != null) out[k] = REDACTED;
|
|
else out[k] = redact(v, depth + 1);
|
|
}
|
|
return out;
|
|
}
|
|
return value;
|
|
}
|