Repository navigation
fix(claude-code)!: sandbox firewall the agent can't disable, closed to SSH and fail-open - #238
Merged
Merged
Conversation
This was referenced Oct 22, 2026
The sandbox gave node NOPASSWD:ALL and ran a workspace-writable init-firewall.sh as root, so an agent with permission prompts skipped could flush iptables or edit the script (#208). - Passwordless sudo moves from the shared stage to the default target. - The sandbox bakes the firewall in at /usr/local/sbin/init-firewall and a volume chown at /usr/local/sbin/chown-volumes, both root-owned. Its sudoers allows only those two, without arguments. - Projects list extra domains in .devcontainer/claude-sandbox/allowed-domains.txt. It's read as data and validated. Bad lines or a non-regular file are skipped with a warning, so the firewall always comes up. - init-firewall refuses to run again once OUTPUT is DROP, so allowlist edits apply only on the next container start. - The template drops the bind mount and calls the baked scripts. README and the upstream-sync skill describe the migration, and CI verify checks the sudo rules in both targets. BREAKING CHANGE: sandbox projects must move extra domains from their init-firewall.sh into allowed-domains.txt, drop the bind mount, and take postCreateCommand/postStartCommand from the template. The old `sudo /usr/local/bin/init-firewall.sh` is refused. Closes #208
…6 gaps - No blanket outbound port 22. `-A OUTPUT -p tcp --dport 22 -j ACCEPT` had no destination match, so `ssh -D` to any host was an exit around the allowlist. Allowlisted hosts (GitHub included) stay reachable on every port through the ipset rule. The script's own verification now checks that example.com:22 is refused. Inherited from Anthropic's reference script; the repo's own .devcontainer/claude-sandbox/init-firewall.sh gets the same fix. - Fail closed. An EXIT trap, cleared only after verification passes, sets the DROP policies if anything stops the script between the flush and the policies (a failed GitHub meta fetch, no default route). Before, the container was left with no rules and ACCEPT policies while Claude Code ran with prompts skipped. Also in the repo's own script. - Read the allowlist with `runuser -u node -- cat` under `timeout 5`, so the kernel refuses what node couldn't read and a FIFO can't block start-up. Replaces the realpath/regular-file/prefix checks and their check-then-read race. - IPv6 gets loopback only when the kernel supports ip6tables, so a Compose network with enable_ipv6 isn't unfiltered. - chown-volumes is baked into both targets and the default template calls it too, so the two variants share one chown.
With NET_ADMIN, the sandbox check now runs /usr/local/sbin/init-firewall, whose own verification fails the job unless example.com is refused on 443 and 22 and GitHub stays reachable. Both workflows add the capability; a local run without it skips the step. The firewall has had no behavioral test in CI until now.
example.com doesn't accept connections on port 22, so the self-check passed even with the old blanket port-22 rule in place. Probe: under the old rules gitlab.com:22 connected and example.com:22 didn't; under the new rules both are refused and github.com:22 (allowlisted) still connects.
gatezh
force-pushed
the
fix/sandbox-firewall-sudo
branch
from
October 22, 2026 22:45
4e08059 to
bdb3658
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The sandbox's firewall now lives in the image, and the agent can't edit it, flush it or run it again. Sudo there is limited to the firewall and a volume chown.
Why
#208: the sandbox gave
nodeNOPASSWD:ALLand raninit-firewall.shas root from a bind mount the agent could write to. Claude Code runs there with permission prompts skipped, so a prompt-injected agent could runsudo iptables -For edit the script and reach any host.Changes
sharedstage to thedefaulttarget. The sandbox's sudoers allows only/usr/local/sbin/init-firewall ""and/usr/local/sbin/chown-volumes "": those two commands, without arguments. The file is validated withvisudo -cduring the build.claude-code/.devcontainer/init-firewall.sh, installed root-owned at/usr/local/sbin/init-firewall): the repo's firewall logic, with built-in domains (npm, Anthropic, VS Code marketplace, MDN MCP, GitHub ranges) plus the project's extras..devcontainer/claude-sandbox/allowed-domains.txt, new in the template): one domain per line, read as data and checked against a domain regex.OUTPUTpolicyDROP), so an edited allowlist can't be swapped in mid-session.chown-volumesreplaces the sandbox'ssudo find … chownandsudo chown -R. It changes the mount points only, with-hand-execdir, so a symlink the agent swaps in can't redirect it.init-firewall.shbind mount, andpostCreateCommand/postStartCommandcall the baked scripts.allowed-domains.txt. Thedevcontainer-upstream-syncskill tracksallowed-domains.txtand listsinit-firewall.shas retired, with the steps to migrate its domains.verify-image.sh): the sandbox checkssudo -lfor both scripts and thatsudo trueis refused, with an explicitif … exit 1, becauseset -eignores a!-negated command. The default checks thatsudo trueworks. WithNET_ADMIN, which both workflows now add, the sandbox check brings the firewall up, so CI runs its self-verification. Nothing in CI exercised the firewall before..devcontainer/claude-sandbox/init-firewall.shwhere it applies):ssh -Dto any host was an exit around the allowlist; the hole is inherited from Anthropic's reference script. Allowlisted hosts, GitHub included, stay reachable on every port. The self-check triesgitlab.com:22:example.comdoesn't listen on 22, so a check against it passes even with the hole open.node(Configure node_modules mount in devcontainer.json #6):timeout 5 runuser -u node -- catinstead of realpath and prefix checks, so the kernel refuses what node couldn't read, a FIFO can't block start-up, and the check-then-read race is gone.chown-volumesis baked into both targets, and the default template calls it too.Notes
Breaking for downstream sandbox users. After pulling the new image, the old
sudo /usr/local/bin/init-firewall.shis refused, sopostStartCommandfails and the firewall doesn't come up until the project migrates:allowed-domains.txt.The README's "Upgrading from a project
init-firewall.sh" section has the steps.Remaining trust gap, documented: the agent can still edit
allowed-domains.txt. The edit takes effect only after a container restart, which the agent can't trigger, and it shows ingit diff. DNS (udp/53) stays open, as before.Verified locally with
--cap-add NET_ADMIN --cap-add NET_RAW:api.cloudflare.comin the allowlistexample.comblocked, Cloudflare and npm reachablesudo iptables -F,sudo init-firewall --x,sudo chown …iptablesas nodeexample.com, thensudo init-firewall$(touch /tmp/pwned)line/etc/shadowHardening probes (sandbox image,
NET_ADMIN):gitlab.com:22(listens on 22, not allowlisted)github.com:22(allowlisted)--dns 127.0.0.1)/etc/shadowip6tables -Safter a runChecks: both targets pass
verify-image.sh, the sandbox with its firewall run. hadolint v2.12.0, actionlint and shellcheck are clean.Out of scope:
claude-bunalready uses the narrow-sudo, baked-script model. The repo's own.devcontainer/claude-sandboxstill bind-mounts its script; it got the SSH and fail-closed fixes, and could move to the baked model in a separate PR.Stack: sits on fix(claude-code): Oxc node path, fresh-volume start, Hugo-proof safe.directory; Playwright MCP via env vars #237.
Closes #208