Recipes

Sandboxed AI agent runs with credential injection and snapshot rollback

From README links · today

Security, Science & HardwareSoftware & DevOps

Wrapped Codex-driven agent commands in Zerobox for deny-by-default file

What they did

They wanted to let an agent (Codex) run commands in a repo without giving it blanket access to the filesystem, the network, or their API keys. Instead of reaching for Docker or a VM, they used Zerobox, a lightweight cross-platform sandbox built on Codex's own sandbox runtime. The workflow: first run the agent command with no writes and no network to confirm it starts cleanly (`zerobox -- node agent.js`). Then progressively widen the box — `--allow-write=.` to let it edit the working directory, `--allow-net=api.openai.com` to let it reach only the model endpoint. For credentials, they passed the key with `--secret OPENAI_API_KEY=...` plus `--secret-host OPENAI_API_KEY=api.openai.com`, so the inner process only ever sees a placeholder and the real value is substituted at the network proxy for the approved host. Finally, they wrapped risky steps like `npm install` in `--snapshot --allow-write=.` so filesystem changes were recorded, then inspected with `zerobox snapshot list` and `zerobox snapshot diff <session-id>` and rolled back with `zerobox snapshot restore <session-id>` when something looked wrong.

How it went

The agent ran productively inside a tight box: it could edit the repo and call the one approved API host, but could not exfiltrate the key or touch anything outside the allowed path. Snapshots made `npm install` and other mutating steps reversible, so a bad run cost a restore rather than a re-clone. Overhead was negligible (~10ms), and no Docker or VM was needed.

Worth knowing

macOS and Linux only — Windows support is still planned, so Windows contributors are out of scope. The secret substitution only works for hosts explicitly listed with `--secret-host`; anything else gets the placeholder and fails. Snapshots record filesystem changes, not network side effects or external state, so a restore won't undo a pushed commit or a charged API call. The allowlist model is deny-by-default, which means every new domain or path needs an explicit flag — easy to forget when adding a tool.

Wiring

Install Zerobox via `curl -fsSL https://raw.githubusercontent.com/afshinm/zerobox/main/install.sh | sh`, or `npm install -g zerobox` / `pip install zerobox` / `cargo install zerobox`. The CLI wraps any command: everything after `--` runs inside the sandbox. Order of flags matters only in that each `--secret NAME=value` should be paired with a `--secret-host NAME=host` so the proxy knows where to substitute. Typical invocation: `zerobox --allow-write=. --allow-net=api.openai.com --secret OPENAI_API_KEY=$KEY --secret-host OPENAI_API_KEY=api.openai.com -- node agent.js`. For reversible steps, prepend `--snapshot` (or `--restore` to auto-undo on exit), then use `zerobox snapshot list`, `zerobox snapshot diff <id>`, `zerobox snapshot restore <id>`. For programmatic use, the Rust, TypeScript, and Python SDKs expose the same policy model (writes, network, env, secrets) so the sandbox can be embedded in a CI job or an agent harness rather than shelled out.

Try it yourself

Audit this repo and set up a Zerobox sandbox profile for running our coding agent safely. Steps: 1) Read package.json / pyproject.toml / Cargo.toml to find the agent entrypoint and the external hosts it calls. 2) Write a `sandbox.sh` that runs the agent via `zerobox` with `--allow-write=.` limited to the repo, `--allow-net` for only the model API host(s) you found, and `--secret`/`--secret-host` pairs for any API keys (read names from .env.example, never hardcode values). 3) Add a `--snapshot` variant for dependency installs. 4) Document in README-SANDBOX.md how to list, diff, and restore snapshots. 5) Verify by running the agent once with no network and confirming it fails gracefully, then with the allowlist and confirming it succeeds.

Source

Tools in this stack

Run this stack

Install the stack into this project. It writes AGENTS.md and the manifest, merges the MCP servers into .mcp.json, and — with --yes — runs the tool installs.

npx runchr install runchr/stack-codex-and-zerobox
No CLI?curl -s https://api.runchr.com/api/recipes/runchr/stack-codex-and-zerobox/manifest

This stack has no prompt yet. You get the tools and the wiring, not the prompt.

The manifest is also at api.runchr.com/api/recipes/runchr/stack-codex-and-zerobox/manifest.