Sandbox: allow outbound connections to localhost
Problem
The sandbox blocks outbound TCP connections to localhost/127.0.0.1/::1 even when these are listed in sandbox.network.allowedDomains. The sock.connect() syscall gets EPERM (Operation not permitted).
This makes it impossible to run integration tests against local Docker services (e.g. http://localhost:8000) from within the sandbox.
What I've tried
In settings.json:
"sandbox": {
"enabled": true,
"network": {
"allowLocalBinding": true,
"allowedDomains": [
"localhost",
"127.0.0.1",
"::1"
]
}
}
None of these settings allow the outbound connection. The only workaround is either dangerouslyDisableSandbox: true on every bash call, or adding the command to excludedCommands, both of which defeat the purpose of the sandbox.
Requested feature
A sandbox setting that allows outbound TCP connections to loopback addresses, e.g.:
"sandbox": {
"network": {
"allowLocalhostConnections": true
}
}
Or have allowedDomains with localhost/127.0.0.1/::1 actually allow the TCP connection, not just DNS resolution.
Use case
Running integration tests against services in local Docker containers. For example, starting a service via docker compose and then running integration tests that hit http://localhost:8000.
Showing cached comments. Read the full discussion on GitHub ↗
6 Comments
I'm hitting this as well, would really like to be able to hit local servers with claude code, and stay inside the sandbox.
Same problem here
On MacOS, there's
network.allowLocalBinding: https://code.claude.com/docs/en/settings#sandbox-settingsWould be nice to have fine-grained per port restrictions though.
Cannot believe this hasn't been fixed yet. It's impossible to do local development if required to test things on the local webapp, etc.
The problem is that claude is running inside an isolated network namespace, so claude's localhost and the real localhost are different. Claude has a proxy to access resources outside of it's network namespace (that's how
allowedDomainsworks). Unfortunately, you almost never want proxy requests to localhost, except in this case.So the easiest fix to start all commands with
no_proxy=""(for example:NO_PROXY="" no_proxy="" ./run-tests.shorcurl --no-proxy "" localhost:1234also works). Unfortunately, I am not sure what the proper way to fix this is. settings the following claude config:Also doesn't seem to work because this setting gets either ignored or overwritten once it reaches the sandbox. Even claude itself has a hard time understanding the problem. I've just updated my skills to use curl --no-proxy "" if it's running inside of a sandbox and wants to access localhost).
This issue also blocks
bin/rails testconnections to PostgreSQL (which is listening on localhost).Basically this sandbox is completely useless without a way to connect to local services / ports.
Ideally, it would be useful to allow sandbox connections to localhost only on specific ports.