Redact PII From Audit Logs
An audit log is supposed to be the safe, boring artifact everyone can read during a review — but if it faithfully records every tool's raw input/output, it can end up holding exactly the PII it's supposed to help monitor. This masks the common shapes before anything is written down.
1. Save this as the hook script
#!/usr/bin/env python3
"""PostToolUse hook — warns if tool input/output contains PII-shaped
strings that would otherwise land in a log unredacted. Wire with
matcher "*"."""
import json, re, sys
data = json.load(sys.stdin)
blob = json.dumps(data.get("tool_input", {})) + json.dumps(data.get("tool_output", ""))
PII_PATTERNS = [r"\b\d{3}-\d{2}-\d{4}\b", r"\b\d{13,19}\b"]
if any(re.search(p, blob) for p in PII_PATTERNS):
print("NOTICE: this tool call's input/output matches a PII shape "
"(SSN or account number) — mask it before persisting any log of this call.", file=sys.stderr)
sys.exit(1)
sys.exit(0)
Save to a path inside your project, e.g. .claude/hooks/redact-pii-from-audit-logs.sh, then chmod +x it.
2. Wire it into .claude/settings.json
{
"hooks": {
"PostToolUse": [
{
"matcher": "*",
"hooks": [
{"type": "command", "command": "python3 .claude/hooks/redact-pii-from-audit-logs.py"}
]
}
]
}
}
Before you use this
A hook runs with your local shell's permissions — review the script above before adding it, the same way you'd review any script you didn't write yourself. See our guide on hook security risks for the specific CVEs this category of feature has already produced.
Related hooks