Udemy
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Claude Code Hooks and Guardrails: Block Risky Commands
New

Claude Code Hooks and Guardrails: Block Risky Commands

Write PreToolUse hooks, deny lists and audit logs to stop Claude Code deleting files, leaking secrets or skipping review
Created byAmagi Labs
Last updated 11/2026
English

What you'll learn

  • Write PreToolUse hooks in Python that read the JSON payload and deny, ask or allow a tool call
  • Block destructive shell commands with a deny list kept in data, and test every rule
  • Keep .env files, environment dumps and real API keys out of Claude Code sessions and your logs
  • Log every tool call and find the attempts that were blocked or never ran
  • Block git push until a human reviews the exact commit, and block "done" until the tests run
  • Install hooks per repo or globally, test them with pytest, and run failure drills

Course content

6 sections • 16 lectures • 1h 40m total length
  • Welcome: what we're building5:22

Requirements

  • Claude Code installed, and some time spent using it
  • Python 3.9 or newer, and pytest (pip install pytest)
  • git, for the review-gate lecture and its tests
  • Comfort editing JSON and running terminal commands

Description

This course contains the use of artificial intelligence.

Claude Code runs shell commands, reads files and edits code on your machine. Most days that's the point. But the same session that fixes a bug can also run rm -rf on the wrong folder, open a .env file and send your keys to the model, or push a change nobody has read. This course builds the hooks that catch those moments before the tool runs, and the tests that prove each hook works.

You write six hooks in plain Python, one lecture at a time, and test each one by hand and with pytest. Every demo runs the starter repo in a throwaway project. The live sessions are real Claude Code runs, so you see what the model actually does after a block.

What you get

  • 16 video lectures: a welcome plus 5 modules, 100 minutes in total

  • The starter repo (zip, attached to the first lecture): Python 3.9+ with no third-party packages, six hooks, settings files for Windows and macOS/Linux, and 186 pytest cases

  • A guardrail checklist in the last lecture, in the order to install things

The modules

  1. How hooks work. The five ways an agent hurts you, the hook events and the JSON payload, exit codes, and when to use a permission rule instead of a hook.

  2. Block destructive commands. Your first PreToolUse hook, a deny list for the shell with 14 rules, and a guard for folders Claude must not write to.

  3. Keep secrets secret. A guard for .env files, environment dumps and real key formats, plus redaction so secrets stay out of your own logs.

  4. Audit and review. A log of every tool call, a review gate that blocks git push until you sign off on that exact commit, and a stop gate that won't let Claude say "done" before the tests have run.

  5. Ship it and keep it working. Per-repo and global install, testing hooks with pytest, failure drills where you break things on purpose, and the checklist.

What it doesn't do

  • It doesn't teach Claude Code from zero. You should already have used it.

  • Hooks read command text. They catch honest mistakes, like an eager push at the end of a task. They aren't a wall against someone set on getting around them. Each lecture shows its own gaps, and the course pairs hooks with permission rules, branch protection and short-lived keys.

  • Tested on Windows 11 with Python 3.14. The macOS and Linux settings files are included but haven't been tested on those systems.

How it was made

Written with AI help (Claude) and reviewed by AmagiLabs before release. Narrated by Ricardo, using an AI voice model of his own voice. AmagiLabs isn't affiliated with Anthropic. Claude and Claude Code are Anthropic's names, used here only to say what the course works with.

Who this course is for:

  • Developers who run Claude Code on machines that hold real credentials, real repos or real data
  • Small-team leads who want a tested set of rules before letting Claude Code work on shared code
  • Anyone who has watched Claude Code reach for rm, .env or git push and wanted a check in front of it