/** * Log lines are filtered for anything that looks like a secret before they leave the server * (AGENT-1.0-PLAN §4, rule 7). The crosscode script never prints .env values, but tools it runs * (npm, Prisma, git) might print a URL with a password or a token in an error. */ const RULES: [RegExp, string][] = [ // user:password@ in any URL (postgresql://, https://). [/([a-z][a-z0-9+.-]*:\/\/[^\s:/@]+:)[^\s@/]+@/gi, "$1***@"], // KEY=value and KEY: value for keys that hold secrets. [/\b([A-Z0-9_]*(SECRET|PASSWORD|PASSWD|TOKEN|API_KEY|PRIVATE_KEY|DSN|LICENCE_KEY|LICENSE_KEY)[A-Z0-9_]*)(\s*[=:]\s*)("[^"]*"|'[^']*'|\S+)/g, "$1$3***"], // Authorization headers and bearer tokens. [/\b(authorization\s*:\s*)(bearer|basic)\s+\S+/gi, "$1$2 ***"], [/\bbearer\s+[A-Za-z0-9._~+/=-]{16,}/gi, "Bearer ***"], // PEM blocks. [/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g, "[private key]"], // Cross Code licence keys (CEH-7K2M-…). [/\b(CEH|CCA|CCW|CCP)-[A-Z0-9]{4}(-[A-Z0-9]{4}){3}\b/g, "$1-****"], // Long random-looking strings (hex or base64 of 32+ characters): likely keys or tokens. [/\b[A-Fa-f0-9]{40,}\b/g, "***"], [/\b[A-Za-z0-9+/_-]{48,}={0,2}/g, "***"], ]; export function redact(line: string): string { let out = line; for (const [re, to] of RULES) out = out.replace(re, to); return out.length > 1000 ? `${out.slice(0, 997)}...` : out; } /** Keeps the last `max` lines of output, redacted, without holding all of it in memory. */ export class LogTail { private lines: string[] = []; private partial = ""; constructor(private max = 200) {} push(chunk: string): void { const parts = (this.partial + chunk).split(/\r?\n/); this.partial = parts.pop() ?? ""; for (const p of parts) this.add(p); } private add(line: string): void { this.lines.push(redact(line)); if (this.lines.length > this.max) this.lines.shift(); } end(): string[] { if (this.partial) { this.add(this.partial); this.partial = ""; } return [...this.lines]; } }