How to fix: WebSocket Message Data Passed Directly to eval()
The page opens a WebSocket connection and, in the same script, calls eval() or new Function() directly on the incoming message's .data property, executing whatever the WebSocket server (or anyone able to inject frames into the connection) sends as JavaScript.
Why it matters
Every message received over the WebSocket is executed as code with full page privileges. A compromised or spoofed WebSocket server, a MITM on an unencrypted ws:// connection, or a server-side bug that echoes attacker input back over the socket all become full client-side remote code execution.
WebSocket messages should be parsed as data (JSON.parse for structured payloads), never executed as code. eval(event.data) or new Function(event.data) treats the remote server as a fully trusted source of executable JavaScript, removing any distinction between a data channel and a code channel.
How to fix it
- 1Replace eval(event.data) / new Function(event.data) with JSON.parse(event.data) and handle the resulting data explicitly.
- 2Validate the parsed message against an expected schema before acting on it.
- 3If the server needs to trigger client behavior dynamically, send a message type/action identifier instead of executable code, and switch on it in a fixed handler.
Code examples
Parse, do not execute
// Bad
const ws = new WebSocket('wss://api.example.com/ws');
ws.onmessage = (event) => eval(event.data);
// Good
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === 'update') applyUpdate(msg.payload);
};References
Detected automatically by VulnRadar. The check id is cs-websocket-eval-message-data, stable across every scan so you can filter or diff on it.
More client-side checks
- HighCSP Allows 'unsafe-inline' Scripts
- MediumCSP Allows 'unsafe-eval'
- HighpostMessage Without Origin Validation
- HighSensitive Data Stored in localStorage
- HighDOM XSS via location.hash Assignment
- Highdocument.write() in Page Scripts
- Higheval() in Client-Side JavaScript
- MediumExternal Script Without Subresource Integrity
Questions
- How severe is WebSocket Message Data Passed Directly to eval()?
- VulnRadar rates this high severity in the client-side category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
- Does VulnRadar check for this automatically?
- Yes. It runs as part of every scan under the check id cs-websocket-eval-message-data, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does WebSocket Message Data Passed Directly to eval() map to?
- It maps to CWE-94. In the OWASP Top 10 it falls under A03:2021.
Missing a check?
The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.