Skip to content

fix(claude-code)!: sandbox firewall the agent can't disable, closed to SSH and fail-open - #238

Merged
gatezh merged 4 commits into
fix/claude-code-issue-batchfrom
fix/sandbox-firewall-sudo
Oct 22, 2026
Merged

gatezh merged 4 commits into
fix/claude-code-issue-batchfrom
fix/sandbox-firewall-sudo

Conversation

@gatezh

@gatezh gatezh commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

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 node NOPASSWD:ALL and ran init-firewall.sh as root from a bind mount the agent could write to. Claude Code runs there with permission prompts skipped, so a prompt-injected agent could run sudo iptables -F or edit the script and reach any host.

Changes

  • Sudo per target: passwordless sudo moves from the shared stage to the default target. 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 with visudo -c during the build.
  • Baked firewall (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.
    • Project allowlist (.devcontainer/claude-sandbox/allowed-domains.txt, new in the template): one domain per line, read as data and checked against a domain regex.
    • Bad input: a bad line or a non-regular file is skipped with a warning, never fatal, so a corrupted allowlist can't keep the firewall down. Invalid lines aren't echoed, so a symlink to a root-only file can't leak it.
    • No second run: it refuses to touch an active firewall (OUTPUT policy DROP), so an edited allowlist can't be swapped in mid-session.
  • chown-volumes replaces the sandbox's sudo find … chown and sudo chown -R. It changes the mount points only, with -h and -execdir, so a symlink the agent swaps in can't redirect it.
  • Template: removes the init-firewall.sh bind mount, and postCreateCommand/postStartCommand call the baked scripts.
  • Docs: README sections, the file tree and the cf/MDN/PyPI allowlist notes now refer to allowed-domains.txt. The devcontainer-upstream-sync skill tracks allowed-domains.txt and lists init-firewall.sh as retired, with the steps to migrate its domains.
  • CI verify (verify-image.sh): the sandbox checks sudo -l for both scripts and that sudo true is refused, with an explicit if … exit 1, because set -e ignores a !-negated command. The default checks that sudo true works. With NET_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.
  • Hardening from the 2026-10-22 review (also in the repo's own .devcontainer/claude-sandbox/init-firewall.sh where it applies):
    • No blanket outbound port 22 (finding Add comprehensive README for devcontainer-hugo-bun image #1). That rule had no destination match, so ssh -D to 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 tries gitlab.com:22: example.com doesn't listen on 22, so a check against it passes even with the hole open.
    • Fail closed (Configure volume mount for node_modules in devcontainer #5). An EXIT trap, cleared only after verification, sets the DROP policies if anything stops the script early.
    • Allowlist read as node (Configure node_modules mount in devcontainer.json #6): timeout 5 runuser -u node -- cat instead 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.
    • IPv6 gets loopback only, where the kernel has ip6tables.
    • chown-volumes is 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.sh is refused, so postStartCommand fails and the firewall doesn't come up until the project migrates:

    1. Move the extra domains into allowed-domains.txt.
    2. Delete the script and its bind mount.
    3. Take the two commands from the template.

    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 in git diff. DNS (udp/53) stays open, as before.

  • Verified locally with --cap-add NET_ADMIN --cap-add NET_RAW:

    Check Result
    Firewall with api.cloudflare.com in the allowlist example.com blocked, Cloudflare and npm reachable
    sudo iptables -F, sudo init-firewall --x, sudo chown … Refused: password required
    iptables as node Permission denied
    Allowlist edited to add example.com, then sudo init-firewall "already active", still blocked
    Allowlist with a $(touch /tmp/pwned) line Line skipped, firewall up, nothing run
    Allowlist symlinked to /etc/shadow File ignored, nothing printed, firewall up
  • Hardening probes (sandbox image, NET_ADMIN):

    Check Before After
    gitlab.com:22 (listens on 22, not allowlisted) connected refused
    github.com:22 (allowlisted) connected connected
    GitHub meta fetch fails mid-script (--dns 127.0.0.1) policies left at ACCEPT policies DROP
    Allowlist symlinked to /etc/shadow ignored not read, nothing printed
    Allowlist is a FIFO — start-up continues after 5 s
    ip6tables -S after a run no rules DROP policies, loopback allowed
  • Checks: 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-bun already uses the narrow-sudo, baked-script model. The repo's own .devcontainer/claude-sandbox still 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

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
gatezh force-pushed the fix/sandbox-firewall-sudo branch from 4e08059 to bdb3658 Compare October 22, 2026 22:45
@gatezh
gatezh changed the base branch from master to fix/claude-code-issue-batch October 22, 2026 22:45
@gatezh gatezh changed the title fix(claude-code)!: sandbox firewall the agent can't disable fix(claude-code)!: sandbox firewall the agent can't disable, closed to SSH and fail-open Oct 22, 2026
@gatezh
gatezh added this pull request to stack #244 October 22, 2026 22:46
@gatezh
gatezh merged commit e4fbd28 into master Oct 22, 2026
19 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] sandbox: NOPASSWD:ALL + workspace-writable firewall script lets the agent disable the firewall

1 participant