Incorrectly returning multiple Sec-WebSocket-Protocol
headers
#141
Labels
bug
Something isn't working
Sec-WebSocket-Protocol
headers
#141
Environment
Node v22.13.1
[email protected]
Reproduction
https://github.com/eltigerchino/crossws-duplicate-headers-repro
Reproduction instructions are included in the repository.
Describe the bug
I'm trying to select a WebSocket protocol during an upgrade request by returning the
Sec-WebSocket-Protocol
header with a specific value. However, thews
,uNetworking/uWebSockets.js
, and Deno libraries, also return this header if the client sends it. This causes the response to be invalid, since only one instance of the protocol header should exist in the response.https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
Fortunately, this behaviour can be turned off by setting the Node adapter's option
serverOptions.handleProtocols
. However, it might be better to standardise this behaviour incrossws
itself. The other adapters do not automatically return the protocol header. Thus, theupgrade
hook can be responsible for selecting the protocol by returning the header or omitting the header.Alternatively, we could go the opposite direction and make all adapters always return the first protocol the client sends but allow overwriting the header from the upgrade hook.
Additional context
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#subprotocols
https://datatracker.ietf.org/doc/html/rfc6455#section-4.2.2
Logs
WebSocket connection to 'ws://localhost:8000/' failed:
The text was updated successfully, but these errors were encountered: