/** * Recursive PII / secret sanitization used by the Sentry adapter and the runtime * `reportError` helper. Lives in its own module so the Hono / BullMQ wiring can * import it directly without pulling the Sentry adapter surface. */ const REDACTED = "[Redacted]"; const CIRCULAR = "[Circular]"; const MAX_STRING_BYTES = 8 * 1024; /** * Header / payload key denylist. Matching is case-insensitive against the trimmed * key. Entries are matched both as exact equals and against canonicalised forms * with `-` and `_` stripped, so `set-cookie`, `setCookie`, and `set_cookie` are * all redacted. */ const SENSITIVE_KEY_DENYLIST = new Set([ // Phase 02 / Sentry-wiring spec (#30) "password", "passwd", "pwd", "token", "accesstoken", "refreshtoken", "idtoken", "secret", "apikey", "authorization", "cookie", "setcookie", "xapikey", "xcsrftoken", "csrftoken", "sessionid", "session", "phone", "phonenumber", "email", "emailaddress", // Legacy denylist preserved from the previous flat sanitizer so existing // call-sites (mail, workflow, queue payloads) keep their guarantees. "body", "headers", "recipient", "templatedata", "to", "connectionstring", "redisurl", ]); const BEARER_TOKEN_PATTERN = /bearer\s+[^\s,;]+/gi; const POSTGRES_URL_PATTERN = /postgres(?:ql)?:\/\/[^\s,;]+/gi; const REDIS_URL_PATTERN = /redis:\/\/[^\s,;]+/gi; const KEY_VALUE_SECRET_PATTERN = /(?:token|secret|password|apiKey|connectionString|redisUrl)=([^\s,;]+)/gi; export function sanitizeErrorReportValue(value: unknown): unknown { return sanitizeValue(value, undefined, new WeakSet()); } function sanitizeValue(value: unknown, key: string | undefined, seen: WeakSet): unknown { if (key && isSensitiveKey(key)) { return REDACTED; } if (value instanceof Error) { return sanitizeError(value, seen); } if (Array.isArray(value)) { if (seen.has(value)) { return CIRCULAR; } seen.add(value); const result = value.map((entry) => sanitizeValue(entry, undefined, seen)); seen.delete(value); return result; } if (value && typeof value === "object") { if (seen.has(value)) { return CIRCULAR; } seen.add(value); const sanitized: Record = {}; for (const [entryKey, entryValue] of Object.entries(value)) { sanitized[entryKey] = sanitizeValue(entryValue, entryKey, seen); } seen.delete(value); return sanitized; } if (typeof value === "string") { return truncateString(redactUnsafeScalar(value)); } return value; } function isSensitiveKey(key: string): boolean { const normalized = key.trim().toLowerCase(); if (SENSITIVE_KEY_DENYLIST.has(normalized)) { return true; } const compact = normalized.replace(/[-_]/g, ""); return SENSITIVE_KEY_DENYLIST.has(compact); } function redactUnsafeScalar(value: string): string { return value .replace(BEARER_TOKEN_PATTERN, "Bearer [Redacted]") .replace(POSTGRES_URL_PATTERN, "[Redacted]") .replace(REDIS_URL_PATTERN, "[Redacted]") .replace(KEY_VALUE_SECRET_PATTERN, () => "[Redacted]"); } function truncateString(value: string): string { // Byte-length aware truncation so multibyte characters never push us over the // limit. We use UTF-8 byte counting through Buffer when available; the // browser/edge runtimes that lack Buffer still get a safe `value.length` // approximation. const bytes = computeByteLength(value); if (bytes <= MAX_STRING_BYTES) { return value; } return ``; } function computeByteLength(value: string): number { if (typeof Buffer !== "undefined" && typeof Buffer.byteLength === "function") { return Buffer.byteLength(value, "utf8"); } // Fallback: TextEncoder is universally available in Node 18+ and browsers. if (typeof TextEncoder !== "undefined") { return new TextEncoder().encode(value).length; } return value.length; } function sanitizeError(error: Error, seen: WeakSet): Record { const sanitized: Record = { errorName: error.name, errorMessage: redactUnsafeScalar(error.message), }; for (const [key, value] of Object.entries(error)) { sanitized[key] = sanitizeValue(value, key, seen); } return sanitized; } export const __internal = { MAX_STRING_BYTES, REDACTED, CIRCULAR, isSensitiveKey, redactUnsafeScalar, truncateString, };