Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What should i use to secure my websocket connection?


Asked by Dalton Bowen on Dec 14, 2021 FAQ



You should secure your connection with SSL/TLS and use https over http and wss over ws. For having auhentication/authorization, you can add query params to the websocket connections and also add something like username/passwprd.
Besides,
WebSocket secure protocol URLs are prefixed with wss://. Note that you don’t have to use HTTPS in order to use WSS, but it is recommended to do that anyway.
And, That is the original protocol that creates an unencrypted connection between client and server. For better security, use WebSocket secure protocol (WSS) that uses SSL/TLS to encrypt the connection and protects against eavesdropping and man-in-the-middle attacks. WebSocket secure protocol URLs are prefixed with wss://.
Next,
The WebSocket protocol uses the HTTP upgrade system (which is normally used for HTTP/SSL) to "upgrade" an HTTP connection to a WebSocket connection. Some proxy servers do not like this and will drop the connection. Thus, even if a given client uses the WebSocket protocol, it may not be possible to establish a connection.
Indeed,
Only the normal mechanisms for HTTP connections are available. That includes HTTP and TLS authentication and cookies. The upgraded handshake still occurs from HTTP to WebSocket. But, the HTTP sends the authentication information directly to WS. This can be exploited and we call this attack Cross-Site WebSocket Hijacking.