fix(host-service,relay2): pin tunnel v2 dial-back to loopback; bind standalone host to 127.0.0.1

Merged
SA
3mo ago
+11-6

Summary

  • Tunnel v2 host client: build the local dial-back URL by assigning url.pathname on a fixed ws://127.0.0.1:<port> base instead of resolving dial.path as a URL reference. //evil/x, /\evil/x and ws://evil/x now stay on loopback.
  • relay2: reject WS upgrade paths that start with // with 400 before dialing the host.
  • Standalone serve.ts (CLI superset start / daemon): bind 127.0.0.1 outside 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 pathname setter keeps host === 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/x matched /hosts/:hostId/* with path="//evil.example/x" before the guard.
  • bunx tsc --noEmit clean in packages/host-service and apps/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 on ws://127.0.0.1:<port> and sets pathname; relay-supplied //... or ws://... 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 to 127.0.0.1 unless SUPERSET_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.