From 0b57406e57e7b1aeec4e9226d8a957d1cd00557c Mon Sep 17 00:00:00 2001 From: Gaurav Dubey Date: Tue, 3 Nov 2026 04:19:37 +0000 Subject: [PATCH] fix(hooks): redact spaced-header JWTs and escaped or $'...' secret values Match JWT headers that start with ey or ew (whitespace after the opening brace) and keep non-eyJ matches only when the header does not decode to an object with an alg field. Parse secret= / api_key= values with escaped double quotes, $'...' quoting and backslash escapes, and redact an unterminated quoted value to the end of the command. Refs #3846 --- scripts/hooks/post-bash-command-log.js | 23 +++++++++++-- tests/scripts/post-bash-command-log.test.js | 36 +++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/scripts/hooks/post-bash-command-log.js b/scripts/hooks/post-bash-command-log.js index b7de1cc1b..7cb09f338 100644 --- a/scripts/hooks/post-bash-command-log.js +++ b/scripts/hooks/post-bash-command-log.js @@ -19,13 +19,32 @@ 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 ''; + try { + const header = JSON.parse(Buffer.from(token.split('.')[0], 'base64url').toString('utf8')); + if (header && typeof header === 'object' && typeof header.alg === 'string') return ''; + } catch { + // Not a JWT header; keep the text. + } + return token; +} + function sanitizeCommand(command) { return String(command || '') .replace(/\n/g, ' ') .replace(/-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY-----|$)/g, '') .replace(/\bsk-[A-Za-z0-9_-]{20,}/g, '') - .replace(/\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*/g, '') - .replace(/(\b|_)(secret|api[_-]?key)(\s*[:=]\s*)("[^"]*"|'[^']*'|[^\s"']+)/gi, '$1$2$3') + .replace(/\be[wy][A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*/g, redactJwt) + // Value: "..." with escapes, $'...' with escapes, '...', or a bare word + // with backslash escapes. Unterminated quotes are redacted to the end. + .replace( + /(\b|_)(secret|api[_-]?key)(\s*[:=]\s*)("(?:[^"\\]|\\.)*(?:"|$)|\$'(?:[^'\\]|\\.)*(?:'|$)|'[^']*(?:'|$)|(?:[^\s"'\\]|\\.)+)/gi, + '$1$2$3' + ) .replace(/--token[= ][^ ]*/g, '--token=') .replace(/Authorization:[: ]*[^ ]*[: ]*[^ ]*/gi, 'Authorization:') .replace(/\bAKIA[A-Z0-9]{16}\b/g, '') diff --git a/tests/scripts/post-bash-command-log.test.js b/tests/scripts/post-bash-command-log.test.js index f94834c43..b347a36f6 100644 --- a/tests/scripts/post-bash-command-log.test.js +++ b/tests/scripts/post-bash-command-log.test.js @@ -95,6 +95,42 @@ if ( passed++; else failed++; +if ( + test('sanitizeCommand redacts JWTs whose header has whitespace after the brace', () => { + const spacedHeader = Buffer.from('{ "alg": "HS256" }').toString('base64url'); + const newlineHeader = Buffer.from('{\n"alg":"HS256"}').toString('base64url'); + assert.ok(spacedHeader.startsWith('eyA'), spacedHeader); + assert.ok(newlineHeader.startsWith('ew'), newlineHeader); + for (const header of [spacedHeader, newlineHeader]) { + const token = [header, 'eyJzdWIiOiJmYWtlIn0', 'ZmFrZXNpZ25hdHVyZQ'].join('.'); + assert.strictEqual(sanitizeCommand(`export TOKEN_VALUE=${token}`), 'export TOKEN_VALUE='); + } + + // Same shape, but the first segment does not decode to a JWT header. + const notJwt = 'eyesightdata.example.com'; + assert.strictEqual(sanitizeCommand(`ping ${notJwt}`), `ping ${notJwt}`); + }) +) + passed++; +else failed++; + +if ( + test('sanitizeCommand redacts whole quoted assignment values, including escapes', () => { + assert.strictEqual( + sanitizeCommand(`app --secret="first\\"${FAKE_VALUE}" --next`), + 'app --secret= --next' + ); + assert.strictEqual( + sanitizeCommand(`app --secret=$'first\\'${FAKE_VALUE}' --next`), + 'app --secret= --next' + ); + assert.strictEqual(sanitizeCommand(`app --secret=first\\"${FAKE_VALUE} --next`), 'app --secret= --next'); + assert.strictEqual(sanitizeCommand(`app --secret="${FAKE_VALUE} unterminated`), 'app --secret='); + }) +) + passed++; +else failed++; + if ( test('sanitizeCommand redacts secret= and api_key= assignments (#3846)', () => { assert.strictEqual(