DEV Community

Sandscale
Sandscale

Posted on Originally published at sandscale.dev

Designing an egress allowlist for agent sandboxes

If you only get to add one control around the code your agent writes, make it outbound network policy. Isolation stops the code from damaging the machine it runs on. Egress policy stops it from sending what it found to somebody else. Most of the bad outcomes in an agent incident, leaked tokens, copied source, a call to an attacker's server, need a way out.

This post is about designing that policy: what to allow, how to find out what a task needs, and the traps that make an allowlist weaker than it looks.

Start from zero, not from "everything except"

There are two ways to write network policy. A denylist blocks known-bad destinations and allows the rest. An allowlist refuses everything and opens specific destinations.

For agent code, only the allowlist makes sense. The set of places an attacker might send data is unbounded: any domain they register tomorrow, any paste site, any public Git host, any webhook service. You cannot enumerate it. The set of places a test suite needs to reach is small, and you can enumerate it.

So the default is no network. Then you open what a task needs, for that task, and nothing else.

Find what the task actually needs

Most agent workloads need the network for one of three reasons:

  1. Installing dependencies. Package registries and the hosts they redirect downloads to.
  2. Fetching source. Git hosts, sometimes with separate hosts for archives and large files.
  3. Calling an API the code is about. The service under test, or a model API if the code calls one.

Here are the common public hosts for the first two. Check them against your own logs, because mirrors, proxies and private registries change the picture:

Ecosystem Hosts usually involved
npm registry.npmjs.org
PyPI (pip) pypi.org for the index, files.pythonhosted.org for the files
Go modules proxy.golang.org, sum.golang.org
Rust (cargo) index.crates.io, static.crates.io
GitHub github.com, plus codeload.github.com for archives and objects.githubusercontent.com for release assets

The way to build the real list is to run the task once with logging and no blocking (on a machine with nothing valuable on it), record the hostnames it resolves and connects to, and review that list by hand. Anything surprising is a question to answer before it goes on the allowlist, not after.

A useful habit: write the reason next to each entry. "registry.npmjs.org: npm install in the test step" is reviewable. A bare hostname six months later is not.

Better still: install before the agent runs

The cleanest egress policy for the agent's own code is none at all. If you can install dependencies in a setup step, from a lockfile, before any model-written code runs, then the phase where untrusted code executes can have the network off entirely.

That split matters because install scripts are code too. npm install runs lifecycle scripts from every package, and pip install from a source distribution runs its build. If the agent added a dependency, that dependency's install script runs with whatever network the install step has. Installing from a reviewed lockfile, in a separate step, keeps the window small.

The traps

Wildcards on shared domains

*.githubusercontent.com looks like "GitHub's files". It also covers content anyone can upload. The same goes for broad wildcards on cloud storage, CDNs and serverless platforms: *.amazonaws.com lets code send data to a bucket an attacker owns. Prefer exact hostnames. When you must use a wildcard, ask who can create a name under it.

IP allowlists behind CDNs

Allowing an IP range because a service resolved to it last week breaks in two ways. The service moves, and your runs fail. Or the range is a shared CDN, and you have just allowed every other customer of that CDN. Hostname-based policy, enforced by something that sees the hostname (the TLS SNI or an HTTP proxy's CONNECT line), is usually the better fit for public services.

DNS as a side channel

Even with all TCP blocked, a resolver that answers any query lets code leak data in the query itself: secret-value.attacker.example reaches the attacker's name server through your resolver. If your sandbox can resolve arbitrary names, it has a slow but real way out. Resolve only the allowed names, or resolve on the policy side and refuse the rest.

Allowed hosts that accept writes

An allowlist entry is permission to talk to a host, not permission to read from it. If the allowlist includes github.com and the run holds a token, code can push to a repository, open an issue or post a gist. If it includes a model API and the run holds a key, code can send your source to it as a prompt. Pair every allowed host with the question: what can this run write there, with the credentials it has?

The allowlist that only grows

Every failed run tempts someone to add a host. Six months later the list is a denylist in disguise. Review it on a schedule, and remove entries that no recent run used.

Make it per task, not per company

One organization-wide allowlist is better than none, but it accumulates the union of every task's needs. A test runner for a Go service does not need PyPI. A data-analysis run does not need GitHub. If your platform lets you set network access per run or per project, use the narrowest policy that lets that kind of task finish.

A reasonable starting set of profiles:

  • No network: running model-written code against files you put in. Analysis, transforms, unit tests with vendored dependencies.
  • Registry only: the install step for one ecosystem.
  • Source fetch: cloning one Git host, read-only credentials or none.
  • Named API: one external service the code is about, with a scoped key.

Most runs should land in the first profile.

What to log

Whatever enforces the policy should record refused connections with the run that tried them. A refused request to an unknown host from a test step is the most useful security signal an agent platform produces: it is either a missing allowlist entry or an injection attempt, and either way a person should look.

How this works on Sandscale

On Sandscale, each sandbox is a Firecracker microVM, and Firecracker itself does not filter traffic, which is why network policy is a separate layer. A sandbox can reach only what your organization's egress allowlist allows. The security page lists network access as allowlist on Free and open or allowlist on paid plans, so you choose how tight to make it.

Network policy limits where data can go. It does not make a secret safe once it is inside the run: code in the sandbox can read any secret you give it. Pair a tight allowlist with short-lived, narrowly scoped credentials, as described in our post on keeping secrets out of agent prompts.

Start with nothing open. Add hosts one at a time, each with a reason. That is most of the work.

Originally published on the Sandscale blog.

Top comments (0)