CIDADAXSEC
← All topics
Practitioner

WebSockets

WebSocket attacks abuse the handshake, the persistent two-way channel, and unauthenticated messages to hijack sessions or forge server-side actions.

Material

A WebSocket replaces a request-and-response conversation with one long-lived channel. That changes the security model: protections that depend on a fresh header on every request, such as SameSite cookie rules, no longer behave the way web developers expect.

How it works

The client sends an HTTP request with Upgrade: websocket. The server answers 101 Switching Protocols and the two sides then exchange frames that carry no HTTP headers at all.

Authentication and authorization therefore have to happen during the handshake or inside every message. A connection that was authorized once stays trusted for its whole lifetime, even after the user logs out.

http
GET /chat HTTP/1.1
Host: lab.example
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Cookie: session=abc123

> frame (teks): {"type":"subscribe","room":"lobby"}
http
GET /chat HTTP/1.1
Host: lab.example
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Origin: https://attacker.test
Cookie: session=abc123

# server menerima Origin: https://attacker.test -> cross-site hijacking mungkin terjadi

Attack shapes

  • Cross-site WebSocket hijacking: a hostile page opens a socket to the target origin and the browser attaches the victim session cookie.
  • Unauthenticated messages that drive administrative actions such as deleting records or issuing tokens.
  • Insecure message rendering, where the server echoes message content that the client drops straight into innerHTML.
  • Weak handshake validation, where the server accepts any Origin and no bearer token.
  • Automatic reactions to client events, where a message handler fires on a DOM event that an attacker can trigger.

Preventing it

  • Validate the Origin header during the handshake and reject anything outside an allowlist.
  • Use a short-lived token in the query string or a subprotocol instead of relying on cookies alone.
  • Re-check authorization on every message, not only when the socket opens.
  • Cap message size and message rate, and close the connection on malformed input.
  • Render incoming messages with textContent, and never build HTML from socket payloads.
javascript
const ws = new WebSocket('wss://target.example/ws');

ws.onopen = () => {
  ws.send(JSON.stringify({ type: 'subscribe', room: 'lobby' }));
};

ws.onmessage = (event) => {
  const msg = JSON.parse(event.data);
  document.querySelector('#log').textContent = msg.body;   // bukan innerHTML
};
Note: cookie behaviour on a WebSocket handshake differs from a normal navigation, so SameSite alone is not a usable identity boundary here. Origin checks plus a bearer token are.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.