# Data Classification Hook

_Reference template. Adapt it to your environment before you use it._

A PreToolUse hook that runs before every file read and shell command. It checks the file about to be read, or the command about to run, against structured patterns. A Restricted match stops the tool call and asks for a human; everything else passes. Deterministic pattern detection, no AI judgment.

## Configuration

Add to your `.claude/settings.json`:

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Read|Bash",
        "hooks": [
          {
            "type": "command",
            "command": "sh .claude/hooks/classify-data.sh"
          }
        ]
      }
    ]
  }
}
```

## Hook Script (`.claude/hooks/classify-data.sh`)

Hook input arrives as JSON on stdin. A `Read` call carries `tool_input.file_path`; a `Bash` call carries `tool_input.command`. Needs `jq`.

```sh
#!/bin/sh
# Exit 0 lets the tool call through. Exit 2 blocks it and shows stderr to the agent.
input=$(cat)
file=$(printf '%s' "$input" | jq -r '.tool_input.file_path // empty')
command=$(printf '%s' "$input" | jq -r '.tool_input.command // empty')

# Restricted: SSNs, card numbers, private keys, API keys with known prefixes
PATTERN='[0-9]{3}-[0-9]{2}-[0-9]{4}|[0-9]{4}[ -][0-9]{4}[ -][0-9]{4}[ -][0-9]{4}|BEGIN [A-Z ]*PRIVATE KEY|AKIA[0-9A-Z]{16}|gh[pousr]_[A-Za-z0-9]{36}|sk-[A-Za-z0-9_-]{20,}'

if [ -n "$file" ] && [ -f "$file" ]; then
  hit=$(grep -E -c -e "$PATTERN" "$file" 2>/dev/null)
  where="$file"
else
  hit=$(printf '%s' "$command" | grep -E -c -e "$PATTERN")
  where="the command"
fi

if [ "${hit:-0}" -gt 0 ]; then
  echo "Data classification: Restricted pattern found in $where. Stop and ask a human before continuing." >&2
  exit 2
fi
exit 0
```

## Classification Levels

| Level | Description | Examples |
|-------|-------------|----------|
| **Public** | No sensitivity | Marketing copy, public docs |
| **Internal** | Business internal | Internal processes, team notes |
| **Confidential** | Sensitive business data | Financial reports, strategy docs |
| **Restricted** | Regulated / PII | SSNs, credentials, deal terms |

## What It Catches (Structured Patterns)
- Social Security Numbers (XXX-XX-XXXX)
- Card-shaped numbers: four groups of four digits (format only, no Luhn check, so expect some false positives)
- API keys and tokens with known prefixes (AWS, GitHub, `sk-`)
- Private key blocks

## What It Does Not Catch (Needs Human Review or More Patterns)
- Email addresses and account numbers; add patterns for the formats your organization uses
- Names and addresses in natural language
- Financial terms embedded in prose
- Contextual PII that regex can't reliably detect

## Key Principle
This is the first pass: structured pattern detection. It catches the high-signal, automatable patterns. Contextual classification requires human judgment or a dedicated review step.
