Block Re-Identification Joins Across Two Anonymized Datasets

Two datasets can each be individually anonymized and still be re-identifying once joined — a known, well-documented privacy failure mode. This tracks which anonymized dataset files a session has already touched and flags any later command that references a second, different one, since a join is the likely next step.

Data Privacy & PII Event: PreToolUse Claude Code Open source · Free

1. Save this as the hook script

#!/usr/bin/env python3
"""PreToolUse hook — flags a session touching a second distinct
anonymized dataset file, since combining them can re-identify
individuals. Wire to matcher "Read|Bash"."""
import json, re, sys
from pathlib import Path

data = json.load(sys.stdin)
session_id = data.get("session_id", "unknown")
tool_input = data.get("tool_input") or {}
text = json.dumps(tool_input)

match = re.search(r"([\w/.-]*anonymized[\w/.-]*)", text, re.IGNORECASE)
if not match:
    sys.exit(0)

state_path = Path(data.get("cwd", ".")) / ".claude" / f"anon_datasets_{session_id}.txt"
seen = set(state_path.read_text().splitlines()) if state_path.exists() else set()
dataset = match.group(1)

if seen and dataset not in seen:
    print(f"NOTICE: this session already touched {sorted(seen)} and is now "
          f"touching {dataset} — joining anonymized datasets can re-identify "
          f"individuals even when each one is anonymized alone.", file=sys.stderr)
    sys.exit(1)

seen.add(dataset)
state_path.parent.mkdir(parents=True, exist_ok=True)
state_path.write_text("\n".join(seen))
sys.exit(0)

Save to a path inside your project, e.g. .claude/hooks/block-reidentification-joins.sh, then chmod +x it.

2. Wire it into .claude/settings.json

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Read|Bash",
        "hooks": [
          {"type": "command", "command": "python3 .claude/hooks/block-reidentification-joins.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

← All hooks
FEEDBACK