Skip to content

Sandbox: enforce default-deny egress with an allowlisting proxy - #16

Merged
yifannnwu merged 3 commits into
mainfrom
sandbox-egress-enforcement
Sep 23, 2026
Merged

yifannnwu merged 3 commits into
mainfrom
sandbox-egress-enforcement

Conversation

@zhuokaizhao

Copy link
Copy Markdown
Collaborator

Problem

The sandbox blocked github.com & co. by pointing them at 127.0.0.1 in /etc/hosts. That is a hostname blocklist applied inside a container where the agent is root, and agents routinely got around it — GitHub CDN mirrors (cdn.jsdelivr.net/gh/…, gh-proxy.com, ghfast.top), read-through proxies (r.jina.ai), DNS-over-HTTPS + curl --resolve, LD_PRELOAD resolver shims for git, and web search to locate the task's own PR. With the upstream fix in hand, a trial measures nothing.

Once the network layer was closed, two indirect channels remained through allowlisted hosts:

  • the LLM API used as a web proxy (web-enabled :online models fetching the PR on the agent's behalf, using a key found in the sandbox);
  • the task's own package pulled from npm/PyPI as a newer release that contains the fix.

Fix

Enforcement moves outside the container (enroot backend), default-deny with an explicit allowlist:

  • Network namespace. Each container runs in an unprivileged user+net namespace with only loopback; every exec enters it via nsenter. The single exit is a relay to a host-side proxy over a unix socket the container cannot see. Nothing the agent does as in-container root changes this.
  • One policy (src/egress_policy.py): model catalog, package registries, and only the LLM host the run's backend needs. Known leak vectors are refused with a reason in the 403 body. proxy.golang.org is denied (it serves any GitHub repo verbatim); Go runs with GOPROXY=off from the pre-baked module cache. Tasks can extend the list via task.toml [network] allow, never with leak vectors.
  • Model-pinned LLM route (src/proxies/egress_proxy.py): opencode talks to 127.0.0.1:3128/openrouter/…; the proxy injects the real API key host-side (the sandbox holds a placeholder) and forwards only the run's model — :online variants, plugins, non-function tools and foreign models get 403. No CONNECT to openrouter.ai for OpenRouter runs.
  • Registry TLS interception (src/proxies/egress_ca.py): a per-job CA terminates TLS for registry hosts so the proxy sees paths; the workspace's own package names (scanned from package.json/pyproject.toml/Cargo.toml at trial start) and registry search are denied at every version. Clients that drop the CA bundle fail closed.
  • Verification built in. An in-container self-test runs before turn 0; failure aborts the trial as infra_failed(egress_policy_unenforced). Every proxied connection is logged to agent/egress.log; an allowed connection to a leak vector is infra_failed(egress_policy_violation). run_eval.py refuses leaderboard trial roots on backends without enforcement unless --allow-porous-sandbox is passed; the Slurm launcher preflights namespace support.

docker/e2b remain all-or-nothing (allow_internet) and are documented as such. Trials recorded before this change carry no egress artefacts and are left untouched by the sentinel.

Verification

  • 249 tests pass (tests/test_egress.py: policy table, proxy parsing, live unix-socket proxy, TLS interception with a local upstream, model pinning, sentinel).
  • Measured on the cluster inside the namespace: curl --noproxy '*' → exit 6/7, --resolve host:443:<ip> → exit 7; through the proxy pip/npm/cargo/apt and the pinned model work; own-package tarballs, foreign models and every GitHub route → 403.
  • Smoke + 5-task canary (Go, Rust, npm ×2, pip) completed on the hardened sandbox with all self-tests passing and 0 leak-vector connections allowed.

The task images blocked github.com & co. by appending them to /etc/hosts. That
is a hostname blocklist applied inside a container where the agent is root, and
agents got around it: GitHub CDN mirrors (cdn.jsdelivr.net/gh, gh-proxy.com,
ghfast.top), read-through proxies (r.jina.ai), DNS-over-HTTPS + curl --resolve,
LD_PRELOAD resolver shims for git, search engines to locate the task's own PR.
Once the network layer was closed, two indirect channels remained through
allowlisted hosts: the LLM API used as a web proxy (web-enabled `:online` models
fetching the PR on the agent's behalf, with a key read from the sandbox), and the
task's own package pulled from npm/PyPI as a newer release containing the fix.

Enforcement now lives outside the container (enroot backend):

* Each container runs in an unprivileged user+network namespace holding only
  loopback (`unshare -Urn`; every exec enters it with `nsenter`). The one exit
  is a relay to a host-side proxy over a unix socket the container cannot see.
* `src/egress_policy.py` is the single default-deny allowlist: model catalog,
  package registries, and only the LLM host the run's backend needs (none by
  CONNECT for OpenRouter runs). Known leak vectors are refused with a reason
  the agent sees in the 403 body. `proxy.golang.org` is denied (it serves any
  GitHub repo verbatim); Go runs with GOPROXY=off from the pre-baked module
  cache. Tasks may extend the list via task.toml `[network] allow`, minus leak
  vectors.
* `src/proxies/egress_proxy.py`: CONNECT/absolute-URI forwarding; a model-pinned
  `/openrouter/` reverse route that injects the real API key host-side (the
  sandbox only holds a placeholder; `:online`, plugins, non-function tools and
  foreign models are refused); registry TLS terminated with a per-job CA
  (`egress_ca.py`) so the task's own package names — scanned from the workspace
  manifests at trial start — and registry search are denied at the path level.
* An in-container self-test runs before turn 0 and the trial aborts as
  `infra_failed(egress_policy_unenforced)` if any check fails; every proxied
  connection is logged to `agent/egress.log`, and an allowed connection to a
  leak vector is `infra_failed(egress_policy_violation)`.
* `run_eval.py` refuses leaderboard trial roots (`trials/canonical_*`) on a
  backend without enforcement unless `--allow-porous-sandbox` is passed; the
  Slurm launcher preflights namespace support. docker/e2b remain all-or-nothing
  and are documented as such.

Measured on the development cluster: inside the namespace `curl --noproxy '*'`
exits 6/7 and `--resolve host:443:<ip>` exits 7; through the proxy pip, npm,
cargo, apt and the pinned model work; own-package tarballs, foreign models and
every GitHub route return 403.
Comment thread tests/test_egress.py Fixed
Comment thread tests/test_egress.py Fixed
Several task images (Rust, plain ubuntu) ship no python3. The opencode config
patch was a Python heredoc, so it failed silently there and opencode kept its
default https://openrouter.ai base URL — harmless before (only the explicit
reasoning variants were lost), fatal under egress enforcement, which serves the
model only through the relay's pinned route. The workspace package scan had the
same dependency.

* The config is now patched on the host (`patch_opencode_config`, the same
  logic as the script) and written with a `cat` heredoc.
* The package scan is POSIX sh + find/sed/tr; the environment fails closed if
  it does not complete.
Addresses the CodeQL alerts on the PR: one shared verifying client context
(TLS >= 1.2) for the proxy's upstream connections, the CA trust context and the
tests, and a dict-key assertion that the substring-sanitization query misread.
@yifannnwu
yifannnwu merged commit 891d19e into main Sep 23, 2026
4 checks passed
@zhuokaizhao
zhuokaizhao deleted the sandbox-egress-enforcement branch September 23, 2026 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants