Cross-Site WebSocket Hijacking (CSWSH)
The article explains that a WebSocket connection starts with an HTTP or HTTPS handshake that upgrades to ws:// or wss://. During that handshake, browsers send session cookies or HTTP authentication data, and the server can use those credentials to authenticate the connection.
It then shows how this becomes Cross-Site WebSocket Hijacking when a malicious page opens a WebSocket to a logged-in application. Because WebSockets are not restrained by the same-origin policy, the browser still sends the victim’s authentication data, which can give the attacker read and write access to private WebSocket traffic. The article recommends checking the Origin header on the server and using session-specific random tokens during the handshake. If the application does not need the web session on the WebSocket side, it suggests handling authentication and authorization separately inside the WebSocket protocol.
Reading notes#
- WebSocket communication begins with an HTTP(S) handshake that upgrades the connection to
ws://orwss://. - Browsers send cookies and HTTP authentication data with the handshake request.
- RFC 6455 allows servers to authenticate the handshake with cookies, HTTP authentication, or TLS authentication.
- A malicious webpage can start a cross-site WebSocket handshake to an application endpoint.
- The browser still sends the victim’s authentication data during that cross-site request.
- This can turn a write-only CSRF-style action into a read/write WebSocket connection.
- The article names this attack Cross-Site WebSocket Hijacking, or CSWSH.
- The
Originheader is present in the WebSocket handshake and should be checked on the server. - Session-specific random tokens can also protect the handshake.
- If the WebSocket side does not need the web session, authentication and authorization can be handled separately inside the WebSocket protocol.
