>samit_hota
Back to research
ETHICAL HACKING

Securing WebSocket Connections: Origin Validation and CSWSH Mitigations

Samit Hota·
#websocket#application-security#burp-suite#web-security

Developers routinely assume that because WebSockets run over HTTPS (wss://), they inherit the browser’s standard security boundaries automatically. They do not. Unlike standard HTTP requests governed by the Same-Origin Policy (SOP), WebSocket connections bypass SOP once established, leaving the initial HTTP upgrade handshake as the sole perimeter control.

The Flaw in the Upgrade Handshake

The WebSocket protocol begins life as a standard HTTP/1.1 request containing an Upgrade: websocket header. The critical architectural distinction lies in browser enforcement: browsers permit cross-origin WebSocket connections by design.

When a client browser executes new WebSocket('wss://api.example.com/stream'), the browser automatically attaches any cookies associated with api.example.com to the outgoing HTTP upgrade request, along with an Origin header indicating the domain hosting the script. If the server authenticates the user based entirely on ambient cookie credentials without validating the Origin header, the endpoint becomes vulnerable to Cross-Site WebSocket Hijacking (CSWSH).

In a CSWSH scenario, an attacker tricks an authenticated user into visiting a malicious site. That site executes JavaScript to open a WebSocket connection to wss://api.example.com/stream. Because the browser includes the victim’s session cookie, the server accepts the upgrade request, establishing a bidirectional channel directly between the attacker’s script and the victim’s session context.

Auditing WebSocket Traffic with Burp Suite

Evaluating WebSocket security requires inspecting both the initial HTTP handshake and the individual frames transmitted over the established socket.

To analyze this behavior using Burp Suite:

  1. Navigate to Proxy > Options and ensure Intercept is configured for WebSocket Messages (both Client-to-Server and Server-to-Client).
  2. Trigger the WebSocket connection within your target application.
  3. Observe the initial HTTP GET request in the Proxy > HTTP history tab. Look specifically for the Upgrade: websocket, Connection: Upgrade, and Origin headers.
  4. Send the upgrade request to Repeater. In Repeater, you can manually modify the Origin header (for example, changing Origin: https://app.example.com to Origin: https://evil.com) to test whether the server enforces origin validation.
  5. Once connected, inspect active frames under the Proxy > WebSockets history tab. Individual frames can be sent to Repeater to test how the backend handles unexpected frame formats or missing data fields.

If the backend returns a 101 Switching Protocols response when supplied with an untrusted Origin header, the server is failing to validate connection origins.

Eliminating CSWSH Vulnerabilities

Remediating WebSocket access control issues requires enforcing explicit checks during the initial handshake before the connection upgrades.

1. Strict Server-Side Origin Validation

The backend handshake handler must check the Origin header against an explicit allowlist of authorized domains. If the header is missing or does not match an allowed origin, reject the upgrade request immediately with an HTTP 403 Forbidden.

Example logic in Node.js (using the ws library):

const WebSocket = require('ws');
const http = require('http');

const server = http.createServer();
const wss = new WebSocket.Server({ noServer: true });

const ALLOWED_ORIGINS = ['https://app.example.com', 'https://admin.example.com'];

server.on('upgrade', (request, socket, head) => {
  const origin = request.headers.origin;

  if (!origin || !ALLOWED_ORIGINS.includes(origin)) {
    socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
    socket.destroy();
    return;
  }

  wss.handleUpgrade(request, socket, head, (ws) => {
    wss.emit('connection', ws, request);
  });
});

2. Challenge-Response Token Authentication

Do not rely exclusively on ambient session cookies for WebSocket authentication. Instead, implement a explicit token mechanism:

  • One-Time Handshake Tickets: Before initializing the WebSocket, the client makes an authenticated REST POST request to retrieve a short-lived, single-use ticket. The client appends this ticket as a query parameter or subprotocol during the WebSocket connection setup (wss://api.example.com/stream?ticket=8f9a2b...). The server validates and invalidates the ticket upon connection.
  • CSRF Tokens in Handshake: If using cookie-based authentication, require a standard anti-CSRF token in a custom header (e.g., X-CSRF-Token) during the HTTP upgrade request. Since cross-origin scripts cannot set custom headers on cross-origin requests without explicit CORS permission, this blocks unauthorized cross-site upgrades.

3. Input Validation on Individual Frames

Because WebSockets stream continuous messages outside the standard HTTP request/response pipeline, individual frames bypass conventional Web Application Firewall (WAF) HTTP rules. Treat every incoming frame payload as untrusted input:

  • Enforce rigid JSON schema validation for incoming WebSocket text frames.
  • Apply parameterized queries for any data extracted from frames used in database operations.
  • Avoid passing frame content directly to internal event buses or command interpreters without sanitization.

Want a second set of eyes on your security posture?

Let's talk about where your real exposure is.

Book an advisory call