From 6b7f267abe2ee2fec4a8f2cfdb8660ba5659c7ec Mon Sep 17 00:00:00 2001 From: Dragan Spiridonov Date: Sun, 22 Nov 2026 20:45:49 +0100 Subject: [PATCH] fix(hooks): redact Google, Cognitum and header/query-string keys in recorded commands (#5062) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(hooks): redact Google, Cognitum and header/query-string keys in recorded commands redactCommandForStorage (#4056) caught Authorization headers, --token flags, *_TOKEN= assignments and well-known token shapes, but stored these verbatim: - Google API keys (AIza…, 39 chars) — Firebase/identitytoolkit ?key= calls and Gemini keys; - Cognitum API keys (cog_…); - keys sent in x-api-key / X-Goog-Api-Key / x-auth-token headers; - ?key= / &api_key= query-string credentials. Adds two rules and two shapes; each is bounded like the existing ones, and the hostile-input test covers the new prefixes. 8 new cases: 7 fail on main, all 35 pass with the fix. Fixtures are built by concatenation so secret scanners do not flag them (gitleaks: no leaks in the diff). Co-Authored-By: Ruflo & AQE Claude-Session: https://claude.ai/code/session_01GLQCdgnsuwts6j51rwcqgB * fix(hooks): close quoted-value and form-body leaks in command redaction Review follow-up for the recorded-command redactor: - header and Authorization rules accept a quoted value, so object-literal headers ({"x-api-key":"..."}) no longer leak - new JSON/object-literal rule for api_key/apiKey/access_token/auth_token and quoted bare 'key' - form bodies: api_key=/apikey= after whitespace or a quote, and bare key= after -d / --data* - more header names (apikey, api-key, Ocp-Apim-Subscription-Key, Private-Token, X-Auth-Key, x-access-token) with an optional scheme; a colon is still required, so `grep -rn x-api-key src` is untouched - cog_ keys matched as cog_ + 64 lowercase hex with hex lookarounds: catches prefix_cog_, leaves cog_SomeIdentifier alone - bare ?token= no longer redacted (pagination cursors); ?key= kept on purpose and pinned by a test - flag rule needs a non-word, non-dash char before its dash, so it no longer fires inside x-api-key (kept the colon of "x-api-key :v") - negative tests with exact output; hostile-input test extended Co-Authored-By: Ruflo & AQE Claude-Session: https://claude.ai/code/session_01GLQCdgnsuwts6j51rwcqgB --- .../__tests__/command-recording-4056.test.ts | 85 +++++++++++++++++++ .../cli/src/services/command-recording.ts | 36 ++++++-- 2 files changed, 116 insertions(+), 5 deletions(-) diff --git a/v3/@claude-flow/cli/__tests__/command-recording-4056.test.ts b/v3/@claude-flow/cli/__tests__/command-recording-4056.test.ts index 352e382a08..a36fda671a 100644 --- a/v3/@claude-flow/cli/__tests__/command-recording-4056.test.ts +++ b/v3/@claude-flow/cli/__tests__/command-recording-4056.test.ts @@ -55,6 +55,15 @@ describe('commandRecordingDisabled', () => { describe('redactCommandForStorage', () => { const GH = 'ghp_' + 'a1B2c3D4e5F6g7H8i9J0k1L2m3N4o5P6q7R8'; const OAI = 'sk-' + 'proj-abcdefghijklmnopqrstuvwxyz0123456789'; + // Google API key shape: "AIza" + 35 chars. Built by concatenation so secret scanners skip the fixture. + const GOOG = 'AI' + 'za' + 'SyA1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6Q'; + // Cognitum key shape: "cog_" + 64 lowercase hex (randomBytes(32).toString('hex')). + const COG = 'cog_' + '0123456789abcdef'.repeat(4); + const FAKE = 'fake' + 'Secret0123456789'; + const XAK = 'x-api' + '-key'; + const SIGNUP_URL = 'https://identitytoolkit.googleapis.com/v1/accounts:signUp?'; + const CB_TOKEN = 'tok_' + 'abcdef0123456789'; + const GOOG_HEADER = 'X-Goog-' + 'Api-Key'; it('leaves an ordinary command alone', () => { expect(redactCommandForStorage('npm test -- --run src/foo.test.ts')).toBe('npm test -- --run src/foo.test.ts'); @@ -71,6 +80,36 @@ describe('redactCommandForStorage', () => { [`git clone https://${GH}@github.com/o/r.git`, GH], [`curl -s https://api.openai.com -d x -H "x-key: ${OAI}"`, OAI], ['psql postgres://admin:s3cretpass@db.internal/app', 's3cretpass'], + // Provider keys that travel outside an Authorization header. + ['wget -qO- "' + SIGNUP_URL + 'key=' + GOOG + '"', GOOG], + ['http GET https://generativelanguage.googleapis.com/v1/models "' + GOOG_HEADER + ': ' + GOOG + '"', GOOG], + [`echo ${GOOG} | wc -c`, GOOG], + [`http GET https://api.example.test/v1/usage "x-api-key: ${COG}"`, COG], + [`wget --header='X-API-Key: plainvalue0123456789' https://x.test`, 'plainvalue0123456789'], + [`echo ${COG} > /dev/null`, COG], + ['curl "https://x.test/v1/items?limit=5&api_key=abcdef0123456789xyz&page=2"', 'abcdef0123456789xyz'], + ['curl "https://x.test/cb?access' + '_token=' + CB_TOKEN + '"', CB_TOKEN], + [`echo prefix_${COG}`, COG], + // Quoted values in headers and bodies. + [`node -e 'fetch(u,{headers:{"${XAK}":"${FAKE}"}})'`, FAKE], + [`node -e "fetch(u,{headers:{'${GOOG_HEADER}':'${FAKE}'}})"`, FAKE], + [`node -e 'fetch(u,{headers:{"Authorization":"Bearer ${FAKE}"}})'`, FAKE], + [`wget --post-data '{"api` + `_key":"${FAKE}"}' https://x.test`, FAKE], + [`node -e 'post({"api` + `Key": "${FAKE}"})'`, FAKE], + [`python3 -c "requests.get(u, params={'key': '${FAKE}'})"`, FAKE], + // Form bodies. + [`http -f POST https://x.test -d key=${FAKE} u`, FAKE], + [`wget https://x.test -d "api` + `_key=${FAKE}"`, FAKE], + [`http POST https://x.test --data-urlencode "key=${FAKE}"`, FAKE], + [`http POST https://x.test apikey=${FAKE}`, FAKE], + // More header names, with and without a scheme. + [`http GET https://x.test "apikey: ${FAKE}"`, FAKE], + [`http GET https://x.test "Api-Key: ${FAKE}"`, FAKE], + [`http GET https://x.test "Ocp-Apim-Subscription-Key: ${FAKE}"`, FAKE], + [`http GET https://x.test "Private-Token: ${FAKE}"`, FAKE], + [`http GET https://x.test "X-Auth-Key: ${FAKE}"`, FAKE], + [`http GET https://x.test "x-access-token: token ${FAKE}"`, FAKE], + [`http GET https://x.test "${XAK}: Bearer ${FAKE}"`, FAKE], ])('redacts the secret in: %s', (cmd, secret) => { const out = redactCommandForStorage(cmd); expect(out).not.toContain(secret); @@ -89,6 +128,36 @@ describe('redactCommandForStorage', () => { expect(out).toMatch(/truncated/); }); + it.each([ + 'curl "https://x.test/v1/items?sort_key=abc"', + 'curl "https://x.test/v1/items?token=nextpage123"', + 'grep -rn x-api-key src', + 'grep -r cog_RegistryEntriesTotalCountMetric src', + 'curl https://x.test/v1/items?limit=5&page=2', + ])('leaves a non-secret command unchanged: %s', cmd => { + expect(redactCommandForStorage(cmd)).toBe(cmd); + }); + + it('redacts only the credential parameter and keeps the rest of the URL', () => { + const fake = 'abcdef' + '0123456789xyz'; + expect(redactCommandForStorage(`curl "https://x.test/v1/items?limit=5&api_key=${fake}&page=2"`)) + .toBe('curl "https://x.test/v1/items?limit=5&api_key=[redacted]&page=2"'); + }); + + it('redacts a bare ?key= parameter (Google/Firebase keys travel that way), accepting the over-redaction', () => { + expect(redactCommandForStorage('curl "https://x.test/v1/items?key=abc&page=2"')) + .toBe('curl "https://x.test/v1/items?key=[redacted]&page=2"'); + }); + + it('keeps the colon of a spaced header and does not let the flag rule eat it', () => { + expect(redactCommandForStorage(`wget --header "${XAK} :abc" https://x.test`)) + .toBe(`wget --header "${XAK} :[redacted]" https://x.test`); + }); + + it('still redacts a real --api-key flag', () => { + expect(redactCommandForStorage('tool --api-key abc123 run')).toBe('tool --api-key [redacted] run'); + }); + it('is linear on hostile input (no catastrophic backtracking)', () => { for (const hostile of [ 'x'.repeat(200_000), @@ -96,6 +165,22 @@ describe('redactCommandForStorage', () => { 'A=b '.repeat(50_000), 'Authorization: '.repeat(20_000), '://'.repeat(60_000), + 'x-api-key: '.repeat(20_000), + '?key='.repeat(40_000), + '&api_key=a'.repeat(30_000), + 'AIza'.repeat(50_000), + 'cog_'.repeat(50_000), + 'a'.repeat(100) + 'cog_' + 'a'.repeat(200_000), + '{"api_key":"'.repeat(20_000), + '{"api_key":"' + 'x'.repeat(200_000), + "'key': '".repeat(20_000), + '-d key='.repeat(30_000), + '--data-urlencode '.repeat(10_000) + 'x', + '-d ' + ' '.repeat(200_000) + 'x', + ' api_key='.repeat(30_000), + 'Private-Token: '.repeat(20_000), + 'x-access-token: token '.repeat(10_000), + 'Ocp-Apim-Subscription-Key:'.repeat(10_000), ]) { const t0 = Date.now(); redactCommandForStorage(hostile); diff --git a/v3/@claude-flow/cli/src/services/command-recording.ts b/v3/@claude-flow/cli/src/services/command-recording.ts index ecafcfc4ec..2529bf1333 100644 --- a/v3/@claude-flow/cli/src/services/command-recording.ts +++ b/v3/@claude-flow/cli/src/services/command-recording.ts @@ -59,18 +59,44 @@ export function commandRecordingDisabled( const QUOTED_OR_BARE = `"[^"]{0,512}"|'[^']{0,512}'|[^\\s"']{1,512}`; -// Order matters little; each rule only ever shortens the text. +// Header names whose value is a credential. Matched only when followed by `:`, +// so `grep -rn x-api-key src` is left alone. +const HEADER_NAMES = + 'x-api-key|x-goog-api-key|x-auth-token|x-auth-key|x-access-token|apikey|api-key|ocp-apim-subscription-key|private-token'; + +// Key names in a JSON / object literal (`{"apiKey":"..."}`, `{'access_token': '...'}`). +const LITERAL_NAMES = 'api[-_]?key|x-api-key|x-goog-api-key|access[-_]?token|auth[-_]?token'; + +// Each rule only ever shortens the text, but order still matters in one place: +// the flag rule must not fire inside a header name (`x-api-key :v`), which is +// why it requires a non-word, non-dash character before its leading dash. const RULES: Array<[RegExp, string]> = [ - // Authorization: Bearer xyz / authorization=Basic xyz - [/(authorization["']?\s*[:=]\s*)(?:(?:bearer|basic|token)\s+)?[^\s"']{1,512}/gi, '$1[redacted]'], + // Authorization: Bearer xyz / authorization=Basic xyz / {"authorization":"Bearer xyz"} + [/(authorization["']?\s*[:=]\s*["']?)(?:(?:bearer|basic|token)\s+)?[^\s"']{1,512}/gi, '$1[redacted]'], + // x-api-key: xyz / {"X-Goog-Api-Key":"xyz"} / Private-Token: xyz / x-access-token: token xyz + [new RegExp(`(\\b(?:${HEADER_NAMES})["']?\\s*:\\s*["']?)(?:(?:bearer|basic|token)\\s+)?[^\\s"']{1,512}`, 'gi'), '$1[redacted]'], + // {"api_key":"xyz"} {apiKey: 'xyz'} and a quoted bare key: params={'key': 'xyz'} + [new RegExp(`((?:["']?(?:${LITERAL_NAMES})["']?|["']key["'])\\s*:\\s*["'])[^"']{1,512}(?=["'])`, 'gi'), '$1[redacted]'], + // ?key=xyz &api_key=xyz ?access_token=xyz (query-string credentials) + // Bare `?key=` is kept on purpose: Google / Firebase API keys travel that way, so a + // harmless `?key=` parameter is redacted too. Bare `?token=` is NOT matched: it is + // usually a pagination cursor. + [/([?&](?:key|api[-_]?key|access[-_]?token|auth[-_]?token)=)[^\s&#"']{1,512}/gi, '$1[redacted]'], + // form bodies: -d "api_key=xyz" apikey=xyz + [/((?` is caught and identifiers such as `cog_SomeMetricName` are not. + [/(?