Repository navigation
Fix WebSocket upgrade 500 on bad Host and missing abort - #434
Open
ZeroOps-HQ wants to merge 2 commits into
Open
ZeroOps-HQ wants to merge 2 commits into
ZeroOps-HQ wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-serverand 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(likeexa[mple) or a bad absolute-form URL.createUpgradeRequestinwebsocket.tslets theTypeErrorfromnew URL()escape, so theinstanceof RequestErrorcheck insetupWebSocketfails and we reply 500. The normal HTTP path inrequest.tsanswers 400 for the same input.Fix:
createUpgradeRequestnow wraps URL building in try/catch and rethrows non-RequestErrorerrors asRequestError('Invalid URL', { cause }), so these requests get 400.2. WebSocket upgrade request signal never aborts on disconnect
When a client drops an
Upgrade: websocketrequest while the handler is still doing auth or a DB lookup,c.req.raw.signalnever fires.createUpgradeRequestinwebsocket.tsbuilds theRequestwithout 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 inlistener.ts.Fix: The upgrade listener now passes an
AbortControllersignal intocreateUpgradeRequestand aborts it on socket close whilefetchCallbackis 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.