Skip to content

Fix WebSocket upgrade 500 on bad Host and missing abort - #434

Open
ZeroOps-HQ wants to merge 2 commits into
honojs:mainfrom
ZeroOps-HQ:zeroops/fixes-20261201
Open

ZeroOps-HQ wants to merge 2 commits into
honojs:mainfrom
ZeroOps-HQ:zeroops/fixes-20261201

Conversation

@ZeroOps-HQ

Copy link
Copy Markdown

Hi, we're ZeroOps. We look for the bugs in distributed systems that only show up when things go wrong at the same time: a retry, a crash between two writes, a dropped connection, two tabs open on the same record. They pass code review and CI, then come back as a ticket nobody can reproduce.

We went through honojs/node-server and found three bugs like that. One can hand clients a truncated, corrupted streamed body as a successful 200, and two make WebSocket upgrades return 500 for bad input or ignore client disconnects. This PR fixes the two we'd fix first.

1. Malformed Host on WebSocket upgrade returns 500

A client sends a WebSocket upgrade with an unparsable Host (like exa[mple) or a bad absolute-form URL. createUpgradeRequest in websocket.ts lets the TypeError from new URL() escape, so the instanceof RequestError check in setupWebSocket fails and we reply 500. The normal HTTP path in request.ts answers 400 for the same input.

Fix: createUpgradeRequest now wraps URL building in try/catch and rethrows non-RequestError errors as RequestError('Invalid URL', { cause }), so these requests get 400.

2. WebSocket upgrade request signal never aborts on disconnect

When a client drops an Upgrade: websocket request while the handler is still doing auth or a DB lookup, c.req.raw.signal never fires. createUpgradeRequest in websocket.ts builds the Request without a signal, and the socket close listener only rejects the waiter. Work that listens on the signal keeps running for nobody, unlike the normal HTTP path in listener.ts.

Fix: The upgrade listener now passes an AbortController signal into createUpgradeRequest and aborts it on socket close while fetchCallback is still pending.

The other one

We'd be glad to walk you through the other one, with the exact sequence of events that triggers it. Email us at contact@zero-ops.com or use our contact form. More on what we do at zero-ops.com.

createUpgradeRequest now wraps its URL building (the absolute-form `new
URL()` and the `buildUrl` call) in try/catch. Any error that is not a
RequestError is rethrown as `new RequestError('Invalid URL', { cause: e
})`, so a malformed Host or absolute-form target on an upgrade request
gets 400 instead of 500.
The WebSocket upgrade listener now makes an AbortController and passes
its signal into the Request built by createUpgradeRequest. The socket
'close' handler aborts that signal if the client disconnects while the
app is still handling the request, but not after fetchCallback has
returned or thrown.
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