diff --git a/scripts/hooks/post-bash-command-log.js b/scripts/hooks/post-bash-command-log.js index d3f214ff4..b231a9e5e 100644 --- a/scripts/hooks/post-bash-command-log.js +++ b/scripts/hooks/post-bash-command-log.js @@ -22,22 +22,50 @@ const MODE_CONFIG = { // A JWT header is base64url JSON, so it starts with `ey` (`{"`, `{ `) or // `ew` (`{` followed by a newline or tab). The common `eyJ` form is always // redacted; other headers must decode to an object with an `alg` field. -function redactJwt(token) { - if (token.startsWith('eyJ')) return ''; +function isJwtHeader(header) { + if (header.startsWith('eyJ')) return true; try { - const header = JSON.parse(Buffer.from(token.split('.')[0], 'base64url').toString('utf8')); - if (header && typeof header === 'object' && typeof header.alg === 'string') return ''; + const decoded = JSON.parse(Buffer.from(header, 'base64url').toString('utf8')); + return Boolean(decoded && typeof decoded === 'object' && typeof decoded.alg === 'string'); } catch { // Not a JWT header; keep the text. + return false; } - return token; +} + +// Constant-time prefilter: the second decoded byte of a JSON object header is +// whitespace or `"`. Rejects word prefixes such as `eyewear` without decoding +// the whole segment. +function mayBeJwtHeader(seg, j) { + const bytes = Buffer.from(seg.slice(j, j + 4), 'base64url'); + return bytes.length >= 2 && (bytes[1] === 0x22 || bytes[1] === 0x20 || bytes[1] === 0x09 || bytes[1] === 0x0a || bytes[1] === 0x0d); +} + +// Full header decodes allowed per segment; keeps adversarial `eyAg-eyAg-...` +// runs linear while still trying every realistic candidate. +const MAX_HEADER_DECODES = 8; + +// Index of the first JWT header in `seg`, starting at its beginning or after a +// `-` or `_`, or -1. A candidate that fails validation (e.g. `eyewear` in +// `eyewear_session_`) does not stop the scan. +function findJwtHeader(seg) { + let decodes = 0; + for (let j = 0; j + 10 <= seg.length; j++) { + if (!((j === 0 || seg[j - 1] === '-' || seg[j - 1] === '_') && seg[j] === 'e' && (seg[j + 1] === 'y' || seg[j + 1] === 'w'))) continue; + if (seg.startsWith('eyJ', j)) return j; + if (decodes < MAX_HEADER_DECODES && mayBeJwtHeader(seg, j)) { + decodes++; + if (isJwtHeader(seg.slice(j))) return j; + } + } + return -1; } // Redact JWTs inside one run of `[A-Za-z0-9_.-]` characters. A JWT may follow // a word prefix joined by `-` or `_` (`session=jwt-`, `session_`), so // each dot-separated segment is checked for a header starting at its beginning // or after a `-` or `_`. -// Every segment is scanned once and decoded at most once, keeping long +// Every segment is scanned once with a bounded number of decodes, keeping long // `ey-ey-...` runs linear (a `\b`-anchored regex retries at each `-`). function redactJwtsInChain(chain) { if (!chain.includes('.')) return chain; @@ -47,25 +75,11 @@ function redactJwtsInChain(chain) { while (i < segs.length) { const seg = segs[i]; if (i + 2 < segs.length && segs[i + 1].length > 0) { - let first = -1; - let preferred = -1; - for (let j = 0; j + 10 <= seg.length; j++) { - if ((j === 0 || seg[j - 1] === '-' || seg[j - 1] === '_') && seg[j] === 'e' && (seg[j + 1] === 'y' || seg[j + 1] === 'w')) { - if (first < 0) first = j; - if (seg.startsWith('eyJ', j)) { - preferred = j; - break; - } - } - } - const start = preferred >= 0 ? preferred : first; + const start = findJwtHeader(seg); if (start >= 0) { - const token = [seg.slice(start), segs[i + 1], segs[i + 2]].join('.'); - if (redactJwt(token) !== token) { - out.push(`${seg.slice(0, start)}`); - i += 3; - continue; - } + out.push(`${seg.slice(0, start)}`); + i += 3; + continue; } } out.push(seg); diff --git a/tests/scripts/post-bash-command-log.test.js b/tests/scripts/post-bash-command-log.test.js index ec3280c0f..d4a769f83 100644 --- a/tests/scripts/post-bash-command-log.test.js +++ b/tests/scripts/post-bash-command-log.test.js @@ -182,6 +182,33 @@ if ( passed++; else failed++; +if ( + test('sanitizeCommand tries later JWT headers after a prefix candidate fails', () => { + const spacedHeader = Buffer.from('{ "alg": "HS256" }').toString('base64url'); + const newlineHeader = Buffer.from('{\n"alg":"HS256"}').toString('base64url'); + for (const header of [spacedHeader, newlineHeader]) { + const token = [header, 'eyJzdWIiOiJmYWtlIn0', 'ZmFrZXNpZ25hdHVyZQ'].join('.'); + assert.strictEqual(sanitizeCommand(`echo eyewear_session_${token}`), 'echo eyewear_session_'); + assert.strictEqual(sanitizeCommand(`echo ey-eye-${token}`), 'echo ey-eye-'); + } + assert.strictEqual(sanitizeCommand('ls eyewear_session_eyebrows.backup.tar'), 'ls eyewear_session_eyebrows.backup.tar'); + }) +) + passed++; +else failed++; + +if ( + test('sanitizeCommand stays linear on long runs of plausible JWT header candidates', () => { + const start = Date.now(); + sanitizeCommand(`${'eyAg-'.repeat(200000)}x.y.z`); + sanitizeCommand(`${'ewog_'.repeat(200000)}x.y.z`); + const elapsed = Date.now() - start; + assert.ok(elapsed < 2000, `took ${elapsed} ms`); + }) +) + passed++; +else failed++; + if ( test('sanitizeCommand stays linear on long ey- runs followed by dots', () => { const start = Date.now();