From b979d0886343b671669622e66f38dff317f73816 Mon Sep 17 00:00:00 2001 From: Karim Masmoudi <125830216+Kernel-Hunter@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:01:05 +0100 Subject: [PATCH 1/8] fix(continuous-learning-v2): redact bare sk- keys and JWTs in observations observe.sh only redacted Anthropic and OpenAI keys when a keyword such as api_key or token sat in front of them, so `echo > key.txt` and file content holding a key were written to observations.jsonl in plain text. JWTs had the same gap. Add value-shaped branches to _SECRET_VALUE_RE in both the observation and parse-error paths: sk-ant-, sk-proj-, sk-svcacct-, sk-admin- and sk-or- keys, legacy alphanumeric sk- keys, and eyJ JWTs. Quantifiers are bounded and the branch only starts at a token boundary, so the matcher stays linear. Refs #3846 --- .../continuous-learning-v2/hooks/observe.sh | 12 ++-- tests/hooks/observe-secret-redaction.test.js | 55 ++++++++++++++++++- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/skills/continuous-learning-v2/hooks/observe.sh b/skills/continuous-learning-v2/hooks/observe.sh index aecb29a07..35c525997 100755 --- a/skills/continuous-learning-v2/hooks/observe.sh +++ b/skills/continuous-learning-v2/hooks/observe.sh @@ -278,8 +278,9 @@ _SECRET_RE = re.compile( r"([A-Za-z0-9_\-/.+=]{8,256})" ) # Value-shaped secrets that carry no keyword: PEM private key blocks (to END or -# end of the truncated text), GitHub tokens, AWS key IDs, and DSN passwords -# (scheme://user:PASSWORD@host keeps scheme and user). Each branch is anchored +# end of the truncated text), GitHub tokens, AWS key IDs, Anthropic and OpenAI +# style sk- keys, JWTs (eyJ header, payload, optional signature) and DSN +# passwords (scheme://user:PASSWORD@host keeps scheme and user). Each branch is anchored # on a literal prefix and uses bounded or non-overlapping classes (#2278). # Tokens may follow a JSON escape (\n, \t, \uXXXX) since dict I/O is encoded # before scrubbing; a DSN cut by truncation before its "@" ends at \Z, @@ -288,6 +289,7 @@ _SECRET_RE = re.compile( _SECRET_VALUE_RE = re.compile( r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)" r"|(?:(? key.txt`, anthropicCanary], + ['bare OpenAI project key', `client = OpenAI(${openaiProjectKey})`, openaiCanary], + ['bare legacy sk- key', `KEYS = [${legacyKey}]`, legacyCanary], + ['bare JWT', `curl -H "X-Session: ${jwt}" https://api.example.com`, jwtCanary] ]) { test(`redacts ${label} before persisting tool input`, () => { const records = capture('pre', { tool_name: 'Write', @@ -92,7 +106,9 @@ if (process.platform === 'win32') { ['GitHub fine-grained token after tab', `col\t${'github_pat_'}CANARY_${'1'.repeat(40)}`, `CANARY_${'1'.repeat(40)}`], ['AWS access key id after CRLF', `id:\r\n${'AKIA'}CANARY0EXAMPLE00`, 'CANARY0EXAMPLE00'], - ['AWS session key id after newline', `id:\n${'ASIA'}CANARY0EXAMPLE00`, 'CANARY0EXAMPLE00'] + ['AWS session key id after newline', `id:\n${'ASIA'}CANARY0EXAMPLE00`, 'CANARY0EXAMPLE00'], + ['Anthropic key after newline', `key:\n${anthropicKey}`, anthropicCanary], + ['JWT after tab', `jwt\t${jwt}`, jwtCanary] ]) { test(`redacts ${label} in structured tool output`, () => { const records = capture('post', { tool_name: 'Read', @@ -102,6 +118,29 @@ if (process.platform === 'win32') { }); } + // The three commands from #3846. Only the first two have a keyword in front of the key. + for (const command of [ + `ANTHROPIC_API_KEY=${anthropicKey} claude -p hi`, + `curl -H "x-api-key: ${anthropicKey}" https://api.example.com`, + `echo ${anthropicKey} > key.txt` + ]) { + test(`redacts the Anthropic key in Bash input and output: ${command.split(' ')[0]}`, () => { + for (const records of [ + capture('pre', { tool_name: 'Bash', tool_input: { command } }), + capture('post', { tool_name: 'Bash', tool_input: { command: 'cat key.txt' }, + tool_response: { stdout: command, stderr: '' } }) + ]) { + assert.ok(!records.includes(anthropicCanary), 'Anthropic key persisted'); + assert.ok(records.includes('[REDACTED]')); + } + }); + } + test('redacts a bare Anthropic key in the parse-error fallback', () => { + const records = capture('post', JSON.stringify([`echo ${anthropicKey}`]), 'parse_error'); + assert.ok(!records.includes(anthropicCanary), 'Anthropic key persisted'); + assert.ok(records.includes('[REDACTED]')); + }); + // Capture truncation (5000 chars, 2000 in the parse-error fallback) can cut a // DSN right before its '@'; the password must still be redacted (#3568). const cutSecret = 'CANARYcutPassw0rd'; @@ -148,7 +187,17 @@ if (process.platform === 'win32') { 'password_hash = hashlib.sha256(pw)', 'fetch(http://localhost:8080/api/v1)', 'git clone git@github.com:org/repo.git', - 'https://user@example.com/path' + 'https://user@example.com/path', + // Hashes, ids and base64 that look random but are not keys (#3846). + 'git show 3f9a1c2b7e4d5f6a8b9c0d1e2f3a4b5c6d7e8f90', + 'sha256 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', + 'id 550e8400-e29b-41d4-a716-446655440000', + 'background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==);', + 'payload: data:application/json;base64,eyJhIjoxLCJiIjoyLCJjIjozfQ==', + 'git checkout -b sk-learn-upgrade-for-pipelines', + 'risk-assessment-for-the-quarterly-report-v2', + 'ssh deploy@eyeglasses.example.com', + 'Keys start with the sk- prefix.' ]; test('keeps ordinary code and credential-free URLs unredacted', () => { const records = capture('post', { tool_name: 'Read', From 61826bdc993ad7f6be7399a151b618259ed82f9f Mon Sep 17 00:00:00 2001 From: Karim Masmoudi <125830216+Kernel-Hunter@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:11:53 +0100 Subject: [PATCH 2/8] chore(context-packs): refresh skill triggers registry digest observe.sh changed, so the registry digest in skill-triggers@1.json moved. --- manifests/context-packs/skill-triggers@1.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifests/context-packs/skill-triggers@1.json b/manifests/context-packs/skill-triggers@1.json index 48081c3fb..3540c8edb 100644 --- a/manifests/context-packs/skill-triggers@1.json +++ b/manifests/context-packs/skill-triggers@1.json @@ -10,7 +10,7 @@ "id": "hand-seeded", "source": "manual-curation-pending-regeneration" }, - "registryDigest": "6f213dc845937b4093bc018d041b25b538d88d78451b9d799ad614989b360ee4", + "registryDigest": "cf364a6536f8fb3b0f0cda52a49fd3751513eff988ce5fadc15abcb1e4577358", "schemaVersion": 1, "triggers": { "skill:agent-runtime-gateway-smoke-test": [ From b56f5e77b0bf3d1234100378eeaa41ed471daa97 Mon Sep 17 00:00:00 2001 From: Karim Masmoudi <125830216+Kernel-Hunter@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:26:05 +0100 Subject: [PATCH 3/8] fix(continuous-learning-v2): redact sk- keys and JWTs cut by capture truncation Observations are cut to 5000 chars (2000 in the parse-error fallback) before scrubbing. A JWT cut inside its payload lost its second dot and a key cut short of 20 body chars missed the length floor, so the remaining header, claims or key prefix were persisted. Accept a match that runs to the end of the capture, the same way the PEM and DSN branches do. Tests cover both cuts in string output, structured output and the parse-error fallback, and compare the JWT and Bash cases against their exact expected values. --- manifests/context-packs/skill-triggers@1.json | 2 +- .../continuous-learning-v2/hooks/observe.sh | 14 ++++--- tests/hooks/observe-secret-redaction.test.js | 39 ++++++++++++++++++- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/manifests/context-packs/skill-triggers@1.json b/manifests/context-packs/skill-triggers@1.json index 3540c8edb..ba61cb296 100644 --- a/manifests/context-packs/skill-triggers@1.json +++ b/manifests/context-packs/skill-triggers@1.json @@ -10,7 +10,7 @@ "id": "hand-seeded", "source": "manual-curation-pending-regeneration" }, - "registryDigest": "cf364a6536f8fb3b0f0cda52a49fd3751513eff988ce5fadc15abcb1e4577358", + "registryDigest": "d065943b86358b5dc5f84c5910de117b21650c0be34071457d20820b28d68e7a", "schemaVersion": 1, "triggers": { "skill:agent-runtime-gateway-smoke-test": [ diff --git a/skills/continuous-learning-v2/hooks/observe.sh b/skills/continuous-learning-v2/hooks/observe.sh index 35c525997..7aa2e4dde 100755 --- a/skills/continuous-learning-v2/hooks/observe.sh +++ b/skills/continuous-learning-v2/hooks/observe.sh @@ -279,8 +279,9 @@ _SECRET_RE = re.compile( ) # Value-shaped secrets that carry no keyword: PEM private key blocks (to END or # end of the truncated text), GitHub tokens, AWS key IDs, Anthropic and OpenAI -# style sk- keys, JWTs (eyJ header, payload, optional signature) and DSN -# passwords (scheme://user:PASSWORD@host keeps scheme and user). Each branch is anchored +# style sk- keys and JWTs (eyJ header, payload, optional signature; either one +# cut by truncation ends at \Z), and DSN passwords (scheme://user:PASSWORD@host +# keeps scheme and user). Each branch is anchored # on a literal prefix and uses bounded or non-overlapping classes (#2278). # Tokens may follow a JSON escape (\n, \t, \uXXXX) since dict I/O is encoded # before scrubbing; a DSN cut by truncation before its "@" ends at \Z, @@ -289,7 +290,7 @@ _SECRET_RE = re.compile( _SECRET_VALUE_RE = re.compile( r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)" r"|(?:(? { const records = capture('post', { tool_name: 'Read', @@ -131,7 +141,8 @@ if (process.platform === 'win32') { tool_response: { stdout: command, stderr: '' } }) ]) { assert.ok(!records.includes(anthropicCanary), 'Anthropic key persisted'); - assert.ok(records.includes('[REDACTED]')); + const field = JSON.parse(records).event === 'tool_start' ? 'command' : 'stdout'; + assert.equal(capturedField(records, field), command.replace(anthropicKey, '[REDACTED]')); } }); } @@ -141,6 +152,32 @@ if (process.platform === 'win32') { assert.ok(records.includes('[REDACTED]')); }); + // Truncation can also cut a bare JWT inside its payload or a key inside its + // body. What is left at the end of the capture must still be redacted. + const jwtKept = jwt.slice(0, jwt.indexOf('.') + 10); + const jwtPayloadStart = jwtKept.slice(jwt.indexOf('.') + 1); + const keyKept = anthropicKey.slice(0, 'sk-ant-api03-'.length + 12); + const cutAt = (end, value, kept) => `${'a'.repeat(end - kept.length - 1)} ${value}`; + for (const [label, value, kept, leak] of [ + ['JWT', jwt, jwtKept, jwtPayloadStart], + ['Anthropic key', anthropicKey, keyKept, keyKept.slice('sk-ant-api03-'.length)] + ]) { + for (const [where, phase, input, expectedEvent] of [ + ['string tool output', 'post', { tool_name: 'Bash', tool_input: { command: 'env' }, + tool_response: cutAt(5000, value, kept) }], + // json.dumps prefixes '{"content": "' (13 chars) before the value. + ['structured tool output', 'post', { tool_name: 'Read', tool_input: { file_path: '/tmp/x' }, + tool_response: { content: cutAt(5000 - 13, value, kept) } }], + ['parse-error fallback', 'post', JSON.stringify([cutAt(2000 - 2, value, kept)]), 'parse_error'] + ]) { + test(`redacts the ${label} cut at the truncation boundary in ${where}`, () => { + const records = capture(phase, input, expectedEvent); + assert.ok(!records.includes(leak), `cut ${label} persisted`); + assert.ok(records.includes('[REDACTED]')); + }); + } + } + // Capture truncation (5000 chars, 2000 in the parse-error fallback) can cut a // DSN right before its '@'; the password must still be redacted (#3568). const cutSecret = 'CANARYcutPassw0rd'; From ac89d627b4e8711eab3c6582de0f936afcf8c566 Mon Sep 17 00:00:00 2001 From: Karim Masmoudi <125830216+Kernel-Hunter@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:27:24 +0100 Subject: [PATCH 4/8] fix(continuous-learning-v2): redact long sk- keys and JWTs whole, cut tails only when cut The sk- and JWT branches capped their token classes at 512 and 1024 chars, so a longer value was only partly replaced. Their classes do not overlap with the separators, so they can run to the end of the token and still match in linear time. The end-of-text match for a key or JWT cut by truncation moves to its own pattern, applied only when the capture was actually shortened. Short output that happens to end in "sk-learn" or a one-segment eyJ blob stays readable. --- manifests/context-packs/skill-triggers@1.json | 2 +- .../continuous-learning-v2/hooks/observe.sh | 47 ++++++++++++++----- tests/hooks/observe-secret-redaction.test.js | 23 +++++++++ 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/manifests/context-packs/skill-triggers@1.json b/manifests/context-packs/skill-triggers@1.json index ba61cb296..255b3ed61 100644 --- a/manifests/context-packs/skill-triggers@1.json +++ b/manifests/context-packs/skill-triggers@1.json @@ -10,7 +10,7 @@ "id": "hand-seeded", "source": "manual-curation-pending-regeneration" }, - "registryDigest": "d065943b86358b5dc5f84c5910de117b21650c0be34071457d20820b28d68e7a", + "registryDigest": "f24282bd1d290f922b4ffe2f996ae7b249f84ef2a18804ff97633012818f3093", "schemaVersion": 1, "triggers": { "skill:agent-runtime-gateway-smoke-test": [ diff --git a/skills/continuous-learning-v2/hooks/observe.sh b/skills/continuous-learning-v2/hooks/observe.sh index 7aa2e4dde..66984e351 100755 --- a/skills/continuous-learning-v2/hooks/observe.sh +++ b/skills/continuous-learning-v2/hooks/observe.sh @@ -279,10 +279,11 @@ _SECRET_RE = re.compile( ) # Value-shaped secrets that carry no keyword: PEM private key blocks (to END or # end of the truncated text), GitHub tokens, AWS key IDs, Anthropic and OpenAI -# style sk- keys and JWTs (eyJ header, payload, optional signature; either one -# cut by truncation ends at \Z), and DSN passwords (scheme://user:PASSWORD@host -# keeps scheme and user). Each branch is anchored -# on a literal prefix and uses bounded or non-overlapping classes (#2278). +# style sk- keys, JWTs (eyJ header, payload, optional signature) and DSN +# passwords (scheme://user:PASSWORD@host keeps scheme and user). Each branch is +# anchored on a literal prefix and uses bounded or non-overlapping classes (#2278); +# the sk- and JWT classes run to the end of the token so a long value is never +# half redacted. # Tokens may follow a JSON escape (\n, \t, \uXXXX) since dict I/O is encoded # before scrubbing; a DSN cut by truncation before its "@" ends at \Z, # except a bare trailing host:port after http(s):// such as http://localhost:3000; @@ -290,9 +291,15 @@ _SECRET_RE = re.compile( _SECRET_VALUE_RE = re.compile( r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)" r"|(?:(? 2000: + raw = _CUT_TAIL_RE.sub("[REDACTED]", raw) print(json.dumps({"timestamp": os.environ["TIMESTAMP"], "event": "parse_error", "raw": raw})) ' >> "$OBSERVATIONS_FILE" exit 0 @@ -367,10 +377,11 @@ _SECRET_RE = re.compile( ) # Value-shaped secrets that carry no keyword: PEM private key blocks (to END or # end of the truncated text), GitHub tokens, AWS key IDs, Anthropic and OpenAI -# style sk- keys and JWTs (eyJ header, payload, optional signature; either one -# cut by truncation ends at \Z), and DSN passwords (scheme://user:PASSWORD@host -# keeps scheme and user). Each branch is anchored -# on a literal prefix and uses bounded or non-overlapping classes (#2278). +# style sk- keys, JWTs (eyJ header, payload, optional signature) and DSN +# passwords (scheme://user:PASSWORD@host keeps scheme and user). Each branch is +# anchored on a literal prefix and uses bounded or non-overlapping classes (#2278); +# the sk- and JWT classes run to the end of the token so a long value is never +# half redacted. # Tokens may follow a JSON escape (\n, \t, \uXXXX) since dict I/O is encoded # before scrubbing; a DSN cut by truncation before its "@" ends at \Z, # except a bare trailing host:port after http(s):// such as http://localhost:3000; @@ -378,15 +389,25 @@ _SECRET_RE = re.compile( _SECRET_VALUE_RE = re.compile( r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)" r"|(?:(?= 5000 + val = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", val) + val = _SECRET_VALUE_RE.sub(lambda m: (m.group(2) or "") + "[REDACTED]", val) + return _CUT_TAIL_RE.sub("[REDACTED]", val) if cut else val if parsed["input"]: observation["input"] = scrub(parsed["input"]) diff --git a/tests/hooks/observe-secret-redaction.test.js b/tests/hooks/observe-secret-redaction.test.js index df5def4bb..d9668e61c 100644 --- a/tests/hooks/observe-secret-redaction.test.js +++ b/tests/hooks/observe-secret-redaction.test.js @@ -178,6 +178,29 @@ if (process.platform === 'win32') { } } + // A long key or JWT is redacted whole, never only up to some length cap. + const longTail = 'CANARYlongTail'; + for (const [label, value] of [ + ['an over-long Anthropic key', `${anthropicKey}${'A'.repeat(600)}${longTail}`], + ['a JWT with a long signature', `${jwt}${'s'.repeat(1500)}${longTail}`] + ]) { + test(`redacts ${label} completely`, () => { + const records = capture('post', { tool_name: 'Read', tool_input: { file_path: '/tmp/x' }, + tool_response: { content: `value ${value} end` } }); + assert.equal(capturedField(records, 'content'), 'value [REDACTED] end'); + }); + } + + // The end-of-capture match only applies to text that was actually cut. + for (const output of ['git checkout sk-learn', 'payload eyJhIjoxLCJiIjoyfQ']) { + test(`keeps short string output ending in "${output.split(' ').pop().slice(0, 8)}" unredacted`, () => { + const records = capture('post', { tool_name: 'Bash', tool_input: { command: 'echo' }, + tool_response: output }); + assert.ok(records.includes(output), 'short output altered'); + assert.ok(!records.includes('[REDACTED]')); + }); + } + // Capture truncation (5000 chars, 2000 in the parse-error fallback) can cut a // DSN right before its '@'; the password must still be redacted (#3568). const cutSecret = 'CANARYcutPassw0rd'; From 16a23737f23a5c95050832d079ee0d0c77cfe240 Mon Sep 17 00:00:00 2001 From: Karim Masmoudi <125830216+Kernel-Hunter@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:43:29 +0100 Subject: [PATCH 5/8] fix(continuous-learning-v2): run shape patterns before the keyword matcher _SECRET_RE stops after 256 value chars. With it first, a longer keyword-labeled key such as OPENAI_API_KEY=sk-proj-... lost its prefix and the shape pattern could no longer see the rest, so the tail was persisted. Running _SECRET_VALUE_RE first fixes both the observation path and the parse-error fallback; "[REDACTED]" is outside the keyword value class, so the second pass leaves it alone. --- manifests/context-packs/skill-triggers@1.json | 2 +- skills/continuous-learning-v2/hooks/observe.sh | 8 ++++++-- tests/hooks/observe-secret-redaction.test.js | 13 +++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/manifests/context-packs/skill-triggers@1.json b/manifests/context-packs/skill-triggers@1.json index 255b3ed61..6798959fa 100644 --- a/manifests/context-packs/skill-triggers@1.json +++ b/manifests/context-packs/skill-triggers@1.json @@ -10,7 +10,7 @@ "id": "hand-seeded", "source": "manual-curation-pending-regeneration" }, - "registryDigest": "f24282bd1d290f922b4ffe2f996ae7b249f84ef2a18804ff97633012818f3093", + "registryDigest": "e93e6248f74627968ef65d9bf0e64a6c0d22abce6efc6342c856613bf3262015", "schemaVersion": 1, "triggers": { "skill:agent-runtime-gateway-smoke-test": [ diff --git a/skills/continuous-learning-v2/hooks/observe.sh b/skills/continuous-learning-v2/hooks/observe.sh index 66984e351..e3bc85e4d 100755 --- a/skills/continuous-learning-v2/hooks/observe.sh +++ b/skills/continuous-learning-v2/hooks/observe.sh @@ -313,8 +313,10 @@ except Exception: raw_full = sys.stdin.read() raw = raw_full[:2000] -raw = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", raw) +# Shape patterns first: _SECRET_RE stops after 256 value chars, which would +# strip the sk-/eyJ prefix from a longer keyword-labeled key and leave its tail. raw = _SECRET_VALUE_RE.sub(lambda m: (m.group(2) or "") + "[REDACTED]", raw) +raw = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", raw) if len(raw_full) > 2000: raw = _CUT_TAIL_RE.sub("[REDACTED]", raw) print(json.dumps({"timestamp": os.environ["TIMESTAMP"], "event": "parse_error", "raw": raw})) @@ -405,8 +407,10 @@ def scrub(val): val = str(val) # Tool I/O is cut to 5000 chars before it reaches this process. cut = len(val) >= 5000 - val = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", val) + # Shape patterns first: _SECRET_RE stops after 256 value chars, which would + # strip the sk-/eyJ prefix from a longer keyword-labeled key and leave its tail. val = _SECRET_VALUE_RE.sub(lambda m: (m.group(2) or "") + "[REDACTED]", val) + val = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", val) return _CUT_TAIL_RE.sub("[REDACTED]", val) if cut else val if parsed["input"]: diff --git a/tests/hooks/observe-secret-redaction.test.js b/tests/hooks/observe-secret-redaction.test.js index d9668e61c..cc500bfd0 100644 --- a/tests/hooks/observe-secret-redaction.test.js +++ b/tests/hooks/observe-secret-redaction.test.js @@ -190,6 +190,19 @@ if (process.platform === 'win32') { assert.equal(capturedField(records, 'content'), 'value [REDACTED] end'); }); } + // The keyword matcher stops after 256 value chars, so a long keyword-labeled + // key must be caught by its shape first or its tail is persisted. + for (const [phase, input, expectedEvent] of [ + ['post', { tool_name: 'Read', tool_input: { file_path: '/tmp/x' }, + tool_response: { content: `OPENAI_API_KEY=${openaiProjectKey}${'B'.repeat(300)}${longTail}` } }], + ['post', JSON.stringify([`OPENAI_API_KEY=${openaiProjectKey}${'B'.repeat(300)}${longTail}`]), 'parse_error'] + ]) { + test(`redacts a long keyword-labeled sk-proj key completely (${expectedEvent || 'tool_complete'})`, () => { + const records = capture(phase, input, expectedEvent); + assert.ok(!records.includes(longTail), 'key tail persisted'); + assert.ok(records.includes('OPENAI_API_KEY=[REDACTED]')); + }); + } // The end-of-capture match only applies to text that was actually cut. for (const output of ['git checkout sk-learn', 'payload eyJhIjoxLCJiIjoyfQ']) { From caeb65b1ee862594785e4a3520d05145cc521e5a Mon Sep 17 00:00:00 2001 From: Karim Masmoudi <125830216+Kernel-Hunter@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:56:35 +0100 Subject: [PATCH 6/8] fix(continuous-learning-v2): redact keyword-labeled values to the end of the token Running the shape patterns first left suffixes visible: in password=sk-...suffix or token=ghp_.../suffix the shape match stopped at the separator and the inserted "[REDACTED]" kept the keyword matcher from covering the rest. Go back to keyword-first and drop the 256-char cap on the keyword value instead. Its class is disjoint from the separators, so it can run to the end of the token in linear time. A long labeled key, a labeled password that starts with a shaped token, and a labeled JWT cut inside a long payload are now redacted whole. --- manifests/context-packs/skill-triggers@1.json | 2 +- .../continuous-learning-v2/hooks/observe.sh | 16 ++++----- tests/hooks/observe-secret-redaction.test.js | 35 +++++++++++++------ 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/manifests/context-packs/skill-triggers@1.json b/manifests/context-packs/skill-triggers@1.json index 6798959fa..2754f65e9 100644 --- a/manifests/context-packs/skill-triggers@1.json +++ b/manifests/context-packs/skill-triggers@1.json @@ -10,7 +10,7 @@ "id": "hand-seeded", "source": "manual-curation-pending-regeneration" }, - "registryDigest": "e93e6248f74627968ef65d9bf0e64a6c0d22abce6efc6342c856613bf3262015", + "registryDigest": "23d2319fbb6e5a75b914fd49baab8f8c1f7aab94513911918f3e71a6f96d0d92", "schemaVersion": 1, "triggers": { "skill:agent-runtime-gateway-smoke-test": [ diff --git a/skills/continuous-learning-v2/hooks/observe.sh b/skills/continuous-learning-v2/hooks/observe.sh index e3bc85e4d..c4cdfff2f 100755 --- a/skills/continuous-learning-v2/hooks/observe.sh +++ b/skills/continuous-learning-v2/hooks/observe.sh @@ -271,11 +271,13 @@ import json, sys, os, re # Linear-time secret matcher. Bounded quantifiers and a fixed set of auth # schemes (instead of a generic [A-Za-z]+\s+ that overlapped the value class) # prevent the catastrophic backtracking that pegged python at 100% CPU (#2278). +# The value class is disjoint from the separators, so it runs to the end of the +# token: a long labeled key is redacted whole instead of leaving its tail. _SECRET_RE = re.compile( r"(?i)((?:api[_-]?key|token|secret|password|authorization|credentials?|auth)(?:_access_key|_key)?)" r"""(["'"'"'\s:=\\]{1,8})""" r"((?:bearer|basic|token|bot)\s+)?" - r"([A-Za-z0-9_\-/.+=]{8,256})" + r"([A-Za-z0-9_\-/.+=]{8,})" ) # Value-shaped secrets that carry no keyword: PEM private key blocks (to END or # end of the truncated text), GitHub tokens, AWS key IDs, Anthropic and OpenAI @@ -313,10 +315,8 @@ except Exception: raw_full = sys.stdin.read() raw = raw_full[:2000] -# Shape patterns first: _SECRET_RE stops after 256 value chars, which would -# strip the sk-/eyJ prefix from a longer keyword-labeled key and leave its tail. -raw = _SECRET_VALUE_RE.sub(lambda m: (m.group(2) or "") + "[REDACTED]", raw) raw = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", raw) +raw = _SECRET_VALUE_RE.sub(lambda m: (m.group(2) or "") + "[REDACTED]", raw) if len(raw_full) > 2000: raw = _CUT_TAIL_RE.sub("[REDACTED]", raw) print(json.dumps({"timestamp": os.environ["TIMESTAMP"], "event": "parse_error", "raw": raw})) @@ -371,11 +371,13 @@ observation = { # Linear-time secret matcher. Bounded quantifiers and a fixed set of auth # schemes (instead of a generic [A-Za-z]+\s+ that overlapped the value class) # prevent the catastrophic backtracking that pegged python at 100% CPU (#2278). +# The value class is disjoint from the separators, so it runs to the end of the +# token: a long labeled key is redacted whole instead of leaving its tail. _SECRET_RE = re.compile( r"(?i)((?:api[_-]?key|token|secret|password|authorization|credentials?|auth)(?:_access_key|_key)?)" r"""(["'"'"'\s:=\\]{1,8})""" r"((?:bearer|basic|token|bot)\s+)?" - r"([A-Za-z0-9_\-/.+=]{8,256})" + r"([A-Za-z0-9_\-/.+=]{8,})" ) # Value-shaped secrets that carry no keyword: PEM private key blocks (to END or # end of the truncated text), GitHub tokens, AWS key IDs, Anthropic and OpenAI @@ -407,10 +409,8 @@ def scrub(val): val = str(val) # Tool I/O is cut to 5000 chars before it reaches this process. cut = len(val) >= 5000 - # Shape patterns first: _SECRET_RE stops after 256 value chars, which would - # strip the sk-/eyJ prefix from a longer keyword-labeled key and leave its tail. - val = _SECRET_VALUE_RE.sub(lambda m: (m.group(2) or "") + "[REDACTED]", val) val = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", val) + val = _SECRET_VALUE_RE.sub(lambda m: (m.group(2) or "") + "[REDACTED]", val) return _CUT_TAIL_RE.sub("[REDACTED]", val) if cut else val if parsed["input"]: diff --git a/tests/hooks/observe-secret-redaction.test.js b/tests/hooks/observe-secret-redaction.test.js index cc500bfd0..3fef5e30c 100644 --- a/tests/hooks/observe-secret-redaction.test.js +++ b/tests/hooks/observe-secret-redaction.test.js @@ -190,19 +190,32 @@ if (process.platform === 'win32') { assert.equal(capturedField(records, 'content'), 'value [REDACTED] end'); }); } - // The keyword matcher stops after 256 value chars, so a long keyword-labeled - // key must be caught by its shape first or its tail is persisted. - for (const [phase, input, expectedEvent] of [ - ['post', { tool_name: 'Read', tool_input: { file_path: '/tmp/x' }, - tool_response: { content: `OPENAI_API_KEY=${openaiProjectKey}${'B'.repeat(300)}${longTail}` } }], - ['post', JSON.stringify([`OPENAI_API_KEY=${openaiProjectKey}${'B'.repeat(300)}${longTail}`]), 'parse_error'] + // A keyword-labeled value is redacted whole, however long it is and whatever + // shaped token sits at its start. + for (const [label, text, expected] of [ + ['long sk-proj key', `OPENAI_API_KEY=${openaiProjectKey}${'B'.repeat(300)}${longTail}`, 'OPENAI_API_KEY=[REDACTED]'], + ['sk- password with a suffix', `password=${'sk-'}${'A'.repeat(30)}.${longTail}`, 'password=[REDACTED]'], + ['GitHub token with a suffix', `token=${'ghp_'}${'0'.repeat(30)}/${longTail}`, 'token=[REDACTED]'] ]) { - test(`redacts a long keyword-labeled sk-proj key completely (${expectedEvent || 'tool_complete'})`, () => { - const records = capture(phase, input, expectedEvent); - assert.ok(!records.includes(longTail), 'key tail persisted'); - assert.ok(records.includes('OPENAI_API_KEY=[REDACTED]')); - }); + for (const [phase, input, expectedEvent] of [ + ['post', { tool_name: 'Read', tool_input: { file_path: '/tmp/x' }, tool_response: { content: text } }], + ['post', JSON.stringify([text]), 'parse_error'] + ]) { + test(`redacts a keyword-labeled ${label} completely (${expectedEvent || 'tool_complete'})`, () => { + const records = capture(phase, input, expectedEvent); + assert.ok(!records.includes(longTail), 'value tail persisted'); + assert.ok(records.includes(expected)); + }); + } } + // A labeled JWT cut inside a payload longer than 256 chars at the capture limit. + test('redacts a keyword-labeled JWT cut inside a long payload', () => { + const labeled = `Authorization: Bearer ${'eyJ'}hbGciOiJIUzI1NiJ9.eyJ${'p'.repeat(600)}${longTail}`; + const records = capture('post', { tool_name: 'Bash', tool_input: { command: 'env' }, + tool_response: `${'a'.repeat(5000 - labeled.length + 5)} ${labeled}` }); + assert.ok(!records.includes('pppp'), 'JWT payload persisted'); + assert.ok(records.includes('Authorization: Bearer [REDACTED]')); + }); // The end-of-capture match only applies to text that was actually cut. for (const output of ['git checkout sk-learn', 'payload eyJhIjoxLCJiIjoyfQ']) { From 0cc28648188d2cd03e0791d93b1b26efbbd85fdb Mon Sep 17 00:00:00 2001 From: Karim Masmoudi <125830216+Kernel-Hunter@users.noreply.github.com> Date: Wed, 4 Nov 2026 12:08:44 +0100 Subject: [PATCH 7/8] fix(continuous-learning-v2): redact PEM blocks before the keyword matcher With a label in front, as in secret=-----BEGIN RSA PRIVATE KEY-----, the keyword matcher replaced only "-----BEGIN" and the PEM pattern could no longer find its opening marker, so the key body was persisted. This was already the case on main. Move the PEM pattern into _PEM_RE and run it first in both the observation path and the parse-error fallback. --- manifests/context-packs/skill-triggers@1.json | 2 +- skills/continuous-learning-v2/hooks/observe.sh | 14 ++++++++++---- tests/hooks/observe-secret-redaction.test.js | 4 +++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/manifests/context-packs/skill-triggers@1.json b/manifests/context-packs/skill-triggers@1.json index 2754f65e9..39101dfb0 100644 --- a/manifests/context-packs/skill-triggers@1.json +++ b/manifests/context-packs/skill-triggers@1.json @@ -10,7 +10,7 @@ "id": "hand-seeded", "source": "manual-curation-pending-regeneration" }, - "registryDigest": "23d2319fbb6e5a75b914fd49baab8f8c1f7aab94513911918f3e71a6f96d0d92", + "registryDigest": "e97d914bd5d871e528da92d905fd768be77b89220a108817f59bac2cbc2f0969", "schemaVersion": 1, "triggers": { "skill:agent-runtime-gateway-smoke-test": [ diff --git a/skills/continuous-learning-v2/hooks/observe.sh b/skills/continuous-learning-v2/hooks/observe.sh index c4cdfff2f..93a115620 100755 --- a/skills/continuous-learning-v2/hooks/observe.sh +++ b/skills/continuous-learning-v2/hooks/observe.sh @@ -290,9 +290,11 @@ _SECRET_RE = re.compile( # before scrubbing; a DSN cut by truncation before its "@" ends at \Z, # except a bare trailing host:port after http(s):// such as http://localhost:3000; # any other scheme (postgres://app:1234) is redacted even when the password is numeric (#3568). +# PEM blocks run first: a label such as secret= in front of the BEGIN marker +# would otherwise let _SECRET_RE eat "-----BEGIN" and hide the block from this match. +_PEM_RE = re.compile(r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)") _SECRET_VALUE_RE = re.compile( - r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)" - r"|(?:(? 2000: @@ -390,9 +393,11 @@ _SECRET_RE = re.compile( # before scrubbing; a DSN cut by truncation before its "@" ends at \Z, # except a bare trailing host:port after http(s):// such as http://localhost:3000; # any other scheme (postgres://app:1234) is redacted even when the password is numeric (#3568). +# PEM blocks run first: a label such as secret= in front of the BEGIN marker +# would otherwise let _SECRET_RE eat "-----BEGIN" and hide the block from this match. +_PEM_RE = re.compile(r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)") _SECRET_VALUE_RE = re.compile( - r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)" - r"|(?:(?= 5000 + val = _PEM_RE.sub("[REDACTED]", val) val = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", val) val = _SECRET_VALUE_RE.sub(lambda m: (m.group(2) or "") + "[REDACTED]", val) return _CUT_TAIL_RE.sub("[REDACTED]", val) if cut else val diff --git a/tests/hooks/observe-secret-redaction.test.js b/tests/hooks/observe-secret-redaction.test.js index 3fef5e30c..51109e1fe 100644 --- a/tests/hooks/observe-secret-redaction.test.js +++ b/tests/hooks/observe-secret-redaction.test.js @@ -195,7 +195,9 @@ if (process.platform === 'win32') { for (const [label, text, expected] of [ ['long sk-proj key', `OPENAI_API_KEY=${openaiProjectKey}${'B'.repeat(300)}${longTail}`, 'OPENAI_API_KEY=[REDACTED]'], ['sk- password with a suffix', `password=${'sk-'}${'A'.repeat(30)}.${longTail}`, 'password=[REDACTED]'], - ['GitHub token with a suffix', `token=${'ghp_'}${'0'.repeat(30)}/${longTail}`, 'token=[REDACTED]'] + ['GitHub token with a suffix', `token=${'ghp_'}${'0'.repeat(30)}/${longTail}`, 'token=[REDACTED]'], + ['PEM private key block', + `secret=-----BEGIN RSA ${'PRIVATE'} KEY-----\n${longTail}\n-----END RSA PRIVATE KEY-----`, 'secret=[REDACTED]'] ]) { for (const [phase, input, expectedEvent] of [ ['post', { tool_name: 'Read', tool_input: { file_path: '/tmp/x' }, tool_response: { content: text } }], From f0f68e6307a3a976738348dca709fa612483e04d Mon Sep 17 00:00:00 2001 From: Karim Masmoudi <125830216+Kernel-Hunter@users.noreply.github.com> Date: Thu, 5 Nov 2026 14:15:21 +0100 Subject: [PATCH 8/8] fix(continuous-learning-v2): redact short-payload JWTs and suffixed sk- keys A JWT with a short claims segment such as e30 ({}) matched neither the full JWT branch nor the cut-tail branch and was saved as is. The payload segment now only needs one character. A generic sk- key followed by "-" or "_" failed its lookahead and was skipped whole. It is now redacted to the end of the token. Branch names like sk-learn-upgrade stay readable because they have fewer than 20 alphanumerics after the prefix. Adds a test that the two pattern copies in observe.sh are identical and that they run in linear time on 300 KB of adversarial input (#2278). --- manifests/context-packs/skill-triggers@1.json | 2 +- .../continuous-learning-v2/hooks/observe.sh | 4 +- tests/hooks/observe-secret-redaction.test.js | 58 ++++++++++++++++++- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/manifests/context-packs/skill-triggers@1.json b/manifests/context-packs/skill-triggers@1.json index 39101dfb0..909217e79 100644 --- a/manifests/context-packs/skill-triggers@1.json +++ b/manifests/context-packs/skill-triggers@1.json @@ -10,7 +10,7 @@ "id": "hand-seeded", "source": "manual-curation-pending-regeneration" }, - "registryDigest": "e97d914bd5d871e528da92d905fd768be77b89220a108817f59bac2cbc2f0969", + "registryDigest": "65b0e26848994515124a8e256e8f2ec996453b98bd89c461407edf3c2d1e3883", "schemaVersion": 1, "triggers": { "skill:agent-runtime-gateway-smoke-test": [ diff --git a/skills/continuous-learning-v2/hooks/observe.sh b/skills/continuous-learning-v2/hooks/observe.sh index 93a115620..fa6ddd7d7 100755 --- a/skills/continuous-learning-v2/hooks/observe.sh +++ b/skills/continuous-learning-v2/hooks/observe.sh @@ -295,7 +295,7 @@ _SECRET_RE = re.compile( _PEM_RE = re.compile(r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)") _SECRET_VALUE_RE = re.compile( r"(?:(? key.txt`, anthropicCanary], ['bare OpenAI project key', `client = OpenAI(${openaiProjectKey})`, openaiCanary], ['bare legacy sk- key', `KEYS = [${legacyKey}]`, legacyCanary], - ['bare JWT', `curl -H "X-Session: ${jwt}" https://api.example.com`, jwtCanary] + ['bare JWT', `curl -H "X-Session: ${jwt}" https://api.example.com`, jwtCanary], + ['bare JWT with empty claims', `curl -H "X-Session: ${emptyClaimsJwt}" https://api.example.com`, jwtCanary], + // Redacted to the end of the token, not skipped because a hyphen follows. + ['bare legacy sk- key with a suffix', `KEYS = [${legacyKey}-CANARYsuffix]`, 'CANARYsuffix'] ]) { test(`redacts ${label} before persisting tool input`, () => { const records = capture('pre', { tool_name: 'Write', @@ -99,6 +104,12 @@ if (process.platform === 'win32') { if (label === 'bare JWT') { assert.equal(capturedField(records, 'content'), content.replace(jwt, '[REDACTED]')); } + if (label === 'bare JWT with empty claims') { + assert.equal(capturedField(records, 'content'), content.replace(emptyClaimsJwt, '[REDACTED]')); + } + if (label === 'bare legacy sk- key with a suffix') { + assert.equal(capturedField(records, 'content'), 'KEYS = [[REDACTED]]'); + } }); test(`redacts ${label} before persisting structured tool output`, () => { const records = capture('post', { tool_name: 'Read', @@ -293,6 +304,51 @@ if (process.platform === 'win32') { for (const line of benign) assert.ok(records.includes(line), `benign text altered: ${line}`); assert.ok(!records.includes('[REDACTED]')); }); + // Both writers carry the same patterns. Run them on 300 KB of adversarial + // input so a pattern that backtracks quadratically (#2278) fails here. + test('scrub patterns stay identical in both writers and linear on 300 KB input', () => { + const hook = fs.readFileSync( + path.join(repoRoot, 'skills/continuous-learning-v2/hooks/observe.sh'), 'utf8'); + const copies = hook.split('_SECRET_RE = re.compile(').slice(1).map((rest) => + `_SECRET_RE = re.compile(${rest.split(/\n(?:import signal|def scrub)/)[0]}` + .replace(/'"'"'/g, "'")); + assert.equal(copies.length, 2); + assert.equal(copies[0], copies[1], 'the parse-error copy of the patterns drifted'); + const driver = String.raw` +import re, sys, time +ns = {"re": re} +exec(sys.stdin.read(), ns) +patterns = [ns[n] for n in ("_PEM_RE", "_SECRET_RE", "_SECRET_VALUE_RE", "_CUT_TAIL_RE")] +n = 300000 +cases = { + "hyphen run": "a-" * (n // 2), + "sk- then hyphen run": "sk-" + "a-" * (n // 2), + "repeated sk-": "sk-" * (n // 3), + "sk- keys followed by a hyphen": (" sk-" + "a" * 40 + "-") * (n // 45), + "eyJ segments": ("eyJ" + "a-" * 10 + ".") * (n // 24), + "repeated eyJ dots": "eyJ." * (n // 4), + "label then hyphens": "token=" + "-" * n, + "repeated labels": "token:" * (n // 6), + "escaped newline before sk-": "\\nsk-" * (n // 5), + "unfinished DSNs": "a://b:" * (n // 6), + "unterminated PEM": "-----BEGIN RSA PRIV" + "ATE KEY-----" + "A" * n, +} +for name, text in cases.items(): + start = time.perf_counter() + for pattern in patterns: + pattern.sub("X", text) + print("%s\t%.3f" % (name, time.perf_counter() - start)) +`; + const result = spawnSync('python3', ['-c', driver], + { input: copies[0], encoding: 'utf8', timeout: 60000 }); + assert.equal(result.status, 0, result.stderr || 'timed out'); + const timings = result.stdout.trim().split('\n'); + assert.equal(timings.length, 11); + for (const line of timings) { + const [name, seconds] = line.split('\t'); + assert.ok(Number(seconds) < 2, `${name} took ${seconds}s`); + } + }); test('keeps a trailing host:port in string output unredacted', () => { const records = capture('post', { tool_name: 'Bash', tool_input: { command: 'npm run dev' }, tool_response: 'listening on http://localhost:3000' });