Skip to content

Replace host-side socat bridges with in-process net.Server listeners - #257

Closed
dylan-conway wants to merge 1 commit into
mainfrom
host-socat-bridge
Closed

dylan-conway wants to merge 1 commit into
mainfrom
host-socat-bridge

Conversation

@dylan-conway

Copy link
Copy Markdown
Collaborator

The two host-side socat processes only do one thing: accept on a Unix socket and forward each connection to the local proxy server's TCP port. They run on the host, in the same trust domain as the node process that already hosts both proxy servers, so there's no reason for them to be separate OS processes.

This replaces them with net.Server listeners on the same event loop, forwarding via socket.pipe(). The in-sandbox socat relays (the TCP-LISTEN → UNIX-CONNECT ones started by buildSandboxCommand inside bwrap) are unchanged and still required.

What goes away

  • The runtime dependency on socat for the host side. (checkLinuxDependencies() is unchanged because the in-sandbox relays still use it.)
  • Two spawned processes per session.
  • ~120 lines of process-lifecycle management in reset(): killBridgeProcess(), the SIGTERM → SIGKILL escalation, the "exit event already fired" race handling, and the BRIDGE_EXIT_TIMEOUT_MS workaround for process: drop EPOLLONESHOT for Linux pidfd exit poll oven-sh/bun#30301. Closing a net.Server is synchronous and can't get into the missed-event state that motivated fix(sandbox): make reset() resilient to missed bridge 'exit' events #255.
  • The poll-for-socket-file loop in initializeLinuxNetworkBridge. server.listen()'s callback is the ready signal; there's no race against an external process creating the file.

Socket placement

The bridge sockets move from /tmp/claude-{http,socks}-<rand>.sock into a private mode-0700 mkdtemp directory (/tmp/srt-bridge-XXXXXX/). The previous layout created the socket file in world-writable /tmp with umask-derived permissions and never tightened them; the new layout makes the sockets unreachable to other local users for their whole lifetime, with no post-creation chmod() race. bwrap bind-mounts the socket files into the sandbox where the uid is identity-mapped, so the sandbox side is unaffected.

Behavioral note

The bridge data plane is now on the node event loop. While a sandboxed command runs the host process is idle, so this doesn't change anything in practice — but it does mean a hypothetical caller that blocks the event loop while a sandboxed command is mid-network-call (e.g. execSync on the wrapped command) would see that traffic stall. The existing async spawn-based callers are unaffected.

The two host-side socat processes only do one thing: accept on a Unix
socket and forward each connection to the local proxy server's TCP port.
They run on the host, in the same trust domain as the node process that
already hosts both proxy servers, so there's no reason for them to be
separate OS processes.

Replace them with net.Server listeners on the same event loop, forwarding
via socket.pipe(). This drops:

- the runtime dependency on socat for the host side (the in-sandbox socat
  relays are unchanged and still required)
- two spawned processes per session
- ~120 lines of process-lifecycle management in reset(): the SIGTERM /
  SIGKILL escalation, the 'exit'-event-already-fired race handling, and
  the BRIDGE_EXIT_TIMEOUT_MS workaround for a Bun pidfd notification bug
- the "poll for the socket file to appear" loop in
  initializeLinuxNetworkBridge: server.listen()'s callback is the ready
  signal

The bridge sockets also move from `/tmp/claude-{http,socks}-<rand>.sock`
into a private mode-0700 mkdtemp directory (`/tmp/srt-bridge-XXXXXX/`).
The previous layout left a window where the socket file existed in
world-writable /tmp with default permissions; the new layout makes the
sockets unreachable to other local users at every point in their
lifetime.
@dylan-conway

Copy link
Copy Markdown
Collaborator Author

Superseded — going with the full launcher replacement instead (proxies listen on the unix socket directly on Linux, vendored srt-launcher replaces socat + apply-seccomp + bwrap, no host-side bridge for the internal-proxy case).

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.

1 participant