techlist.io

Appcontainer

1 posts

OpenAI2026-05-125 min readCurated summary

Building a safe, effective sandbox to enable Codex on Windows

# Building a Safe, Effective Sandbox for Codex on Windows OpenAI's Codex coding agent had no sandbox on Windows, forcing users to either approve nearly every command or disable restrictions entirely. The author's team built a custom sandbox because none of the existing Windows isolation tools fit an agent that needs to work like a real developer. The final design, which requires elevation at setup time, combines restricted tokens, dedicated sandbox users, firewall rules, and a separate command-runner binary. The author concludes that Windows offers no single primitive for a safe autonomous coding agent, so the solution had to be composed from several parts. ## Why Existing Windows Tools Fell Short - **AppContainer** offers a real OS isolation boundary, but it is built for apps that know their exact access needs in advance. Codex runs open-ended workflows such as shells, Git, Python, and package managers, which makes AppContainer a poor fit. - **Windows Sandbox** provides a strong, disposable VM with broad software compatibility. However, Codex needs to act directly on the user's real checkout and tools, and Windows Sandbox is unavailable on Windows Home editions. - **Mandatory Integrity Control (MIC)** labeling could allow a non-admin, low-integrity sandbox. However, relabeling a workspace as low integrity lets any low-integrity process write there, which turns the user's real checkout into a broadly writable target and changes the trust model in a way that is hard to contain. ## First Prototype: The Unelevated Sandbox The first prototype aimed to run without administrator privileges and limited two things: file writes and network access. ### Limiting File Writes - A synthetic SID named `sandbox-write` was created for the sandbox. - That SID received write, execute, and delete access to the current working directory and any `writable_roots` set in `config.toml`. - It was explicitly denied write access to read-only subpaths such as `<cwd>/.git`, `<cwd>/.codex`, and `<cwd>/.agents`. - Commands launched under a write-restricted token whose restricted SID list included `Everyone`, the logged-in session SID, and `sandbox-write`. A write succeeds only if both the user identity and a restricted SID are granted access. ### Limiting Network Access - Windows Firewall could not be used without admin rights, so network control was advisory. - The sandbox set environment variables such as `HTTPS_PROXY`, `ALL_PROXY`, and `GIT_HTTPS_PROXY` to point at a dead endpoint (`127.0.0.1:9`), and set `GIT_SSH_COMMAND` to fail. - A `denybin` directory with stub SSH and SCP scripts was prepended to `PATH`, with `PATHEXT` reordered so the stubs resolved first. - This blocked common tool traffic but could be bypassed by programs that ignored the variables or opened sockets directly. ### Tradeoffs The prototype was lightweight and granular and avoided elevation prompts. Its drawbacks were: - Applying workspace ACLs could be slow, depending on directory structure. - It modified real ACLs on the developer's system, though only for the sandbox's own SID. - Changing sandbox semantics required slow ACL updates, unlike macOS, where the Seatbelt profile can be regenerated. - Network protection was weak and not designed to resist adversarial code. ## Redesign: The Elevated Sandbox Windows Firewall rules could not target only the commands the agent spawns. They could match a specific binary, but that would also affect `codex.exe` and other copies of the same program. They also could not match a restricted token's SID, and user-scoped rules applied to the real user. Isolating network access therefore required running sandboxed commands as a separate Windows principal, which meant accepting an elevation requirement at setup. ### Two Sandbox Users - Commands run under a restricted token whose principal is one of two local users created by Codex: `CodexSandboxOffline`, which firewall rules block from all outbound network access, and `CodexSandboxOnline`, which is not restricted. ### A Dedicated Setup Step Setup now needs to: - Create the synthetic SID and the two sandbox users if they do not exist. - Store the users' credentials encrypted with the Windows Data Protection API (DPAPI), in a location the sandbox users cannot read. - Create or validate firewall rules that block all outbound traffic for `CodexSandboxOffline`. - Grant read ACLs to the sandbox users on common directories such as `C:\Users\<real-user>`, `C:\Windows\`, `C:\Program Files\`, `C:\Program Files (x86)\`, and `C:\ProgramData\`. Because the sandbox user is a different principal, it cannot otherwise read other users' profile directories. This step runs asynchronously so it does not block the user. - Live in a separate binary, `codex-windows-sandbox-setup.exe`. This crosses the UAC boundary only when needed and keeps `codex.exe` a normal unelevated process. ### A Separate Command Runner The first approach, in which `codex.exe` logged on as the sandbox user, created a restricted token, and spawned the child, failed at `CreateProcessAsUserW(...)` because of a privilege restriction. The fix was a new binary, `codex-command-runner.exe`: - `codex.exe` launches the runner as the sandbox user with `CreateProcessWithLogonW(...)`, without a restricted token. - Inside the runner, `OpenProcessToken(GetCurrentProcess(), ...)` gets the sandbox user's token, and `GetTokenInformation(...)` extracts the logon SID. - The runner calls `CreateRestrictedToken(...)` and then `CreateProcessAsUserW(...)` to launch the actual child process. ### Final Architecture 1. `codex.exe`, the unelevated harness 2. `codex-windows-sandbox-setup.exe`, for elevated setup 3. `codex-command-runner.exe`, for launching restricted-token commands 4. The child process ## Lessons - Windows did not provide one primitive for a safe autonomous coding agent, so the design combines several mechanisms. - Some early approaches were dead ends, and the final design is a hybrid of earlier prototypes. - Coding agent security differs from classic application security. The work required balancing compatibility with real developer workflows against actual enforcement.