fix(hooks): check whole credential names and redact shell values to the next space

The bounded name parts (64 characters around the credential word) let a longer name such as API_KEY_ plus 65 characters go unredacted, and the field-bound bare value left `,beta` of CLIENT_SECRET=alpha,beta in the summary.

Find each name and separator first, starting a name only where a run of name characters starts, so every run is scanned once whatever its length, and test the whole name for the credential word. A bare value after a quoted JSON key still ends at the next field; any other runs to the next space.
This commit is contained in:
L4XB committed 2026-10-23 00:01:03 +02:00
1 parent abff545aa8
commit 08f605eed0
2 files changed
+51 -15

No files matched your search

+37 -11
View File
@@ -30,23 +30,49 @@ const FILE_PATH_KEYS = new Set([
'new_file_path',
]);
// A name that carries a credential word anywhere (`AWS_SECRET_ACCESS_KEY`,
// `DOCKER_AUTH_CONFIG`, Docker's `"auth":`), but not `max_tokens`, `tokenizer`
// or `author`.
const CREDENTIAL_WORD = /secret|token(?!s|iz)|passw(?:or)?d|api[_-]?key|access[_-]?key|private[_-]?key|credential|auth\b|auth[_-]?config/i;
// A whole name and its separator. A name starts only where a run of name
// characters starts, so each run is scanned once and long output stays linear
// however long the name is.
const ASSIGNMENT = /(?<![\w.-])([\w.-]+)(["']?)\s*([:=])\s*/g;
// A quoted value may hold escaped quotes, and one that opens with { or [ is JSON
// that can hold spaces, so it is redacted to the end. A bare value after a quoted
// JSON key ends at the next field; any other (`NAME=a,b`, `secret: a,b`) runs to
// the next space.
const JSON_FIELD_VALUE = /"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[{[].*|[^\s,;}\]"']+/y;
const TEXT_VALUE = /"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[{[].*|\S+/y;
function redactCredentialValues(text) {
let redacted = '';
let copied = 0;
ASSIGNMENT.lastIndex = 0;
for (let match = ASSIGNMENT.exec(text); match; match = ASSIGNMENT.exec(text)) {
if (!CREDENTIAL_WORD.test(match[1])) continue;
const valueStart = match.index + match[0].length;
const valuePattern = match[2] && match[3] === ':' ? JSON_FIELD_VALUE : TEXT_VALUE;
valuePattern.lastIndex = valueStart;
const value = valuePattern.exec(text);
if (!value) continue;
redacted += `${text.slice(copied, valueStart)}<REDACTED>`;
copied = valueStart + value[0].length;
ASSIGNMENT.lastIndex = copied;
}
return redacted + text.slice(copied);
}
function redactSecrets(value) {
return String(value || '')
// The credential values cover what governance-capture detects, since tool
// output such as `cat .env` lands in these summaries too.
return redactCredentialValues(String(value || '')
.replace(/\n/g, ' ')
.replace(/--token[= ][^ ]*/g, '--token=<REDACTED>')
.replace(/Authorization:[: ]*[^ ]*[: ]*[^ ]*/gi, 'Authorization:<REDACTED>')
.replace(/\bAKIA[A-Z0-9]{16}\b/g, '<REDACTED>')
.replace(/\bASIA[A-Z0-9]{16}\b/g, '<REDACTED>')
.replace(/password[= ][^ ]*/gi, 'password=<REDACTED>')
// The rest covers what governance-capture detects, since tool output such as
// `cat .env` lands in these summaries too.
// A name that carries a credential word anywhere (`AWS_SECRET_ACCESS_KEY`,
// `DOCKER_AUTH_CONFIG`, Docker's `"auth":`), but not `max_tokens`, `tokenizer`
// or `author`. The name parts around the word are bounded, which keeps long
// output linear. A quoted value may hold escaped quotes; a JSON value can hold
// spaces, so one that opens with { or [ is redacted to the end; a bare value
// ends at the next field.
.replace(/\b([\w.-]{0,64}(?:secret|token(?!s|iz)|passw(?:or)?d|api[_-]?key|access[_-]?key|private[_-]?key|credential|auth\b|auth[_-]?config)[\w.-]{0,64})(["']?\s*[:=]\s*)(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[{[].*|[^\s,;}\]"']+)/gi, '$1$2<REDACTED>')
.replace(/password[= ][^ ]*/gi, 'password=<REDACTED>'))
.replace(/\beyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g, '<REDACTED>')
.replace(/-----BEGIN [A-Z ]*PRIVATE KEY-----.*/g, '<REDACTED>')
.replace(/\bgh[pousr]_[A-Za-z0-9_]+\b/g, '<REDACTED>')
+14 -4
View File
@@ -526,13 +526,23 @@ function runTests() {
assert.strictEqual(summarizeOutput('-----BEGIN RSA PRIVATE KEY-----\nMIIE...'), '<REDACTED>');
assert.strictEqual(summarizeOutput(`ghu_${'C'.repeat(36)}`), '<REDACTED>');
assert.strictEqual(summarizeOutput('Unexpected tokens: 0, max_tokens=4096, tokenizer=bpe, author=Jane'), 'Unexpected tokens: 0, max_tokens=4096, tokenizer=bpe, author=Jane');
// A bare value ends at the next field, and a quoted one may hold escaped quotes.
// A bare value after a quoted JSON key ends at the next field, and a quoted one
// may hold escaped quotes.
assert.strictEqual(summarizeOutput('{"token":null,"status":"failed","exit_code":1}'), '{"token":<REDACTED>,"status":"failed","exit_code":1}');
assert.strictEqual(summarizeOutput('{"api_key":"part1\\" part2"}'), '{"api_key":<REDACTED>}');
// Any other bare value runs to the next space: a comma can be part of it.
assert.strictEqual(summarizeOutput('CLIENT_SECRET=alpha,beta DEBUG=1'), 'CLIENT_SECRET=<REDACTED> DEBUG=1');
assert.strictEqual(summarizeOutput('client_secret: alpha,beta'), 'client_secret: <REDACTED>');
// The whole name is checked, however long.
const longName = `API_KEY_${'A'.repeat(65)}`;
assert.strictEqual(summarizeOutput(`${longName}=example-secret-value`), `${longName}=<REDACTED>`);
assert.strictEqual(summarizeOutput(`${'A'.repeat(100)}_SECRET=example-secret-value`), `${'A'.repeat(100)}_SECRET=<REDACTED>`);
// Separator-heavy output stays linear: unbounded name parts took seconds here.
const started = Date.now();
summarizeOutput('a.'.repeat(64000));
assert.ok(Date.now() - started < 2000, 'Redacting 128,000 characters should stay within its time budget');
for (const output of ['a.'.repeat(64000), 'secret'.repeat(21000), 'x.secret:'.repeat(14000), '"a":'.repeat(32000)]) {
const started = Date.now();
summarizeOutput(output);
assert.ok(Date.now() - started < 2000, `Redacting ${output.length} characters should stay within its time budget`);
}
}) ? passed++ : failed++);
(test('extracts file paths from nested arrays while filtering duplicates and remote URIs', () => {