fix(host-service,relay2): pin tunnel v2 dial-back to loopback; bind standalone host to 127.0.0.1
Summary
- Tunnel v2 host client: build the local dial-back URL by assigning
url.pathnameon a fixedws://127.0.0.1:<port>base instead of resolvingdial.pathas a URL reference.//evil/x,/\evil/xandws://evil/xnow stay on loopback. - relay2: reject WS upgrade paths that start with
//with 400 before dialing the host. - Standalone
serve.ts(CLIsuperset start/ daemon): bind127.0.0.1outside sandbox mode, matching the desktop entry; the log line prints the real bind address.
Why
new URL(dial.path, "ws://127.0.0.1:<port>") treats a leading // as an authority, so a crafted dial path made the host open its dial-back socket — which carries the host-service token in ?token= — to a non-loopback origin. Hono routes GET /hosts/<id>//x to the WS handler with path="//x" and relay2 forwarded it verbatim in stream:dial. The host-side pin is the fix and holds regardless of what the relay sends; the relay-side 400 is defence in depth.
Desktop hosts already bind loopback; CLI/daemon hosts listened on all interfaces with no way to change it. Sandboxes keep the unspecified bind because the provider edge is the caller.
Manual QA
- Verified with Bun's URL implementation that the
pathnamesetter keepshost === 127.0.0.1:<port>for//evil.example/x,/\evil.example/x,ws://evil.example/x,/a?b#c, and empty string. - Verified in-process with Hono that
/hosts/<id>//evil.example/xmatched/hosts/:hostId/*withpath="//evil.example/x"before the guard. bunx tsc --noEmitclean inpackages/host-serviceandapps/relay2; pinned biome check clean on the three files.- Not exercised end to end against a running relay2 — the change is two lines on each side and the URL/route behaviour was reproduced in isolation.
Risks / Rollout
- HTTP dial (
forwardHttp) was already safe by construction (string concat on a fixed origin); unchanged. - The tunnel client and the CLI health poll already dial
127.0.0.1, so the loopback bind changes nothing for normal CLI hosts. Anyone reaching a CLI host directly from another machine (not through the relay) will lose that; nothing in the repo does. - The host-side half only protects hosts once they run a build that includes it; the relay half protects unpatched hosts against the through-the-relay path as soon as relay2 deploys.
Summary by cubic
Pins tunnel v2 dial-back to 127.0.0.1 and binds the standalone host to loopback outside sandbox to prevent origin spoofing and unintended network exposure. Previously, //-prefixed paths could override the loopback authority and expose the host-service token; now the client always dials loopback and the relay rejects double-slash paths.
- Tunnel client (
packages/host-service/src/tunnel/tunnel-client-v2.ts): builds the local WS URL onws://127.0.0.1:<port>and setspathname; relay-supplied//...orws://...cannot change the authority. Only affects WS tunnel; HTTP dial unchanged. - Relay (
apps/relay2): returns 400 for WS upgrade paths that start with//before dialing the host (defense in depth). - Standalone host (
packages/host-service/src/serve.ts): binds to127.0.0.1unlessSUPERSET_HOST_RUN_MODE === "sandbox"and logs the actual listener address. Required: reach CLI/daemon hosts through the relay; direct cross-machine access is no longer available.
Summary by CodeRabbit
-
Bug Fixes
- Improved handling of malformed WebSocket paths by returning a clear 400 response.
- Fixed local WebSocket URL construction for more reliable connections.
-
Improvements
- Host services now bind to the appropriate network interface based on the runtime environment.
- Startup logs accurately report the active hostname.