Cross-site WebSocket hijacking — the auth bug your API team probably hasn't tested for

Published November 6, 2026 · 3 min read

ross-site WebSocket Hijacking — The Auth Bug Your API Team Probably Hasn't Tested For

When it comes to securing your web applications, WebSockets often fall through the cracks. While your team may be adept at handling traditional cross-site scripting (XSS) and cross-site request forgery (CSRF), Cross-site WebSocket Hijacking (CSWSH) is a lesser-known vulnerability that can expose your application to serious security risks. This article explores CSWSH, how it exploits WebSockets, and why implementing origin checks is crucial for your API security.

Understanding CSWSH

WebSockets provide a full-duplex communication channel over a single, long-lived connection. They are widely used in modern applications for real-time functionalities like chat, streaming, and notifications. However, the convenience of WebSockets can come with security pitfalls, particularly when developers overlook origin checks.

CSWSH occurs when an attacker establishes a WebSocket connection to a server using a victim's credentials, without the victim's consent or knowledge. This is possible if the server does not properly verify the origin of the WebSocket request, allowing an attacker to hijack the session and potentially exfiltrate sensitive data.

How CSWSH Works

To understand how CSWSH works, consider the following scenario:

  1. Attacker Setup: The attacker sets up a malicious website designed to exploit CSWSH.
  2. Victim Visit: The victim visits this malicious site while logged into a legitimate service that uses WebSockets.
  3. WebSocket Connection: The malicious site initiates a WebSocket connection to the legitimate service using the victim’s browser, leveraging the victim's session cookies.
  4. Data Exfiltration: If the legitimate service does not check the origin of the WebSocket request, the attacker can communicate with the service as if they were the victim, potentially accessing or modifying sensitive information.

Here's a simplified example of how an attacker might initiate a WebSocket connection:

const socket = new WebSocket('wss://victim-service.com/socket');

socket.onopen = function() {
    // Send a message to the WebSocket server
    socket.send(JSON.stringify({ action: 'stealData' }));
};

socket.onmessage = function(event) {
    // Log the received data
    console.log('Received:', event.data);
};

Without proper origin checks, the server might accept this connection, allowing the attacker to interact with the service using the victim's session.

Mitigating CSWSH with Origin Checks

The primary defense against CSWSH is to implement strict origin checks on the server-side. When a WebSocket connection request is received, the server should verify the Origin header to ensure that the request originates from a trusted source.

Implementing Origin Checks

Here's a basic example of how you might implement origin checks in a Node.js WebSocket server:

const WebSocket = require('ws');

const server = new WebSocket.Server({ port: 8080 });

server.on('connection', (ws, req) => {
    const origin = req.headers['origin'];
    if (origin !== 'https://trusted-origin.com') {
        ws.close(1008, 'Origin not allowed');
        return;
    }

    ws.on('message', (message) => {
        console.log('Received:', message);
        // Handle the message
    });
});

In this example, the server checks if the Origin header matches the expected trusted origin. If not, the connection is terminated immediately with a status code indicating a policy violation.

WebSocket Security Checklist

To secure your WebSocket implementations, consider the following checklist:

Security MeasureDescription
Origin ChecksValidate the Origin header to ensure requests come from trusted sources.
Secure WebSocket (wss)Use wss:// to encrypt WebSocket traffic.
AuthenticationRequire authentication for sensitive WebSocket connections.
Rate LimitingImplement rate limiting to prevent abuse.
Logging and MonitoringLog WebSocket activity and monitor for unusual patterns.

These practices should be part of a comprehensive security strategy. For a more detailed approach, consider our Automated Assessment or explore our Penetration Testing services.

Conclusion

Cross-site WebSocket Hijacking (CSWSH) is a significant threat that can compromise the security of your web applications if not addressed properly. By implementing robust origin checks and following a comprehensive security checklist, you can mitigate the risks associated with WebSockets and protect your users' data. As WebSockets continue to be a vital part of real-time web applications, ensuring their security is not optional—it's essential.

Want to see this in practice?

Run a free single-domain scan in three minutes — same engine, smaller scope, no signup. We'll email you the PDF.

Run a free scan