Skip to content

Lesson 13 of 19 · 18m

WebSockets: The Origin Check Nobody Writes

The WebSocket handshake is a plain GET that carries cookies and asks for no CSRF token — origin allowlisting, handshake tickets, and the authorization lifetime problem.

new WebSocket('wss://app.example.com/feed') runs fine from a page on evil.example — and the browser attaches the user's session cookies to the handshake. Your app checked auth at login and on every REST endpoint, then opened a bidirectional channel where the origin model is different and nobody wrote the check.

The handshake is HTTP; after 101 Switching Protocols it stops being HTTP. There are no more headers, no cookies, no CSRF tokens — the authorization decision you made at upgrade applies to every message for the life of the connection. Whoever connects as the victim stays the victim.

Cross-Site WebSocket Hijacking

Cookies ride the handshake. SameSite=Lax withholds them on cross-site subresource requests in modern Chrome and Firefox — but SameSite=None is common in apps that embed elsewhere, older clients differ, and a SameSite policy you never deliberately set is not a control. PortSwigger named the result Cross-Site WebSocket Hijacking: an attacker's page opens a WebSocket as the victim and reads or writes whatever the channel carries — chat history, notifications, collaborative document ops.

The defense is one line of intent: the browser sends an Origin header on the handshake, and an in-browser attacker cannot forge it — Origin is a forbidden header. Check it against an allowlist at upgrade. That's the whole fix, and most WebSocket libraries don't do it for you. Node's ws gives you verifyClient; gorilla/websocket has CheckOrigin, which half the tutorials override to return true to "fix staging," and the override ships. In Go codebases that line is the vuln.

Two limits on the check. Non-browser clients can send any Origin they like — the check defeats browser-mediated CSWSH, it is not an authorization primitive. And it says nothing about who connected; authentication still happens separately, just at handshake time.

In socket.io the same check lives in allowRequest on the server options or in io.use middleware — the middleware sees the handshake request and can reject on origin, missing auth, or both. Whatever the library: if the handshake handler you shipped doesn't mention Origin, the check doesn't exist.

Tokens in URLs land in logs

wss://api.example.com/feed?token=eyJ... puts a credential in every access log, proxy log, and browser history entry it touches. Prefer a short-lived ticket: an authenticated POST /ws-ticket returns a 30-second nonce, the client presents it in the first WebSocket message or in Sec-WebSocket-Protocol, and it dies on use. socket.io supports an auth payload in the handshake; whatever the transport, keep the credential out of the URL.

Authorization has a lifetime problem

A connection that opened while the user was a member stays open after the user is banned. Nothing re-checks — there is no request middleware on a frame. Bind the connection's authorization to something you can re-validate: a session object consulted on privilege change, or a periodic re-auth on sensitive channels. And give the channel a budget — connections per IP, messages per second, max frame size. A WebSocket has no browser-imposed rate limit; one open connection is an amplification channel for floods.

The receipts are boring and reliable: PortSwigger's Web Security Academy runs dedicated CSWSH labs, and the pattern recurs in bounty writeups for chat, notification, and collaborative features — not because it's subtle, but because nothing forces the check to exist. gorilla/websocket's own README says to validate Origin carefully, which tells you how often nobody does.

At upgrade: allowlist Origin, authenticate with a ticket instead of a URL token, log the handshake — it's the only request you'll ever see for that channel — and set connection budgets. After upgrade: re-check on privilege change, and treat the connection's lifetime as an authorization decision you keep making, not one you made once.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.