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
This commit is contained in:
Gaurav Dubey committed 2026-11-03 04:19:37 +00:00
1 parent 08fb3c8a1c
commit 0b57406e57
2 files changed
+57 -2

No files matched your search

+21 -2
View File
@@ -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 '<REDACTED>';
try {
const header = JSON.parse(Buffer.from(token.split('.')[0], 'base64url').toString('utf8'));
if (header && typeof header === 'object' && typeof header.alg === 'string') return '<REDACTED>';
} 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, '<REDACTED>')
.replace(/\bsk-[A-Za-z0-9_-]{20,}/g, '<REDACTED>')
.replace(/\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*/g, '<REDACTED>')
.replace(/(\b|_)(secret|api[_-]?key)(\s*[:=]\s*)("[^"]*"|'[^']*'|[^\s"']+)/gi, '$1$2$3<REDACTED>')
.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<REDACTED>'
)
.replace(/--token[= ][^ ]*/g, '--token=<REDACTED>')
.replace(/Authorization:[: ]*[^ ]*[: ]*[^ ]*/gi, 'Authorization:<REDACTED>')
.replace(/\bAKIA[A-Z0-9]{16}\b/g, '<REDACTED>')
@@ -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=<REDACTED>');
}
// 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=<REDACTED> --next'
);
assert.strictEqual(
sanitizeCommand(`app --secret=$'first\\'${FAKE_VALUE}' --next`),
'app --secret=<REDACTED> --next'
);
assert.strictEqual(sanitizeCommand(`app --secret=first\\"${FAKE_VALUE} --next`), 'app --secret=<REDACTED> --next');
assert.strictEqual(sanitizeCommand(`app --secret="${FAKE_VALUE} unterminated`), 'app --secret=<REDACTED>');
})
)
passed++;
else failed++;
if (
test('sanitizeCommand redacts secret= and api_key= assignments (#3846)', () => {
assert.strictEqual(