Sub topic 12
Permissions, approvals, and sandboxes
This chapter assumes you can read the agent loop from the earlier chapters and write basic Python. It covers how a harness limits what an agent can do, because the model's tool calls run with your privileges.
Why the model is not the security boundary
The model only proposes tool calls. The harness runs them. That makes the harness the one place where you can enforce rules. A prompt that says "never delete files" is a request. A check in the harness is a control.
Tools differ in risk:
- Reading files changes nothing.
- Editing files changes the workspace, but version control can undo it.
- Running commands can do almost anything the user account can do, including deleting data or sending it over the network.
Three layers of control
| Layer | Question it answers | Example |
|---|---|---|
| Permission rules | Is this kind of call allowed at all? | Allow `read_file`, deny writes outside the workspace |
| Approvals | Should a human confirm this specific call? | Ask before running `git push` |
| Sandbox | What can the call reach if it runs? | A container with no network and a single mounted directory |
Each layer covers gaps in the others. Rules are cheap but only match what you anticipated. Approvals catch the unexpected but cost the user attention. A sandbox limits damage even when a rule or an approval is wrong.
A permission gate in the harness
This gate sits in front of the tool registry from the earlier chapters. It checks the path and asks for approval on commands. Denials are returned as text, so the model can adjust instead of the loop crashing.
from pathlib import Path
WORKSPACE = Path("/work/project").resolve()
READ_ONLY = {"read_file"}
NEEDS_APPROVAL = {"run_command"}
def inside_workspace(path):
return (WORKSPACE / path).resolve().is_relative_to(WORKSPACE)
def ask_user(name, arguments):
answer = input(f"Allow {name} {arguments}? [y/N] ")
return answer.strip().lower() == "y"
def guarded_call(tools, name, arguments):
if name not in tools:
return f"error: unknown tool {name}"
if "path" in arguments and not inside_workspace(arguments["path"]):
return "error: path is outside the workspace"
if name in NEEDS_APPROVAL and not ask_user(name, arguments):
return "error: the user rejected this call"
return tools[name](**arguments)In run_agent, replace TOOLS[call["name"]](**call["arguments"]) with guarded_call(TOOLS, call["name"], call["arguments"]).
The resolve() call matters. It collapses .. segments and follows symlinks before the check, so ../../etc/passwd is caught.
Common mistakes
- Matching command strings. Blocking the text
rm -rfis easy to bypass, for example withpython -cor a shell variable. Treat command filters as a convenience, not a boundary. - Approval fatigue. If every call asks, users approve without reading. Auto-allow read-only calls and reserve prompts for calls that change state or leave the machine.
- Broad standing approvals. Approving "all
pythoncommands" also approves any script the agent just wrote. - A sandbox with network access. Code can still send workspace files out. Allow only the hosts the task needs.
Choosing a setup
Use the least privilege that finishes the task. For a code review, grant read-only tools. For editing, allow writes inside the workspace and ask before commands. For unattended runs, drop approvals and use a sandbox with no network and no credentials.
Exercise
An agent has run_command auto-approved, and your harness blocks the string curl. Name two ways this fails to stop data from leaving the machine, and one control that would.
Show answer
The agent can use another program that makes network requests, such as python -c "import urllib.request ..." or wget, or can build the command so the string curl never appears. String matching only catches what you listed. A sandbox that blocks outbound network access, or allows only specific hosts, stops the data regardless of which program is used.