|
| 1 | +/** |
| 2 | + * Redaction utility to scrub sensitive data from logs |
| 3 | + */ |
| 4 | + |
| 5 | +const SENSITIVE_KEYS = [ |
| 6 | + 'api_key', 'apikey', 'secret', 'token', 'password', 'passwd', 'pwd', |
| 7 | + 'auth', 'credential', 'private_key', 'client_secret', 'access_key' |
| 8 | +]; |
| 9 | + |
| 10 | +const SECRET_PATTERNS = [ |
| 11 | + // AWS Access Key ID |
| 12 | + /\b(AKIA|ASIA)[0-9A-Z]{16}\b/g, |
| 13 | + // GitHub Personal Access Token (classic) |
| 14 | + /\bghp_[a-zA-Z0-9]{36}\b/g, |
| 15 | + // Generic Private Key |
| 16 | + /-----BEGIN [A-Z ]+ PRIVATE KEY-----/g, |
| 17 | + // Bearer Token (simple heuristic - starts with Bearer, followed by base64-ish chars) |
| 18 | + /\bBearer\s+[a-zA-Z0-9\-\._~+/]+=*/g, |
| 19 | +]; |
| 20 | + |
| 21 | +// Regex for Key-Value assignments like "key=value" or "key: value" where key is sensitive |
| 22 | +// This catches "export AWS_SECRET_KEY=..." or JSON "password": "..." |
| 23 | +// We construct this dynamically from SENSITIVE_KEYS |
| 24 | +const SENSITIVE_KEY_PATTERN = new RegExp( |
| 25 | + `\\b([a-zA-Z0-9_]*(${SENSITIVE_KEYS.join('|')})[a-zA-Z0-9_]*)\\s*[:=]\\s*['"]?([^\\s'"]{8,})['"]?`, |
| 26 | + 'gi' |
| 27 | +); |
| 28 | + |
| 29 | +export function redactString(str: string): string { |
| 30 | + if (!str) return str; |
| 31 | + |
| 32 | + let redacted = str; |
| 33 | + |
| 34 | + // 1. Redact specific patterns (like AWS keys) |
| 35 | + for (const pattern of SECRET_PATTERNS) { |
| 36 | + redacted = redacted.replace(pattern, '[REDACTED]'); |
| 37 | + } |
| 38 | + |
| 39 | + // 2. Redact key-value pairs where key suggests sensitivity |
| 40 | + // We use a callback to preserve the key and redact the value |
| 41 | + redacted = redacted.replace(SENSITIVE_KEY_PATTERN, (match, key, keyword, value) => { |
| 42 | + // If value is already redacted, skip |
| 43 | + if (value === '[REDACTED]') return match; |
| 44 | + // Replace the value part with [REDACTED] |
| 45 | + return match.replace(value, '[REDACTED]'); |
| 46 | + }); |
| 47 | + |
| 48 | + return redacted; |
| 49 | +} |
| 50 | + |
| 51 | +export function redactObject(obj: any, visited = new WeakSet<any>()): any { |
| 52 | + if (obj === null || obj === undefined) return obj; |
| 53 | + |
| 54 | + if (typeof obj === 'string') { |
| 55 | + return redactString(obj); |
| 56 | + } |
| 57 | + |
| 58 | + if (typeof obj !== 'object') { |
| 59 | + return obj; |
| 60 | + } |
| 61 | + |
| 62 | + if (obj instanceof Date) { |
| 63 | + return obj; |
| 64 | + } |
| 65 | + |
| 66 | + if (visited.has(obj)) { |
| 67 | + return '[CIRCULAR]'; |
| 68 | + } |
| 69 | + visited.add(obj); |
| 70 | + |
| 71 | + if (Array.isArray(obj)) { |
| 72 | + return obj.map(item => redactObject(item, visited)); |
| 73 | + } |
| 74 | + |
| 75 | + if (typeof obj === 'object') { |
| 76 | + const newObj: any = {}; |
| 77 | + for (const [key, value] of Object.entries(obj)) { |
| 78 | + // If the key itself is sensitive, redact the value blindly if it's a string/number |
| 79 | + const isSensitiveKey = SENSITIVE_KEYS.some(k => key.toLowerCase().includes(k)); |
| 80 | + if (isSensitiveKey && (typeof value === 'string' || typeof value === 'number')) { |
| 81 | + newObj[key] = '[REDACTED]'; |
| 82 | + } else { |
| 83 | + newObj[key] = redactObject(value, visited); |
| 84 | + } |
| 85 | + } |
| 86 | + return newObj; |
| 87 | + } |
| 88 | + |
| 89 | + return obj; |
| 90 | +} |
0 commit comments