diff --git a/mods/memory/hooks/register.test.ts b/mods/memory/hooks/register.test.ts index edcbc79..3a80b3d 100644 --- a/mods/memory/hooks/register.test.ts +++ b/mods/memory/hooks/register.test.ts @@ -87,7 +87,8 @@ async function boot($: Engine, on: On, o: Opts = {}): Promise { on('env.get', () => ({ value: HOME })) on('fs.exists', (_a, e) => { if (w.existsFails.has(e.path)) throw new Error('EIO') - return { value: w.files.has(e.path) || e.path === SWITCH } + return { value: w.files.has(e.path) || + (e.path === SWITCH && w.switchText !== undefined) } }) on('fs.stat', (_a, e) => { if (w.statFails.has(e.path)) throw new Error('EIO') @@ -173,10 +174,19 @@ test('an absent switch file keeps the mod on', async ($, on) => { test('a malformed switch file keeps the mod on', async ($, on) => { const w = await boot($, on, { switchText: '{nope' }) expect(await bash($, 'ls')).toEqual(BASH_ANSWER) - expect(w.logs).toEqual([]) + expect(await bash($, 'ls')).toEqual(BASH_ANSWER) + expect(w.logs).toEqual([ + 'memory: ~/.claude/memory.json unreadable, capture stays on', + ]) expect(w.tools).toEqual(['memory_pending']) }) +test('an absent switch file logs nothing', async ($, on) => { + const w = await boot($, on) + await bash($, 'ls') + expect(w.logs).toEqual([]) +}) + test('the tool is not registered when the switch is off', async ($, on) => { const w = await boot($, on, { switchText: '{"enabled": false}' }) expect(w.tools).toEqual([]) @@ -1365,3 +1375,93 @@ test('session.end lets a hook error log again', async ($, on) => { await bash($, 'ls') expect(warns()).toHaveLength(2) }) + +// ---- 6-bis: vendor prefixes, flags, headers, JSON, pairs, URL user ------ + +const SIX_BIS: [string, string, string][] = [ + ['anthropic', 't ' + 'sk-' + 'ant-' + 'api03-AbC_dEf-123 z', + 't z'], + ['openai', 't ' + 'sk-' + 'proj-' + 'AbCdEf_123-xyz z', + 't z'], + ['github pat', 't ' + 'github_' + 'pat_' + '11ABCDEFG_0123456789 z', + 't z'], + ['gitlab', 't ' + 'gl' + 'pat-' + 'abcDEF-12345_xyz z', + 't z'], + ['google', 't ' + 'AI' + 'za' + 'SyA-1234567890_abcdefGHIJ z', + 't z'], + ['npm', 't ' + 'npm' + '_' + 'abcdEFGH0123 z', 't z'], + ['flag token', 'cli --tok' + 'en abc123 run', 'cli --token run'], + ['flag api-key=', 'cli --api' + '-key=abc123 run', + 'cli --api-key run'], + ['flag secret', 'cli --sec' + 'ret abc123', 'cli --secret '], + ['header x-api-key', 'curl -H "x-api' + '-key: abc 123" u', + 'curl -H "x-api-key: '], + ['header private-token', 'PRIVATE-' + 'TOKEN: abc', + 'PRIVATE-TOKEN: '], + ['header cookie', 'Coo' + 'kie: a=1; b=2\nnext', 'Cookie: \nnext'], + ['json', '{"pass' + 'word": "a b", "x": 1}', + '{"password": "", "x": 1}'], + ['json upper key', '{"TOK' + 'EN":"abc"}', '{"TOKEN": ""}'], + ['pair colon', 'tok' + 'en: abc123 end', 'token: end'], + ['pair spaced', 'aws_sec' + 'ret_access_key = abc123 end', + 'aws_secret_access_key: end'], + ['pair quoted', 'PASS' + 'WORD="a b" end', 'PASSWORD= end'], + ['pair single quoted', "sec" + "ret: 'a b' end", 'secret: end'], + ['pair after pattern 1 not doubled', 'X_TOK' + 'EN=abc end', + 'X_TOKEN= end'], + ['hex url user', 'git clone https://' + 'a'.repeat(20) + '1'.repeat(20) + + '@host/x', 'git clone https://@host/x'], +] + +for (const [name, input, expected] of SIX_BIS) { + test(`redact 6-bis ${name}`, () => { + expect(redact(input)).toBe(expected) + }) +} + +test('redact 6-bis keeps a bare 40-hex SHA and a short hex URL user', () => { + const sha = 'a'.repeat(20) + '1'.repeat(20) + expect(redact('at ' + sha + '@host')).toBe('at ' + sha + '@host') + expect(redact('https://abc123@host')).toBe('https://abc123@host') +}) + +test('redact 6-bis residual: user:p@ss@host keeps its tail', () => { + expect(redact('git clone https://user:p@ss@host/x')) + .toBe('git clone https://@ss@host/x') +}) + +test('TIME1: each 6-bis rule stays fast on 64 KiB adversarial input', () => { + const inputs = [ + 'sk-ant-'.repeat(9362), 'github_pat_'.repeat(5958), 'npm_'.repeat(16384), + '--token '.repeat(8192), 'Cookie:'.repeat(9362), '"token":'.repeat(8192), + 'token: '.repeat(9362), 'a_token ='.repeat(7282), 'x://'.repeat(16384), + 'a'.repeat(30) + ' token:'.repeat(9362), + '"password": "'.repeat(5041), "token='".repeat(9362), + ('a'.repeat(60) + 'token' + 'b'.repeat(60) + ' ').repeat(361), + ] + for (const text of inputs) { + const t0 = now() + redact(text) + expect(now() - t0).toBeLessThan(10) + } +}) + +test('the registry index caps a 1 MiB span before normalize', () => { + const body = '## BLK-001 x\n`' + 'fatal word '.repeat(95326) + '`\n' + const t0 = now() + const index = buildIndex([body]) + expect(now() - t0).toBeLessThan(50) + expect(index.size).toBeGreaterThan(0) +}) + +test('an inner CR in a stored line shows as the newline mark', async ( + $, on, +) => { + const seed = { [`p:${ROOT}`]: { sessions: [{ + id: 's1', at: 5, milestones: [], red: [], + failures: [{ key: 'k', line: 'fatal a\rb', excerpt: '', command: 'c', + agent: false, at: 1, count: 1 }], + }] } } + await boot($, on, { seed }) + expect(await pending($)).toContain('- NEW \u00d71 fatal a\u23ceb \u2014') +}) diff --git a/mods/memory/hooks/register.ts b/mods/memory/hooks/register.ts index 7a90a41..46f405d 100644 --- a/mods/memory/hooks/register.ts +++ b/mods/memory/hooks/register.ts @@ -18,17 +18,24 @@ const SWITCH_FILE = '.claude/memory.json' const MAX_SWITCH_BYTES = 65536 const TOOL = 'memory_pending' const SERVED_TOOL = 'mcp__memory__memory_pending' +const SWITCH_UNREADABLE = + 'memory: ~/.claude/memory.json unreadable, capture stays on' const MAX_REGEX_INPUT = 65536 const PEM = '-----' const PEM_KIND = '(?:[A-Z]+ )?PRIVATE KEY' +// a value: a quoted string up to its closing quote, else up to a space +const VALUE = '("[^"\\n]{0,400}"|\'[^\'\\n]{0,400}\'|\\S+)' +const SECRET_KEY = '(?:password|token|secret|api_key)' // Redaction patterns, applied in order: the JWT shape first (so a secret // named variable still ends as `NAME=` through pattern 1, and any // other name keeps `NAME=`), then the name rule, the vendor // shapes, PEM, Bearer, URL credentials, and last the command-line shapes -// (-u user:pass, --password, Authorization). Repeats are bounded where the -// input is free text, so no pattern can run away on a 64 KiB input. +// (-u user:pass, --password, Authorization), then the 6-bis shapes: vendor +// prefixes, --token flags, API-key headers, JSON and `key: value` pairs, a +// hex URL user. Repeats are bounded where the input is free text, so no +// pattern can run away on a 64 KiB input. const REDACTIONS: [RegExp, string][] = [ [new RegExp( '(?'], [new RegExp( '\\b([A-Za-z0-9_]{0,60}(?:TOKEN|API_KEY|SECRET|PASSWORD|PASSWD)' + - '[A-Za-z0-9_]{0,60})=\\S+', + '[A-Za-z0-9_]{0,60})=' + VALUE, 'gi', ), '$1='], [/sk_live_[0-9A-Za-z]{10,}/g, ''], @@ -57,6 +64,26 @@ const REDACTIONS: [RegExp, string][] = [ [/--password(?:=|[ \t]{1,5})\S*/g, '--password '], [/Authorization:[ \t]{0,5}(Basic|Bearer|token)[ \t]{1,5}\S*/gi, 'Authorization: $1 '], + [/sk-ant-[A-Za-z0-9_-]{10,}/g, ''], + [/sk-proj-[A-Za-z0-9_-]{10,}/g, ''], + [/github_pat_[A-Za-z0-9_]{10,}/g, ''], + [/glpat-[A-Za-z0-9_-]{10,}/g, ''], + [/AIza[A-Za-z0-9_-]{20,}/g, ''], + [/npm_[A-Za-z0-9]{10,}/g, ''], + [/--(token|api-key|secret)(?:=|[ \t]{1,5})\S*/g, '--$1 '], + [/(X-Api-Key|PRIVATE-TOKEN|Cookie):[ \t]{0,5}[^\n]{1,400}/gi, + '$1: '], + [new RegExp( + `"(${SECRET_KEY})"[ \\t]{0,5}:[ \\t]{0,5}"[^"\\n]{0,400}"`, + 'gi', + ), '"$1": ""'], + // a value already `` (pattern 1) is not marked twice + [new RegExp( + `\\b([A-Za-z0-9_]{0,60}${SECRET_KEY}[A-Za-z0-9_]{0,60})` + + `[ \\t]{0,5}[:=][ \\t]{0,5}(?!'], + [/([a-z][a-z0-9+.-]{0,30}:\/\/)[0-9a-f]{40}@/gi, '$1@'], ] const HEX_RUN = /\b[0-9a-f]{7,}\b/g @@ -122,6 +149,7 @@ const MAX_MILESTONES = 20 const MAX_RED = 10 const MAX_RED_LEN = 300 const MAX_LINE = 300 +const MAX_SPAN = 300 // a registry span is capped before normalize const MAX_COMMAND = 300 const EXCERPT_LEN = 600 const MAX_ENTRY = 2048 @@ -386,7 +414,7 @@ export function grams(normLine: string): string[] { function spansOf(body: string): string[] { return [...body.matchAll(SPAN)] - .map(m => m[1] ?? '') + .map(m => (m[1] ?? '').slice(0, MAX_SPAN)) .filter(span => span.length >= MIN_SPAN) } @@ -487,7 +515,8 @@ function failureText({ f, id, earlier }: Row): string { const marks = (f.agent ? ' (sub-agent)' : '') + (earlier ? ' (earlier session)' : '') const cmd = f.command.replace(NEWLINES, '⏎') - const head = `- ${tag} ×${f.count}${marks} ${f.line} — command: ${cmd}` + const line = f.line.replace(NEWLINES, '⏎') + const head = `- ${tag} ×${f.count}${marks} ${line} — command: ${cmd}` if (f.excerpt === '') return head const shown = tail(f.excerpt, EXCERPT_SHOWN).replace(NEWLINES, '⏎') return `${head}\n excerpt: ${shown}` @@ -642,27 +671,39 @@ function newState(): State { /** Logs a hook's fail-open once per session; never throws itself. */ function warnOnce(st: State, $: Api, hook: string, kind: string): void { - if (st.warned.has(hook)) return - st.warned.add(hook) + logOnce(st, $, hook, `memory: ${hook} hook failed (${kind}); passed through`) +} + +/** Logs `text` once per session under `key`; never throws itself. */ +function logOnce(st: State, $: Api, key: string, text: string): void { + if (st.warned.has(key)) return + st.warned.add(key) try { - $.ui.log(`memory: ${hook} hook failed (${kind}); passed through`) + $.ui.log(text) } catch { // a failing log must not break the fail-open itself } } -/** True unless the user's switch file says `enabled: false`. */ -async function loadEnabled($: Api): Promise { +/** + * True unless the user's switch file says `enabled: false`. A file that + * exists but cannot be read or parsed keeps capture on and logs one line. + */ +async function loadEnabled($: Api, st: State): Promise { + let path = '' try { const home = await $.env.get('HOME') if (!home) return true - const path = `${home}/${SWITCH_FILE}` + path = `${home}/${SWITCH_FILE}` const info = await $.fs.stat(path) if (info.kind !== 'file' || info.size > MAX_SWITCH_BYTES) return true const data: unknown = JSON.parse(await $.fs.read(path)) if (typeof data !== 'object' || data === null) return true return (data as { enabled?: unknown }).enabled !== false } catch { + if (path !== '' && !(await isGone($, path))) { + logOnce(st, $, 'switch', SWITCH_UNREADABLE) + } return true } } @@ -695,7 +736,7 @@ async function registerTool($: Api): Promise { */ async function ensureEnabled($: Api, st: State): Promise { if (st.enabled !== null) return st.enabled - st.enabled = await loadEnabled($) + st.enabled = await loadEnabled($, st) if (st.enabled) await registerTool($) return st.enabled }