From c6a5fcea9ec7cbffbc6fa7cb12085993826abcdb Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 4 Nov 2026 14:14:05 +0100 Subject: [PATCH] fix(memory): linear JWT regex, whole-token credential redaction --- mods/memory/hooks/register.test.ts | 15 +++++++++++++++ mods/memory/hooks/register.ts | 13 ++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/mods/memory/hooks/register.test.ts b/mods/memory/hooks/register.test.ts index b99d31d..edcbc79 100644 --- a/mods/memory/hooks/register.test.ts +++ b/mods/memory/hooks/register.test.ts @@ -1252,6 +1252,21 @@ test('I2: a JWT is redacted alone and inside an env value', () => { expect(redact('MY_' + 'TOKEN=' + JWT)).toBe('MY_TOKEN=') }) +test('TIME1: redact stays fast on 64 KiB of eyJ repeats', () => { + const t0 = now() + redact('eyJ'.repeat(21845)) + expect(now() - t0).toBeLessThan(500) +}) + +test('I2: a long credential leaves no tail in clear', () => { + const long = 'p'.repeat(300) + expect(redact('x --pass' + 'word ' + long + ' z')) + .toBe('x --password z') + expect(redact('curl -u bob:' + long)).toBe('curl -u ') + expect(redact('Authorization: Ba' + 'sic ' + long)) + .toBe('Authorization: Basic ') +}) + test('I2: credentials on the command line are redacted', () => { expect(redact('curl -u ' + 'bob:hunter2 https://x')) .toBe('curl -u https://x') diff --git a/mods/memory/hooks/register.ts b/mods/memory/hooks/register.ts index e4e0a67..7a90a41 100644 --- a/mods/memory/hooks/register.ts +++ b/mods/memory/hooks/register.ts @@ -30,8 +30,11 @@ const PEM_KIND = '(?:[A-Z]+ )?PRIVATE KEY' // (-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. const REDACTIONS: [RegExp, string][] = [ - [/eyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g, - ''], + [new RegExp( + '(?'], [new RegExp( '\\b([A-Za-z0-9_]{0,60}(?:TOKEN|API_KEY|SECRET|PASSWORD|PASSWD)' + '[A-Za-z0-9_]{0,60})=\\S+', @@ -50,9 +53,9 @@ const REDACTIONS: [RegExp, string][] = [ ), ''], [/\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{1,200}/g, '$1 '], - [/--password(?:=|[ \t]{1,5})\S{1,200}/g, '--password '], - [/Authorization:[ \t]{0,5}(Basic|Bearer|token)[ \t]{1,5}\S{1,400}/gi, + [/(?<=^|\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, 'Authorization: $1 '], ]