From cc1da66fd43dd78a81963a71c7983e62e5552574 Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 4 Nov 2026 11:39:43 +0100 Subject: [PATCH] feat(memory): redaction and normalization --- mods/memory/hooks/register.test.ts | 75 ++++++++++++++++++++++++++++++ mods/memory/hooks/register.ts | 55 ++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/mods/memory/hooks/register.test.ts b/mods/memory/hooks/register.test.ts index 7e542bd..99e4fab 100644 --- a/mods/memory/hooks/register.test.ts +++ b/mods/memory/hooks/register.test.ts @@ -1,6 +1,7 @@ import { test, expect, mock } from 'claude-code/testing' import type { Engine } from 'claude-code/testing' import type { On } from 'claude-code' +import { cap, tail, redact, normalize } from './register' const HOME = '/home/t' const SWITCH = `${HOME}/.claude/memory.json` @@ -106,3 +107,77 @@ test('FAILOPEN: a rejecting session.root still returns the answer', const warns = w.logs.filter(l => l.startsWith('memory: Bash hook')) expect(warns).toHaveLength(1) }) + +// Secret-shaped inputs are built at runtime; expectations are literals. +const A = 'A'.repeat(40) +const now = () => + typeof performance === 'undefined' ? Date.now() : performance.now() +const PEM = '-----' + +const REDACT_CASES: [string, string, string][] = [ + ['1 env assignment', 'run X_' + 'TOKEN=abc123 make', + 'run X_TOKEN= make'], + ['1 case-insensitive', 'pass' + 'word=hunter2 x', 'password= x'], + ['2 stripe', 'k ' + 'sk_' + 'live_' + 'a1B2c3D4e5F6 z', + 'k z'], + ['3 aws', 'deploy KEY ' + 'AK' + 'IA' + 'Q'.repeat(16) + ' done', + 'deploy KEY done'], + ['4 github', 't ' + 'gh' + 'p_' + A.slice(0, 36) + ' z', + 't z'], + ['5 slack', 't ' + 'xo' + 'xb-' + '1234567890-abcdef' + ' z', + 't z'], + ['6 sendgrid', + 't ' + 'S' + 'G.' + 'a'.repeat(20) + '.' + 'b'.repeat(20) + ' z', + 't z'], + ['7 supabase', 't ' + 'sb_' + 'secret_' + 'abcdefghij12' + ' z', + 't z'], + ['8 private key', + 'a ' + PEM + 'BEGIN RSA PRIVATE KEY' + PEM + '\nMIIB\n' + + PEM + 'END RSA PRIVATE KEY' + PEM + ' b', + 'a b'], + ['8 unterminated key', + 'a ' + PEM + 'BEGIN PRIVATE KEY' + PEM + '\nMIIB\nmore', + 'a '], + ['9 bearer', 'H: Bearer ' + 'abc.DEF-123_456' + ' z', + 'H: Bearer z'], + ['10 url credentials', 'git clone ' + 'https://' + 'user:pw' + '@host/x', + 'git clone https://@host/x'], +] + +for (const [name, input, expected] of REDACT_CASES) { + test(`redact pattern ${name}`, () => { + expect(redact(input)).toBe(expected) + }) +} + +test('redact keeps a 40-hex SHA', () => { + const sha = 'a'.repeat(20) + '1'.repeat(20) + expect(redact('commit ' + sha)).toBe('commit ' + sha) +}) + +test('redact caps a 70 KiB input at 65536 code units', () => { + expect(redact('x'.repeat(70 * 1024)).length).toBeLessThanOrEqual(65536) +}) + +test('cap keeps the first n, tail keeps the last n', () => { + expect(cap('abcdef', 3)).toBe('abc') + expect(tail('abcdef', 3)).toBe('def') + expect(tail('abc', 0)).toBe('') + expect(tail('abc', 10)).toBe('abc') +}) + +test('normalize collapses paths, hashes, numbers and spaces', () => { + expect(normalize('Error: /home/u/x/y.ts:42 failed')) + .toBe('error: :# failed') + expect(normalize('commit 9a24a62e failed')).toBe('commit failed') + expect(normalize(' a \t b 7 ')).toBe('a b #') +}) + +test('TIME1: redact and normalize stay fast on a 64 KiB run of letters', () => { + const text = 'a'.repeat(65536) + for (const fn of [redact, normalize]) { + const t0 = now() + fn(text) + expect(now() - t0).toBeLessThan(500) + } +}) diff --git a/mods/memory/hooks/register.ts b/mods/memory/hooks/register.ts index 327774d..2f6ebbb 100644 --- a/mods/memory/hooks/register.ts +++ b/mods/memory/hooks/register.ts @@ -18,6 +18,61 @@ const SWITCH_FILE = '.claude/memory.json' const MAX_SWITCH_BYTES = 65536 const TOOL = 'memory_pending' +const MAX_REGEX_INPUT = 65536 +const PEM = '-----' +const PEM_KIND = '(?:[A-Z]+ )?PRIVATE KEY' + +// Redaction patterns, applied in order. Repeats are bounded where the +// input is free text, so no pattern can run away on a 64 KiB input. +const REDACTIONS: [RegExp, string][] = [ + [/\b([A-Za-z_]*(?:TOKEN|API_KEY|SECRET|PASSWORD|PASSWD)[A-Za-z_]*)=\S+/gi, + '$1='], + [/sk_live_[0-9A-Za-z]{10,}/g, ''], + [/AKIA[0-9A-Z]{16}/g, ''], + [/gh[pousr]_[A-Za-z0-9]{30,}/g, ''], + [/xox[abprs]-[A-Za-z0-9-]{10,}/g, ''], + [/SG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/g, ''], + [/sb_secret_[A-Za-z0-9_-]{10,}/g, ''], + [new RegExp( + `${PEM}BEGIN ${PEM_KIND}${PEM}[\\s\\S]*?` + + `(?:${PEM}END ${PEM_KIND}${PEM}|$)`, + 'g', + ), ''], + [/\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/g, 'Bearer '], + [/([a-z][a-z0-9+.-]{0,30}:\/\/)[^\s/@:]+:[^\s/@]+@/gi, '$1@'], +] + +const HEX_RUN = /\b[0-9a-f]{7,}\b/g +const PATH = /(?:\.{0,2}\/)?[\w.-]{1,200}(?:\/[\w.-]{1,200}){1,30}/g + +/** The first `n` code units of `text` (input guard for regex work). */ +export function cap(text: string, n: number): string { + return text.slice(0, n) +} + +/** The last `n` code units of `text` (the end of an output holds the error). */ +export function tail(text: string, n: number): string { + return n <= 0 ? '' : text.slice(-n) +} + +/** Masks secret-shaped text; reads at most the first 64 KiB. */ +export function redact(text: string): string { + let out = cap(text, MAX_REGEX_INPUT) + for (const [pattern, mask] of REDACTIONS) out = out.replace(pattern, mask) + return out +} + +/** A comparison key: volatile parts (hashes, paths, numbers) collapsed. */ +export function normalize(line: string): string { + return line + .toLowerCase() + .replace(HEX_RUN, '') + .replace(PATH, '') + .replace(/\d+/g, '#') + .replace(/\s+/g, ' ') + .trim() +} + function newState(): State { return { enabled: null,