fix(memory): linear JWT regex, whole-token credential redaction

This commit is contained in:
bchanot committed 2026-11-04 14:14:05 +01:00
1 parent f620d8f894
commit c6a5fcea9e
2 files changed
+23 -5

No files matched your search

+15
View File
@@ -1252,6 +1252,21 @@ test('I2: a JWT is redacted alone and inside an env value', () => {
expect(redact('MY_' + 'TOKEN=' + JWT)).toBe('MY_TOKEN=<redacted>')
})
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 <redacted> z')
expect(redact('curl -u bob:' + long)).toBe('curl -u <redacted>')
expect(redact('Authorization: Ba' + 'sic ' + long))
.toBe('Authorization: Basic <redacted>')
})
test('I2: credentials on the command line are redacted', () => {
expect(redact('curl -u ' + 'bob:hunter2 https://x'))
.toBe('curl -u <redacted> https://x')
+8 -5
View File
@@ -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,
'<redacted:jwt>'],
[new RegExp(
'(?<![A-Za-z0-9_-])eyJ[A-Za-z0-9_-]{8,}\\.eyJ[A-Za-z0-9_-]{8,}' +
'\\.[A-Za-z0-9_-]{8,}',
'g',
), '<redacted:jwt>'],
[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][] = [
), '<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>@'],
[/(?<=^|\s)(-u|--user)[ \t]{1,5}\S{1,200}:\S{1,200}/g, '$1 <redacted>'],
[/--password(?:=|[ \t]{1,5})\S{1,200}/g, '--password <redacted>'],
[/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 <redacted>'],
[/--password(?:=|[ \t]{1,5})\S*/g, '--password <redacted>'],
[/Authorization:[ \t]{0,5}(Basic|Bearer|token)[ \t]{1,5}\S*/gi,
'Authorization: $1 <redacted>'],
]