From f0eee4b5439c3661ff0141e44ce0d6b4d8441ee1 Mon Sep 17 00:00:00 2001 From: Gaurav Dubey Date: Fri, 6 Nov 2026 03:30:52 +0000 Subject: [PATCH] fix(hooks): redact JWTs joined to an underscore-prefixed label The JWT chain scanner only accepted a header at the start of a segment or after `-`, so `session_` reached the audit and cost logs unredacted. Treat `_` as a prefix boundary too and cover it in the tests. Co-Authored-By: Claude Opus 5.5 --- scripts/hooks/post-bash-command-log.js | 7 ++++--- tests/scripts/post-bash-command-log.test.js | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/hooks/post-bash-command-log.js b/scripts/hooks/post-bash-command-log.js index 0fca25282..d3f214ff4 100644 --- a/scripts/hooks/post-bash-command-log.js +++ b/scripts/hooks/post-bash-command-log.js @@ -34,8 +34,9 @@ function redactJwt(token) { } // Redact JWTs inside one run of `[A-Za-z0-9_.-]` characters. A JWT may follow -// a word prefix joined by `-` (`session=jwt-`), so each dot-separated -// segment is checked for a header starting at its beginning or after a `-`. +// 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 // `ey-ey-...` runs linear (a `\b`-anchored regex retries at each `-`). function redactJwtsInChain(chain) { @@ -49,7 +50,7 @@ function redactJwtsInChain(chain) { let first = -1; let preferred = -1; for (let j = 0; j + 10 <= seg.length; j++) { - if ((j === 0 || seg[j - 1] === '-') && seg[j] === 'e' && (seg[j + 1] === 'y' || seg[j + 1] === 'w')) { + 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; diff --git a/tests/scripts/post-bash-command-log.test.js b/tests/scripts/post-bash-command-log.test.js index 1e9f46f08..ec3280c0f 100644 --- a/tests/scripts/post-bash-command-log.test.js +++ b/tests/scripts/post-bash-command-log.test.js @@ -166,7 +166,7 @@ if ( else failed++; if ( - test('sanitizeCommand redacts JWTs joined to a hyphenated prefix', () => { + test('sanitizeCommand redacts JWTs joined to a hyphenated or underscored prefix', () => { assert.strictEqual( sanitizeCommand(`curl --cookie "session=jwt-${FAKE_JWT}" https://example.com`), 'curl --cookie "session=jwt-" https://example.com' @@ -174,7 +174,9 @@ if ( assert.strictEqual(sanitizeCommand(`echo prefix-${FAKE_JWT}`), 'echo prefix-'); assert.strictEqual(sanitizeCommand(`echo ey-${FAKE_JWT}`), 'echo ey-'); assert.strictEqual(sanitizeCommand(`open https://x.example.com/a.${FAKE_JWT}`), 'open https://x.example.com/a.'); - assert.strictEqual(sanitizeCommand('ping eyesightdata.example.com'), 'ping eyesightdata.example.com'); + assert.strictEqual(sanitizeCommand('ping eyesightdata.example.com'), 'ping eyesightdata.example.com'); assert.strictEqual(sanitizeCommand(`echo session_${FAKE_JWT}`), 'echo session_'); + assert.strictEqual(sanitizeCommand(`export AUTH=bearer_${FAKE_JWT}`), 'export AUTH=bearer_'); + assert.strictEqual(sanitizeCommand('ls my_eyes.backup.tar'), 'ls my_eyes.backup.tar'); }) ) passed++;