Limit the network of a Docker sandbox
A sandbox of kind docker reaches the whole internet, because codeband sets no network rules. Limit it when an agent reads text that you don’t control, such as a ticket, because it can tell the agent to send your code to another server. Here you put the sandboxes of one definition behind a proxy that lets through only the domains of an allow list. At the end, a shell step gets 200 from pypi.org, a request to example.com fails with CONNECT tunnel failed, response 403, and the proxy log shows that Claude Code connected to api.anthropic.com. If a step fails, see Troubleshooting.
Before you start
Section titled “Before you start”- You did Run a program in the target repository, so
build/codebandand the pipelinepy-factory/run_programexist. - Docker:
docker versionshows aServersection. - An image with curl and Claude Code, here
codeband-sandbox:docker run --rm --entrypoint curl codeband-sandbox --versionprints the version of curl. CLAUDE_CODE_OAUTH_TOKENorANTHROPIC_API_KEYin the daemon’s environment, for the agent step of the check.
Run each command from the codeband folder.
How the parts fit together
Section titled “How the parts fit together”The sandbox containers run on the internal Docker network codeband-sandboxes, which has no route out. The tinyproxy container codeband-proxy is on that network and on Docker’s default network bridge, and forwards only requests to domains of the allow list ~/codeband-proxy/allowed. The definition [sandbox.default] puts each container on the network and sets HTTPS_PROXY, which curl, git, pip, Go and Claude Code read. For a pipeline that names another sandbox definition, put the keys of step 4 there.
-
Make a network without a way out
Terminal window docker network create --internal codeband-sandboxesDocker prints the ID of the network. Your ID is different:
Output 5f8219eebdf2d2ce5e6ab4d23831566025d71e947a7d8b68ab2de5968176e6dc -
Write the allow list
Make a folder with the config file of the proxy and its allow list:
Terminal window mkdir ~/codeband-proxycat > ~/codeband-proxy/tinyproxy.conf <<'EOF'Port 8888Timeout 600LogLevel ConnectFilter "/etc/tinyproxy/allowed"FilterType ereFilterDefaultDeny YesConnectPort 443EOFcat > ~/codeband-proxy/allowed <<'EOF'^api\.anthropic\.com$^pypi\.org$^files\.pythonhosted\.org$EOFWith
FilterDefaultDeny Yes, the proxy answers 403 to a request whose domain matches no line ofallowed. The tinyproxy manual describes the other settings.^and$make a line match the whole domain, so^pypi\.org$lets nopypi.org.example.netthrough. Claude Code needsapi.anthropic.com, and pip needspypi.organdfiles.pythonhosted.org. Add the domains of your model provider and package registries, such asproxy.golang.organdsum.golang.orgfor Go. -
Start the proxy
Build an image with tinyproxy, start it on the internal network, and connect it to
bridgetoo:Terminal window docker build --tag codeband-proxy - <<'EOF'FROM alpine:3.20RUN apk add --no-cache tinyproxyUSER nobodyCMD ["tinyproxy", "-d"]EOFdocker run --detach --name codeband-proxy --restart unless-stopped \--network codeband-sandboxes --volume ~/codeband-proxy:/etc/tinyproxy:ro codeband-proxydocker network connect bridge codeband-proxydocker runprints the ID of the container.-dkeeps tinyproxy in the foreground, so its log goes todocker logs. Check that the proxy is on both networks:Terminal window docker ps --filter name=codeband-proxy --format 'table {{.Names}}\t{{.Status}}\t{{.Networks}}'Output NAMES STATUS NETWORKScodeband-proxy Up Less than a second bridge,codeband-sandboxes -
Point the sandbox definition at the network and the proxy
In
~/.config/codeband/codeband.toml, add the network and the proxy to[sandbox.default]. Make the file if it does not exist, and keep its other tables:~/.config/codeband/codeband.toml [sandbox.default]kind = "docker"image = "codeband-sandbox"[sandbox.default.docker]NetworkMode = "codeband-sandboxes"[sandbox.default.env]HTTPS_PROXY = { value = "http://codeband-proxy:8888" }HTTP_PROXY = { value = "http://codeband-proxy:8888" }NetworkModeputs each container oncodeband-sandboxesonly, where Docker resolves the namecodeband-proxy. The variables reach the agent, its commands and every shell step, as thecodeband.tomlreference lists. Both addresses usehttp://, because a program talks plain HTTP to the proxy.When the steps need no network,
NetworkMode = "none"is simpler and needs no proxy, but an agent step on Claude Code then cannot reach its model provider. -
Start the daemon again
The daemon reads the sandbox definitions when it starts. Stop
codeband devwith Ctrl-C if it runs, and start it inexamples/py-factory:Terminal window just examples/py-factory/devThe daemon logs the config file that it read, then
Listening:Output Nov 29 21:38:21.237 INF Read the config file comp=daemon path=…/.config/codeband/codeband.toml…Nov 29 21:38:21.697 INF Listening comp=daemon socket=…/codeband/daemon.sock -
Request a domain of the allow list
In the first terminal, run curl in a shell step:
Terminal window build/codeband run -v py-factory/run_program --input '{"argv": ["curl", "-sS", "-o", "/dev/null", "-w", "%{http_code}\\n", "https://pypi.org/simple/codeband-sdk/"]}'curl prints only the status code. PyPI answers
200, socodeband runexits with code 0. Your run ID is different:Output Started run ectj2pwmn6▸ program#0 shell step: curl -sS -o /dev/null -w %{http_code}\n https://pypi.org/simple/codeband-sdk/200✓ program#0 exit code 0 after 1.9sno code change -
Request another domain
Terminal window build/codeband run py-factory/run_program --input '{"argv": ["curl", "-sS", "-o", "/dev/null", "-w", "%{http_code}\\n", "https://example.com/"]}'The proxy refuses the tunnel to
example.comwith 403, so curl prints000and exits with code 56, andcodeband runexits with code 3:Output Started run n31k8bnhjz▸ program#0 shell step: curl -sS -o /dev/null -w %{http_code}\n https://example.com/✗ program#0 exit code 56 after 2.0s│ curl: (56) CONNECT tunnel failed, response 403│ 000stopped: the program exited with code 56:curl: (56) CONNECT tunnel failed, response 403000The proxy log names the refused domain:
Terminal window docker logs codeband-proxy 2>&1 | grep refusedOutput NOTICE Nov 29 20:38:46.415 [1]: Proxying refused on filtered domain "example.com"
Check that it worked
Section titled “Check that it worked”Check that an agent step on Claude Code reaches its model provider. Press Ctrl-C in the terminal of codeband dev and start it in examples/hello, whose pipeline hello runs such a step:
just examples/hello/devIn the first terminal, run hello:
build/codeband run hello/hello --patch /tmp/hello.patchcodeband run prints the text of the agent and the path of the patch. Then read the connections of Claude Code in the proxy log:
docker logs codeband-proxy 2>&1 | grep api.anthropic.comCONNECT Nov 29 20:39:22.145 [1]: Request (file descriptor 5): CONNECT api.anthropic.com:443 HTTP/1.1CONNECT Nov 29 20:39:22.145 [1]: Request (file descriptor 6): CONNECT api.anthropic.com:443 HTTP/1.1CONNECT Nov 29 20:39:22.167 [1]: Established connection to host "api.anthropic.com" using file descriptor 7.CONNECT Nov 29 20:39:22.168 [1]: Established connection to host "api.anthropic.com" using file descriptor 8.…Claude Code connected to no other domain in our runs. When an agent step fails, a refused line from the time of its attempt names the domain that the agent or a command needed.
Optional: Allow another domain
Section titled “Optional: Allow another domain”The proxy reads its files when it starts, so add a line to allowed and restart it:
echo '^registry\.npmjs\.org$' >> ~/codeband-proxy/alloweddocker restart codeband-proxydocker restart prints codeband-proxy. The daemon needs no restart. Start codeband dev in examples/py-factory again as in step 5, and request the new domain:
build/codeband run -v py-factory/run_program --input '{"argv": ["curl", "-sS", "-o", "/dev/null", "-w", "%{http_code}\\n", "https://registry.npmjs.org/"]}'The shell step prints 200:
Started run wx4nyxy6fa▸ program#0 shell step: curl -sS -o /dev/null -w %{http_code}\n https://registry.npmjs.org/200✓ program#0 exit code 0 after 1.9sno code changeWhat this setup does not cover
Section titled “What this setup does not cover”| Limit | Detail |
|---|---|
| A program that ignores the proxy variables gets no connection | curl --noproxy '*' https://pypi.org/simple/codeband-sdk/ prints curl: (6) Could not resolve host: pypi.org. curl reads only the lowercase http_proxy for an http:// address. Set the variable that it reads in [sandbox.default.env] |
| An allowed domain is a way out too | The proxy checks only the domain. An agent can send your code to the model provider with another login. Allow only the domains that your steps need, and none where anyone can store data without your login, such as a paste site |
| The run journal records no refused request | A refusal shows only in the proxy log, the output of a shell step and the agent transcript |
Every definition on codeband-sandboxes gets the same list |
For a second list, make another network and proxy |
Clean up
Section titled “Clean up”Remove the proxy, its image, its network and its folder:
docker rm --force codeband-proxydocker image rm codeband-proxydocker network rm codeband-sandboxesrm -rf ~/codeband-proxy /tmp/hello.patchDelete the tables [sandbox.default.docker] and [sandbox.default.env] of step 4, or all of [sandbox.default] if you had none before: without it, default is of kind host. Then stop codeband dev with Ctrl-C.
Troubleshooting
Section titled “Troubleshooting”codeband: start the sandbox: start container: Error response from daemon: network codeband-sandbox not found
Section titled “codeband: start the sandbox: start container: Error response from daemon: network codeband-sandbox not found”NetworkMode names a network that Docker does not have, here with a typo, so the container does not start:
✗ program#0 failed after 0.1s │ codeband: start the sandbox: start container: Error response from daemon: network codeband-sandbox not foundstopped: the program did not run to its end:codeband: start the sandbox: start container: Error response from daemon: network codeband-sandbox not foundCorrect NetworkMode or make the network as in step 1, then start the daemon again.
curl: (5) Could not resolve proxy: codeband-proxy
Section titled “curl: (5) Could not resolve proxy: codeband-proxy”The proxy does not run, and the docker ps command of step 3 prints only its header. Start it with docker start codeband-proxy.
curl: (56) CONNECT tunnel failed, response 500
Section titled “curl: (56) CONNECT tunnel failed, response 500”The proxy is not on bridge, so it reaches no server, and the docker ps command of step 3 shows only codeband-sandboxes. Run docker network connect bridge codeband-proxy.
curl: (56) CONNECT tunnel failed, response 403 for a domain that you allowed
Section titled “curl: (56) CONNECT tunnel failed, response 403 for a domain that you allowed”No line of allowed matches the whole domain: ^pypi\.org$ does not match www.pypi.org. The refused line in docker logs codeband-proxy names the domain. Add a line for it, as in Optional: Allow another domain.
Related
Section titled “Related”- Sandbox explains what a sandbox of kind
dockerreaches. - The
codeband.tomlreference lists the settings of thedockertable. - Run a program in the target repository explains how a shell step runs.
- The tinyproxy manual describes every setting of
tinyproxy.conf.