WSTG - Latest

WebSockets

ID
WSTG-CLNT-10

Summary

Traditionally, the HTTP protocol only allows one request/response per TCP connection. Asynchronous JavaScript and XML (AJAX) allows clients to send and receive data asynchronously (in the background without a page refresh) to the server, however, AJAX requires the client to initiate the requests and wait for the server responses (half-duplex).

WebSockets allow the client or server to create a ‘full-duplex’ (two-way) communication channel, allowing the client and server to truly communicate asynchronously. WebSockets conduct their initial upgrade handshake over HTTP and from then on all communication is carried out over TCP channels by use of frames. For more, see the WebSocket Protocol.

Origin

It is the server’s responsibility to verify the Origin header in the initial HTTP WebSocket handshake. If the server does not validate the origin header in the initial WebSocket handshake, the WebSocket server may accept connections from any origin. This could allow attackers to communicate with the WebSocket server cross-domain allowing for CSRF-like issues. See also A01:2025 Broken Access Control. The exploit for this weakness is called Cross-Site Websocket Hijacking (CSWH or CSWSH).

Confidentiality and Integrity

WebSockets can be used over unencrypted TCP or over encrypted TLS. To use unencrypted WebSockets the ws:// URI scheme is used (default port 80), to use encrypted (TLS) WebSockets the wss:// URI scheme is used (default port 443). See also A04:2025 Cryptographic Failures.

Input Sanitization

As with any data originating from untrusted sources, the data should be properly sanitized and encoded. See also A05:2025 Injection (includes XSS).

Test Objectives

  • Identify the usage of WebSockets.
  • Assess its implementation by using the same tests on normal HTTP channels.
  • Verify that authentication and authorization are enforced for the handshake and for every subsequent frame, subscription, and pushed event.
  • Verify that Origin allowlisting uses exact scheme, host, and port comparison.

How to Test

Black-Box Testing

  1. Identify that the application is using WebSockets.
    • Inspect the client-side source code for the ws:// or wss:// URI scheme.
    • Use Google Chrome’s Developer Tools to view the Network WebSocket communication.
    • Use ZAP’s WebSocket tab.
  2. Origin.
    • A standalone WebSocket client can set any Origin header, so a successful connection from one does not show that the server fails to validate it. Test from a browser instead. While logged in to the target in that browser, open a page served from a different origin that you control and run new WebSocket("wss://target.example/socket") from its console or script. The browser supplies the victim’s cookies and the page’s Origin automatically. If the handshake succeeds and the page can read data or send authenticated messages, the server is vulnerable to Cross-Site WebSocket Hijacking.
    • Cross-Site WebSocket Hijacking matters when the socket authenticates with cookies (or another credential the browser sends automatically). If the only credential is a token in the first message or a non-cookie header, a foreign page cannot use the victim’s session just by opening a socket, and Origin checks are not the main control.
    • Browsers send a lowercase Origin. Focus on values a real browser or embed can produce: the exact allowed origin, a malicious prefix or suffix that still matches a loose check (https://attackerallowed.example / https://allowed.example.attacker.test), an unexpected port, an IDNA/punycode twin, scheme mismatches (http versus https), and Origin: null from a sandboxed iframe. The server must compare a parsed scheme, host, and port, not a substring or loose regular expression.
  3. Confidentiality and Integrity.
    • Check that the WebSocket connection is using TLS to transport sensitive information wss://.
    • Check the HTTPS Implementation for security issues (Valid Certificate, BEAST, CRIME, RC4, etc). Refer to the Weak Transport Layer Security section of this guide.
  4. Authentication.
    • WebSockets do not handle authentication, normal black-box authentication tests should be carried out. Refer to the Authentication Testing sections of this guide.
    • If the handshake carries a token in the URL, query string, or first message, confirm it is not logged by intermediaries and that it expires with the HTTP session.
  5. Authorization.
    • WebSockets do not handle authorization by themselves. Apply the Authorization Testing tests to the handshake and to individual frames. A successful low-privilege connection must not imply access to every topic, room, tenant, or object on that socket.
    • Connect with two tester-owned accounts that have different roles or object membership.
    • Capture a legitimate subscribe, join, listen, or GraphQL subscription frame from account A.
    • Replay it on account B’s connection after substituting user, session, room, channel, tenant, conversation, or object identifiers.
    • Confirm both the immediate response and later pushed events. Authorization that filters only the first snapshot but continues to broadcast unauthorized events is a finding.
    • After logout, password change, role removal, or token expiry on one device, verify that the still-open socket stops accepting privileged frames and stops receiving private events. Reconnect with the revoked credential and confirm the handshake or first authenticated frame is denied.
    • If the application also exposes HTTP or long-poll fallbacks for the same events, compare authorization decisions across transports.
  6. Input Sanitization.
    • Use ZAP’s WebSocket tab to replay and fuzz WebSocket requests and responses. Refer to the Injection Testing section of this guide.

Per-Message Authorization

Suppose account A belongs to conversation c_a and account B belongs to conversation c_b. After both accounts open authenticated sockets, capture A’s subscribe frame:

{"type":"subscribe","channel":"conversation","id":"c_a"}

On B’s socket, send the same frame with "id":"c_a". Secure behavior denies the subscribe and never pushes A’s messages. Insecure behavior returns the history for c_a, or later emits events for c_a on B’s connection.

To test revocation on a live subscription, keep A’s socket subscribed to c_a, then remove A’s membership (or log A out) and confirm that socket stops receiving c_a events and rejects further privileged frames. Checking B after B logs out does not exercise revocation of an authorized subscriber.

Intercepting With ZAP

Once we have identified that the application is using WebSockets (as described above) we can use the Zed Attack Proxy (ZAP) to intercept the WebSocket request and responses. ZAP can then be used to replay and fuzz the WebSocket request/responses.

ZAP WebSockets
Figure 4.11.10-1: ZAP WebSockets

Cross-Origin Handshake Client

A standalone WebSocket client (one can be found in the Tools section below) is useful for manual protocol testing, such as replaying intercepted handshakes and messages and trying different Origin values. It can set any Origin header, so a successful connection from it cannot establish Cross-Site WebSocket Hijacking. Use the browser-based cross-origin test in the Origin step above to confirm it.

WebSocket Client
Figure 4.11.10-2: WebSocket Client

Gray-Box Testing

Gray-box testing is similar to black-box testing. In gray-box testing, the pentester has partial knowledge of the application. The only difference here is that you may have API documentation for the application being tested which includes the expected WebSocket request and responses.

Tools

References