From 181bcfc7f2b1909dd69a1728339eb8b563a27516 Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 4 Nov 2026 15:25:25 +0100 Subject: [PATCH] fix(memory): hyphenated api-key, URL scheme bound, cookie flag scoped to curl api[_-]?key in JSON, pair and header rules. URL scheme bound 200 behind a lookbehind. -b redacted only in a curl segment (quote-aware cut); --cookie stays global. Timing test slack for load. --- mods/memory/hooks/register.test.ts | 35 +++++++++++++++++++++- mods/memory/hooks/register.ts | 48 +++++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/mods/memory/hooks/register.test.ts b/mods/memory/hooks/register.test.ts index b8ebcc8..8af39f5 100644 --- a/mods/memory/hooks/register.test.ts +++ b/mods/memory/hooks/register.test.ts @@ -1440,9 +1440,10 @@ test('TIME1: each 6-bis rule stays fast on 64 KiB adversarial input', () => { ('a'.repeat(60) + 'token' + 'b'.repeat(60) + ' ').repeat(361), ] for (const text of inputs) { + redact(text.slice(0, 1024)) // warm-up: regex compilation is not the cost const t0 = now() redact(text) - expect(now() - t0).toBeLessThan(10) + expect(now() - t0).toBeLessThan(30) // measured 1-3 ms; slack for load } }) @@ -1508,3 +1509,35 @@ test('a switch file without a boolean enabled logs one line', async ( ]) expect(w.tools).toEqual(['memory_pending']) }) + +// ---- round 3: hyphenated api-key, URL scheme bound, cookie scope -------- + +test('hyphenated api-key forms are redacted', () => { + expect(redact('{"x-api' + '-key": "v"}')).toBe('{"x-api-key": ""}') + expect(redact('api' + '-key: v')).toBe('api-key: ') + expect(redact('X-API-' + 'KEY: v w')).toBe('X-API-KEY: ') +}) + +test('a credential URL glued to 40 scheme characters is redacted', () => { + expect(redact('a'.repeat(40) + 'https://user:pw' + '@host')) + .toBe('a'.repeat(40) + 'https://@host') +}) + +test('-b is a cookie jar only in a curl segment', () => { + expect(redact('git checkout -b feature/x')) + .toBe('git checkout -b feature/x') + expect(redact("curl -b 'sid=1' h")).toBe('curl -b h') + expect(redact('X=1 curl -b a=b h && git checkout -b y')) + .toBe('X=1 curl -b h && git checkout -b y') + expect(redact("curl -b 'a=1; b=2' h")).toBe('curl -b h') +}) + +test('TIME1: round 3 shapes stay fast on 64 KiB', () => { + const inputs = ['curl -b '.repeat(8192), "';".repeat(32768), + 'x-api-'.repeat(10922), 'a'.repeat(65536), 'ab://'.repeat(13107)] + for (const text of inputs) { + const t0 = now() + redact(text) + expect(now() - t0).toBeLessThan(50) + } +}) diff --git a/mods/memory/hooks/register.ts b/mods/memory/hooks/register.ts index eaf8e8b..7530d66 100644 --- a/mods/memory/hooks/register.ts +++ b/mods/memory/hooks/register.ts @@ -28,7 +28,7 @@ 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}` @@ -50,8 +50,8 @@ const REDACTIONS: [RegExp, string][] = [ '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(`(?<=^|\\s)--cookie(?:=|[ \\t]{1,5})${VALUE}`, 'g'), + '--cookie '], [new RegExp( '\\b([A-Za-z0-9_]{0,60}(?:TOKEN|API_KEY|SECRET|PASSWORD|PASSWD)' + '[A-Za-z0-9_]{0,60})=' + VALUE, @@ -69,7 +69,7 @@ const REDACTIONS: [RegExp, string][] = [ 'g', ), ''], [/\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/g, 'Bearer '], - [/(?@'], [/(?<=^|\s)(-u|--user)[ \t]{1,5}\S{1,200}:\S*/g, '$1 '], [/--password(?:=|[ \t]{1,5})\S*/g, '--password '], @@ -82,8 +82,11 @@ 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]*/gi, - '$1: '], + [new RegExp( + '(?'], [new RegExp( `"(${JSON_KEY})"[ \\t]{0,5}:[ \\t]{0,5}"[^"\\n]{0,400}"`, 'gi', @@ -94,10 +97,18 @@ const REDACTIONS: [RegExp, string][] = [ `[ \\t]{0,5}[:=][ \\t]{0,5}(?!'], - [/(?@'], ] +const QUOTED = /'[^'\n]*'|"[^"\n]*"/g +const SEPARATOR = /[;&|\n]+/g +const CURL_SEGMENT = new RegExp( + '^[ \\t(]{0,5}(?:rtk[ \\t]{1,5})?' + + '(?:[A-Za-z_]\\w{0,60}=\\S{0,200}[ \\t]{1,5}){0,10}curl\\b', +) +const COOKIE_FLAG = new RegExp(`(?<=^|\\s)-b[ \\t]{1,5}${VALUE}`, 'g') + const HEX_RUN = /\b[0-9a-f]{7,}\b/g const PATH = /(?:\.{0,2}\/)?[\w.-]{1,200}(?:\/[\w.-]{1,200}){1,30}/g @@ -111,9 +122,30 @@ export function tail(text: string, n: number): string { return n <= 0 ? '' : text.slice(-n) } +/** + * `curl -b ` redacted per command segment: `-b` means a cookie jar + * only after `curl`. Segments are cut on separators outside quotes (the + * blanked copy has the same length, so its cuts apply to the text). + */ +function redactCurlCookie(text: string): string { + if (!text.includes('-b')) return text + const blank = text.replace(QUOTED, q => ' '.repeat(q.length)) + const parts: string[] = [] + let from = 0 + for (const m of [...blank.matchAll(SEPARATOR), null]) { + const end = m?.index ?? text.length + const seg = text.slice(from, end) + const hit = seg.includes('-b') && CURL_SEGMENT.test(seg) + parts.push(hit ? seg.replace(COOKIE_FLAG, '-b ') : seg) + parts.push(m?.[0] ?? '') + from = end + (m?.[0].length ?? 0) + } + return parts.join('') +} + /** Masks secret-shaped text; reads at most the first 64 KiB. */ export function redact(text: string): string { - let out = cap(text, MAX_REGEX_INPUT) + let out = redactCurlCookie(cap(text, MAX_REGEX_INPUT)) for (const [pattern, mask] of REDACTIONS) out = out.replace(pattern, mask) return out }