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.
This commit is contained in:
bchanot committed 2026-11-04 15:25:25 +01:00
1 parent f80b00a997
commit 181bcfc7f2
2 files changed
+74 -9

No files matched your search

+34 -1
View File
@@ -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": "<redacted>"}')
expect(redact('api' + '-key: v')).toBe('api-key: <redacted>')
expect(redact('X-API-' + 'KEY: v w')).toBe('X-API-KEY: <redacted>')
})
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://<redacted>@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 <redacted> h')
expect(redact('X=1 curl -b a=b h && git checkout -b y'))
.toBe('X=1 curl -b <redacted> h && git checkout -b y')
expect(redact("curl -b 'a=1; b=2' h")).toBe('curl -b <redacted> 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)
}
})
+40 -8
View File
@@ -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',
), '<redacted:jwt>'],
// before pattern 1: a cookie jar `k=v; token=x` is one value
[new RegExp(`(?<=^|\\s)(-b|--cookie)(?:=|[ \\t]{1,5})${VALUE}`, 'g'),
'$1 <redacted>'],
[new RegExp(`(?<=^|\\s)--cookie(?:=|[ \\t]{1,5})${VALUE}`, 'g'),
'--cookie <redacted>'],
[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',
), '<redacted:private-key>'],
[/\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/g, 'Bearer <redacted>'],
[/(?<![a-z0-9+.-])([a-z][a-z0-9+.-]{0,30}:\/\/)[^\s/@:]+:[^\s/@]+@/gi,
[/(?<![a-z0-9+.-])([a-z][a-z0-9+.-]{0,200}:\/\/)[^\s/@:]+:[^\s/@]+@/gi,
'$1<redacted>@'],
[/(?<=^|\s)(-u|--user)[ \t]{1,5}\S{1,200}:\S*/g, '$1 <redacted>'],
[/--password(?:=|[ \t]{1,5})\S*/g, '--password <redacted>'],
@@ -82,8 +82,11 @@ const REDACTIONS: [RegExp, string][] = [
[/AIza[A-Za-z0-9_-]{20,}/g, '<redacted:google>'],
[/npm_[A-Za-z0-9]{10,}/g, '<redacted:npm>'],
[/--(token|api-key|secret)(?:=|[ \t]{1,5})\S*/g, '--$1 <redacted>'],
[/(X-Api-Key|PRIVATE-TOKEN|Cookie):[ \t]{0,5}[^\n]*/gi,
'$1: <redacted>'],
[new RegExp(
'(?<![A-Za-z0-9-])([A-Za-z0-9-]{0,30}api[_-]?key|PRIVATE-TOKEN|Cookie):' +
'[ \\t]{0,5}[^\\n]*',
'gi',
), '$1: <redacted>'],
[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}(?!<redacted)${VALUE}`,
'gi',
), '$1: <redacted>'],
[/(?<![a-z0-9+.-])([a-z][a-z0-9+.-]{0,30}:\/\/)[0-9a-f]{40}@/gi,
[/(?<![a-z0-9+.-])([a-z][a-z0-9+.-]{0,200}:\/\/)[0-9a-f]{40}@/gi,
'$1<redacted>@'],
]
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 <jar>` 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 <redacted>') : 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
}