RFC 6455: WebSocket: Bidirectional web connections
Summary
In December 2011, Ian Fette and Alexey Melnikov specified the WebSocket protocol in RFC 6455, which opens a persistent two-way message channel between browser and server over a single TCP connection. The connection starts as an ordinary HTTP request and switches protocol with status code 101. Because experiments during development made proxies vulnerable, clients must mask every frame.
From HTTP upgrade to message channel
The client sends an HTTP request with Upgrade: websocket and a random value in the Sec-WebSocket-Key header. The server appends a fixed UUID, computes a SHA-1 hash and sends it back base64-encoded; this proves it really understands WebSocket. After that, frames flow with text, binary data or control information such as ping, pong and close.
Because WebSocket runs over ports 80 and 443 and starts with HTTP, it works through most firewalls and proxies. The Origin header allows the server to check which website the connection comes from.
Why clients must mask
Section 10.3 describes an experiment from the development period: over an HTTP connection with upgrade, an attacker sent data that looked like a GET request, and some of the proxies on the net stored the forged response in their cache. In this way foreign scripts could have been delivered under trusted addresses.
RFC 6455’s answer: clients must mask every frame with a random key using XOR, so that an attacker cannot control what the bytes look like on the wire. Servers must not mask their frames.
Ideas
- WebSocket starts as an HTTP request and switches via upgrade.
- The server proves with a hash that it understands WebSocket.
- Frames carry text, binary data and control information.
- Clients mask every frame, servers never do.
- The Origin header shows which website the connection comes from.
- Ping and pong check whether the connection is still alive.
Insights
- A new protocol on old infrastructure must also protect devices that do not even know it.
- Persistent connections change scaling, timeouts and monitoring of web applications in ways quite different from single requests.
Facts
- RFC 6455 appeared in December 2011.
- A successful handshake ends with HTTP 101 Switching Protocols.
- The fixed UUID in the handshake is 258EAFA5-E914-47DA-95CA-C5AB0DC85B11.
- RFC 8441 from September 2018 describes WebSocket over HTTP/2.
References
- RFC 6455: The WebSocket Protocol
- RFC 8441: Bootstrapping WebSockets with HTTP/2 (2018)
- RFC 6454: The Web Origin Concept (2011)
Remarks
- Reverse proxies such as nginx only forward WebSocket if the Upgrade and Connection headers are explicitly passed through.
Recommendations
- Check Origin and authentication on the server during the handshake.
- Limit message size, idle time and number of connections per client.
- Set longer timeouts for WebSocket paths in the reverse proxy than for normal requests.
Read the RFC at the RFC Editor
Found a mistake or have something to add? Get in touch.
Links to the original source and the Web Archive open in a new tab.