Cross-Site WebSocket Hijacking

The WebSocket handshake is authenticated by cookies alone and its Origin is not checked, so a malicious site can open a connection as a signed-in visitor.

SeverityMedium
CVSS 3.15.9 CVSS:3.1/AV:N/AC:H/PR:N/UI:R/S:U/C:H/I:L/A:N Open in the calculator
CVSS 4.06 CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:P/VC:H/VI:L/VA:N/SC:N/SI:N/SA:N Open in the calculator
CWECWE-1385: Missing Origin Validation in WebSockets
CategoryBroken Access Control

Description

A WebSocket connection starts with an HTTP handshake request, to which the browser attaches the site's cookies. Neither the same-origin policy nor CORS stops a page on another site from opening a WebSocket connection to any server. If the server authenticates the connection only by the session cookie, or by other credentials the browser sends automatically, and does not check the Origin header of the handshake, a malicious page visited by a signed-in user can open a connection as that user.

Unlike cross-site request forgery, the attacker's page can both send messages and read every response, so it has two-way, interactive access with the user's privileges for as long as the connection stays open. It can read data the application streams to the user, such as messages, notifications or account information, and send messages that perform actions, such as posting content, changing settings or placing orders. The impact depends on what the WebSocket carries, and is greatest where it handles sensitive data or privileged operations.

The attack works only in a browser that sends the session cookie with the handshake, so the SameSite cookie attribute affects the result: cookies marked Lax or Strict, or treated as Lax by browsers that apply it by default, are not sent with a cross-site handshake. However, cookies marked None are sent, as are cookies without the attribute in browsers that do not apply the default, unless the browser blocks third-party cookies, as some now do by default. Pages on sibling subdomains count as the same site, so the attribute does not stop a handshake started from one of them.

The weakness is identified by examining the handshake for authentication that relies only on cookies, with no token in the request or the first message, then replaying it with an Origin header for another site and checking whether the server still accepts it (101 Switching Protocols) and answers messages. A proof of concept page hosted elsewhere confirms it.

Remediation

Authenticate WebSocket connections in a way that a third-party page cannot reproduce:

  • Check the Origin header of every handshake against an exact allowlist of the application's own origins, and refuse any other value. Avoid prefix, suffix or loose pattern matches.
  • Do not rely on cookies alone. Require a token that a cross-site page cannot obtain, such as an anti-CSRF token or a short-lived, single-use ticket issued by an authenticated request, in the handshake or the first message, and accept no other messages until it is validated. Prefer the first message to the address, which may be logged.
  • Set session cookies with SameSite=Lax or Strict as defence in depth.
  • Authorise each message on the server, as for any other request.
// ASP.NET Core var options = new WebSocketOptions(); options.AllowedOrigins.Add("https://app.example.com"); app.UseWebSockets(options); // Spring registry.addHandler(handler, "/ws").setAllowedOrigins("https://app.example.com");

Confirm the fix by repeating the handshake with a foreign Origin header and from a test page on another site: both should be refused.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Web Applications Findings

Every Web Applications finding