diff --git a/scripts/hooks/session-activity-tracker.js b/scripts/hooks/session-activity-tracker.js index e2167486a..6f286f5da 100644 --- a/scripts/hooks/session-activity-tracker.js +++ b/scripts/hooks/session-activity-tracker.js @@ -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 = /(?`; + 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=') .replace(/Authorization:[: ]*[^ ]*[: ]*[^ ]*/gi, 'Authorization:') .replace(/\bAKIA[A-Z0-9]{16}\b/g, '') .replace(/\bASIA[A-Z0-9]{16}\b/g, '') - .replace(/password[= ][^ ]*/gi, 'password=') - // 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') + .replace(/password[= ][^ ]*/gi, 'password=')) .replace(/\beyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g, '') .replace(/-----BEGIN [A-Z ]*PRIVATE KEY-----.*/g, '') .replace(/\bgh[pousr]_[A-Za-z0-9_]+\b/g, '') diff --git a/tests/hooks/session-activity-tracker.test.js b/tests/hooks/session-activity-tracker.test.js index 9d1df6b22..a2006d76d 100644 --- a/tests/hooks/session-activity-tracker.test.js +++ b/tests/hooks/session-activity-tracker.test.js @@ -526,13 +526,23 @@ function runTests() { assert.strictEqual(summarizeOutput('-----BEGIN RSA PRIVATE KEY-----\nMIIE...'), ''); assert.strictEqual(summarizeOutput(`ghu_${'C'.repeat(36)}`), ''); 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":,"status":"failed","exit_code":1}'); assert.strictEqual(summarizeOutput('{"api_key":"part1\\" part2"}'), '{"api_key":}'); + // 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= DEBUG=1'); + assert.strictEqual(summarizeOutput('client_secret: alpha,beta'), 'client_secret: '); + // The whole name is checked, however long. + const longName = `API_KEY_${'A'.repeat(65)}`; + assert.strictEqual(summarizeOutput(`${longName}=example-secret-value`), `${longName}=`); + assert.strictEqual(summarizeOutput(`${'A'.repeat(100)}_SECRET=example-secret-value`), `${'A'.repeat(100)}_SECRET=`); // 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', () => {