'use strict'; // Masks secrets (API tokens, passwords in URLs) before anything is written to // the chat history or sent to a browser. What Claude actually ran is not // changed; only the stored and displayed copy is. Shared by the chat server // and scripts/agentweb-boot.js (which cleans logs written by older versions). // Well-known token shapes. Each keeps its prefix and last 4 characters, so a // masked token can still be recognised ("hf_…aGaL") but not used. const TOKEN_PATTERNS = [ /\bhf_[A-Za-z0-9]{30,}\b/g, // Hugging Face /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{30,}\b/g, // GitHub /\bgithub_pat_[A-Za-z0-9_]{40,}\b/g, /\bglpat-[A-Za-z0-9_-]{20,}\b/g, // GitLab /\bsk-ant-[A-Za-z0-9_-]{20,}\b/g, // Anthropic /\bsk-(?:proj-|svcacct-)?[A-Za-z0-9_-]{32,}\b/g, // OpenAI style /\bsk_[a-f0-9]{40,}\b/g, // ElevenLabs /\bKGAT_[A-Za-z0-9]{20,}\b/g, // Kaggle /\bAKIA[0-9A-Z]{16}\b/g, // AWS access key id /\bAIza[0-9A-Za-z_-]{35}\b/g, // Google API key /\bya29\.[0-9A-Za-z_-]{20,}\b/g, // Google OAuth access token /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g, // Slack /\bnpm_[A-Za-z0-9]{36}\b/g, // npm /\bpypi-[A-Za-z0-9_-]{40,}\b/g, // PyPI ]; // https://user:secret@host -> https://user:***@host const URL_CREDENTIALS = /\b([a-z][a-z0-9+.-]*:\/\/)([^\s:@/'"`]{1,200}):([^\s@/'"`]{3,})@/gi; // FOO_TOKEN=value / API_KEY="value" (not $VAR or $(command) references). const ASSIGNMENT = /\b([A-Z0-9_]*(?:TOKEN|SECRET|PASSWORD|PASSWD|API_KEY|APIKEY|ACCESS_KEY)[A-Z0-9_]*)=(["']?)([^\s"'$`][^\s"'`]{7,})\2/g; function maskToken(token) { const prefix = (/^[A-Za-z0-9]{1,12}?[_-](?:[a-z]{2,5}-)?/.exec(token) || [''])[0] || token.slice(0, 4); return `${prefix}…${token.slice(-4)}`; } function alreadyMasked(value) { return value.includes('…') || /^\*+$/.test(value); } function redactText(text) { if (typeof text !== 'string' || text.length < 8) return text; let out = text; for (const re of TOKEN_PATTERNS) { re.lastIndex = 0; out = out.replace(re, (m) => maskToken(m)); } out = out.replace(URL_CREDENTIALS, (m, scheme, user, secret) => (alreadyMasked(secret) ? m : `${scheme}${user}:***@`)); out = out.replace(ASSIGNMENT, (m, name, quote, value) => (alreadyMasked(value) ? m : `${name}=${quote}***${quote}`)); return out; } // Every string inside an event (tool inputs and results included). function redactDeep(value, depth = 0) { if (typeof value === 'string') return redactText(value); if (!value || typeof value !== 'object' || depth > 8) return value; if (Array.isArray(value)) return value.map((v) => redactDeep(v, depth + 1)); const out = {}; for (const [key, v] of Object.entries(value)) out[key] = redactDeep(v, depth + 1); return out; } // For raw JSON lines (transcripts): tokens never contain characters that // JSON escapes, so the line stays valid. function redactJsonLine(line) { return redactText(line); } function containsSecret(text) { return typeof text === 'string' && redactText(text) !== text; } module.exports = { redactText, redactDeep, redactJsonLine, containsSecret, maskToken };