From 9281b5b5f4621da997ad069849d37e5bd209c122 Mon Sep 17 00:00:00 2001 From: Mohammad Alsayyedan Date: Sun, 20 Sep 2026 22:08:56 -0500 Subject: [PATCH 1/3] Redact compound secret key names and doubled-quote JSON MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit redactSecrets masks transcript text before it is sent to a language model, but two common shapes slipped through it unchanged. Both assignment patterns anchor the key name with \b, which does not match between an underscore and a letter. `client_secret`, `refresh_token`, `app_token` and `db_password` therefore never matched, while the bare `secret` and `token` forms did. Match an optional compound prefix so the qualified names are covered too. The quote group also accepted a single quote character only. JSON embedded in a shell command is routinely escaped by doubling its quotes, as Claude Code does when recording a curl invocation in a permission allow rule, so `""client_secret"":""…""` matched neither pattern. Accept quote runs of one or two characters. Add the Context7 key prefix alongside the other vendor tokens; its `ctx7sk-` form is not reachable by the existing `sk-` pattern because the leading digit suppresses the word boundary. Checked against a real settings file: two credential-shaped strings survived redaction before, none do now, and prose mentioning "token", "secret" or "password" is still left intact. --- src/core/redact-secrets.test.ts | 32 ++++++++++++++++++++++++++++++++ src/core/redact-secrets.ts | 14 ++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/core/redact-secrets.test.ts b/src/core/redact-secrets.test.ts index fa463c3a..75a23853 100644 --- a/src/core/redact-secrets.test.ts +++ b/src/core/redact-secrets.test.ts @@ -125,3 +125,35 @@ 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-ijklmnop"'); + expect(out).not.toContain('ctx7sk-1234abcd'); + }); + + it('leaves ordinary prose mentioning those words intact', () => { + for (const text of [ + 'The token bucket algorithm smooths bursty traffic.', + '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..1cf68d8e 100644 --- a/src/core/redact-secrets.ts +++ b/src/core/redact-secrets.ts @@ -11,6 +11,11 @@ * 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 are matched with an optional compound prefix, so `client_secret` + * and `refresh_token` are caught as well as bare `secret` and `token`. Quote + * runs of one or two characters are accepted, because JSON embedded in a + * shell command is commonly escaped by doubling its quotes. */ const PRIVATE_KEY_BLOCK = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY( BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY( BLOCK)?-----|$)/g; @@ -21,16 +26,20 @@ 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; // Quoted assignment whose value may contain spaces: "password": "two words here". -const QUOTED_SECRET_ASSIGNMENT = /\b(api[_-]?key|access[_-]?key|secret|token|password|passwd|credentials?)(["']?\s*[:=]\s*)(["'])((?:(?!\3).){4,512})\3/gi; +const SECRET_KEY_NAME = String.raw`(?:[A-Za-z0-9]+[_-])*(?:api[_-]?key|access[_-]?key|secret|token|password|passwd|credentials?)`; +const QUOTED_SECRET_ASSIGNMENT = new RegExp( + String.raw`\b(${SECRET_KEY_NAME})(["']{0,2}\s*[:=]\s*)(["']{1,2})((?:(?!\3).){4,512})\3`, 'gi'); const JWT = /\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g; const AUTH_HEADER = /\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{16,512}/gi; -const KEY_VALUE_ASSIGNMENT = /\b(api[_-]?key|access[_-]?key|secret|token|password|passwd|credentials?)(["']?\s*[:=]\s*)(["']?)[^\s"'`;,]{8,512}\3/gi; +const KEY_VALUE_ASSIGNMENT = new RegExp( + String.raw`\b(${SECRET_KEY_NAME})(["']{0,2}\s*[:=]\s*)(["']{0,2})[^\s"'\`;,]{8,512}\3`, 'gi'); /** Mask credential-shaped substrings, keeping surrounding text intact. */ export function redactSecrets(text: string): string { @@ -44,6 +53,7 @@ export function redactSecrets(text: string): string { .replaceAll(GOOGLE_API_KEY, '[REDACTED:google-api-key]') .replaceAll(NPM_TOKEN, '[REDACTED:npm-token]') .replaceAll(GITLAB_PAT, '[REDACTED:gitlab-token]') + .replaceAll(CONTEXT7_KEY, '[REDACTED:api-key]') .replaceAll(STRIPE_KEY, '[REDACTED:stripe-key]') .replaceAll(SK_API_KEY, '[REDACTED:api-key]') .replaceAll(JWT, '[REDACTED:jwt]') From 2950c6aabbf1b085b79a20baaeb9e9ee279b2a6a Mon Sep 17 00:00:00 2001 From: Mohammad Alsayyedan Date: Sun, 27 Sep 2026 19:13:20 -0500 Subject: [PATCH 2/3] Use dictionary words in redaction test fixtures cspell flagged 'ijklmnop' and 'bursty' in the new tests, which fails npm run check. Replace them without changing what the tests assert. --- src/core/redact-secrets.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/redact-secrets.test.ts b/src/core/redact-secrets.test.ts index 75a23853..b19c1682 100644 --- a/src/core/redact-secrets.test.ts +++ b/src/core/redact-secrets.test.ts @@ -143,13 +143,13 @@ describe('compound secret key names', () => { }); it('redacts a Context7 api key', () => { - const out = redactSecrets('"CONTEXT7_API_KEY": "ctx7sk-1234abcd-5678-efgh-9012-ijklmnop"'); + const out = redactSecrets('"CONTEXT7_API_KEY": "ctx7sk-1234abcd-5678-efgh-9012-a1b2c3d4"'); expect(out).not.toContain('ctx7sk-1234abcd'); }); it('leaves ordinary prose mentioning those words intact', () => { for (const text of [ - 'The token bucket algorithm smooths bursty traffic.', + 'The token bucket algorithm smooths out traffic spikes.', 'Set a secret in your CI provider, then reference it.', 'const tokenizer = new Tokenizer();', ]) { From 438f20ca7d33c81998dc8c287567c937a4329311 Mon Sep 17 00:00:00 2001 From: Mohammad Alsayyedan Date: Tue, 3 Nov 2026 16:55:09 -0600 Subject: [PATCH 3/3] Apply review: escaped quotes, empty values, linear key match - Allow an optional backslash before each quote, so raw JSON files (settings.local.json) and JSON.stringify output are redacted. - Give the doubled-quote form its own pattern, so an empty value no longer swallows the fields after it. - Use a lookbehind for the key name instead of a compound prefix, so matching stays linear on long hyphen or underscore runs. Suggested by @san360 in review. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RLDXsjE1DMDor8E6cSLBpH --- src/core/redact-secrets.test.ts | 14 ++++++++++++++ src/core/redact-secrets.ts | 19 ++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/core/redact-secrets.test.ts b/src/core/redact-secrets.test.ts index b19c1682..83e5eae0 100644 --- a/src/core/redact-secrets.test.ts +++ b/src/core/redact-secrets.test.ts @@ -147,6 +147,20 @@ describe('compound secret key names', () => { 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.', diff --git a/src/core/redact-secrets.ts b/src/core/redact-secrets.ts index 1cf68d8e..912307eb 100644 --- a/src/core/redact-secrets.ts +++ b/src/core/redact-secrets.ts @@ -12,10 +12,9 @@ * well-known token prefixes and explicit key/value assignments only -- so * ordinary prose and code remain readable. * - * Key names are matched with an optional compound prefix, so `client_secret` - * and `refresh_token` are caught as well as bare `secret` and `token`. Quote - * runs of one or two characters are accepted, because JSON embedded in a - * shell command is commonly escaped by doubling its quotes. + * 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; @@ -32,14 +31,19 @@ const SK_API_KEY = /\bsk-[A-Za-z0-9_-]{20,250}\b/g; 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`(?