Skip to content
AI Skills Central

Type to search Agent Skills, workflows, tutorials, tools and the glossary.

Tutorial

Permissions and sandboxing for coding agents

Set up Claude Code permission modes and rules, the Codex sandbox and approvals, and a dev container, so an agent can work without free rein.

By Updated Intermediate45 min hands-on
Bookmark

Bookmarks are kept in your free account, so you can find this page again on any device.

Sign in to bookmark
Quiz at the end
On this page

A coding agent runs commands on your machine with your permissions. It can read your SSH keys, push to your remote and install packages. Most of the time it does what you asked. The settings in this tutorial are for the other times: a misunderstood task, a prompt injection hidden in a README, a dependency script that does something odd.

There are three layers, and you want all of them:

  1. Permission rules decide which tool calls the agent may make without asking.
  2. An OS sandbox limits what shell commands can touch, whatever the agent intended.
  3. A container or VM limits the blast radius if everything else fails.

The sections below set up each layer in Claude Code and Codex. Facts were checked against both tools’ documentation on 20 November 2026 and again on 25 November 2026 (Claude Code 2.1.332, Codex CLI 0.172.0).

Why “skip all prompts” is risky

Every agent has a flag that turns approvals off: --dangerously-skip-permissions in Claude Code, --yolo (alias of --dangerously-bypass-approvals-and-sandbox) in Codex. The names are honest. With prompts off and no sandbox, a single bad instruction can delete files outside the project, leak credentials over the network or force-push over a colleague’s work, and nothing stops it.

Prompt fatigue is real, though. The answer is to pre-approve the safe, repetitive things and contain the rest, not to switch everything off.

Claude Code: permission modes

The mode decides the default answer to “may I?”. Cycle through them with Shift+Tab, or set one at start-up with --permission-mode.

Mode What runs without asking
default (shown as Manual) Reads in the working directory and a built-in set of read-only commands. Everything else prompts.
acceptEdits Also file edits and common filesystem commands (mkdir, mv, cp) inside the working directory.
plan Reads, plus exploratory commands the auto mode classifier approves when auto mode is available. No edits to your source files until you approve the plan.
auto Reads and edits in the working directory; everything else after a background classifier checks it. Needs a supported model.
dontAsk Only what your allow rules cover. Anything that would prompt is denied. Useful in CI.
bypassPermissions Everything except a few protected actions. Only inside a container or VM.

If nothing sets a mode, interactive terminal and VS Code sessions on Claude Code v2.1.283 or later start in auto where it is available, and in Manual otherwise (permission modes). claude -p and Agent SDK runs behave differently: calls that need approval are denied unless a rule, hook or callback approves them. To set a default for new sessions, put defaultMode in a settings file:

{
  "permissions": {
    "defaultMode": "acceptEdits"
  }
}

Claude Code: permission rules

Rules are more precise than modes. They live in the permissions block of .claude/settings.json (shared), .claude/settings.local.json (yours, git-ignored) or ~/.claude/settings.json (all projects). There are three lists: allow, ask and deny. Deny wins over ask, and ask wins over allow.

A sensible starting point for a Node project:

{
  "permissions": {
    "allow": [
      "Bash(npm run test *)",
      "Bash(npm run lint)",
      "Bash(npm run build)",
      "Bash(git status)",
      "Bash(git diff *)",
      "Bash(git log *)"
    ],
    "ask": [
      "Bash(git push *)",
      "Bash(npm install *)"
    ],
    "deny": [
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(./secrets/**)",
      "Bash(curl *)",
      "Bash(wget *)"
    ]
  }
}

How to read the syntax:

  • Bash(npm run test *): the space before * matters. It matches npm run test with or without arguments, but not npm run testing.
  • Read(./.env) blocks Claude’s file tools from that file. It does not stop a script Claude runs from opening it, which is why the sandbox below matters.
  • A deny rule cannot be carved back open by a narrower allow rule. Bash(curl *) in deny blocks every curl, including one you also allowed.

Edit rules interactively with /permissions, which also shows which file each rule came from. When you answer “Yes, and don’t ask again” to a prompt, Claude Code writes an allow rule for you; review those now and then, because they accumulate.

Claude Code: auto mode and your rules

Because auto mode is now the default for many sessions, check how it treats the rules above:

  • Deny rules still win. They are checked before the classifier and block in every mode, including bypassPermissions.
  • Ask rules still prompt. A content-scoped ask rule such as Bash(git push *) produces a prompt in auto mode; the classifier cannot approve a matching call. That is the recommended way to keep a person in the loop before pushes and pull requests.
  • Broad allow rules are dropped. Entering auto mode removes allow rules such as Bash(*), Bash(python*) and Agent allow rules, so they cannot wave everything past the classifier. Narrow rules such as Bash(npm run test *) stay.
  • By default the classifier lets Claude push to any branch of the repository you are working in, including the default branch. If that is not what you want, the ask rule above is the fix.
  • The classifier’s own settings live in your user settings, not the project’s: autoMode in .claude/settings.json is ignored, so a cloned repository cannot loosen it.

Claude Code auto mode explained covers how the classifier decides, how to describe your trusted infrastructure to it, and what changed in v2.1.319 for sessions behind LLM gateways.

Claude Code: the Bash sandbox

The sandbox wraps every shell command Claude runs in an OS-level boundary: Seatbelt on macOS, bubblewrap on Linux and WSL2. It is off by default. Turn it on with /sandbox, or in settings:

{
  "sandbox": {
    "enabled": true,
    "network": {
      "allowedDomains": ["registry.npmjs.org", "github.com"]
    },
    "allowUnsandboxedCommands": false
  }
}

With it on:

  • Shell commands can only write to the working directory, a temp directory and folders you add.
  • Network traffic goes through a local proxy that only lets through the domains you list. The list starts empty.
  • Sandboxed commands run without a permission prompt, because the boundary contains them. This is where most of the prompt fatigue goes away.
  • allowUnsandboxedCommands: false stops Claude from retrying a blocked command outside the sandbox.

Know the gaps. The sandbox covers shell commands, not Claude’s built-in Read, Edit and WebFetch tools (those follow permission rules), and not hooks or local MCP servers, which run with your full permissions. By default it also allows reads across most of your home directory, including ~/.ssh. Add sandbox.filesystem.denyRead entries for credential folders you care about.

Codex: sandbox modes and approvals

Codex puts the sandbox first. Every command runs inside it, and the approval policy decides when Codex asks to step outside.

Sandbox mode Shell commands can
read-only Read files. No writes, no network.
workspace-write Write inside the workspace and temp directories. Network off unless you enable it.
danger-full-access Anything. No sandbox.
Approval policy Behaviour
on-request Codex asks before doing something the sandbox does not allow.
never Never asks. Anything outside the sandbox fails instead.

In a Git repository Codex starts in its Auto preset, which is workspace-write with on-request approvals. Outside version control it starts read-only. Set your defaults in ~/.codex/config.toml:

sandbox_mode = "workspace-write"
approval_policy = "on-request"

[sandbox_workspace_write]
network_access = false

Or per session:

codex --sandbox workspace-write --ask-for-approval on-request

The sandbox uses Seatbelt on macOS and bubblewrap with seccomp on Linux, and is enforced by the OS, so it holds even if the model is talked into trying something else. Turning on network_access is the most common reason to weaken it; prefer installing dependencies yourself before the session starts.

Scheduled tasks and full access

The Codex app can run tasks on a schedule, and those runs happen without anyone watching. OpenAI’s documentation warns that with full access, “background scheduled tasks carry elevated risk, as ChatGPT may change files, run commands, and access network” without asking, and recommends workspace-write instead. In a Git repository, let scheduled runs use a new worktree so their changes stay apart from your unfinished work (scheduled tasks docs). Claude Code’s cloud scheduled tasks run on Anthropic’s infrastructure in auto mode, inside the cloud environment’s network settings, so review which repositories, connectors and network access each task gets.

Containers: the outer wall

Rules and sandboxes run on your machine, next to your browser sessions, SSH keys and cloud credentials. For long unattended runs, put the agent in a container with nothing valuable in it.

A dev container is the low-friction version. Claude Code ships a Dev Container Feature:

{
  "image": "mcr.microsoft.com/devcontainers/base:ubuntu",
  "features": {
    "ghcr.io/anthropics/devcontainer-features/claude-code:1.0": {}
  },
  "remoteUser": "vscode"
}

Save it as .devcontainer/devcontainer.json and run “Dev Containers: Reopen in Container” in VS Code (or Cursor, JetBrains, Codespaces). Commands run inside the container; your project is bind-mounted, so edits appear on the host. The anthropics/claude-code repository has a fuller reference container with an egress firewall script that only allows the domains Claude Code needs.

Inside such a container you can reasonably use bypassPermissions for unattended work. The CLI refuses that flag when run as root, so keep remoteUser non-root. Codex users can do the same with --sandbox danger-full-access inside a container they control.

What a container does not protect:

  • Your project files. The workspace is mounted from the host, so a bad edit is still a bad edit. Commit before long runs.
  • Anything you mount or pass in. Do not mount ~/.ssh or cloud credential files. Use short-lived, repository-scoped tokens.
  • The agent’s own credentials, stored inside the container. A malicious repository could exfiltrate them if the network is open, which is why the firewall matters.

A setup that works for most teams

  1. Commit a shared .claude/settings.json with allow rules for your test, lint and build commands, ask rules for pushes and installs, and deny rules for secret files.
  2. Turn on the Claude Code sandbox with a short domain allowlist, or keep Codex on workspace-write with network off.
  3. Add a PreToolUse hook for the few commands that must never run (see Claude Code hooks).
  4. Run long or unattended tasks in a dev container with an egress firewall, never on a laptop with bypass mode.
  5. For CI, use dontAsk in Claude Code or --sandbox workspace-write in Codex, with a narrow allow list. Run coding agents headless in CI covers that.

Check your setup

Ask the agent to do something your rules forbid and confirm it is stopped:

print the contents of .env
run curl https://example.com

Both should be refused or prompted, not run. Then ask it to run your tests and confirm that goes through without a prompt. If either check fails, run /permissions (Claude Code) or /status (Codex) to see which settings are actually loaded.

Pitfalls

Setting defaultMode: "auto" in the project’s settings and nothing changes. Claude Code ignores auto (and bypassPermissions) as a default in .claude/settings.json and .claude/settings.local.json, so a repository cannot pick a looser mode for you. Set it in ~/.claude/settings.json or pass --permission-mode.

A Bash(*) allow rule that seems to stop working. Auto mode drops broad allow rules on entry. Replace it with narrow rules for the commands you run often.

An ask rule that a reworded command slips past. Bash(git push *) does not match git -C repo push. Rules match command text; for a check that must hold, add a PreToolUse hook that reads the whole command.

Trusting the sandbox with your SSH keys. The Claude Code sandbox allows reads across most of your home directory by default. Add sandbox.filesystem.denyRead entries for ~/.ssh and cloud credential folders.

Turning on Codex network access to fix one install. It stays on for every later command. Install dependencies before the session, or enable network only in a profile you use for that job.

Running a scheduled agent with full access. Nobody is there to stop a bad command. Use workspace-write and a worktree for scheduled Codex runs, and an ask or deny rule for anything that leaves the repository.

Next steps

Covers

Check your understanding

Permissions and sandboxing quiz

Claude Code permission modes and rules, the Bash sandbox, Codex sandbox modes and approvals, and containers as the outer wall.

7 questions · pass mark 70%

Question 1 of 70 answered
  1. What are the three layers of protection the tutorial recommends?
  • Claude Code hooks: run checks around every tool call

    Write Claude Code PreToolUse and PostToolUse hooks that block destructive shell commands, format every file Claude edits and log each command for later audit.

    TutorialIntermediate40 min

  • Run coding agents headless in CI

    Run claude -p and codex exec in GitHub Actions with pinned versions, narrow permissions, scoped secrets and separate jobs for writing back to the pull request.

    TutorialAdvanced1 h

  • OWASP Top 10 for LLM applications, explained with examples

    The 2026 OWASP Top 10 for LLM applications in plain language, with an agent-era example and a practical fix for each risk, plus what changed from 2025.

    Article

  • Sandbox

    An isolated environment that limits what an agent or its code can read, write or reach on the network, so any mistakes stay contained.

    Glossary

  • Least privilege

    Granting an agent or tool only the permissions and data access its task requires, and nothing more, so a mistake does less damage.

    Glossary

Back to top