Cross-site WebSocket hijacking: the handshake nobody checked

Cross-site WebSocket hijacking (CSWSH) happens when a page on one site makes a visitor's browser open a WebSocket to another site, and that second site accepts the connection because the visitor's cookies came along. Nothing in the browser stops the page from opening the socket; the only gate is a check on the server, and many servers never wrote it.

Section Web attacks Updated Weakness CWE-1385

Quick facts

Short name
CSWSH, also written CSWH
Weakness
CWE-1385, missing origin validation in WebSockets
Browser sends
An Origin header on every WebSocket handshake (RFC 6455, section 4.1)
Server should
Verify the Origin against sites it expects and answer 403 otherwise (RFC 6455, section 10.2)
Main fix
Check Origin against an explicit allowlist on every handshake, and authenticate the connection without relying on cookies alone

Protocol statements follow RFC 6455, the WebSocket protocol. Defensive guidance follows the OWASP WebSocket Security Cheat Sheet and the MDN guide to writing WebSocket servers. The weakness entry is CWE-1385.

Why WebSockets escape the usual checks

A WebSocket starts life as an ordinary HTTP request: a GET with an Upgrade: websocket header. If the server agrees, the same TCP connection stops carrying HTTP and starts carrying framed messages in both directions. The MDN WebSockets overview describes the API a page uses to do it, and the constructor takes a ws: or wss: address with no restriction to the page's own site.

That is the difference from a normal cross-site fetch. When a page asks the browser for data from another origin, the browser lets the request go out but hides the response unless the other server opts in; that opt-in is what CORS misconfiguration gets wrong. A WebSocket handshake has no such step. The MDN server guide says all browsers send an Origin header and that the server may use it to allow or deny the connection. The decision sits entirely on the server.

The RFC puts the intent plainly in its introduction: the Origin header field is there to protect against unauthorized cross-origin use of a WebSocket server. It is a request the server is meant to act on, not a rule the browser enforces.

How the attack runs

The setup needs three things: an application that authenticates WebSocket connections with a cookie, a signed-in victim, and a page the attacker controls that the victim opens. Nothing needs to be installed and no vulnerability in the browser is involved.

  1. The victim is signed in. Their browser holds a session cookie for the application.
  2. The victim opens another site. A link, an advert or a forum post is enough. The page contains a short script that opens a WebSocket to the application.
  3. The browser adds the cookie. The handshake goes to the application's host, so the browser attaches that host's cookies, as the OWASP cheat sheet notes. The Origin header names the attacker's site.
  4. The server does not look at Origin. It sees a valid session and accepts the upgrade.
  5. The script now has a live socket authenticated as the victim. It can send messages the victim could send and read everything the server pushes back.

The reading half is what separates this from CSRF. A cross-site request forgery fires a request and, because of the same-origin policy, cannot read the answer. A hijacked WebSocket is two-way, so the attacker's page sees live data: chat history, account balances, notifications, whatever the application streams to that user. The OWASP cheat sheet adds CSRF tokens on the handshake as an additional defense, which shows how close the two problems sit.

No exploit string is needed; the attack uses the protocol as designed. That is why it is a missing check and not an input-filtering bug, and why it is listed under broken access control.

THREE HANDSHAKES ORIGIN SENT COOKIE ATTACHED SERVER SHOULD ACCEPT OWN PAGE HOSTILE PAGE NON-BROWSER SCRIPT // a script outside a browser may send any Origin, or none
// the Origin check separates the first two rows; authentication handles the third

The Origin check

RFC 6455, section 10.2 says that servers meant to process input only from certain sites should verify that the Origin field is one they expect, and that a server rejecting the origin should answer the handshake with HTTP 403 Forbidden. The OWASP cheat sheet says to validate Origin on every handshake against an explicit allowlist, and to avoid wildcards and substring matching.

The substring warning is practical. A test such as "the Origin contains example.com" also passes an attacker's host named example.com.evil.test. Compare the whole origin, meaning scheme, host and port, against exact entries.

What the check cannot do

Browsers set Origin themselves and a page script cannot change it, which is why the check works against a hostile page. Other clients are different. MDN warns that non-browser agents can send a faked Origin, and RFC 6455 section 10.1 tells servers to be careful about assuming they talk to scripts from known origins. A command-line tool simply writes whatever header it likes.

So the check closes the cross-site browser route and nothing more. Whoever connects still has to prove who they are, and what they may do still has to be checked.

Missing Origin

RFC 6455 requires a browser client to include Origin; other clients may omit it. MDN adds that most applications reject requests without the header. Whether you can do that depends on whether you serve non-browser clients such as native apps. If you do, treat a missing Origin as a client that gets no help from the browser and rely on the token described next.

Cookies, tokens and SameSite

Section 10.5 of the RFC says the protocol prescribes no particular way for servers to authenticate clients, and that a server may use any mechanism a generic HTTP server has: cookies, HTTP authentication or TLS client authentication. Cookies are the common choice for browser applications, and they are exactly what makes the hijack work, because the browser sends them without being asked.

The OWASP cheat sheet recommends, for browser clients, sending a token in the first message after the connection opens instead of relying on the handshake cookie. A page on another site cannot read that token, so it cannot produce a valid first message. The same sheet advises keeping tokens out of URL query strings, because they can end up in logs, and refusing protected data until authentication has succeeded.

A SameSite attribute on the session cookie tells the browser to withhold it from cross-site requests; MDN documents the Strict, Lax and None values in its guide to cookies. OWASP lists SameSite among the measures that reduce the problem. It stays a second layer. "Same site" is a wider idea than "same origin", so a page on a sibling subdomain can still get the cookie sent, and the Origin check and a handshake token are what cover that case.

Closely related is session hijacking: there the attacker takes the session itself, here they borrow it for the length of one page view. The remedies overlap, including short-lived credentials, secure cookie flags and encrypted transport, and the general rules are collected on API authentication.

After the connection opens

A correct handshake is the start of the job. The OWASP cheat sheet lists what a long-lived connection still needs:

  • Treat every message as untrusted input and validate its structure and content, and never pass it to eval().
  • Authorize each action, since a connection is not a licence for everything. A socket that lets a user subscribe to a channel should check, per message, that the user may read that channel; skipping that is a classic broken access control pattern.
  • Cap message size; the sheet gives 64 KB or less as a typical limit.
  • Throttle message volume, so one connection cannot flood the server.
  • Use wss://, which is WebSocket over TLS. RFC 6455 section 10.6 says implementations must support TLS and should use it.

Those limits are application decisions. The cheat sheet's figures are starting points, not values to copy; your own traffic should set them.

Where a WAF helps and where it does not

The upgrade request is plain HTTP, so a filter in front of the application can read it. It can compare the Origin header with a list of sites you expect, flag upgrade requests that carry none, and refuse those from unexpected origins before they reach the application. That is useful when the application cannot be changed quickly, or as a second check behind a correct one. Host-related trust problems get the same treatment; see host header injection.

What it cannot do is reach inside. After the upgrade the traffic is framed WebSocket messages, and what a message means, who may send it and whether the user is allowed that channel is known only to the application. A filter also cannot tell a legitimate Origin from a legitimate-looking one it was never told about, so the allowlist has to come from you. A WAF filters requests; it does not repair the missing check in the code, and it does not replace authentication.

Bridge WAF works in three modes. In off nothing is inspected. In monitor matches are recorded and every request is served, so the activity log shows which upgrade requests came from origins you did not list. In block matching requests are refused and still recorded. Start in monitor mode, confirm that your own pages, apps and partners appear in the log with the expected origins, and only then move to block. For the general idea of the tool, see web application firewall.

How to prevent it

  1. Validate Origin on every handshake. Compare scheme, host and port with an exact allowlist and answer 403 otherwise, as RFC 6455 section 10.2 describes.
  2. Do not rely on the cookie alone. Require a token in the first message, or a CSRF token on the handshake, and accept no protected data before that succeeds.
  3. Set SameSite on session cookies. Treat it as an extra layer.
  4. Authorize per message. Check what the user may do on each action, not only at connect time.
  5. Use wss and keep tokens out of URLs. Query strings leak into logs.
  6. Limit size and rate. Set caps from your own traffic and see rate limiting for the general approach.

The wider family of cross-site and browser-trust flaws is on the web attacks page.

Questions

What is cross-site WebSocket hijacking?

Cross-site WebSocket hijacking is an attack in which a page on another site makes the visitor's browser open a WebSocket to your server. The browser attaches the visitor's cookies to the handshake, so if the server does not check the Origin header, the socket is authenticated as the visitor and the attacker's script can read and send messages as them.

Does the same-origin policy protect WebSockets?

No. A page can open a WebSocket to any host, and the browser sends the Origin header and the cookies for that host. Checking the Origin on the server is the server's job, which RFC 6455 states in its section on origin considerations.

Is an Origin check enough to stop it?

It stops the browser-based attack, because browsers set Origin and page scripts cannot change it. It does not stop non-browser clients, which can send any Origin, so the connection still needs real authentication and per-message authorization.

Does SameSite on the session cookie prevent it?

It helps, because the browser withholds a SameSite cookie from cross-site requests. It is a second layer, not a replacement: same-site pages such as a sibling subdomain are not cross-site, and the Origin check and a token in the first message cover that gap.

Can a WAF block cross-site WebSocket hijacking?

A filter can compare the Origin of an upgrade request with an allowlist and record or refuse mismatches. It cannot see what happens inside the open socket, and it does not replace the check in your application. Start in monitor mode and read the activity log before switching to block.