You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I stumbled upon this one (on Dream 1.0.0~alpha8) implementing a WebSocket server with authentication through the Sec-WebSocket-Protocol (see it explained e.g., here).
In short: because WebSocket does not allow for extra headers, some people use the header Sec-WebSocket-Protocol to pass authentication tokens (instead of using HTTP's Authorization header).
This can easily be done with Dream, for example as follows (adapted from example/k-websocket):
This works perfectly fine in Firefox. In Chrome however, the connection is closed upon receiving the 101 reply from the server. As a developer perspective, the only information we see is this in Chrome's console:
(index):6 WebSocket connection to 'ws://localhost:8080/websocket' failed:
From Dream, we see the request correctly logged. Looking in more details at what happens (through Wireshark, as I didn't manage to see much details using Chrome's debug tools), we can arrive at the conclusion that this is because the reply is malformed according to the WebSocket protocol. From MDN:
In a response it specifies the sub-protocol selected by the server. This must be the first sub-protocol that the server supports from the list provided in the request header.
It seems that Firefox is lenient with the response, while Chrome is not.
The current solution is to add a header to the response:
Now, is this something that should be handled by Dream or by the application? I'm not sure what's best. But clearly, I spent quite some time figuring that out, until I looked at the headers through a Wireshark capture (because I didn't manage to see the headers of the response through Chrome's debug tools), and compared Dream behavior with the one from node's ws module.
For comparison, the following Node server transparently adds the Sec-WebSocket-Protocol to the response when it is provided as part of the request. It selects the first element of the comma-separated list, adding Sec-WebSocket-Protocol: Authorization to the headers (just as I did in the example above).
constWebSocket=require('ws');constwss=newWebSocket.Server({port: 8080});wss.on('connection',(ws)=>{ws.send('Hello from WebSocket server!');ws.on('message',(message)=>{ws.send(message)});});
Even if you prefer not to add it as the default behavior, this ticket would serve as useful documentation for anyone trying to do the same.
The text was updated successfully, but these errors were encountered:
My first thought here is that inside the implementation of val websocket, we don't have access to the request, so we don't know what headers were sent. It could be doable in a middleware though: if there is a request header matching Sec-WebSocket-Protocol: Authorization, something and a response header Upgrade: websocket, then add a new response header Sec-WebSocket-Protocol: Authorization.
Not sure if the Dream API is the right home for such a middleware, especially as this 'auth' method itself seems kinda non-standard, and we would have to cross-reference it from all WebSocket documentation.
Hi!
I stumbled upon this one (on Dream 1.0.0~alpha8) implementing a WebSocket server with authentication through the
Sec-WebSocket-Protocol
(see it explained e.g., here).In short: because WebSocket does not allow for extra headers, some people use the header
Sec-WebSocket-Protocol
to pass authentication tokens (instead of using HTTP'sAuthorization
header).This can easily be done with Dream, for example as follows (adapted from
example/k-websocket
):This works perfectly fine in Firefox. In Chrome however, the connection is closed upon receiving the 101 reply from the server. As a developer perspective, the only information we see is this in Chrome's console:
From Dream, we see the request correctly logged. Looking in more details at what happens (through Wireshark, as I didn't manage to see much details using Chrome's debug tools), we can arrive at the conclusion that this is because the reply is malformed according to the WebSocket protocol. From MDN:
It seems that Firefox is lenient with the response, while Chrome is not.
The current solution is to add a header to the response:
Now, is this something that should be handled by Dream or by the application? I'm not sure what's best. But clearly, I spent quite some time figuring that out, until I looked at the headers through a Wireshark capture (because I didn't manage to see the headers of the response through Chrome's debug tools), and compared Dream behavior with the one from node's
ws
module.For comparison, the following Node server transparently adds the
Sec-WebSocket-Protocol
to the response when it is provided as part of the request. It selects the first element of the comma-separated list, addingSec-WebSocket-Protocol: Authorization
to the headers (just as I did in the example above).Even if you prefer not to add it as the default behavior, this ticket would serve as useful documentation for anyone trying to do the same.
The text was updated successfully, but these errors were encountered: