/** * Fail-closed secret redaction for imported session content (issue #3709). * * Every string that crosses the import boundary passes through * {@link redactImportedText}. Patterns are conservative: a false positive costs * one `[REDACTED]` span in reconstructed context, a false negative leaks a * credential into a new session transcript, so the balance favors redaction. */ export const IMPORT_REDACTED_PLACEHOLDER = "[REDACTED]"; /** Bumped when patterns change; persisted in import provenance. */ export const IMPORT_SANITIZER_VERSION = 6; // Terminal escapes and invisible/bidi controls never survive import: they can // restyle or hide transcript text when the imported session is rendered. const UNSAFE_CONTROL = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f\u200b-\u200f\u202a-\u202e\u2060\u2066-\u2069\ufeff]/gu; const HEADER_CREDENTIAL = /(\b(?:authorization|cookie|set-cookie)\b[ \t]*:[ \t]*)([^\r\n]*)/giu; const JSON_AUTHORIZATION = /((?:\\?")authorization(?:\\?")\s*:\s*(?:\\?")?)([^",}\r\n]+)/giu; interface RedactionRule { readonly id: string; readonly pattern: RegExp; } const HEX = "[0-9a-fA-F]"; /** * Ordered first-match-wins rules. Each rule replaces its full match with the * placeholder, so a matched credential never survives partially. */ const REDACTION_RULES: readonly RedactionRule[] = [ // PEM private-key blocks, header through footer. { id: "pem-private-key", pattern: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/g, }, // AWS access key ids. { id: "aws-access-key", pattern: /(?:AKIA|ASIA|ABIA|ACCA)[0-9A-Z]{16}/g }, // Standalone AWS secret access keys are 40 mixed-case base64 characters. { id: "aws-secret-key", pattern: /(?= n) break; const kind = input.charCodeAt(esc + 1); let end = -1; if (kind === 0x5b) end = csiSequenceEnd(input, esc + 2); else if (kind === 0x5d && oscLive) end = oscSequenceEnd(input, esc + 2); // No BEL and no ST anywhere after this prefix, so later OSC cannot terminate. if (kind === 0x5d && end < 0) oscLive = false; if (end < 0) { i = esc + 1; continue; } if (esc > from) parts.push(input.slice(from, esc)); from = end; i = end; } if (from === 0) return input; if (from < n) parts.push(input.slice(from)); return parts.join(""); } function csiSequenceEnd(input: string, j: number): number { const n = input.length; while (j < n && input.charCodeAt(j) >= 0x30 && input.charCodeAt(j) <= 0x3f) j++; while (j < n && input.charCodeAt(j) >= 0x20 && input.charCodeAt(j) <= 0x2f) j++; const finalByte = input.charCodeAt(j); return finalByte >= 0x40 && finalByte <= 0x7e ? j + 1 : -1; } function oscSequenceEnd(input: string, index: number): number { // One forward scan. A second search to the end of the suffix is quadratic for repeated OSC sequences. const n = input.length; for (let j = index; j < n; j++) { const code = input.charCodeAt(j); if (code === 0x07) return j + 1; if (code === 0x1b && j + 1 < n && input.charCodeAt(j + 1) === 0x5c) return j + 2; } return -1; } export interface ImportRedactionResult { value: string; redacted: number; kinds: string[]; } /** * Redact every known credential shape from imported text. The same placeholder * is used for all kinds so a partial pattern overlap cannot leak fragments. */ export function redactImportedText(input: string): ImportRedactionResult { let value = stripImportedAnsiEscapes(input).replace(UNSAFE_CONTROL, ""); let redacted = 0; const kinds = new Set(); value = value.replace(HEADER_CREDENTIAL, (_match, prefix: string, raw: string) => { if (!raw.trim()) return `${prefix}${raw}`; redacted++; kinds.add("credential-header"); return `${prefix}${IMPORT_REDACTED_PLACEHOLDER}`; }); value = value.replace(JSON_AUTHORIZATION, (_match, prefix: string, raw: string) => { if (!raw.trim()) return `${prefix}${raw}`; redacted++; kinds.add("authorization-json"); return `${prefix}${IMPORT_REDACTED_PLACEHOLDER}`; }); for (const rule of REDACTION_RULES) { rule.pattern.lastIndex = 0; value = value.replace(rule.pattern, match => { redacted++; kinds.add(rule.id); if (rule.id === "url-credential") { // Keep the scheme visible; the match ends at "@" so the host survives. const schemeEnd = match.indexOf("://") + 3; return `${match.slice(0, schemeEnd)}${IMPORT_REDACTED_PLACEHOLDER}@`; } if (rule.id === "secret-assignment" || rule.id === "authorization-credential") { const sep = /(["']?\s*[:=]\s*)/.exec(match); if (sep) { const head = match.slice(0, (sep.index ?? 0) + sep[0].length); return `${head}${IMPORT_REDACTED_PLACEHOLDER}`; } } if (rule.id === "bearer-token") { const bearerAt = /bearer\s+/i.exec(match); if (bearerAt) return `${match.slice(0, bearerAt.index + bearerAt[0].length)}${IMPORT_REDACTED_PLACEHOLDER}`; } return IMPORT_REDACTED_PLACEHOLDER; }); } return { value, redacted, kinds: [...kinds].sort() }; } /** Redact a string, returning only the sanitized value. */ export function sanitizeImportedString(input: string): string { return redactImportedText(input).value; }