|
| 1 | +/** |
| 2 | + * Secret redaction for captured transcripts (Codex review #9). |
| 3 | + * |
| 4 | + * Transcripts capture full prompts + responses, which can include |
| 5 | + * source pulled from the target repo, stack traces, and — the real |
| 6 | + * risk — secrets that happen to appear in that material. Redaction |
| 7 | + * masks well-known secret token shapes before anything is written to |
| 8 | + * disk. It is deliberately conservative (mask known shapes, don't try |
| 9 | + * to be a general-purpose DLP) and runs by default; callers can opt out |
| 10 | + * with `{ redact: false }` on the instrumented callers. |
| 11 | + */ |
| 12 | + |
| 13 | +export interface RedactionRule { |
| 14 | + name: string; |
| 15 | + pattern: RegExp; |
| 16 | +} |
| 17 | + |
| 18 | +/** Known secret token shapes. Ordered roughly by specificity. */ |
| 19 | +export const DEFAULT_REDACTION_RULES: RedactionRule[] = [ |
| 20 | + // Anthropic keys: sk-ant-… (long). |
| 21 | + { name: 'anthropic-key', pattern: /sk-ant-[A-Za-z0-9_-]{16,}/g }, |
| 22 | + // OpenAI keys: sk-…, sk-proj-…, sk-svcacct-… . |
| 23 | + { name: 'openai-key', pattern: /sk-(?:proj-|svcacct-)?[A-Za-z0-9_-]{20,}/g }, |
| 24 | + // GitHub tokens: ghp_, gho_, ghu_, ghs_, ghr_ + 36+ chars. |
| 25 | + { name: 'github-token', pattern: /gh[posur]_[A-Za-z0-9]{36,}/g }, |
| 26 | + // GitHub fine-grained PAT. |
| 27 | + { name: 'github-pat', pattern: /github_pat_[A-Za-z0-9_]{22,}/g }, |
| 28 | + // AWS access key id. |
| 29 | + { name: 'aws-akid', pattern: /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g }, |
| 30 | + // Google API key. |
| 31 | + { name: 'google-key', pattern: /\bAIza[A-Za-z0-9_-]{35}\b/g }, |
| 32 | + // Slack token. |
| 33 | + { name: 'slack-token', pattern: /xox[baprs]-[A-Za-z0-9-]{10,}/g }, |
| 34 | + // Bearer header values. |
| 35 | + { name: 'bearer', pattern: /[Bb]earer\s+[A-Za-z0-9._-]{20,}/g }, |
| 36 | + // Generic `<SECRETY_NAME>=<value>` / `: <value>` assignments for |
| 37 | + // env vars whose NAME looks secret-y. Masks only the value. |
| 38 | + { |
| 39 | + name: 'assigned-secret', |
| 40 | + pattern: |
| 41 | + /\b([A-Z0-9_]*(?:KEY|TOKEN|SECRET|PASSWORD|PASSWD|CREDENTIAL)S?)\b(\s*[:=]\s*)(['"]?)([^\s'"]{6,})\3/gi, |
| 42 | + }, |
| 43 | +]; |
| 44 | + |
| 45 | +const MASK = '«REDACTED»'; |
| 46 | + |
| 47 | +/** |
| 48 | + * Redact secret-shaped substrings from `text`. Returns the masked text |
| 49 | + * and a count of redactions made (useful for a "N secrets masked" |
| 50 | + * note). Idempotent-ish: re-running over masked text is a no-op for the |
| 51 | + * token rules. |
| 52 | + */ |
| 53 | +export function redactSecrets( |
| 54 | + text: string, |
| 55 | + rules: RedactionRule[] = DEFAULT_REDACTION_RULES, |
| 56 | +): { text: string; redactions: number } { |
| 57 | + let redactions = 0; |
| 58 | + let out = text; |
| 59 | + for (const rule of rules) { |
| 60 | + if (rule.name === 'assigned-secret') { |
| 61 | + out = out.replace(rule.pattern, (_m, name, sep, quote, _val) => { |
| 62 | + redactions += 1; |
| 63 | + return `${name}${sep}${quote}${MASK}${quote}`; |
| 64 | + }); |
| 65 | + } else { |
| 66 | + out = out.replace(rule.pattern, () => { |
| 67 | + redactions += 1; |
| 68 | + return MASK; |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | + return { text: out, redactions }; |
| 73 | +} |
0 commit comments