fix(memory): bound redaction pattern 1, adversarial timing cases

This commit is contained in:
bchanot committed 2026-11-04 11:55:04 +01:00
1 parent 6072522558
commit 51828461fd
2 files changed
+24 -5

No files matched your search

+19 -3
View File
@@ -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', () => {
+5 -2
View File
@@ -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=<redacted>'],
[new RegExp(
'\\b([A-Za-z_]{0,60}(?:TOKEN|API_KEY|SECRET|PASSWORD|PASSWD)' +
'[A-Za-z_]{0,60})=\\S+',
'gi',
), '$1=<redacted>'],
[/sk_live_[0-9A-Za-z]{10,}/g, '<redacted:stripe>'],
[/AKIA[0-9A-Z]{16}/g, '<redacted:aws>'],
[/gh[pousr]_[A-Za-z0-9]{30,}/g, '<redacted:github>'],