Sub topic 14
What Hooks Cannot Guarantee
This chapter assumes you can read and write a small Python hook, you know the event-in, exit-code-out contract, and you have written deny patterns for shell commands. Here we look at what those checks cannot promise.
A hook checks a description, not an effect
A pre-action hook sees the text of a proposed tool call. It does not see what that call will do. A pattern match on rm -rf / stops the literal string. It does not stop the same result reached another way.
import re
BLOCKED = r"\brm\s+-[a-zA-Z]*r[a-zA-Z]*f[a-zA-Z]*\s+/(\s|$)"
commands = [
"rm -rf /",
"rm -rf /*",
"rm -fr /",
"find / -delete",
"python3 -c \"import shutil; shutil.rmtree('/')\"",
"bash -c 'rm -r -f /'",
]
for command in commands:
hit = re.search(BLOCKED, command, re.IGNORECASE)
print(f"{'BLOCKED' if hit else 'allowed':8} {command}")Running this shows that only the first command is blocked. The pattern misses the glob, the other flag order, the find form, the Python form, and split flags. Every one of these deletes files.
Common bypass routes
| Route | Example | Why the hook misses it | Column |
|---|---|---|---|
| Rewording | `find / -delete` instead of `rm` | The deny list names commands, not outcomes | |
| Indirection | `bash -c "$(echo cm0gLXJmIC8= \ | base64 -d)"` | The dangerous text never appears in the command |
| Other tools | A file-edit tool writing to `.env` | The shell hook is not matched to that tool | |
| Script contents | Agent writes `cleanup.sh`, then runs `./cleanup.sh` | The hook sees only the script name | |
| Path tricks | `../` segments or symlinks | Raw strings differ from real locations |
Normalizing paths with os.path.realpath, as in the earlier path-protection chapter, closes the last row. The others need different defenses.
Gaps outside the hook
Some failures happen where the hook cannot look:
- Matcher coverage. A hook scoped to the shell tool says nothing about file writes, MCP tools, or tools added later.
- Time of check and time of use. The hook checks a path, then the state changes before the tool runs. A symlink created in between points somewhere new.
- Side effects of allowed commands.
npm installcan run package scripts.makeruns whatever the Makefile says. The hook approved a harmless-looking name. - Hook failure modes. A crash, a timeout, or a misconfigured path can disable the check. Failing closed, as covered earlier, reduces this but does not remove it.
- The agent's own reach. If the agent can edit the hook script or its configuration, it can remove the check.
What a hook is good for
Hooks reliably catch common, well-understood mistakes. They give the agent a readable reason and let you log each decision. They are cheap to write and easy to test. These strengths are real. The limit is the claim of safety, not the usefulness.
Where the guarantee comes from
Real enforcement happens below the agent, at a layer the agent cannot rewrite. Examples are operating system permissions, a container with a read-only mount, a scoped credential, or a network policy. The next chapter covers how to combine these with hooks.
Exercise
A hook blocks any command matching rm -rf /. The agent runs find / -delete and files are removed. Name two reasons the hook failed and one control outside the hook that would have limited the damage.
Show answer
The deny list matched a specific command string, not the outcome, so a different command with the same effect passed. A shell-only matcher or a pattern list that was never extended to find also leaves gaps. A read-only filesystem mount, a container with only the project directory writable, or a non-root user without write access to those paths would have limited the damage regardless of the command used.