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).
This commit is contained in:
Karim Masmoudi committed 2026-11-05 14:15:21 +01:00
1 parent 0cc2864818
commit f0f68e6307
3 files changed
+60 -4

No files matched your search

@@ -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": [
@@ -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"(?:(?<![A-Za-z0-9_])|(?<=\\[bfnrt])|(?<=\\u[0-9a-fA-F]{4}))(?:gh[pousr]_[A-Za-z0-9]{20,255}|github_pat_[A-Za-z0-9_]{20,255}|(?:AKIA|ASIA)[A-Z0-9]{16})\b"
r"|(?:(?<![A-Za-z0-9_-])|(?<=\\[bfnrt])|(?<=\\u[0-9a-fA-F]{4}))(?:sk-(?:ant|proj|svcacct|admin|or)-[A-Za-z0-9_-]{20,}|sk-[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]*)"
r"|(?:(?<![A-Za-z0-9_-])|(?<=\\[bfnrt])|(?<=\\u[0-9a-fA-F]{4}))(?:sk-(?:ant|proj|svcacct|admin|or)-[A-Za-z0-9_-]{20,}|sk-[A-Za-z0-9]{20,}[A-Za-z0-9_-]*|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*)"
r"""|(?<=://)(?:(?<=https://)|(?<=http://)|(?<!https://)(?<!http://)())([^\s:/@"\\]{1,256}:)(?(1)|(?!\d{1,5}\Z))[^\s@/"\\]{1,256}(?=@|\Z)"""
)
# A capture cut at its length limit can end inside an sk- key or JWT that the
@@ -398,7 +398,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"(?:(?<![A-Za-z0-9_])|(?<=\\[bfnrt])|(?<=\\u[0-9a-fA-F]{4}))(?:gh[pousr]_[A-Za-z0-9]{20,255}|github_pat_[A-Za-z0-9_]{20,255}|(?:AKIA|ASIA)[A-Z0-9]{16})\b"
r"|(?:(?<![A-Za-z0-9_-])|(?<=\\[bfnrt])|(?<=\\u[0-9a-fA-F]{4}))(?:sk-(?:ant|proj|svcacct|admin|or)-[A-Za-z0-9_-]{20,}|sk-[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]*)"
r"|(?:(?<![A-Za-z0-9_-])|(?<=\\[bfnrt])|(?<=\\u[0-9a-fA-F]{4}))(?:sk-(?:ant|proj|svcacct|admin|or)-[A-Za-z0-9_-]{20,}|sk-[A-Za-z0-9]{20,}[A-Za-z0-9_-]*|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*)"
r"""|(?<=://)(?:(?<=https://)|(?<=http://)|(?<!https://)(?<!http://)())([^\s:/@"\\]{1,256}:)(?(1)|(?!\d{1,5}\Z))[^\s@/"\\]{1,256}(?=@|\Z)"""
)
# A capture cut at its length limit can end inside an sk- key or JWT that the
+57 -1
View File
@@ -68,6 +68,8 @@ if (process.platform === 'win32') {
const legacyKey = `${'sk-'}${legacyCanary}`;
const jwtCanary = 'Q0FOQVJZc2lnbmF0dXJl';
const jwt = `${'eyJ'}hbGciOiJIUzI1NiJ9.eyJzdWIiOiJjYW5hcnkifQ.${jwtCanary}`;
// e30 is base64url for {}, a valid but very short claims segment.
const emptyClaimsJwt = `${'eyJ'}hbGciOiJIUzI1NiJ9.e30.${jwtCanary}`;
for (const [label, content, secret = canary] of [
['double-quoted source', 'const apiKey = "CANARY_TOKEN_abcdefghijk";'],
['single-quoted source', "const password = 'CANARY_TOKEN_abcdefghijk';"],
@@ -89,7 +91,10 @@ if (process.platform === 'win32') {
['bare Anthropic key', `echo ${anthropicKey} > 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' });