#!/usr/bin/env python3 """scrub.py -- redact a Claude Code session JSONL, one JSON object per line, in place. Redacts, replacing each with a fixed marker so the shape of the log survives: - the solveathome bearer token, and any prefix of it (the grep patterns typed while checking for leaks re-enter the log, so a literal full-string replace never converges) - the X-Session id issued at registration, and any prefix - Claude Code session / bridge / account / organisation ids (UUID and 24-hex forms) - absolute paths outside the working directory (the home directory) - e-mail addresses Redaction runs on DECODED string values, never on the raw line: a regex applied to the raw JSON text can consume the "n" of a \\n escape and leave a stray backslash behind, which is how this file came to exist. Token-usage lines, tool results and the key structure are left exactly as they were; nothing is rewritten into prose. usage: scrub.py """ import json, re, sys TOKEN, SESSION, SRC, DST = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4] HOME = "~" TOKPRE = re.compile(r'sah_[A-Za-z0-9]{4,}') SESPRE = re.compile(r'\b' + re.escape(SESSION[:8]) + r'[0-9a-f]*') UUID = re.compile(r'\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b') HEX24 = re.compile(r'\b[0-9a-f]{24}\b') EMAIL = re.compile(r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}') SECRET_KEYS = {"ownerAccountUuid", "ownerOrganizationUuid", "bridgeSessionId", "sessionId", "accountUuid", "organizationUuid", "userID", "userId"} def s(t): t = t.replace(TOKEN, "").replace(SESSION, "") t = TOKPRE.sub("", t) t = SESPRE.sub("", t) t = EMAIL.sub("", t) t = HEX24.sub("", t) t = UUID.sub("", t) return t.replace(HOME, "~") def walk(o, key=None): if isinstance(o, dict): return {k: walk(v, k) for k, v in o.items()} if isinstance(o, list): return [walk(v) for v in o] if isinstance(o, str): return "" if key in SECRET_KEYS else s(o) return o n = 0 with open(SRC, encoding="utf-8") as f, open(DST, "w", encoding="utf-8") as g: for line in f: if not line.strip(): continue g.write(json.dumps(walk(json.loads(line)), ensure_ascii=False) + "\n") n += 1 print("lines written: %d" % n, file=sys.stderr)