Skip to content

WebSocket Security Testing & CSWSH Defense

How to test and secure WebSocket apps: prevent Cross-Site WebSocket Hijacking (CSWSH), validate handshake Origins, and authorize every message.

By BugSnaps Security Research · 1 min read · Updated

Add BugSnaps as a preferred source on Google(opens in a new tab)

Want more of our security guides? Select BugSnaps and confirm your choice on Google.

WebSockets provide persistent, bidirectional communication for live dashboards, chat, notifications, and collaborative tools. Because a WebSocket connection starts as an HTTP `Upgrade` handshake and then exchanges raw frames over a long-lived socket, standard per-request HTTP middleware and browser Same-Origin Policy protections do not automatically protect WebSocket traffic.

Three WebSocket vulnerabilities to test

When assessing an application that uses `wss://` endpoints, testers evaluate the initial handshake, the ongoing session lifecycle, and per-message authorization:

  • Cross-Site WebSocket Hijacking (CSWSH): if a WebSocket handshake relies on ambient browser cookies for authentication and fails to validate the HTTP `Origin` header, a malicious third-party page can open a `new WebSocket('wss://app.example.com/ws')` from the victim's browser and read or send live messages.
  • Missing per-message object authorization (BOLA over WebSockets): authenticating the user during the initial connection upgrade, but failing to verify on subsequent `subscribe` or `update` frames whether the user is allowed to subscribe to `channel:tenant_b` or edit `doc_id: 402`.
  • Stale privileges on long-lived sockets: keeping a WebSocket connection open and authorized for hours after the user logged out, had their session revoked, or was removed from a workspace.

How to harden WebSocket handshakes and frame handlers

Always require encrypted `wss://` connections in production. During the HTTP upgrade handshake, validate the `Origin` header against an exact allowlist of trusted origins and require a short-lived, single-use authentication token (or `SameSite` cookie paired with an anti-CSRF token).

Inside your message dispatcher, validate the schema and payload size of every incoming JSON frame, apply per-connection rate limits, check object-level and tenant-level permissions on every subscription or action event, and terminate active sockets immediately when a user's session or workspace membership is revoked.

Validate the Origin header on every WebSocket upgrade handshake, authorize each incoming message frame, and close sockets when sessions expire.

Run a vulnerability scan on your website.

Verify your domain and use MyPentest to check for supported web and API weaknesses. Review findings and coverage; a clean scan does not rule out every CVE.