Many of the translated audio tracks on this channel are produced with HeyGen.
If you are interested in AI video translation, dubbing, and multilingual content creation, try HeyGen here:
https://bit.ly/4o4bn9P
Signing up through this link helps the channel - thank you!
While lesson 29 showed the PostToolUse that triggers after a tool finishes, lesson 30 is about PreToolUse, which triggers right before execution to prevent the call itself. It stops rm -rf before it goes to the shell and refuses .env edits – a security gateway guaranteed by the system, not the model's judgment.
📌 Topics Covered
Trigger condition: Right before tool execution — can block, modify, or allow the call. The exact opposite of PostToolUse.
Input JSON: Extracting tool_name, tool_input, tool_use_id with jq. No tool_output (since it hasn't run yet).
Two blocking methods: exit 2 + stderr vs exit 0 + permissionDecision JSON. The pitfall: exit 2 blocks, but exit 1 does not.
4 permissionDecision values and permissions system: allow (skips prompt) / deny (cancels) / ask (user confirmation) / defer (SDK only). The Hook only tightens constraints — deny beats bypassPermissions, but allow cannot override a deny rule.
4 Practical Patterns: Blocking dangerous Bash commands (rm -rf, DROP TABLE) / Protecting sensitive files (.env, .git/) / Approval escalation with ask / Input sanitization with updatedInput.
Common pitfalls: exit 2 blocking inversion, JSON is only for exit 0, vulnerability of static Bash argument patterns, shell profiles contaminating JSON output.
📌 Environment Info
Latest Claude Code version (Hooks system, PreToolUse event support)
.claude/settings.json (team sharing), .claude/hooks/*.sh (script files), CLAUDE_PROJECT_DIR environment variable
The if field (argument pattern matching) requires Claude Code v2.1.85+
External tool: jq
📌 Chapters
0:00 What is PreToolUse — the pre-execution checkpoint
0:28 Trigger conditions and comparing with PostToolUse
1:33 Input JSON and extracting fields with jq
2:50 Two blocking methods: exit 2 and exit 0 + JSON
4:19 4 permissionDecision decisions
5:08 Hook and permission system: Hook only tightens
7:26 Narrowing the matcher: matcher and if
8:26 Pattern 1: Blocking dangerous Bash commands
10:02 Pattern 2: Protecting sensitive files
11:30 Pattern 3: Approval escalation with ask
12:27 Pattern 4: Input sanitization with updatedInput
13:42 Common pitfalls and summary
📌 Useful Links
Blog: https://neosarchizo.github.io/posts/c...
Hooks official docs: https://code.claude.com/docs/en/hooks
Hooks guide: https://code.claude.com/docs/en/hooks...
Permissions official docs: https://code.claude.com/docs/en/permi...
Permission modes official docs: https://code.claude.com/docs/en/permi...
Settings official docs: https://code.claude.com/docs/en/settings
Bash command validation example: https://github.com/anthropics/claude-...
jq manual: https://jqlang.org/manual/
#ClaudeCode #PreToolUse #Security