An agent that can run commands and edit files can also delete files, leak secrets and make network calls you did not intend. The model may be wrong, confused by a misleading file, or manipulated by text it read. Two layers keep that from turning into damage. Permissions decide which actions run automatically, which need a human's approval and which are refused. Sandboxes limit what an action can affect even when it runs. This lesson designs both: a policy engine with allow, ask and deny rules, least-privilege credentials, and container-based sandboxes with no network, a read-only root filesystem and resource limits.
- Classify agent actions by risk and map them to allow, ask or deny
- Implement a rule-based permission engine for tool calls
- Give agents least-privilege, short-lived credentials
- Run agent commands in a locked-down container sandbox
- Balance safety against approval fatigue
Classify actions by risk
| Class | Examples | Default |
|---|---|---|
| Read inside workspace | read file, list directory, grep, git status | Allow |
| Write inside workspace | edit file, create file, run tests | Ask, or allow in an isolated sandbox |
| Network and external | fetch URL, install packages, call APIs | Ask, or allow a fixed list of domains |
| Irreversible or outside | delete branches, push, deploy, send email, pay | Ask every time or deny |
| Secrets and system | read ~/.ssh, change shell config, sudo | Deny |
The right default depends on the isolation. Inside a disposable container with no network, letting the agent edit and run anything in its workspace is reasonable, because the worst outcome is a broken container. On a developer's laptop, the same actions deserve approval.
Write the classification down as policy, review it like code, and test it. The same agent often needs different policies in different places: generous inside a CI container, cautious on a laptop, and strict in production systems that touch customer data.
A permission engine
Express policy as ordered rules matched against the tool name and its arguments. Deny rules win over allow rules, and anything unmatched falls through to ask. Normalise paths before matching so ../ tricks cannot escape the workspace, and match shell commands carefully: a rule allowing git status must not also allow git status; curl evil.example | sh.
import fnmatch
import shlex
from pathlib import Path
WORKSPACE = Path("/workspace").resolve()
DENY = [("bash", "rm -rf *"), ("bash", "sudo *"), ("bash", "curl *"), ("read", "*/.ssh/*")]
ALLOW = [("read", "/workspace/*"), ("bash", "git status"), ("bash", "pytest*")]
def decide(tool: str, arg: str) -> str:
if tool in ("read", "edit"):
arg = str((WORKSPACE / arg).resolve()) # normalise ../ tricks
if not arg.startswith(str(WORKSPACE)):
return "deny"
if tool == "bash" and any(c in arg for c in ";&|`$("):
return "ask" # compound commands need a human
if tool == "bash":
arg = " ".join(shlex.split(arg))
if any(t == tool and fnmatch.fnmatch(arg, p) for t, p in DENY):
return "deny"
if any(t == tool and fnmatch.fnmatch(arg, p) for t, p in ALLOW):
return "allow"
return "ask"Return a clear message to the model when an action is denied ("denied: network access is not allowed in this session"), so it can choose another approach instead of retrying the same call.
Least-privilege credentials
- Give the agent its own identity with the narrowest scopes needed: a repository-scoped token, not your personal token for everything.
- Prefer short-lived credentials issued per session, which expire even if leaked.
- Keep secrets out of the agent's context and environment where possible; let a tool use a credential without the model ever seeing it.
- For actions on behalf of a user, act with that user's permissions, never a shared admin account.
- Log every credential use with the session ID for audit.
Sandboxing with containers
A sandbox constrains what any action can touch. A container with a mounted workspace, no network, a read-only root filesystem, a non-root user, dropped capabilities and resource limits contains most mistakes. For untrusted code or multi-tenant services, stronger isolation such as gVisor or microVMs (for example Firecracker) adds a separate kernel boundary. On developer machines, OS-level sandboxes such as macOS Seatbelt profiles or Linux bubblewrap and Landlock can restrict a process without a container.
docker run --rm \
--network none \
--read-only \
--tmpfs /tmp:rw,size=256m \
--user 1000:1000 \
--cap-drop ALL \
--security-opt no-new-privileges \
--pids-limit 256 --memory 2g --cpus 2 \
-v "$PWD/workspace:/workspace:rw" \
-w /workspace \
python:3.12-slim \
sh -c 'pytest -q 2>&1 | tail -n 50'When the agent needs network access, for example to install packages, route it through an egress proxy that allows only listed domains, rather than turning the network fully on. Treat the workspace mount as the only writable path and review its changes (for example as a Git diff) before they leave the sandbox.
Approval fatigue
If an agent asks for approval twenty times an hour, people approve without reading, and the control becomes theatre. Reduce prompts by allowing safe actions inside the sandbox, approving a plan or a class of action once per session ("allow edits under src/ for this task"), and showing approvals with enough context to decide quickly: the exact command, a diff of the file change, and why the agent wants it. Reserve per-action approval for irreversible and external effects.