Flag Prompt-Injection Markers From Pasted External Text

Deliberately a warn, not a block: the phrasing shows up constantly in legitimate security research and in users quoting an injection attempt they found elsewhere. This flags it for visibility rather than refusing the prompt outright, which would make the hook actively hostile to the exact security work this catalog is for.

Prompt & Input Safety Event: UserPromptSubmit Claude Code Open source · Free

1. Save this as the hook script

#!/usr/bin/env python3
"""UserPromptSubmit hook — flags (does not block) classic prompt-injection
phrasing in a submitted prompt."""
import json, re, sys

data = json.load(sys.stdin)
prompt = data.get("user_prompt", "")

MARKERS = [r"(?i)ignore (all )?previous instructions", r"(?i)you are now (in )?developer mode"]

if any(re.search(p, prompt) for p in MARKERS):
    print("NOTICE: this prompt contains phrasing commonly seen in prompt-"
          "injection payloads. Not blocked — flagged for visibility.", file=sys.stderr)
    sys.exit(1)

sys.exit(0)

Save to a path inside your project, e.g. .claude/hooks/flag-prompt-injection-markers.sh, then chmod +x it.

2. Wire it into .claude/settings.json

{
  "hooks": {
    "UserPromptSubmit": [
      {
        "hooks": [
          {"type": "command", "command": "python3 .claude/hooks/flag-prompt-injection-markers.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