Sub topic 6
Blocking Dangerous Shell Commands
This chapter assumes you can read a short Python script, know basic regular expressions, and understand that a pre-action hook receives a tool-call event as JSON on stdin and answers with an exit code. Field names vary by agent, so check your agent's documentation.
Why shell commands come first
The shell tool can do almost anything: delete files, rewrite git history, or change a database. A single string carries all of that power. That makes the command field the most valuable thing to check in a pre-action hook.
The approach is simple. Read the command, compare it with a list of deny patterns, and exit with code 2 if one matches. Exit code 0 lets the command run.
A minimal guard
#!/usr/bin/env python3
import json
import re
import sys
BLOCKED_PATTERNS = [
r"\brm\s+-[a-zA-Z]*r[a-zA-Z]*f[a-zA-Z]*\s+/(\s|$)",
r"\bgit\s+push\b.*--force",
r"\bdrop\s+table\b",
]
event = json.load(sys.stdin)
command = event.get("tool_input", {}).get("command", "")
for pattern in BLOCKED_PATTERNS:
if re.search(pattern, command, re.IGNORECASE):
print(f"Blocked: command matches risky pattern {pattern!r}. Use a safer alternative.", file=sys.stderr)
sys.exit(2)
sys.exit(0)Each pattern targets one kind of damage:
| Pattern | What it catches | Example |
|---|---|---|
| `rm` with `-rf` flags, then `/` | Recursive delete of the filesystem root | `rm -rf /` |
| `git push` with `--force` | Overwriting remote history | `git push origin main --force` |
| `drop table` | Deleting a database table | `psql -c "DROP TABLE users"` |
Three details matter here:
\bmarks a word boundary, sormmatches as a command and not as the end of a longer word.re.IGNORECASEcatchesDROP TABLEanddrop table.- The
rmpattern allows the flags in any order within one group, such as-rfor-fr, because of the[a-zA-Z]*pieces. It does not match separate flags like-r -f.
Choosing patterns well
A good deny list balances two errors.
- A pattern that is too narrow lets harmful variants through.
--forceis caught, but-fongit pushis not. - A pattern that is too broad blocks safe work. Blocking every command containing
rmwould stopgit rm --cached file.txtand many harmless scripts.
Start with commands that are hard to undo: recursive deletes, force pushes, disk formatting, and destructive SQL. Add a pattern when you find a real gap. Keep each pattern readable, and put a comment beside any that is hard to follow.
How the flow works
The message on stderr is what the agent reads after a block. A message that names the rule and suggests an alternative, as in the script above, lets the agent recover without your help.
Common mistakes
- Matching the wrong field. If
tool_inputorcommandis missing from the event,.getreturns an empty string and nothing matches. The hook then allows everything. Later chapters cover failing closed. - Forgetting case. Without
re.IGNORECASE, SQL keywords in lowercase slip past patterns written in uppercase. - Treating the list as complete. A command wrapped in
bash -c, built from variables, or run from a script file can avoid your patterns.
Exercise
A teammate adds the pattern r"\bgit\s+push\b.*--force" and believes force pushes are now impossible. Name one command that still works, and say how you would improve the check.
Show answer
git push -f origin main still works, because the pattern only looks for the long --force option. Extend the pattern to also match -f (and --force-with-lease if you want to block that too), for example r"\bgit\s+push\b.*(--force|\s-f\b)". Also remember that a pattern list cannot catch every way of writing a command, so keep other layers of protection.