diff --git a/.claude/tasks/plans/2026-11-03-memory-capture-spike/probe-register.ts b/.claude/tasks/plans/2026-11-03-memory-capture-spike/probe-register.ts index baa1e35..ed507a2 100644 --- a/.claude/tasks/plans/2026-11-03-memory-capture-spike/probe-register.ts +++ b/.claude/tasks/plans/2026-11-03-memory-capture-spike/probe-register.ts @@ -3,13 +3,15 @@ // probe. import type { Register } from 'claude-code' -const LOG = `${process.env.CLAUDE_JOB_DIR}/tmp/probe4b/log.jsonl` +const JOB_DIR = process.env.CLAUDE_JOB_DIR +const LOG = `${JOB_DIR}/tmp/probe4b/log.jsonl` function cut(v: any, n: number): any { return v === undefined || v === null ? v : String(v).slice(0, n) } async function append($: any, obj: any): Promise { + if (JOB_DIR === undefined) return // no job dir: no log try { const prev = (await $.fs.exists(LOG)) ? await $.fs.read(LOG) : '' const text = typeof prev === 'string' ? prev : (prev?.text ?? '') diff --git a/mods/memory/hooks/register.test.ts b/mods/memory/hooks/register.test.ts index 3a80b3d..b8ebcc8 100644 --- a/mods/memory/hooks/register.test.ts +++ b/mods/memory/hooks/register.test.ts @@ -1465,3 +1465,46 @@ test('an inner CR in a stored line shows as the newline mark', async ( await boot($, on, { seed }) expect(await pending($)).toContain('- NEW \u00d71 fatal a\u23ceb \u2014') }) + +// ---- re-scan: header tail, cookie flags, JSON key affixes --------------- + +test('a 420-char Cookie header leaves nothing after the mark', () => { + const out = redact("curl -H 'cookie: k=" + 'v'.repeat(420) + + "; sid=SESSION123'") + expect(out).toBe("curl -H 'cookie: ") + expect(out.includes('sid=')).toBe(false) +}) + +test('curl cookie flags are redacted to the closing quote', () => { + expect(redact("curl -b 'sessionid=abc123def; csrftoken=x' https://h")) + .toBe('curl -b https://h') + expect(redact('curl --cookie=a=1 https://h')) + .toBe('curl --cookie https://h') +}) + +test('JSON keys carry affixes: access_token, client_secret, apiKey', () => { + expect(redact('{"access_' + 'token": "x"}')) + .toBe('{"access_token": ""}') + expect(redact('{"client_' + 'secret":"x","refresh-' + 'token": "y"}')) + .toBe('{"client_secret": "","refresh-token": ""}') + expect(redact('{"api' + 'Key": "x"}')).toBe('{"apiKey": ""}') +}) + +test('TIME1: redact on 64 KiB of plain letters stays under 50 ms', () => { + const t0 = now() + redact('lorem ipsum dolor sit amet '.repeat(2521)) + expect(now() - t0).toBeLessThan(50) +}) + +test('a switch file without a boolean enabled logs one line', async ( + $, on, +) => { + const w = await boot($, on, { switchText: '{"enabled": "false"}' }) + await bash($, 'ls') + await bash($, 'ls') + expect(w.logs).toEqual([ + 'memory: ~/.claude/memory.json has no boolean "enabled", ' + + 'capture stays on', + ]) + expect(w.tools).toEqual(['memory_pending']) +}) diff --git a/mods/memory/hooks/register.ts b/mods/memory/hooks/register.ts index 46f405d..eaf8e8b 100644 --- a/mods/memory/hooks/register.ts +++ b/mods/memory/hooks/register.ts @@ -20,13 +20,20 @@ const TOOL = 'memory_pending' const SERVED_TOOL = 'mcp__memory__memory_pending' const SWITCH_UNREADABLE = 'memory: ~/.claude/memory.json unreadable, capture stays on' +const SWITCH_INVALID = + 'memory: ~/.claude/memory.json has no boolean "enabled", 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)' +const SECRET_KEY = '(?:password|token|secret|api_?key)' +// the JSON key may hold `-`; the bare pair key may not (a `-` run would +// make every word start rescan 60 characters) +const keyWord = (cls: string) => `${cls}{0,60}${SECRET_KEY}${cls}{0,60}` +const JSON_KEY = keyWord('[A-Za-z0-9_-]') +const PAIR_KEY = keyWord('[A-Za-z0-9_]') // Redaction patterns, applied in order: the JWT shape first (so a secret // named variable still ends as `NAME=` through pattern 1, and any @@ -42,6 +49,9 @@ const REDACTIONS: [RegExp, string][] = [ '\\.[A-Za-z0-9_-]{8,}', 'g', ), ''], + // before pattern 1: a cookie jar `k=v; token=x` is one value + [new RegExp(`(?<=^|\\s)(-b|--cookie)(?:=|[ \\t]{1,5})${VALUE}`, 'g'), + '$1 '], [new RegExp( '\\b([A-Za-z0-9_]{0,60}(?:TOKEN|API_KEY|SECRET|PASSWORD|PASSWD)' + '[A-Za-z0-9_]{0,60})=' + VALUE, @@ -59,7 +69,8 @@ const REDACTIONS: [RegExp, string][] = [ 'g', ), ''], [/\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/g, 'Bearer '], - [/([a-z][a-z0-9+.-]{0,30}:\/\/)[^\s/@:]+:[^\s/@]+@/gi, '$1@'], + [/(?@'], [/(?<=^|\s)(-u|--user)[ \t]{1,5}\S{1,200}:\S*/g, '$1 '], [/--password(?:=|[ \t]{1,5})\S*/g, '--password '], [/Authorization:[ \t]{0,5}(Basic|Bearer|token)[ \t]{1,5}\S*/gi, @@ -71,19 +82,20 @@ const REDACTIONS: [RegExp, string][] = [ [/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, + [/(X-Api-Key|PRIVATE-TOKEN|Cookie):[ \t]{0,5}[^\n]*/gi, '$1: '], [new RegExp( - `"(${SECRET_KEY})"[ \\t]{0,5}:[ \\t]{0,5}"[^"\\n]{0,400}"`, + `"(${JSON_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})` + + `\\b(${PAIR_KEY})` + `[ \\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 @@ -698,8 +710,10 @@ async function loadEnabled($: Api, st: State): Promise { 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 + const enabled = (data as { enabled?: unknown } | null)?.enabled + if (typeof enabled === 'boolean') return enabled + logOnce(st, $, 'switch', SWITCH_INVALID) + return true } catch { if (path !== '' && !(await isGone($, path))) { logOnce(st, $, 'switch', SWITCH_UNREADABLE)