Repository navigation
feat(sandbox): confine WSL sandbox egress behind a guest firewall - #227
Merged
Merged
Conversation
quickbeard
force-pushed
the
fix/wsl-broker-bridge
branch
from
August 13, 2026 15:31
4b0a765 to
8a48e97
Compare
quickbeard
force-pushed
the
feat/wsl-egress-firewall
branch
from
August 13, 2026 15:42
c09d7aa to
b8f19db
Compare
The vz and bwrap guests are NIC-less by construction; a WSL guest shares Windows' network, so its isolation rested on the guest's own routing. Per-distro Hyper-V firewall rules cannot deliver the intent: rules target a VMCreatorId (all of WSL at once) and every WSL2 distro shares one utility VM, netns and IP, so a creator-wide block would cut off the user's own distros. Enforce inside the shared guest kernel instead: the boot script mounts cgroup v1 net_cls, marks this sandbox's process tree with a classid derived from its reserved server port, and installs nftables rules that allow loopback plus the host-side broker endpoint for that classid and reject everything else. meta cgroup rather than socket cgroupv2 because Microsoft's WSL kernel ships without CONFIG_NFT_SOCKET (observed live). Fail closed: any setup failure aborts the boot. The guest image gains the nftables package; sandboxes on an older image refuse to start until Reset. e2e (script/e2e-wsl.ts --egress) now serves a local broker upstream and probes from inside the running guest: broker reachable from the sandbox cgroup, direct egress from the cgroup rejected, the same direct egress outside the cgroup untouched. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Stacked on #226 (the stdin boot-script delivery this feature's fail-closed script depends on).
Why not "per-distro Hyper-V firewall rules"
The planned mechanism cannot deliver the intent, verified live on Windows 11 / WSL 2.7.11:
/proc/self/ns/netinode and the same eth0 address. A creator-wideDefaultOutboundAction Blockwould cut off the user's own distros and Docker Desktop.The only enforcement point that can tell this sandbox's traffic from its neighbours' is the shared guest kernel.
Design
The root boot script, before dropping privileges: mounts cgroup v1 net_cls, gives the sandbox's group a classid derived from its host-reserved server port (unique per running sandbox), moves itself in (server, socat and every agent tool inherit), and installs an nftables table:
meta cgroup <classid> oifname "lo" accept— the sandbox's own server and the broker stubmeta cgroup <classid> ip daddr $HOST_IP tcp dport <brokerPort> accept— only when a broker is configuredmeta cgroup <classid> reject— everything else fails immediately, matching the NIC-less backendsmeta cgroup(classid) rather thansocket cgroupv2: Microsoft's kernel ships# CONFIG_NFT_SOCKET is not set— the cgroup2 match fails to load (observed:Could not process rule: No such file or directory), whileCONFIG_CGROUP_NET_CLASSID=y. Fail closed: every step aborts the boot on failure, no elevation needed (all guest-side root).The guest image gains the
nftablespackage (~1 MB with libs). Rollout: a sandbox on an older image fails to start with "nft is missing from the guest image" until Reset — the image must be published before/with the app release.Verification (live, Windows 11 / WSL 2.7.11 / kernel 6.18.33.2)
bun script/e2e-wsl.ts --image dist-images\bwrap-x64.tar.gz --bundle ..\opencode\dist\node --egresswith a locally built image:Plus 24 unit tests in
packages/codev-sandbox(bun test src/wsl/), typecheck clean.Residual exposure (documented in firewall.ts)
loallow means a sandbox can reach neighbour distros' 127.0.0.1 services. Not closable by port rules without breaking the agent's own localhost dev-server workflows.🤖 Generated with Claude Code