Sandboxed Execution
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
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:
- Complete Lesson 1 — Getting Started with Codex CLI
- Have a test project directory ready (preferably git-initialized)
- Understand basic file permissions concepts
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 initif needed)
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.
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:
- Refuse the request, explaining it's outside the sandbox
- 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)
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"
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.
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"
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.
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:
- Is my work committed to git? (If no → stay with
untrusted) - Do I understand what Codex will do? (If no → stay with
untrusted) - Could the task damage other files? (If yes → stay with
untrusted) - Am I in a production environment? (If yes → stay with
untrusted) - Is this a repeatable, low-risk operation? (If yes → consider
on-request)
Questions & Answers
Key Takeaways
- Multi-layered protection: Codex uses file system boundaries, network controls, and approval policies to keep your system safe
- File system sandbox: Codex can only read and write within your project directory — never above it
- Network disabled by default: Shell commands cannot access the internet unless you set
danger-full-accesssandbox mode - Two control axes: Approval policies (untrusted/on-request/never) and sandbox modes (read-only/workspace-write/danger-full-access) work independently
- Start restrictive: Begin with
untrustedpolicy and relax restrictions only when you're confident - 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.