Prompts · Tes propres outils pour Claude Code
Un hook garde-fou qui bloque ce qui est interdit
Claude écrit un hook qui empêche vraiment une action (fichier protégé, commande dangereuse), le branche dans tes réglages, puis prouve qu'il bloque ce qu'il faut et laisse passer le reste.
Le prompt, en français
4 cases sont vides : elles partiront entre crochets, et Claude te posera la question.
Voir la version anglaise, celle qui est copiée
Create a Claude Code hook that acts as a guardrail.
What it must prevent: [Ce que le hook doit empêcher]
What must stay allowed: [Ce qui doit rester permis]
Scope: [Pour ce projet, pour toi seul, ou pour tous tes projets]
My system: [Ton système]
1. Check the documentation
Read https://code.claude.com/docs/en/hooks and https://code.claude.com/docs/en/hooks-guide before writing: the exact event name, the shape of the setting, what the hook receives as input and how to block. Do not rely on your memory: hooks change often.
2. Choose
- Most of the time, a `PreToolUse` hook (before the action), with a `matcher` on the relevant tools: for example `Edit|Write` for files, `Bash` for commands. Tell me if another event fits better, and why.
- Think about workarounds: a file can be read by the Read tool or by a command (`cat`, `type`, `Get-Content`), and changed by Edit, Write or a command. Cover the ones that matter and tell me which ones you don't cover.
- Also tell me whether a simple permission rule (`deny` in the settings) would be enough instead of a script.
3. Write
- The script in `.claude/hooks/`, preferably in Node.js so it works on Windows, macOS and Linux. It reads the JSON it receives on standard input, normalizes the path (on Windows it arrives with backslashes), compares, then blocks with exit code 2 and a clear message on standard error that tells Claude what to do instead.
- If it cannot read its input, it says so clearly instead of failing silently.
- The setting goes in `.claude/settings.json` (shared project), `.claude/settings.local.json` (this project, just for me) or `~/.claude/settings.json` (all my projects), depending on the scope. For a project hook, the script path uses `$CLAUDE_PROJECT_DIR`.
- Do not touch any other setting: add the hook to what exists, without overwriting anything.
4. Prove it
- Test the script on its own, without Claude, by sending it fake JSON: one case that must be blocked, one allowed case, one exception. Show me the commands and their results.
- Tell me how to see it with `/hooks` and how to debug it (`claude --debug`).
- During the tests, never read the content of a real secrets file.
Done when the script and the setting are written, the three tests give the expected result, and you have told me in two lines what the hook does not cover. Talk to me in French.Comment t’en servir
Ouvre Claude Code à la racine du projet à protéger et colle le prompt. Claude lit d'abord la documentation officielle des hooks, car leurs réglages changent souvent, puis te dit quel événement il choisit et pourquoi.
Il écrit un petit script en Node.js (il marche sous Windows, macOS et Linux), l'ajoute à tes réglages sans écraser ceux qui existent, puis le teste seul avec de faux exemples : un cas bloqué, un cas permis, une exception. Lis le script avant de l'accepter : un hook s'exécute sur ta machine, avec tes droits.
Vérifie ensuite avec /hooks qu'il apparaît bien, et demande à Claude de faire l'action interdite : il doit être arrêté avec le message prévu. La formation « Les hooks : des garde-fous automatiques » explique les événements, le code de sortie 2 et le débogage.
Pour comprendre tout ce que fait ce prompt, et l’adapter : la formation Les hooks : des garde-fous automatiques.