|
| 1 | +const FULL_MASK = "***"; |
| 2 | +const MIDDLE_MASK = "*****"; |
| 3 | +const MIN_HIDDEN_CHARS = 4; |
| 4 | +const LAST_VISIBLE_CHARS = 6; |
| 5 | + |
| 6 | +// Map of route pattern -> response body fields that should be masked before logging. |
| 7 | +export const SENSITIVE_RESPONSE_FIELDS_BY_ROUTE = { |
| 8 | + "/tokens/embed/referrals": ["publicToken"], |
| 9 | +} as const; |
| 10 | + |
| 11 | +// Stripe-style partial mask: visible prefix through the last `_` (e.g. `sk_live_`), |
| 12 | +// a fixed middle mask, and the last 6 characters (e.g. `sk_live_*****xyz123`). |
| 13 | +// Values without enough characters between prefix and suffix fully mask. |
| 14 | +export function maskSensitiveValue(value: unknown): string { |
| 15 | + if (typeof value !== "string" || value.length === 0) { |
| 16 | + return FULL_MASK; |
| 17 | + } |
| 18 | + |
| 19 | + const suffixLen = Math.min(LAST_VISIBLE_CHARS, value.length); |
| 20 | + const suffixStart = value.length - suffixLen; |
| 21 | + |
| 22 | + const lastUnderscore = value.lastIndexOf("_"); |
| 23 | + const prefixEnd = lastUnderscore >= 0 ? lastUnderscore + 1 : 0; |
| 24 | + |
| 25 | + if (prefixEnd > suffixStart) { |
| 26 | + return FULL_MASK; |
| 27 | + } |
| 28 | + |
| 29 | + if (suffixStart - prefixEnd < MIN_HIDDEN_CHARS) { |
| 30 | + return FULL_MASK; |
| 31 | + } |
| 32 | + |
| 33 | + const prefix = value.slice(0, prefixEnd); |
| 34 | + const suffix = value.slice(suffixStart); |
| 35 | + |
| 36 | + return `${prefix}${MIDDLE_MASK}${suffix}`; |
| 37 | +} |
| 38 | + |
| 39 | +// Recursively mask the given keys in an object/array. Returns a new value and |
| 40 | +// does not mutate the input. Non-object values are returned as-is. |
| 41 | +export function maskSensitiveFields<T>({ |
| 42 | + body, |
| 43 | + keys, |
| 44 | +}: { |
| 45 | + body: T; |
| 46 | + keys: string[]; |
| 47 | +}): T { |
| 48 | + if (!body || keys.length === 0) { |
| 49 | + return body; |
| 50 | + } |
| 51 | + |
| 52 | + const keySet = new Set(keys); |
| 53 | + |
| 54 | + const mask = (value: unknown): unknown => { |
| 55 | + if (Array.isArray(value)) { |
| 56 | + return value.map(mask); |
| 57 | + } |
| 58 | + |
| 59 | + if (value && typeof value === "object") { |
| 60 | + const result: Record<string, unknown> = {}; |
| 61 | + for (const [k, v] of Object.entries(value)) { |
| 62 | + result[k] = keySet.has(k) ? maskSensitiveValue(v) : mask(v); |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + return value; |
| 68 | + }; |
| 69 | + |
| 70 | + return mask(body) as T; |
| 71 | +} |
0 commit comments