fix(hooks): try later JWT header candidates after a failed prefix match

A word prefix such as `eyewear_session_` was taken as the only JWT
header candidate; when it failed validation the real header after `_`
was never tried and the token stayed in the command log. Keep scanning
eligible candidates, with a constant-time prefilter and a per-segment
decode cap so long candidate runs stay linear.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Gaurav DubeyandClaude Opus 5.5 committed 2026-11-07 03:31:34 +00:00
1 parent f0eee4b543
commit 5f72adbd83
2 files changed
+65 -24

No files matched your search

+38 -24
View File
@@ -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 '<REDACTED>';
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 '<REDACTED>';
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_<JWT>`) 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-<JWT>`, `session_<JWT>`), 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)}<REDACTED>`);
i += 3;
continue;
}
out.push(`${seg.slice(0, start)}<REDACTED>`);
i += 3;
continue;
}
}
out.push(seg);
@@ -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_<REDACTED>');
assert.strictEqual(sanitizeCommand(`echo ey-eye-${token}`), 'echo ey-eye-<REDACTED>');
}
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();