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-...<dot>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.
This commit is contained in:
Karim Masmoudi committed 2026-11-04 11:56:35 +01:00
1 parent 16a23737f2
commit caeb65b1ee
3 files changed
+33 -20

No files matched your search

@@ -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": [
@@ -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"]:
+24 -11
View File
@@ -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']) {