Sub topic 7
Controlling network access and egress
This chapter assumes you can run Docker containers, know what a bridge network is, and have read the earlier chapters on hardening a container. Network control matters because a coding agent that can reach the internet can also send your code and credentials to it.
Why egress is the main concern
Egress is outbound traffic from the sandbox. Inbound traffic is rarely a problem, because a container with no published ports accepts no connections from outside. The risk is the agent, or a prompt-injected instruction hidden in a file or web page, sending data out. It might also download and run something you did not review.
Filesystem scoping limits what the agent can read. Egress control limits where that data can go. You need both.
Three levels of network access
| Level | Docker setting | What the agent can reach | Typical use |
|---|---|---|---|
| None | `--network none` | Only the loopback interface | Running tests and builds that need no downloads |
| Internal only | `--internal` network | Other containers on that network | An agent plus a local database or proxy |
| Filtered | Internal network plus an allowlisting proxy | Only approved hosts | Installing packages, calling a model API |
Start at "none" and add access only when a task fails without it.
No network
The simplest control is to give the container no network at all:
docker run --rm -it --network none python:3.12-slim bashInside, only the loopback interface exists. Any attempt to reach another host fails. Many agent tasks, such as editing code and running unit tests, work fine this way.
An internal network
An internal Docker network lets containers talk to each other but gives them no route to the outside world:
docker network create --internal agent-net
docker run --rm -it --network agent-net python:3.12-slim bashThe Compose equivalent is a network with internal: true, as in the hardened service from the previous chapter.
Allowing only some destinations
Real work often needs a package index or a model API. An internal network alone blocks both. The usual design places a forward proxy between the agent and the internet:
The proxy container sits on two networks: the internal one shared with the agent, and an external one. The agent has no direct route out, so it must use the proxy. You point the agent at the proxy with the standard HTTPS_PROXY variable.
services:
agent:
image: python:3.12-slim
command: ["sleep", "infinity"]
environment:
HTTPS_PROXY: http://proxy:3128
networks:
- sandbox
proxy:
image: ubuntu/squid
volumes:
- ./squid.conf:/etc/squid/squid.conf:ro
networks:
- sandbox
- outside
networks:
sandbox:
internal: true
outside: {}The allowlist lives in squid.conf, which the agent cannot modify because it is not mounted into the agent container.
Common errors
- Trusting the proxy variable alone.
HTTPS_PROXYis a convention. A program can ignore it. The protection comes from the internal network, which leaves no other route. - Allowing a broad domain. Allowing a whole cloud storage or code hosting domain lets the agent upload data to an account the attacker controls. Allow specific hosts.
- Forgetting DNS. DNS queries can carry data in the hostname. On an internal network the agent has no external resolver, which closes this path. Do not add a general resolver back.
- Reaching the host. On the default bridge network, containers can often reach services on the host, such as a local database. Check what is listening, and prefer
--network noneor an internal network. - Cloud metadata endpoints. On a cloud VM, the address
169.254.169.254can return instance credentials. Block it or keep the agent off networks that route to it.
Exercise
An agent container must run pip install from PyPI but must not reach any other site. Which setup do you use, and why is HTTPS_PROXY alone not enough?
Show answer
Put the agent on an internal network with no route out, and add a proxy on both the internal and an external network that allows only the PyPI hosts. Set HTTPS_PROXY in the agent so tools use the proxy. The variable alone is not enough because a program can ignore it and connect directly. The internal network is what removes the direct route.