Sandfort / Article

How to restrict network egress from an AI agent sandbox

Put the sandbox on a network that can reach only a forward proxy, and let the proxy decide which hostnames each sandbox may connect to. A hostname allowlist stops connections to unlisted hosts, to internal addresses and to the cloud metadata service, but it does not stop data from leaving through a host you allowed.

Published: 2026-11-24Sandfort 0.1.0

Code that an agent writes can read whatever the sandbox can read: mounted volumes, a cloned repository, its environment. With open outbound internet it can send that anywhere. Inside your own network it can also try internal services and the metadata address. A narrow goal looks like this: the sandbox may reach the model provider, a package index if it installs packages, and nothing else.

This article walks through how Sandfort implements that control, why it picked a proxy over the alternatives, and where the control ends. The reasoning about firewall rules, DNS filters and proxies carries over to most sandbox setups, including one you build yourself. The host rules shown here are specific to Linux Docker hosts with the iptables backend.

Why not a firewall rule or a DNS filter?

The design document (docs/egress-allowlist-design.md) compared three mechanisms before choosing one.

  • Per-sandbox firewall rules by IP. Operators think in names, rules work on addresses. Large providers change addresses often and share CDN addresses with unrelated tenants, so a resolved set goes stale or admits more than intended. The addresses a name resolves to at create time can differ from the ones the sandbox resolves later. Every create, stop and delete edits shared tables, and crash cleanup needs the reconciler to understand firewall state. Not chosen.
  • A DNS-filtering resolver. It answers only allowlisted names. Code can connect to an IP address directly, hard-code an address or ship its own resolver. It controls discovery, not reachability. Dismissed as a control.
  • A forward proxy with per-sandbox credentials. The sandbox network admits only the proxy. The proxy reads the requested hostname, checks it against that sandbox's list and relays or refuses. Software that ignores the proxy variables can reach nothing but the proxy endpoint, so the control fails closed on a host where deploy/host-setup.sh has run. Chosen.

How does traffic get forced through the proxy?

Two pieces work together. Neither is enough alone.

  • A separate bridge. A sandbox created with an allowlist joins sf-egress-net (interface sf-br1) instead of the default sf-net. deploy/host-setup.sh installs the SANDFORT-EGRESS-PROXY chain for that bridge, which rejects everything except replies and the proxy endpoint. A second chain, SANDFORT-HOST-GUARD-PROXY, allows exactly one new connection to the host, tcp to the proxy address and port. This network rule is what blocks direct IP connections, so the design calls it load-bearing.
  • Proxy variables with a signed token. The server sets http_proxy, https_proxy, HTTP_PROXY and HTTPS_PROXY in the sandbox environment (src/sandfort/egress/env.py). The proxy URL carries the sandbox id and an HMAC-signed egress token as userinfo, which clients send as Proxy-Authorization. The token holds the sandbox id and its allowlist, so the proxy needs no database lookup (src/sandfort/egress/tokens.py).

The control fails closed at create time too. If SANDFORT_EGRESS_PROXY_ADDRESS or SANDFORT_EGRESS_SECRET is unset on the API, an allowlisted create is refused with 409 and never runs with open egress. If sf-egress-net is missing on a node, Docker rejects the create (docs/operations.md, "Egress proxy").

What does the proxy check on each connection?

The proxy is a small asyncio process, one per node (src/sandfort/egress/proxy.py). For every connection it first takes a global slot and a per-source slot before authentication (503 or 429 when full), then runs these checks in order. It answers the first failed check with an error status and closes the connection. A connection that errors or times out is dropped without a status.

CheckWhat it doesOn failure
MethodOnly CONNECT is accepted. A plain http:// request through HTTP_PROXY is refused, so use https:// URLs.405, Allow: CONNECT
CredentialVerifies the token signature and expiry, and that the username matches the sandbox id inside the token.407
PortThe target port must be in SANDFORT_EGRESS_ALLOWED_PORTS, default 443,80.403
HostExact match, or the domain and its subdomains under a leading-dot entry.403
Tunnel capAt most 64 open tunnels per sandbox by default.429
ResolutionThe proxy resolves the name itself. Every answer must pass, not just the one it connects to.502 if it does not resolve, 403 for a blocked address
ConnectDials one of the vetted addresses, so a second DNS answer cannot redirect the connection.502

The address check is what stops an allowlisted name from becoming a path into your network. A tenant who controls DNS for an allowed name could point it at 10.0.0.5 or at the node itself. The proxy refuses loopback, link-local and RFC 1918 ranges by default (SANDFORT_EGRESS_DENIED_CIDRS), always adds the node's own interface addresses, and also runs check_address from src/sandfort/webhooks/ssrf.py, which rejects metadata addresses and anything not publicly routable.

Each decision is logged to stderr with the sandbox id and target host, never the credential. Idle tunnels close after 300 seconds by default.

How do you create a sandbox with an allowlist?

Pass egress_allowlist at create time. With the Python SDK:

python
from sandfort_sdk import Client

client = Client(base_url="http://127.0.0.1:8000", api_key="sf_...")
sbx = client.create_sandbox(
    name="pip-only",
    snapshot="python:3.12-slim",
    egress_allowlist=["pypi.org", "files.pythonhosted.org"],
    isolation="gvisor",
    wait=True,
)

This create needs the egress proxy from the section below, and a server that offers the gvisor tier (client.isolation() lists them). Drop isolation to use the server default.

The CLI takes --egress HOST, repeatable, on sandfort sandbox create. The TypeScript SDK takes egressAllowlist on createSandbox. The rules for entries, from the design document, src/sandfort/domain/sandbox.py (check_egress_allowlist) and src/sandfort/egress/proxy_limits.py (host_allowed):

  • Hostnames only. No scheme, port, path or IP literal. At most 16 entries by default (SANDFORT_EGRESS_ALLOWLIST_MAX_ENTRIES).
  • Leading dot for a domain. .example.com admits example.com and every subdomain. example.com admits that name only. There are no other wildcards, patterns or globs.
  • List every host the tool touches. For pip installing from PyPI that is the index host, pypi.org, and the file download host, files.pythonhosted.org, as in the example. Direct URLs, VCS installs and other indexes need their own hosts.
  • An empty list means no network. [] creates the sandbox with block_all set. Leaving the field out keeps the deployment’s default egress. An operator can make a network block the default, or enforce it, and with the enforced setting a create that names an allowlist is refused with 403.
  • No combination with a full block. block_all together with a non-empty allowlist is rejected as contradictory.

The allowlist is fixed for the life of the sandbox. There is no route to change it on a running sandbox.

How do you run the proxy?

From docs/operations.md. Run it where sandboxes can reach it, with the same secret the API uses:

bash
SANDFORT_EGRESS_SECRET=<same secret as the api> \
SANDFORT_EGRESS_LISTEN_HOST=0.0.0.0 SANDFORT_EGRESS_LISTEN_PORT=3128 \
python -m sandfort.egress.proxy

Then point the API at it with SANDFORT_EGRESS_PROXY_ADDRESS (host:port) and run deploy/host-setup.sh as root on each Docker host. Give it the same endpoint with SF_EGRESS_PROXY=host:port. If you leave SF_EGRESS_PROXY unset, the script derives the endpoint from SANDFORT_EGRESS_PROXY_ADDRESS in its env file, else from the gateway of sf-egress-net. The port must match in all three places, or sandboxes cannot reach the proxy. sudo sh deploy/host-setup.sh --dry-run prints the iptables commands without applying them.

At startup the proxy logs its denied networks and a message egress allowed ports: followed by the port list, for example egress allowed ports: 443,80. Read it after every deploy. A proxy left running from an older image keeps its old rules.

What does a hostname allowlist not stop?

This part matters more than the mechanism. Each item comes from the design document or the per-tier notes in docs/isolation-tiers.md.

  • Exfiltration through an allowed host. TLS is tunneled, so the proxy sees the hostname and nothing else. It cannot see paths, bodies or which account the request uses. Allowing a host allows everything that host accepts, including an account an attacker controls on the same service.
  • DNS on the container tier. Names still resolve through Docker's resolver, so a looked-up name can carry data even though the connection is refused. On gvisor and microvm direct lookups fail at once. If that side channel matters for your data, use one of those tiers with the allowlist.
  • The credential is readable. The proxy variables, including the token, are visible to code in the sandbox. The token works only through the proxy and only for that sandbox's own list, so the exposure stays within that list.
  • Content and volume. There is no packet inspection, no TLS interception and no limit on how much goes to an allowed destination.
  • Anything without the host rules. The server cannot read iptables rules, but in prod it tests them at startup. Its egress probe starts a short-lived canary container on each managed network and tries to reach the metadata address, the bridge gateway’s SSH port, a private address and, on sf-egress-net, one public address. Startup is refused if any connect succeeds. SANDFORT_EGRESS_ACKNOWLEDGED=true turns that refusal into a logged warning, so setting it without running the script removes the protection. With the probe off, prod falls back to requiring the acknowledgement, which is the operator’s word and not a check. The script needs the iptables backend, and public IPv6 destinations are not filtered (docs/security.md, Network).

On a host where the script has run, software that ignores the proxy variables gets no network beyond the proxy. That is a cost for some tools, and it is also the intended failure mode.

Limits

The current limits of the allowlist in Sandfort 0.1.0:

  • Hostnames only. No CIDR rules.
  • Set at create. It cannot be changed on a running sandbox.
  • Allowed ports are one list for the whole node (SANDFORT_EGRESS_ALLOWED_PORTS), not per sandbox.
  • Not available in jobs mode, which multi-node placement requires. An allowlist and multi-node placement cannot be combined today.
  • The egress token has a lifetime (7 days by default, SANDFORT_EGRESS_TOKEN_TTL_S, shortened to the auto-delete horizon plus one day when one is set). A sandbox that outlives its token loses proxy access.
  • Allowlisted sandboxes bypass the warm pool.
  • The proxy is a CONNECT proxy by hostname. It is not a firewall and not full egress control.
  • The allowlist does not change the isolation boundary. The container tier shares the host kernel, and gvisor is a user-space kernel, not a hardware boundary. The microvm tier (Kata Containers) needs KVM. A Firecracker driver exists only as a skeleton and is not offered.

The gate script examples/gate8.py runs this path against real Docker: an allowed host is fetched through the proxy, a fetch of an unlisted host fails, and so does a fetch with a wrong proxy password. The script checks that those fetches exit non-zero, not which status the proxy returned (403 and 407, from src/sandfort/egress/proxy.py). It does not exercise direct IP connections, the metadata address or software that ignores the proxy, and it does not run the Linux host chain. For the tiers themselves, see the earlier article Run the code your agent writes on your own hosts, and the comparison page for other ways to run sandboxes on your own hardware.

Sources

Repository paths, read on 2026-11-24. The server is source-available under FSL-1.1-Apache-2.0.

  • docs/egress-allowlist-design.md: mechanisms compared, failure modes, implementation status and proxy limits
  • src/sandfort/egress/proxy.py, proxy_limits.py, env.py, tokens.py: the proxy and its config class, the sandbox environment and the token
  • src/sandfort/config.py: proxy defaults, the entry cap, token lifetime and the startup egress probe setting
  • src/sandfort/domain/sandbox.py: check_egress_allowlist, the entry validation
  • src/sandfort/api/app.py (egress_gaps) and src/sandfort/drivers/docker/egress_probe.py: the startup egress probe
  • src/sandfort/webhooks/ssrf.py: check_address
  • docs/operations.md, "Egress proxy" and "Egress rules on the host": running the proxy and deploy/host-setup.sh
  • docs/isolation-tiers.md, "Network modes and DNS, per tier", also at docs.sandfort.dev/isolation-tiers
  • Security page, Network: docs.sandfort.dev/security
  • sdk/python/src/sandfort_sdk/client.py (create_sandbox), cli/src/sandfort_cli/sandboxes.py (--egress)
  • examples/gate8.py: the live gate for the proxy path