Skip to content

Chapter 0

What a WebSocket is

One handshake. Then frames, for as long as the socket stays open.

4 min readWebSocket

Every later chapter rests on this model. If you know what the handshake checks, you know where the bugs are.

CLIENT Browser Sends cookies and Origin SERVER WS Handler Switches to raw frames PHASE 1 · ONE-TIME HTTP HANDSHAKE (MIDDLEWARE RUNS HERE ONLY) GET /chat · Upgrade: websocket · Origin: https://evil.test · Cookie: sid=... HTTP/1.1 101 Switching Protocols · Missing Origin check = CSWSH PHASE 2 · PERSISTENT FRAMES (NO HTTP ROUTES, NO CSRF TOKENS) {"action":"subscribe","channel":"orders","orderId":5552} (IDOR / AuthZ) {"event":"order_update","customer":"bob@target.com"}
Figure 1 HTTP middleware only sees the initial 101 handshake. Every frame after that bypasses route-level checks unless the socket handler enforces them.
  1. The client sends one HTTP request with an Upgrade header.
  2. The server answers 101 Switching Protocols, and the connection then carries frames.
  3. Either side can send a frame at any time, with no method, path, or headers.

That is the whole protocol. Memorize the handshake below. It is the only HTTP request in the life of the socket. The Origin header on it decides the cross-site chapter.

GET /chat HTTP/1.1
Host: target.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Origin: https://target.com
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

RFC 6455 also sends Connection: Upgrade on that response. The sample above is the pair to recognize in a proxy log. The key and the accept value are the worked example from RFC 6455.

Sec-WebSocket-Accept is a protocol checksum, not an authentication token. The server appends the fixed RFC GUID 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 to your key, hashes it with SHA-1, and Base64-encodes the result. Anyone can compute s3pPLMBiTxaQ9kYGzzhZRbK+xOo= from the sample key, so it proves only that the server speaks WebSocket.

After status 101, the bytes are frames.

{"action": "subscribe", "channel": "orders"}
{"event": "order_update", "orderId": 5551, "status": "shipped"}

The first object is the client. The second object is the server. Neither object is an HTTP request.

A frame has a FIN bit, an opcode, a mask bit, a length, and a payload. Opcode 1 is text. Opcode 2 is binary. Opcode 8 closes the socket. Opcode 9 is ping. Opcode 10 is pong. The client must mask every frame it sends. The server must not mask frames it sends. Burp, wscat, and websocat apply the mask for you. A raw script that skips the mask is dropped by a correct server.

Ping frames are how an idle socket stays open. A quiet socket can still be closed by a proxy idle timer. That timer is not a session check.

Everything that guards a REST API is blind inside the frames.

Control REST API WebSocket
Authorization on each route Runs on each request Runs on the handshake, if it runs at all
WAF inspection Runs on each request Usually cannot read frames
Rate limit Counts each request Usually does not count frames
Session expiry Checked on each request The open socket can outlive the session
What scanners list The HTTP routes Frames stay out of HTTP history until you open the WebSocket tab

The 101 request does land in Burp HTTP history. The frames do not, until you open a WebSocket session from that request. That is why a default crawl looks empty.

The handshake is the login. Later frames are trusted because that handshake happened.

That pair of claims is the bug class. Every bug in the later chapters is a check that ran once, or a check that never ran.

Two headers the page does not get to choose

Section titled “Two headers the page does not get to choose”

From a page, the browser sends Origin. The browser also attaches cookies for that host, when the cookie rules allow it. Those rules are in the CSWSH chapter. Test them. Do not assume them. Page script can set Sec-WebSocket-Protocol. Page script cannot set Cookie or Origin on this API. SameSite is a cookie rule. It decides whether the browser attaches a cookie to a cross-site request. CORS means Cross-Origin Resource Sharing. CORS decides whether a page may read an HTTP response. The browser does not apply CORS to a WebSocket. Access-Control-Allow-Origin does not decide whether the script may use the socket. If the server returns 101, the script may use it. The server has to reject a bad Origin itself. The weakness id for a missing Origin check is CWE-1385. The CSWSH chapter tests both rules.

Next, Chapter 1 shows how to find the socket.

You intercept this upgrade request in Burp, change the Origin header to https://evil.test, and replay it. The server still returns 101 Switching Protocols and streams private messages. What bug did you just confirm?

GET /ws/support HTTP/1.1
Host: app.target.com
Upgrade: websocket
Connection: Upgrade
Origin: https://evil.test
Cookie: session=eyJhbGciOiJIUzI1NiJ9...
Notice how the socket authenticates using a browser cookie with no custom token in the URL or first frame.
Cross-Site WebSocket Hijacking (CSWSH). Browsers send ambient cookies on cross-origin WebSocket upgrades. Because the server skipped checking Origin and required no anti-CSRF token, any malicious site can open the socket as the victim and read or send frames. Read the full CSWSH chapter next.
Validate the Origin header against a strict allowlist on the server and require a short-lived, unpredictable token during the handshake or initial auth frame.
Connections1 backlink · 5 outgoing · 5 cross-guideFull graph →
Open local graph around this page
Open full graph →
Click a node to open · Drag to reposition · Scroll to zoom

WebSocketFor authorized testing only.