[CVE-2023-32695] Socket.IO DoS Trought Javascript Property Manipulation on WebSockets
Socket.IO passed an attacker-supplied event name straight into Node's EventEmitter without checking it was a string. By sending an object whose toString property was not a function, an attacker could crash the server with a single WebSocket message.
Published on rafa.hashnode.dev
In this blog post, I will show a recent CVE that I discovered that exploits a Denial-of-Service (DoS) vulnerability in the widely used Socket.IO library.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Socket.IO is one of the most widely used JavaScript libraries, commonly paired with Node.js to provide WebSocket communication between a browser client and a server. The source lives at https://github.com/socketio/socket.io. The vulnerability described here is CVE-2023-32695, a denial-of-service issue that the author discovered in the library and disclosed on 18 June 2023.

To follow the bug you need to understand the shape of a Socket.IO message. A simple example looks like this:
42["eventName", "String Value", {"Object":"value"}]
42is the Socket.IO packet type.["eventName", "String Value", {"Object":"value"}]is the payload carried inside the custom event. It is an array with three elements:"eventName", the first element, is the event name defined on both the back end and the front end so each side knows what to do with the packet."String Value"is a string example associated with the event; it can be any data.{"Object":"value"}is an example of object data sent as part of the event payload.
- The array can hold as many elements as you like, but the back end has to be written to process them, otherwise they are useless.
Setup
The proof of concept uses a minimal Node.js server built on Express, the built-in http module and the Socket.IO Server class. The server serves an index.html on /, logs each connection, and registers two handlers: msg, which logs the received message and re-emits it to all clients as resp, and disconnect, which logs the reason. It listens on port 3000.
const express = require('express');
const http = require('http');
const { Server } = require("socket.io");
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log(`connect ${socket.id}`);
socket.on('msg', (msg) => {
console.log(msg)
io.emit('resp', msg);
});
socket.on("disconnect", (reason) => {
console.log(`disconnect ${socket.id} due to ${reason}`);
});
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
To exploit the vulnerability, you first create a valid WebSocket connection with the server. No account or special role is involved in the example.
Finding it
Socket.IO uses the Node.js EventEmitter library for asynchronous communication between different parts of the code. The problem is that the emit() function is called with unsanitised user input, and that input can be an object rather than a string. Because of this, an attacker can override the toString() function that is called automatically during EventEmitter's execution, which produces an unrecoverable crash on the server.
When a WebSocket message reaches the server, the emitUntyped() function is called, and it in turn calls emit() from EventEmitter.

The ev argument carries the event name, and the event name can be supplied in the first position of the array in the WebSocket message. That is the entry point: the attacker controls what is passed as the event name, including its type.
Exploitation
Step 1: Understand where the crash happens
The DoS occurs because an attacker can send a JavaScript object instead of a string as the event name. When EventEmitter's emit() is called, it crashes if the toString() property is overridden in the object that was sent. The crash happens in the following code of EventEmitter (https://github.com/nodejs/node/blob/main/lib/events.js):

When the malicious object reaches const handler = events[type]; (line 505), JavaScript tries to convert the object into a string in order to obtain a reference into the events object. Converting an object to a string normally calls its toString() property automatically, but if toString is an attribute of the sent object, JavaScript throws an exception. The server crashes because toString is no longer a function.
Step 2: Open a valid WebSocket connection
To exploit the vulnerability you first create a valid WebSocket connection with the server, exactly as a normal client would.
Step 3: Send the malicious packet
Once connected, send the following payload, which triggers the DoS:
42[{"toString":"1337"},"1337"]
The 42 prefix marks the packet as a Socket.IO event. The first element of the array, {"toString":"1337"}, is an object placed where the event name string is expected; its toString attribute is a string, not a function. The second element, "1337", is the payload data. When the server processes the packet, the object is used as the event name, the implicit string conversion fails, and the process crashes.

Root cause
The mistake is that the event name taken from the wire is passed to EventEmitter's emit() without being validated as a string. EventEmitter indexes its handler map with the event name (const handler = events[type];), and JavaScript's property lookup coerces the key to a string by calling toString(). If the attacker supplies an object whose toString property is not a function, that coercion throws, and the exception takes down the server process.
Impact
The issue was considered high severity because it can crash a server simply by using Socket.IO. Since the library is the most used for WebSockets on Node servers, millions of websites could be taken down with a single request. The crash is unrecoverable for the server.
Fix
A fix was released in the latest version of Socket.IO. The report does not describe the code change itself; upgrading to the fixed release is the remediation it points to.
Variants to test
- Send other non-string values in the event-name position, such as arrays or nested objects, and watch for a crash or an unhandled exception on the server. A positive result is the server crashing.
- Override other properties that JavaScript coerces during property lookup, for example
valueOf, in the same position. - Try the same object-in-place-of-string trick on other Socket.IO fields that end up as EventEmitter event names, such as reserved or internal event names.
- Test other Node.js libraries that forward user-controlled keys into EventEmitter (
emit,on,once).
What happened, in plain words
A security researcher found a bug in a very popular piece of software called Socket.IO. Socket.IO is a tool that programmers use to make websites and apps talk to their servers in real time, for example to build chat rooms, live notifications or multiplayer games. The bug meant that anyone who could open a valid WebSocket connection to a server built with Socket.IO could send one specially crafted message and make the server crash. The crash could not be recovered from, so the website would go down. It was given the official identifier CVE-2023-32695, and a fix was released. He published his write-up on 18 June 2023.
This article walks through the same story for someone who has never done security work before. Every new term is explained the first time it appears, and there is a glossary at the end.
What is Socket.IO?
Socket.IO is one of the most used libraries for JavaScript. A library is a ready-made bundle of code that programmers reuse instead of writing everything from scratch, a bit like buying a flat-pack shelf instead of sawing the wood yourself. Socket.IO is widely used together with Node.js, which is a program that lets JavaScript run on a server rather than only inside a browser. Together they provide WebSocket communication.
A WebSocket is a connection between a browser and a server that stays open in both directions, so either side can send a message at any moment without asking first. Compare it to a phone call that stays connected, rather than sending a letter and waiting for a reply each time. That is what makes chat apps and live updates feel instant.

The source code of Socket.IO is public and lives at https://github.com/socketio/socket.io.
Understanding the Socket.IO packet structure
Before the bug makes sense, you need to know what a Socket.IO message looks like. A message is called a packet. Here is a simple example of a Socket.IO WebSocket message:
42["eventName", "String Value", {"Object":"value"}]
What each part means:
42is the first part of the message and it is the Socket.IO packet type. Think of it as the label on an envelope that tells the post office what kind of letter is inside. Here it is the Socket.IO packet type.["eventName", "String Value", {"Object":"value"}]is the second part, the payload, meaning the actual content being sent inside the custom event. It is an array, which in JavaScript is an ordered list written between square brackets. This array has three elements:"eventName"is the first element. It is the event name, a label defined on both the back end (the server) and the front end (the browser) so each side knows what to do with this packet. It works like the subject line of an email: both people need to agree what it means."String Value"is a string example associated with the event. A string is simply text, a sequence of characters. It can be any data.{"Object":"value"}is an example of object data sent as part of the event payload. An object in JavaScript is a collection of named values written between curly braces, like a labelled box with compartments.
- The array can hold as many elements as you want, but the back end has to be written to process them. Otherwise the extra elements are useless, like writing extra lines on a form that nobody reads.
With that picture in mind, the vulnerability becomes easier to follow.
The vulnerability
Socket.IO uses the EventEmitter library that comes with Node.js for asynchronous communication between different parts of the code. Asynchronous means the parts do not wait for each other; they pass messages and carry on, like colleagues dropping notes on each other's desks instead of standing in a queue. EventEmitter is the mechanism that delivers those notes: one part of the program says "something happened, tell everyone who is listening", and EventEmitter calls the code registered for that event.
The problem is that the emit() function, the one that sends the event out, is called with unsanitised user input. Unsanitised means the data coming from the outside is used as-is, without being checked or cleaned first. In this case the input can be an object rather than a string. Because of that, an attacker can override the toString() function that is called automatically during EventEmitter's execution, which causes an unrecoverable crash on the server. Override here means replacing something that already exists with something of your own.
When a WebSocket message is sent to the server, the emitUntyped() function is called, and it in turn calls the emit() function from EventEmitter:

The ev argument carries the event name, and the event name can be sent in the first position of the list in the WebSocket message. That is the entry point: the attacker controls what is passed as the event name, including what type of thing it is, a piece of text or an object.
The DoS vulnerability occurs because attackers can send JavaScript objects instead of strings in the event name parameter. DoS stands for Denial of Service, a class of attack whose goal is not to steal data but to make a service stop working for everyone, like blocking the entrance of a shop so no customer can get in. When the emit() function from EventEmitter is called, it will crash if the toString() property is overridden in the sent object.
The crash occurs at the following code of EventEmitter (https://github.com/nodejs/node/blob/main/lib/events.js):

When the malicious object from the attacker arrives at const handler = events[type]; (line 505), JavaScript tries to convert the object into a string in order to get a reference to the events object. A reference here means a way to look up the right entry in a list. When JavaScript tries to convert an object into a string, the toString() property is called automatically. But if toString is an attribute of the sent object, meaning the attacker has put their own value under that name, JavaScript throws an exception. An exception is an error signal that interrupts normal execution. The server crashes because toString is not a function anymore, it is just a piece of text where the program expected something it could run.
Proof of concept
A proof of concept is a small, working demonstration that shows a bug is real, rather than only theoretical. In this example the researcher uses a simple Node.js server that uses WebSocket with the Socket.IO library. Below is the source code:
const express = require('express');
const http = require('http');
const { Server } = require("socket.io");
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log(`connect ${socket.id}`);
socket.on('msg', (msg) => {
console.log(msg)
io.emit('resp', msg);
});
socket.on("disconnect", (reason) => {
console.log(`disconnect ${socket.id} due to ${reason}`);
});
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
Line by line, in plain words:
const express = require('express');loads a library called Express, which makes it easy to build a web server.requireis how Node.js pulls in another piece of code.const http = require('http');loads Node.js's built-in module for handling web traffic.const { Server } = require("socket.io");loads the Socket.IO server class.const app = express();creates the Express application.const server = http.createServer(app);wraps that application in an HTTP server, the thing that actually listens for connections.const io = new Server(server);attaches Socket.IO to that HTTP server.app.get('/', (req, res) => { res.sendFile(__dirname + '/index.html'); });says: when someone visits the home page/, send them the fileindex.html.__dirnameis the folder the script lives in.io.on('connection', (socket) => { ... });registers code to run every time a new client connects. Thesocketobject represents that one client.console.log(connect ${socket.id});prints the connection and the client's identifier to the server's log.socket.on('msg', (msg) => { ... });listens for an event calledmsgcoming from that client, and receives its content asmsg.console.log(msg)prints the received message.io.emit('resp', msg);sends the same message back out to all connected clients under the event nameresp. This is the line that makes the server useful for a chat: one person writes, everyone receives.socket.on("disconnect", (reason) => { ... });runs when the client goes away, and logs the reason.server.listen(3000, () => { ... });starts the server on port 3000. A port is a numbered door on a computer; 3000 is a common choice for development.
To exploit the vulnerability, you need to create a valid WebSocket connection with the server, and then send the following payload, which will exploit the DoS issue:
42[{"toString":"1337"},"1337"]
What this payload does, piece by piece:
42marks the packet as a Socket.IO event, exactly as in the normal example earlier.[{"toString":"1337"},"1337"]is the array. The first element,{"toString":"1337"}, is an object placed where the event name string is expected. ItstoStringattribute is the text"1337", not a function. The second element,"1337", is the payload data.- When the server processes the packet, the object is used as the event name. The automatic conversion to text fails because
toStringis no longer something the program can run, and the process crashes.

Conclusion
It was considered a high-severity vulnerability because it can crash a server just by using Socket.IO. Since this library is the most used for WebSockets in Node servers, millions of websites can be shot down with a single request. A fix was released in the latest version of Socket.IO.
References
https://nvd.nist.gov/vuln/detail/CVE-2023-32695https://security.snyk.io/vuln/SNYK-JS-SOCKETIOPARSER-5596892https://github.com/advisories/GHSA-cqmj-92xf-r6r9
Glossary
- API: a defined way for one piece of software to talk to another, like a menu of requests a service accepts.
- Array: an ordered list of values in JavaScript, written between square brackets.
- Asynchronous: work that does not wait for a previous step to finish before continuing, with messages passed between parts of the program.
- Attribute: a named value attached to an object, like a label on a compartment.
- Back end: the part of a system that runs on the server, out of sight of the user.
- CVE: a public identifier for a known security vulnerability, so people can refer to the same bug unambiguously.
- Denial of Service (DoS): an attack that makes a service stop working for its normal users, rather than stealing data.
- Event: a named happening in a program, such as a message arriving, that other code can listen for.
- EventEmitter: a Node.js library that delivers events to the code registered to listen for them.
- Exception: an error signal that interrupts normal execution of a program.
- Express: a popular Node.js library for building web servers.
- Front end: the part of a system that runs in the user's browser.
- Function: a named block of code that can be run when called.
- High severity: a rating meaning the impact of a vulnerability is serious.
- HTTP: the protocol browsers and servers use to exchange web pages and requests.
- JavaScript: the programming language that runs in browsers and, through Node.js, on servers.
- Library: a reusable bundle of code that programmers include in their own projects.
- Node.js: a program that lets JavaScript run on a server instead of only in a browser.
- Object: a collection of named values in JavaScript, written between curly braces.
- Override: to replace an existing property or function with your own version.
- Packet: a single message sent over a connection.
- Payload: the actual content carried inside a message, as opposed to its label or type.
- Port: a numbered door on a computer through which network traffic arrives.
- Proof of concept: a small working demonstration that proves a vulnerability is real.
- Property: a named value stored on an object.
- Reference: a way to point at or look up a particular entry.
- Server: a computer or program that provides a service to other computers over a network.
- Socket.IO: a JavaScript library for real-time, two-way communication between browser and server.
- Source code: the human-readable text of a program.
- String: a piece of text, a sequence of characters.
- toString(): a standard JavaScript function that turns a value into text; it is called automatically in some situations.
- Type: the kind of a value, such as text, number or object.
- Unsanitised: used as received from the outside, without checking or cleaning first.
- WebSocket: a connection that stays open in both directions so either side can send messages at any time.