fix(memory): security re-scan — header tail, cookie flags, JSON key affixes, URL lookbehind
Header rule redacts to end of line. -b/--cookie values redacted before pattern 1. JSON keys take affixes. URL rules get a lookbehind. A switch file with no boolean enabled logs one line. Probe skips logging without CLAUDE_JOB_DIR.
This commit is contained in:
1 parent
24f53afa99
commit
f80b00a997
3 files changed
+68
-9
No files matched your search
@@ -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<void> {
|
||||
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 ?? '')
|
||||
|
||||
@@ -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: <redacted>")
|
||||
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 <redacted> https://h')
|
||||
expect(redact('curl --cookie=a=1 https://h'))
|
||||
.toBe('curl --cookie <redacted> https://h')
|
||||
})
|
||||
|
||||
test('JSON keys carry affixes: access_token, client_secret, apiKey', () => {
|
||||
expect(redact('{"access_' + 'token": "x"}'))
|
||||
.toBe('{"access_token": "<redacted>"}')
|
||||
expect(redact('{"client_' + 'secret":"x","refresh-' + 'token": "y"}'))
|
||||
.toBe('{"client_secret": "<redacted>","refresh-token": "<redacted>"}')
|
||||
expect(redact('{"api' + 'Key": "x"}')).toBe('{"apiKey": "<redacted>"}')
|
||||
})
|
||||
|
||||
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'])
|
||||
})
|
||||
@@ -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=<redacted>` through pattern 1, and any
|
||||
@@ -42,6 +49,9 @@ const REDACTIONS: [RegExp, string][] = [
|
||||
'\\.[A-Za-z0-9_-]{8,}',
|
||||
'g',
|
||||
), '<redacted:jwt>'],
|
||||
// before pattern 1: a cookie jar `k=v; token=x` is one value
|
||||
[new RegExp(`(?<=^|\\s)(-b|--cookie)(?:=|[ \\t]{1,5})${VALUE}`, 'g'),
|
||||
'$1 <redacted>'],
|
||||
[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',
|
||||
), '<redacted:private-key>'],
|
||||
[/\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/g, 'Bearer <redacted>'],
|
||||
[/([a-z][a-z0-9+.-]{0,30}:\/\/)[^\s/@:]+:[^\s/@]+@/gi, '$1<redacted>@'],
|
||||
[/(?<![a-z0-9+.-])([a-z][a-z0-9+.-]{0,30}:\/\/)[^\s/@:]+:[^\s/@]+@/gi,
|
||||
'$1<redacted>@'],
|
||||
[/(?<=^|\s)(-u|--user)[ \t]{1,5}\S{1,200}:\S*/g, '$1 <redacted>'],
|
||||
[/--password(?:=|[ \t]{1,5})\S*/g, '--password <redacted>'],
|
||||
[/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, '<redacted:google>'],
|
||||
[/npm_[A-Za-z0-9]{10,}/g, '<redacted:npm>'],
|
||||
[/--(token|api-key|secret)(?:=|[ \t]{1,5})\S*/g, '--$1 <redacted>'],
|
||||
[/(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: <redacted>'],
|
||||
[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": "<redacted>"'],
|
||||
// a value already `<redacted…>` (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}(?!<redacted)${VALUE}`,
|
||||
'gi',
|
||||
), '$1: <redacted>'],
|
||||
[/([a-z][a-z0-9+.-]{0,30}:\/\/)[0-9a-f]{40}@/gi, '$1<redacted>@'],
|
||||
[/(?<![a-z0-9+.-])([a-z][a-z0-9+.-]{0,30}:\/\/)[0-9a-f]{40}@/gi,
|
||||
'$1<redacted>@'],
|
||||
]
|
||||
|
||||
const HEX_RUN = /\b[0-9a-f]{7,}\b/g
|
||||
@@ -698,8 +710,10 @@ async function loadEnabled($: Api, st: State): Promise<boolean> {
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user