diff --git a/skills/continuous-learning-v2/hooks/observe.sh b/skills/continuous-learning-v2/hooks/observe.sh index a502281fd..2e38a9b47 100755 --- a/skills/continuous-learning-v2/hooks/observe.sh +++ b/skills/continuous-learning-v2/hooks/observe.sh @@ -281,10 +281,13 @@ _SECRET_RE = re.compile( # 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 # 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, +# except a bare trailing host:port such as http://localhost:3000 (#3568). _SECRET_VALUE_RE = re.compile( r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)" - r"|\b(?:gh[pousr]_[A-Za-z0-9]{20,255}|github_pat_[A-Za-z0-9_]{20,255}|(?:AKIA|ASIA)[A-Z0-9]{16})\b" - r"""|(?<=://)([^\s:/@"\\]{1,256}:)[^\s@/"\\]{1,256}(?=@)""" + r"|(?:(? { + const records = capture('post', { tool_name: 'Read', + tool_input: { file_path: '/tmp/example.js' }, tool_response: { content } }); + assert.ok(!records.includes(secret), 'seeded credential 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'; + const dsnCutAt = (offset) => `${'a'.repeat(offset)} postgres://app:${cutSecret}@db.internal/app`; + const dsnPad = (atIndex) => atIndex - ' postgres://app:'.length - cutSecret.length; + for (const [label, phase, input, expectedEvent] of [ + ['string tool output', 'post', { tool_name: 'Bash', tool_input: { command: 'env' }, + tool_response: dsnCutAt(dsnPad(5000)) }], + // 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: dsnCutAt(dsnPad(5000 - 13)) } }], + ['structured tool input', 'pre', { tool_name: 'Write', + tool_input: { content: dsnCutAt(dsnPad(5000 - 13)) } }], + // A top-level array is valid JSON but not a hook payload: parse-error + // fallback, which persists the first 2000 raw chars ('["' prefix). + ['parse-error fallback', 'post', JSON.stringify([dsnCutAt(dsnPad(2000 - 2))]), 'parse_error'] + ]) { + test(`redacts DSN password truncated before '@' in ${label}`, () => { + const records = capture(phase, input, expectedEvent); + assert.ok(!records.includes(cutSecret), 'truncated DSN password persisted'); + assert.ok(records.includes('postgres://app:[REDACTED]')); + }); + } + // Ordinary code, URLs without credentials and SSH remotes stay readable. const benign = [ 'const tokenizer = new Tokenizer(config);', @@ -98,6 +142,12 @@ if (process.platform === 'win32') { for (const line of benign) assert.ok(records.includes(line), `benign text altered: ${line}`); assert.ok(!records.includes('[REDACTED]')); }); + 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' }); + assert.ok(records.includes('http://localhost:3000')); + assert.ok(!records.includes('[REDACTED]')); + }); } console.log(`Passed: ${passed}`); console.log(`Failed: ${failed}`);