Sub topic 15
Layering hooks with permissions and sandboxes
This chapter assumes you can write a pre-action hook that reads a tool-call event from stdin and exits with code 2 to block it. It also assumes you know basic Docker or container usage. The earlier chapters showed that a hook is a script that checks a proposed action. Here you combine that script with two other controls, so one mistake does not cause damage.
Why one layer is not enough
A hook inspects the text of a tool call. It does not see what the command does once it runs. A deny pattern for rm -rf / misses the following command:
python3 -c "import shutil; shutil.rmtree('/home/me/project')"Other bypasses include a script the agent writes and then runs, an encoded command, or a path reached through a symlink you did not anticipate. You can patch each case, but the list never ends. Instead, add layers that fail in different ways.
The three layers
| Layer | Who enforces it | What it checks | Typical failure |
|---|---|---|---|
| Permissions | The agent's own configuration | Which tools and command prefixes may run, and which need approval | Rules are coarse. They match tool names or prefixes, not intent. |
| Hooks | Your script | Command text and paths, with any logic you can write | Text inspection misses indirect actions. A crash or timeout can leave a gap if you do not fail closed. |
| Sandbox | The operating system or container | What the process can actually read, write, and reach on the network | Setup is more work, and a loose mount or shared credential weakens it. |
Permissions are the cheap, broad filter. Check your agent's documentation for the exact syntax of its allow, deny, and ask rules. Hooks add the project-specific rules. The sandbox sets a hard limit that applies even when the first two layers are wrong.
A sandbox that backs up the hook
Suppose your hook protects .env. You can make the same rule true at the OS level by mounting an empty read-only file over it:
docker run --rm -it \
--network none \
--read-only \
--tmpfs /tmp \
-v "$PWD":/work \
-v /dev/null:/work/.env:ro \
-w /work \
python:3.12-slim bashInside this container, the agent sees an empty .env even if it evades the hook. --network none stops a leaked secret from being sent out. --read-only limits writes to the mounted project and /tmp.
Choosing what goes where
- Put broad, stable rules in permissions: which tools may run, and which always need approval.
- Put rules that need logic in hooks: pattern checks, path normalization, audit logs, and messages that tell the agent how to recover.
- Put hard limits in the sandbox: secrets, production paths, network access, and anything where one failure is costly.
Keep the hook fail-closed even when a sandbox exists. The layers should overlap. If you remove one, the others should still hold.
Common errors
- Treating the hook as the only guard because it passed your tests.
- Writing a hook rule with no matching permission or sandbox rule, so a single bug exposes the asset.
- Running the agent with your own full credentials, so the sandbox has nothing to restrict.
- Never testing the sandbox. Run a blocked action inside it and confirm that it fails.
Exercise
Your hook blocks cat .env, but the agent runs python3 -c "print(open('.env').read())". Name one control from a different layer that would stop this, and say why the hook misses it.
Show answer
Mounting an empty read-only file over .env in the sandbox (for example -v /dev/null:/work/.env:ro) stops it, because the process only sees an empty file. The hook misses the command because it matches the text of the shell command, and the file read happens inside the Python interpreter, which the pattern does not describe.