fix(deno): echo negotiated WebSocket subprotocol in upgrade response#4955
Merged
Conversation
The Deno adapter did not set the `Sec-WebSocket-Protocol` header on the upgrade response, so browsers such as Chrome rejected connections that requested a subprotocol. Read the first value from the request's `Sec-WebSocket-Protocol` header and pass it as the `protocol` option to `Deno.upgradeWebSocket`, while still honoring an explicitly provided `protocol` option. Closes honojs#4439
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR updates the Deno WebSocket upgrade adapter to echo a requested subprotocol back to the client (to avoid browser handshake rejection) and adds tests to validate the behavior.
Changes:
- Read
Sec-WebSocket-Protocolfrom the request and pass the first value toDeno.upgradeWebSocketas theprotocoloption. - Add test coverage for protocol header present/absent scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/adapter/deno/websocket.ts | Adds logic to forward the request’s subprotocol into the upgrade options. |
| src/adapter/deno/websocket.test.ts | Adds tests ensuring the protocol option is set/unset based on request headers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| const { response, socket } = Deno.upgradeWebSocket(c.req.raw, { | ||
| ...(subprotocol ? { protocol: subprotocol } : {}), | ||
| ...options, |
Comment on lines
+10
to
+12
| // Echo the negotiated subprotocol back to the client. Browsers (e.g. Chrome) | ||
| // reject the connection when a subprotocol was requested but the response is | ||
| // missing the `Sec-WebSocket-Protocol` header. |
Comment on lines
+86
to
+92
| Deno.upgradeWebSocket = (_req, options) => { | ||
| passedOptions = options | ||
| return { | ||
| response: new Response(), | ||
| socket, | ||
| } | ||
| } |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4955 +/- ##
=======================================
Coverage 92.97% 92.97%
=======================================
Files 177 177
Lines 12226 12230 +4
Branches 3711 3713 +2
=======================================
+ Hits 11367 11371 +4
Misses 859 859 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
What
The Deno adapter's
upgradeWebSocketdoes not set theSec-WebSocket-Protocolheader on the upgrade response. When a client opens a socket with a subprotocol (e.g.new WebSocket(url, ["foobar"])), Chrome rejects the connection because the negotiated subprotocol is missing from the 101 response. Firefox is more lenient, which is why this looked Chrome-specific.How
Deno.upgradeWebSocketonly emits theSec-WebSocket-Protocolresponse header when given aprotocoloption. This reads the first value from the request'sSec-WebSocket-Protocolheader and passes it through asprotocol. An explicitly providedprotocoloption still takes precedence, so existing behavior is preserved.Tests
Added unit tests that mock the
Deno.upgradeWebSocketglobal and assert theprotocoloption is derived from the request header (and left unset when no subprotocol is requested).tsc --noEmit, eslint, and prettier all pass.Closes #4439