Sub topic 14
Restricting network egress
This chapter assumes you know what an agent's tool loop is, how indirect prompt injection works, and how to read basic Python and Docker commands. Egress means traffic that leaves your environment. Restricting egress removes the outbound channel from the lethal trifecta. An injected instruction can still reach the agent, but it has no easy route for sending data out.
Why egress matters
An attacker who plants text in an issue or document usually wants data: tokens, environment variables, or source code. The agent has to send that data somewhere. Typical routes are an HTTP request, a package install from an attacker's server, or a URL fetched with secrets in the query string. If the sandbox can only reach a few named hosts, most of these routes fail.
Two layers of control
The cheapest control is no network at all. Many tasks, such as running tests on code already in the repository, do not need it. The container below has none:
docker run --rm \
--network none \
--cap-drop ALL \
--read-only \
--tmpfs /tmp \
-v "$PWD":/work \
-w /work \
python:3.12-slim \
python -m pytest -qIf the agent needs some access, such as installing dependencies, allow only named hosts. Here is a check that permits HTTPS requests to an allowlist:
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_HOSTSThis check works for tools you write yourself. An agent can also call a shell command that makes its own connections. For real enforcement, apply the rule below the agent, in a proxy, a firewall, or the container's network settings. Then the model cannot bypass it.
Common gaps
- Allowed hosts that accept uploads. Code hosting sites, paste services, and cloud storage let anyone store data. If the allowlist includes a host where the attacker can create content, the allowlist is also an exfiltration channel.
- DNS. A blocked HTTP request can still leak data in DNS lookups, such as
secret.attacker.example. Resolve names through a controlled resolver. - Rendered output. An image URL in a comment or chat reply makes the viewer's browser send a request. Egress rules on the sandbox do not cover this, so also limit what the agent may render.
- Redirects. An allowed host may redirect to another host. Check the final destination, or turn redirects off.
- Broad patterns. Allow exact hostnames, not wildcards such as
*.com.
Practical steps
- Start from no network and add hosts only when a task fails.
- Record each allowed host and the reason for it.
- Log blocked requests. A blocked request to an unknown host is a sign that an injection attempt reached the agent.
Exercise
An agent in a container may reach only github.com over HTTPS. Explain how an attacker could still get a secret out, and give one way to reduce the risk.
Show answer
The attacker's injected text can tell the agent to push the secret to a place on github.com that the attacker can read, such as a public gist, a public repository, or an issue comment. The allowlisted host is itself an outbound channel. To reduce the risk, give the agent a token that cannot write to public locations, or limit it to specific repositories, and keep secrets out of the sandbox.