Sub topic 14
Restricting Network Egress
Egress is traffic that leaves a machine for somewhere else. This chapter covers how to limit what an agent's environment can send out, so an injected instruction has no easy way to deliver stolen data. It assumes you know the earlier chapters on the lethal trifecta (private data, untrusted input, an outbound channel), sandboxing, and least privilege. It also assumes you can read basic Python and run Docker commands.
Why egress is the cheapest link to cut
An injection attack against a coding agent usually needs three things. The agent must read attacker text, reach something worth stealing, and send it somewhere. You often cannot stop the first, because reading issues and docs is the job. You often cannot remove the second, because the agent needs the code. The third is frequently unnecessary. A test run or a refactor rarely needs to talk to arbitrary servers.
Cutting egress does not stop the model from being persuaded. It limits what a persuaded model can do. The attacker's instruction may still arrive, but the data has no path out.
The working model: deny by default
The rule is simple: block all outbound traffic, then add named exceptions. Each exception should answer three questions.
- Which host is needed, and for what task?
- Is that host needed while the agent runs, or only when the environment is built?
- Can anyone, including an attacker, write data to that host?
The strongest setup separates two phases. In the build phase you install dependencies with network access, before any untrusted content is read. In the run phase the agent works with no network, or with a short allowlist.
docker run --rm \
--network none \
--cap-drop ALL \
--read-only \
--tmpfs /tmp \
-v "$PWD":/work \
-w /work \
my-project-test:latest \
python -m pytest -q--network none gives the container only a loopback interface. The image my-project-test:latest is a placeholder. It must already contain pytest and your dependencies, because nothing can be downloaded at run time. This is the build and run split in practice.
Where to enforce the allowlist
A check inside the agent's own tool code is useful, but it is the weakest layer. Enforce at several layers, and prefer the ones the agent cannot change.
| Layer | What it does | Weakness |
|---|---|---|
| Tool wrapper check | Rejects URLs before the agent's fetch tool runs | Covers only that tool. A shell command can bypass it |
| Container or VM network rules | Drops traffic at the sandbox boundary | Needs a proxy if you want per-host rules |
| Forward proxy | Allows only named hostnames and logs every request | Programs that ignore proxy settings fail or bypass it |
| Firewall on the host or network | Blocks everything not approved, outside the agent's control | Rules by IP address break when CDNs change addresses |
For a per-host allowlist, the common design puts the sandbox on an internal network with no route out. The only reachable machine is a proxy, and the proxy applies the allowlist. A process that ignores proxy environment variables then simply cannot connect, which is the safe failure.
A host check that holds up
This is the check for a fetch tool. It permits only HTTPS and exact hostnames.
from urllib.parse import urlparse
ALLOWED_HOSTS = {'pypi.org', 'files.pythonhosted.org'}
def url_allowed(url):
parts = urlparse(url)
return parts.scheme == 'https' and parts.hostname in ALLOWED_HOSTS
assert url_allowed('https://pypi.org/simple/requests/')
assert not url_allowed('http://pypi.org/simple/')
assert not url_allowed('https://pypi.org.evil.example/')
assert not url_allowed('https://evil.example/?next=pypi.org')
assert not url_allowed('https://[email protected]/')
print('all checks passed')The test cases show the usual mistakes. A substring or endswith test accepts pypi.org.evil.example. Checking the whole URL string accepts a query parameter that contains the name. Parsing the URL and comparing the exact hostname avoids both. The user@host form is handled because urlparse reports the real host after the @.
Redirects are a further gap. An allowed host may redirect to a disallowed one. Disable automatic redirects in your HTTP client and run the check on every hop.
What an allowlist does not stop
An allowlist limits destinations, not content. Several gaps remain.
- Allowed hosts that accept writes. If you allow
github.com, an injected agent can open an issue, push a branch, or create a gist containing secrets. The same goes for package registries that accept uploads and cloud storage domains. Prefer narrow hosts (a package mirror, an internal API) over broad platforms. - DNS. A sandbox that blocks connections but still resolves names can leak data in lookups such as
secret-value.attacker.example. Resolve names only through the proxy, or block direct DNS from the sandbox. - The model API. The agent must reach its model provider, and that traffic carries everything in the context. Treat the provider as a trusted party and keep that route separate from the tool sandbox where possible.
- Client-side rendering. If the chat interface renders an image whose URL contains stolen data, the user's own browser makes the request. Sandbox egress rules do not apply. Defend against this by not rendering remote images from agent output, or by proxying them through an allowlist.
- IP literals and alternate schemes. Reject URLs whose host is a raw IP address, and allow only the schemes you expect.
Package installs
Installing packages is the most common reason people open the network. Treat it as a separate step with its own rules. Pin dependencies in a lockfile, install them when you build the image, and allow only the registry hosts you need, if the agent must ever install at run time. Be aware that install scripts execute code, so an attacker who can influence the dependency list can get code running inside the sandbox. Outbound limits reduce what that code can send, but they do not make it safe.
Logging blocks
Log every blocked request with the destination, the time, and the task that triggered it. A blocked request to an unfamiliar host is a useful signal that an injection reached the agent. Review these logs as part of regular monitoring, and test the setup with a harmless request to a host you control that is not on the allowlist. It should fail.
Exercises
Exercise
A developer writes the check 'pypi.org' in url. Give one URL that passes this check but points to an attacker, and say what to use instead.
Show answer
https://evil.example/?ref=pypi.org passes, as does https://pypi.org.evil.example/. Parse the URL with urlparse, require the https scheme, and test whether parts.hostname is in a set of exact allowed hostnames.
Exercise
Your sandbox runs with no network except an allowlist containing github.com. Explain how an injected agent could still exfiltrate a secret, and name one change that reduces the risk.
Show answer
The agent can write the secret to an allowed destination on github.com, such as a new issue, a comment, a gist, or a pushed branch. To reduce the risk, remove github.com from the allowlist and use a narrow mirror or internal endpoint, or give the agent a token that cannot write (read-only scope) and require human approval for any write.