Skip to content

fix(serve): a workspace's or root's refusal of a WebSocket reaches the caller through a host with its own status, not 502 (swamp-club#4894) - #3977

Merged
stack72 merged 2 commits into
mainfrom
4894
Nov 26, 2026
Merged

stack72 merged 2 commits into
mainfrom
4894

Conversation

@stack72

@stack72 stack72 commented Nov 26, 2026

Copy link
Copy Markdown
Member

Summary

The host's router (src/serve/host/workspace_router.ts, relayWebSocket) opens its upstream with Deno's WebSocket client, which reports a refused handshake only as an error event. Every refusal by a workspace or the root — a refused or expired token (401), an origin refusal (403), a misdirected request (421), a rate limit (429), a child shutting down (503) — reached the caller as 502 "The workspace could not be reached.". The CLI could not tell a refused credential from a crashed child, and its login hint never showed.

The router now reads the child's status from Deno's handshake failure text (Invalid status code: N) — the same text src/cli/remote_run.ts and src/worker/connect.ts already read — and answers a 4xx or 5xx with that status and a body of its own.

Decisions

  • Status only, not a raw handshake. A raw upstream handshake would keep the child's body and Retry-After, but no Deno or browser WebSocket client can read a refused handshake's body or headers, and it would put a hand-written frame codec on every relayed socket. The child's body and headers are dropped; nothing from the child but the status number crosses.
  • The child is asked once. One upstream handshake per caller attempt, so a refused credential is never sent twice (no re-ask, which #4877 tried and reverted).
  • Fallbacks unchanged. A redirect (its Location cannot be passed on), a child that never answered, or a close before any error still reach the caller as 502; the open timeout stays 504. If Deno ever rewords its message, the router falls back to today's 502, and the router tests against a real Deno.serve child fail on that toolchain bump.
  • Older hosts. The CLI's 5xx hint for an exchanged workspace token (workspaceTokenRefusalHint) is kept, since an older host still answers 502.

User-visible impact

  • Behind a host, a workspace refusing an exchanged token now shows the CLI's 401 message: Authentication failed — the workspace refused the token from your host sign-in; sign in to the host again to replace it: swamp auth server-login --server (was a server-error message with "HTTP 502").
  • A worker behind a host with a refused credential or a rate limit now gives up after MAX_PRE_ENROLL_FAILURES (3) attempts, exactly as it does against serve directly, instead of retrying the router's 502 as a serve restart for the 5-minute budget. No worker code change.

Test Plan

  • integration/serve_host_workspace_router_test.ts: the 502 pin is replaced by a stand-in child that refuses by the credential and headers it receives (401 no/wrong bearer, 403 foreign Origin, 421 mismatched forwarded host, 429 busy with Retry-After, 503 draining). Each case asserts the caller's status through the host, the host's own JSON body, no child header, and exactly one child handshake. A good bearer is relayed after one handshake; a 301 child stays 502. Existing unreachable-502, timeout and hang-up tests kept.
  • integration/serve_workspace_cli_sign_in_test.ts: a workspace refusing its exchanged token through the real router now yields the exact 401 login hint.
  • Mutation-checked: always-502, passing 3xx through, and always-401 each fail the matching tests; the two files pass with --repeats=10.
  • The triage reproduction (a real router in front of a child answering 401/429) now returns 401/429 instead of 502.
  • Verification workflows (build, reviews, skills) pass on 4a7f1d565; attestation posted.

Docs: design/surfaces/serve-workspaces.md and design/enablers/remote-execution.md updated.

🤖 Generated with Claude Code

stack72 and others added 2 commits November 25, 2026 23:48
…e caller through a host with its own status, not 502 (swamp-club#4894)

The host's router opened its upstream with Deno's WebSocket client, which
reports a refused handshake only as an error event, so every refusal by a
workspace or the root (a refused or expired token, an origin refusal, a
misdirected request, a rate limit) reached the caller as 502 "The
workspace could not be reached." The CLI could not tell a refused
credential from a crashed child, and its login hint never showed.

The router now reads the child's status from Deno's handshake failure text
(`Invalid status code: N`), the same text the CLI and worker already read,
and answers a 4xx or 5xx with that status and a body of its own. The
child is asked once, so a refused credential is never sent twice. A
redirect, whose Location cannot be passed on, and a child that never
answered still reach the caller as 502; a timeout stays 504. The child's
body and headers do not survive, which no WebSocket client could read
anyway.

- Router tests use a child that refuses by the credential and headers it
  receives (401, 403, 421, 429, 503), each asserting the caller's status,
  the host's own body and exactly one handshake.
- The CLI sign-in test for a workspace refusing its exchanged token now
  sees the 401 login hint through the host instead of HTTP 502.
- The 502 hint for a workspace token stays for older hosts.
- A worker behind a host with a refused credential now gives up after
  three attempts, as it does against serve directly, instead of retrying
  the router's 502 as a restart.
- design/surfaces/serve-workspaces.md and design/enablers/remote-execution.md
  describe the new behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…b#4894)

- The comment above refusalStatus no longer reads as if only a redirect
  without a Location falls to 502; every redirect does.
- The router logs the upstream failure text only when there is one, so a
  timeout or abort no longer ends its line with an empty string.
- workspaceTokenRefusalHint's JSDoc wraps at 80 columns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attestation Approved

Verified commit 4a7f1d56 — attestation gate passed (12/17 steps).

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial Review

I read src/serve/host/workspace_router.ts (relayWebSocket, the new refusalStatus), host_reply.ts, the hint change in server_login_hint.ts, and the router and sign-in tests.

Critical / High

None.

I checked these paths and found them correct:

  • Settle races. failure is written only from the error listener, and only the non-open branches read it. If the timeout settles first and an error arrives later, the code still takes the 504 branch. A caller or host abort leaves failure empty, so it falls back to 502. A close without an error also gives 502. No path changes outcome.
  • Status range. refusalStatus only accepts 400–599, and the \b stops a 4-or-more-digit run from being read as a 3-digit status. 101, redirects and anything unparseable still give 502. So a 1xx, 2xx or 3xx status can't reach Response.json, which would otherwise throw a RangeError for statuses below 200.
  • What crosses from the child. Only the status number does. The body comes from the host (hostReply), and the child's headers are dropped. Nothing downstream of router.handle (serve_host.ts:248) branches on the status, so passing it through can't affect any host-side rate limiting or auth bookkeeping.
  • Asking the child once. There is still one upstream handshake per caller attempt, so a refused credential is never sent to the child twice.

Medium

None.

Low

  1. workspace_router.ts:261: the host now passes through any 4xx/5xx a child chooses to send, including 401 or 407 with no WWW-Authenticate or Proxy-Authenticate header. A misbehaving workspace child could therefore make the host appear to refuse the caller's credential. That is the intended behaviour, and it is low risk because the children are spawned by the host. If a tighter contract is wanted later, you could allowlist the statuses the CLI and worker actually act on (401/403/404/421/429/503) and map everything else to 502.
  2. workspace_router.ts:139-144: failure is Deno's internal error text and is logged at info level. Today it only carries the status, but if Deno ever adds URL or header detail to that text, it would land in host logs. This is theoretical.

Verdict

PASS. The status pass-through is limited to 4xx/5xx and every non-matching path keeps the old 502/504 answers.

@stack72
stack72 merged commit e4529fe into main Nov 26, 2026
6 checks passed
@stack72
stack72 deleted the 4894 branch November 26, 2026 00:27
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