import type { VaultService } from '../security/vault.js'; export interface RedactionResult { text: string; redactedCount: number; truncated: boolean; } function escapeRegExp(literal: string): string { return literal.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } export function collectVaultSecretStrings(vaultService: VaultService): string[] { if (!vaultService.isRuntimeReady) return []; const collected = new Set(); try { const creds = vaultService.exportAllCredentials(); for (const cred of Object.values(creds)) { if (cred.type === 'ssh-password' || cred.type === 'sudo-password') { if (cred.password.length >= 6) collected.add(cred.password); } else if (cred.type === 'webhook-secret') { if (cred.secret.length >= 6) collected.add(cred.secret); } else if (cred.type === 'api-token') { if (cred.token.length >= 6) collected.add(cred.token); } else if (cred.type === 'ssh-key') { const trimmed = cred.privateKey.trim(); if (trimmed.length >= 6) collected.add(trimmed); for (const line of trimmed.split(/\r?\n/)) { const l = line.trim(); if (l.length >= 12 && !l.startsWith('-----')) { collected.add(l); } } if (cred.passphrase && cred.passphrase.length >= 6) { collected.add(cred.passphrase); } } } } catch { return []; } return [...collected].sort((a, b) => b.length - a.length); } export function redactSecrets( input: string, knownSecrets: string[] = [], maxChars = 16_000, ): RedactionResult { let redactedCount = 0; let text = input ?? ''; // 1. Strip PEM / OpenSSH private key blocks const keyBlockRe = /-----BEGIN [A-Z0-9 -]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 -]*PRIVATE KEY-----/g; text = text.replace(keyBlockRe, () => { redactedCount += 1; return '[REDACTED_PRIVATE_KEY]'; }); // 2. Strip known secrets currently held in Vault for (const secret of knownSecrets) { if (secret.length < 6) continue; const re = new RegExp(escapeRegExp(secret), 'g'); text = text.replace(re, () => { redactedCount += 1; return '[REDACTED_VAULT_SECRET]'; }); } // 3. Strip HTTP Authorization / Bearer / custom secret headers text = text.replace( /\b(Authorization\s*:\s*(?:Bearer|Basic)\s+)([^\r\n]+)/gi, (_m, prefix: string, val: string) => { if (val.includes('[REDACTED')) return _m; redactedCount += 1; return `${prefix}[REDACTED_TOKEN]`; }, ); text = text.replace( /\bBearer\s+([A-Za-z0-9._~+/=-]{12,})/g, (_m, val: string) => { if (val.includes('[REDACTED')) return _m; redactedCount += 1; return 'Bearer [REDACTED_TOKEN]'; }, ); text = text.replace( /\b(X-(?:API-Key|Telegram-Bot-Token|Auth-Token)\s*:\s*)([^\s\r\n]+)/gi, (_m, prefix: string, val: string) => { if (val.includes('[REDACTED')) return _m; redactedCount += 1; return `${prefix}[REDACTED_TOKEN]`; }, ); // 4. Strip .env / shell KEY=value secret assignments text = text.replace( /(^|[\s;|&])([A-Za-z0-9_]*(?:PASSWORD|PASSWD|SECRET|TOKEN|API_KEY|APIKEY|PRIVATE_KEY|PASSPHRASE)[A-Za-z0-9_]*)=("[^"\n]*"|'[^'\n]*'|[^\s\r\n]+)/gim, (_m, lead: string, key: string, rawVal: string) => { if (rawVal.includes('[REDACTED')) return _m; redactedCount += 1; return `${lead}${key}=[REDACTED_SECRET]`; }, ); // 5. Strip JSON "key": "secret" string properties text = text.replace( /("(?:[A-Za-z0-9_-]*(?:password|passwd|secret|token|apiKey|api_key|privateKey|private_key|passphrase)[A-Za-z0-9_-]*)"\s*:\s*)"([^"\\]*(?:\\.[^"\\]*)*)"/gi, (_m, keyPrefix: string, val: string) => { if (val.includes('[REDACTED')) return _m; redactedCount += 1; return `${keyPrefix}"[REDACTED_SECRET]"`; }, ); const cap = Math.max(256, maxChars); let truncated = false; if (text.length > cap) { truncated = true; const prefix = '...[truncated older context]...\n'; const tailLen = Math.max(64, cap - prefix.length); text = `${prefix}${text.slice(-tailLen)}`; } return { text, redactedCount, truncated }; }