diff --git a/mods/memory/hooks/register.test.ts b/mods/memory/hooks/register.test.ts index d0fafff..e728267 100644 --- a/mods/memory/hooks/register.test.ts +++ b/mods/memory/hooks/register.test.ts @@ -201,8 +201,10 @@ test('redact keeps a 40-hex SHA', () => { expect(redact('commit ' + sha)).toBe('commit ' + sha) }) -test('redact caps a 70 KiB input at 65536 code units', () => { - expect(redact('x'.repeat(70 * 1024)).length).toBeLessThanOrEqual(65536) +test('redact drops everything past 65536 code units', () => { + const out = redact('x'.repeat(65536) + 'END_MARK') + expect(out.length).toBe(65536) + expect(out.includes('END_MARK')).toBe(false) }) test('cap keeps the first n, tail keeps the last n', () => { @@ -219,7 +221,7 @@ test('normalize collapses paths, hashes, numbers and spaces', () => { expect(normalize(' a \t b 7 ')).toBe('a b #') }) -test('TIME1: redact and normalize stay fast on a 64 KiB run of letters', () => { +test('TIME1: redact and normalize stay fast on 64 KiB of letters', () => { const text = 'a'.repeat(65536) for (const fn of [redact, normalize]) { const t0 = now() @@ -228,6 +230,20 @@ test('TIME1: redact and normalize stay fast on a 64 KiB run of letters', () => { } }) +test('TIME1: redact stays fast on adversarial 64 KiB inputs', () => { + const inputs = [ + 'TOKEN'.repeat(13107), + 'secret'.repeat(10922), + 'Bearer '.repeat(9362), + 'a://b:'.repeat(10922), + ] + for (const text of inputs) { + const t0 = now() + redact(text) + expect(now() - t0).toBeLessThan(500) + } +}) + const EXIT1 = 'Exit code 1\n' test('failureLine: the last line after the first, trailers stripped', () => { diff --git a/mods/memory/hooks/register.ts b/mods/memory/hooks/register.ts index 0d049cb..95ac6df 100644 --- a/mods/memory/hooks/register.ts +++ b/mods/memory/hooks/register.ts @@ -25,8 +25,11 @@ const PEM_KIND = '(?:[A-Z]+ )?PRIVATE KEY' // Redaction patterns, applied in order. Repeats are bounded where the // input is free text, so no pattern can run away on a 64 KiB input. const REDACTIONS: [RegExp, string][] = [ - [/\b([A-Za-z_]*(?:TOKEN|API_KEY|SECRET|PASSWORD|PASSWD)[A-Za-z_]*)=\S+/gi, - '$1='], + [new RegExp( + '\\b([A-Za-z_]{0,60}(?:TOKEN|API_KEY|SECRET|PASSWORD|PASSWD)' + + '[A-Za-z_]{0,60})=\\S+', + 'gi', + ), '$1='], [/sk_live_[0-9A-Za-z]{10,}/g, ''], [/AKIA[0-9A-Z]{16}/g, ''], [/gh[pousr]_[A-Za-z0-9]{30,}/g, ''],