diff --git a/src/core/redact-secrets.test.ts b/src/core/redact-secrets.test.ts index fa463c3a..83e5eae0 100644 --- a/src/core/redact-secrets.test.ts +++ b/src/core/redact-secrets.test.ts @@ -125,3 +125,49 @@ describe('redactSecrets', () => { expect(reparsed.sessions[0].note).toBe('ok'); }); }); + +describe('compound secret key names', () => { + it('redacts underscore-prefixed key names that a word boundary would skip', () => { + for (const name of ['client_secret', 'refresh_token', 'app_token', 'db_password']) { + const out = redactSecrets(`"${name}":"abcdefgh12345678"`); + expect(out).toContain('[REDACTED]'); + expect(out).not.toContain('abcdefgh12345678'); + } + }); + + it('redacts JSON escaped by doubling its quotes inside a shell command', () => { + const rule = 'Bash(curl -d \'{""client_secret"":""8bavjPwL9uoXDp""}\')'; + const out = redactSecrets(rule); + expect(out).not.toContain('8bavjPwL9uoXDp'); + expect(out).toContain('[REDACTED]'); + }); + + it('redacts a Context7 api key', () => { + const out = redactSecrets('"CONTEXT7_API_KEY": "ctx7sk-1234abcd-5678-efgh-9012-a1b2c3d4"'); + expect(out).not.toContain('ctx7sk-1234abcd'); + }); + + it('keeps the fields after an empty secret value', () => { + const text = '{"token": "", "user": "bob", "note": ""}'; + expect(redactSecrets(text)).toBe(text); + }); + + it('redacts a doubled-quote rule as it is stored in settings.local.json', () => { + const settings = JSON.stringify({ permissions: { allow: [ + 'Bash(curl -d \'{""client_secret"":""8bavjPwL9uoXDp""}\')', + ] } }, null, 2); + const out = redactSecrets(settings); + expect(out).not.toContain('8bavjPwL9uoXDp'); + expect(JSON.parse(out)).toBeTruthy(); + }); + + it('leaves ordinary prose mentioning those words intact', () => { + for (const text of [ + 'The token bucket algorithm smooths out traffic spikes.', + 'Set a secret in your CI provider, then reference it.', + 'const tokenizer = new Tokenizer();', + ]) { + expect(redactSecrets(text)).toBe(text); + } + }); +}); diff --git a/src/core/redact-secrets.ts b/src/core/redact-secrets.ts index 4ed9d945..912307eb 100644 --- a/src/core/redact-secrets.ts +++ b/src/core/redact-secrets.ts @@ -11,6 +11,10 @@ * keys, tokens, private keys). Matching is intentionally conservative -- * well-known token prefixes and explicit key/value assignments only -- so * ordinary prose and code remain readable. + * + * Key names may follow `_` or `-`, so `client_secret` and `refresh_token` are + * caught as well as bare `secret` and `token`. JSON embedded in a shell command + * is commonly escaped by doubling its quotes, so that form has its own pattern. */ const PRIVATE_KEY_BLOCK = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY( BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY( BLOCK)?-----|$)/g; @@ -21,16 +25,25 @@ const SLACK_APP_TOKEN = /\bxapp-[0-9]-[A-Za-z0-9-]{10,250}\b/g; const GOOGLE_API_KEY = /\bAIza[0-9A-Za-z_-]{35}\b/g; const NPM_TOKEN = /\bnpm_[A-Za-z0-9]{36}\b/g; const GITLAB_PAT = /\bglpat-[A-Za-z0-9_-]{20,80}\b/g; +const CONTEXT7_KEY = /\bctx7sk-[A-Za-z0-9_-]{10,250}\b/g; const SK_API_KEY = /\bsk-[A-Za-z0-9_-]{20,250}\b/g; // Stripe / underscore-prefixed keys (sk_live_, sk_test_, rk_live_, pk_live_, …). const STRIPE_KEY = /\b[srp]k_(?:live|test)_[0-9A-Za-z]{10,255}\b/g; // Credentials embedded in a connection-string URI (scheme://user:pass@host). const CONNECTION_STRING_CREDS = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s:@/]+:[^\s@/]+@/gi; +// The key name may follow `_` or `-` (client_secret, refresh_token) but not a letter or digit. +// Quotes may be backslash-escaped, as in raw JSON files and JSON.stringify output. +const SECRET_KEY_NAME = String.raw`(?