Sandboxed Execution

45 min beginner Lesson 2

Learning Outcomes

  • Explain what sandboxing means in the context of Codex CLI and why it matters
  • Identify the file system boundaries that restrict where Codex can read and write
  • Configure network access controls for shell commands
  • Switch between approval policies (untrusted, on-request, never) and understand their trade-offs
  • Configure sandbox modes (read-only, workspace-write, danger-full-access) for different scenarios
NOTE
Tool Version
This lesson covers Codex CLI v0.136+. The tool is actively developed — check github.com/openai/codex for the latest.

Lesson Plan

Segment Duration Topic
Intro 3 min Why sandboxing matters for AI coding agents
Explain 7 min The sandbox architecture — layers of protection
Demo 1 8 min File system boundaries in action
Demo 2 7 min Network controls — what Codex can and cannot reach
Explain 5 min Approval policies and sandbox modes explained
Demo 3 8 min Switching between policies, seeing differences
Demo 4 4 min What happens when Codex hits a boundary
Wrap-up 3 min Choosing the right settings for your situation

Before You Begin

Pre-work:

Shopping List:

  • Codex CLI installed and working
  • A test project with at least 3-4 files
  • Terminal access with authentication configured
  • A git-initialized project (run git init if needed)

1 Understanding the Sandbox

When you give an AI agent the ability to run shell commands and edit files on your machine, safety becomes critical. Codex CLI addresses this with a multi-layered sandbox that restricts what the agent can do.

The sandbox provides three layers of protection:

Layer What It Controls Default Behaviour
File system Which directories Codex can read/write Current directory and below only
Network Whether commands can access the internet Disabled by default
Approval Whether actions require your confirmation All actions require approval

Why this matters:

Without sandboxing, a coding agent could:

  • Delete files outside your project
  • Install malicious packages
  • Send your code to external servers
  • Modify system configuration files
  • Execute destructive commands (rm -rf /)

Codex CLI's sandbox makes these scenarios impossible by default. Even if the AI model generates a dangerous command, the sandbox prevents execution.

On macOS, Codex uses Apple's native Seatbelt sandboxing framework to enforce file system and network restrictions at the OS level. This means even if a command tries to escape, the operating system blocks it.

On Windows, Codex uses native Windows sandbox or bubblewrap (in WSL2). On Linux, it uses bubblewrap (bwrap) for container-like isolation. The exact mechanism depends on your platform, but the protections are equivalent.

NOTE
How It Works
The sandbox is applied to every shell command Codex executes — not just the ones you see. Even intermediate commands (like checking file existence) run inside the sandbox.

2 File System Boundaries

Codex CLI restricts file operations to your project directory (the working directory where you launched it). This is the most important safety boundary.

What Codex CAN access:

  • All files and subdirectories within your project folder
  • Files you explicitly reference in your prompts
  • Temporary files it creates within the project

What Codex CANNOT access:

  • Files above your project directory (no ../ escape)
  • Your home directory configuration files
  • System files (/etc, /usr, C:\Windows)
  • Other project directories
  • SSH keys, credentials, or secrets outside the project

See it in action:

Try asking Codex to read a file outside your project:

> Show me the contents of ~/.bashrc

Codex will either:

  1. Refuse the request, explaining it's outside the sandbox
  2. Attempt a command that the sandbox blocks, then report the failure

Testing the boundary:

> Create a file at /tmp/test.txt with the content "hello"

This will fail because /tmp is outside your project directory. Codex will report that the operation was blocked by the sandbox.

The writable directory structure:

my-project/          ← Codex launched here (root of sandbox)
├── src/             ← Codex can read and write
├── tests/           ← Codex can read and write
├── docs/            ← Codex can read and write
├── package.json     ← Codex can read and write
└── .git/            ← Codex can read (but shouldn't modify)
WARNING
Watch Out
While Codex can technically write to your .git directory, you should never approve changes to git internals. Let git manage its own directory through normal git commands.
TIP
Tip
If you need Codex to work across multiple directories, launch it from a parent directory that contains all the folders you need. The sandbox boundary is always the launch directory.

3 Network Controls

By default, shell commands executed by Codex CLI have no network access. This prevents commands from downloading malicious payloads, exfiltrating your code, or making unexpected API calls.

Default network behaviour:

Operation Allowed?
Codex talking to OpenAI API Yes (always — this is how it works)
Shell commands accessing the internet No (blocked by default)
curl, wget in commands Blocked
npm install, pip install Blocked (they need network)
git push, git pull Blocked
Reading local files Allowed

When you need network access:

Some tasks legitimately require network:

  • Installing dependencies (npm install, pip install)
  • Fetching remote resources
  • Running tests that call APIs
  • Git operations with remotes

Enabling network access:

You can enable network access by adjusting the sandbox mode in your configuration:

# In config.toml
sandbox_mode = "danger-full-access"

Or via command-line for a single session:

codex --sandbox danger-full-access "Install express and create a basic server"
WARNING
Watch Out
Enabling full network access removes an important safety layer. Only do this when you understand and trust the commands Codex will execute. Prefer enabling it for specific tasks, then switching back.

What happens when network is blocked:

If Codex tries to run a command that needs network while it's disabled:

$ npm install express
npm ERR! network request failed
npm ERR! This is a problem with network connectivity.

Codex will report the failure and may suggest enabling network access or offer an alternative approach.

NOTE
How It Works
Network isolation is enforced at the sandbox level, not by Codex itself. Even if the AI model crafts a clever command to bypass restrictions, the OS-level sandbox blocks the network socket.

4 Approval Policies and Sandbox Modes

Codex CLI uses two independent axes of control: approval policies (what requires human confirmation) and sandbox modes (what the OS allows).

Approval policies:

Policy File Reads File Writes Shell Commands Best For
untrusted Automatic Requires approval Requires approval Learning, sensitive code
on-request Automatic Automatic Requires approval Trusted file edits, faster workflow
never Automatic Automatic Automatic Automated pipelines, trusted tasks

You can also use granular for fine-grained per-tool control (auto, prompt, or approve for each tool type).

Sandbox modes:

Mode Capability Risk Level
read-only Codex can read files but not write anything Safest — exploration only
workspace-write Can write within project directory, no network Default — balanced safety
danger-full-access Unrestricted file and network access Highest risk — use with caution

Setting approval policy:

# Launch with a specific policy
codex --approval-policy on-request

# Or in config.toml
# approval_policy = "on-request"

Setting sandbox mode:

# Launch with a specific sandbox mode
codex --sandbox workspace-write

# Or in config.toml
# sandbox_mode = "workspace-write"
WARNING
Watch Out
The `never` approval policy combined with `danger-full-access` sandbox gives Codex complete autonomy. Only use this combination in throwaway environments, CI/CD pipelines, or when you have git to revert any mistakes.

Switching policies during a session:

The /approval command from older versions is deprecated. To change policies, exit and relaunch with the desired flags or update your config.toml.

TIP
Tip
A good workflow: start with `untrusted` policy and `workspace-write` sandbox to see what Codex plans, then relaunch with `on-request` once you trust its approach for that project.

5 Choosing the Right Settings for Your Task

Knowing when to use each combination is essential for balancing productivity with safety. Here's a decision framework:

Use untrusted + workspace-write when:

  • You're working on production code
  • You're unfamiliar with what Codex might do
  • The task involves destructive operations (deleting files, modifying configs)
  • You're still learning Codex CLI
  • You want to understand the AI's reasoning step by step

Use on-request + workspace-write when:

  • You're doing repetitive refactoring (rename variable across files)
  • You trust the file edits but want to control shell commands
  • You're working in a git repo with a clean state (easy to revert)
  • Speed matters more than reviewing every character

Use never + danger-full-access when:

  • You're in a disposable environment (containers, CI)
  • The task is well-defined and low-risk (generate boilerplate)
  • You're running batch operations you've tested manually first
  • You have comprehensive git history to fall back on

Practical example — choosing settings for different tasks:

# Exploring and understanding code → safest settings
codex --approval-policy untrusted --sandbox read-only "Explain the auth flow"

# Refactoring with trust → faster edits
codex --approval-policy on-request "Add type hints to all Python files in src/"

# Generating boilerplate in fresh project → full autonomy
codex --approval-policy never --sandbox danger-full-access "Create a basic Express app"

Safety checklist before choosing settings:

  1. Is my work committed to git? (If no → stay with untrusted)
  2. Do I understand what Codex will do? (If no → stay with untrusted)
  3. Could the task damage other files? (If yes → stay with untrusted)
  4. Am I in a production environment? (If yes → stay with untrusted)
  5. Is this a repeatable, low-risk operation? (If yes → consider on-request)
NOTE
How It Works
Regardless of approval policy, the sandbox mode always applies independently. Even with 'never' approval, the workspace-write sandbox still prevents writing outside your project directory.
TIP
Tip
When in doubt, start with the defaults (untrusted + workspace-write). You can always relaunch with less restrictive settings once you're confident in what Codex is doing.

Questions & Answers

Q: Can Codex access my environment variables or secrets?
Codex can see environment variables that are set in your shell session. However, the file system sandbox prevents it from reading files like ~/.ssh/id_rsa or ~/.aws/credentials (since those are outside your project directory). Be cautious about .env files within your project — Codex can read those. Add sensitive files to your context exclusion list in config.toml.
Q: What happens if I approve a destructive command by mistake?
If you're in a git repository, you can immediately revert with `git checkout -- .` or `git restore .`. If you're not using git, the changes are permanent. This is the strongest argument for always working in git-initialized projects and committing before starting a Codex session.
Q: Does the sandbox slow down Codex operations?
The sandbox overhead is negligible — typically a few milliseconds per command. You won't notice any performance difference between sandboxed and unsandboxed execution. The time spent is in API calls to OpenAI, not in sandbox enforcement.
Q: Can I customize which directories are writable?
The default sandbox restricts writes to your project directory. The boundary is determined by where you launch Codex. If you need access to multiple directories, launch Codex from a common parent. For advanced setups, you can run `codex sandbox setup --elevated` to configure system-level sandbox permissions.

Key Takeaways

  1. Multi-layered protection: Codex uses file system boundaries, network controls, and approval policies to keep your system safe
  2. File system sandbox: Codex can only read and write within your project directory — never above it
  3. Network disabled by default: Shell commands cannot access the internet unless you set danger-full-access sandbox mode
  4. Two control axes: Approval policies (untrusted/on-request/never) and sandbox modes (read-only/workspace-write/danger-full-access) work independently
  5. Start restrictive: Begin with untrusted policy and relax restrictions only when you're confident
  6. Git is your safety net: Always commit before starting a Codex session so you can revert any mistakes

Next Steps: In Lesson 3 — File Operations, you'll learn how to create, read, edit, and manage files efficiently using Codex CLI.