Skip to main content
Skip to Content
interlace
Plugin: node-securityRules

require-ws-message-authorization

Require ws message handlers to authorize message-chosen keys

Next step

require-ws-message-authorization ships in eslint-plugin-node-security, with 31 of its 47 rules in the recommended preset.

Install
npm install -D eslint-plugin-node-security
eslint.config.js
import { configs } from 'eslint-plugin-node-security';

export default [configs.recommended];

Plugin pagenpmSource on GitHub

Detects a ws message handler that reads or writes a record chosen by the message without checking that record against the connection's user. This rule is part of eslint-plugin-node-security and provides LLM-optimized error messages with fix suggestions.

🚨 Security rule | Not in recommended; on in strict

Quick Summary

AspectDetails
CWE ReferenceCWE-639 (Authorization Bypass Through User-Controlled Key)
SeverityHigh (any logged-in user reads or writes any record)
Auto-FixNone
CategorySecurity
Best ForChat, collaboration and dashboard servers built on ws

Vulnerability and Risk

Vulnerability: A WebSocket authenticates once, at login or at the upgrade. Every message after that names its own target: { type: 'join', room: 'staff' }. A handler that indexes server state with that target and sends the result back trusts the client to ask only for what it may see.

Risk: Any authenticated client names another user's room, document or account and gets it. The protocol-ctf ws-1 lab is this shape: its say handler checks USERS[ws.user].rooms.includes(msg.room), its join handler checks only that the room exists, so a guest who sends join staff receives the staff history.

Rule Details

The rule reads an inline ws.on('message', fn) registered inside an inline wss.on('connection', (ws) => ...) listener, where wss is proven by import provenance to be a ws server (WebSocketServer, Server, WebSocket.Server; default, namespace, named and require forms). Inside the handler it reports a computed access STORE[key] when:

  • key is derived from the handler's parameters (the raw frame, through JSON.parse, destructuring and assignment);
  • STORE is neither the connection (ws.drafts[...] is the client's own state) nor derived from the message;
  • the access is used: it reaches a call argument (sent, stringified, broadcast) or a method is called on it (ROOMS[key].push(...));
  • no guard covers it. A guard is an if, ternary or && around the access, or an earlier if in an enclosing block whose consequent returns or throws, whose test names both the connection's identity (a ws.<prop> read, or a variable derived from one) and the same key.

Examples

❌ Incorrect

import { WebSocketServer } from 'ws';

const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', (ws) => {
  ws.on('message', (raw) => {
    const msg = JSON.parse(raw);
    if (!ws.user) return; // logged in, but not authorized for this room
    if (!HISTORY[msg.room]) return; // the room exists, nothing more
    ws.send(JSON.stringify({ history: HISTORY[msg.room] }));
  });
});

✅ Correct

import { WebSocketServer } from 'ws';

const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', (ws) => {
  ws.on('message', (raw) => {
    const msg = JSON.parse(raw);
    if (!ws.user) return;
    if (!USERS[ws.user].rooms.includes(msg.room)) return; // per-message check
    ws.send(JSON.stringify({ history: HISTORY[msg.room] }));
  });
});

Not a finding

  • A lookup bound to a variable (const u = USERS[msg.user]) or only tested (if (!HISTORY[msg.room])). The login credential check has exactly that shape, and the value never leaves the server as written.
  • State on the connection itself (ws.drafts[msg.slot]): it is the client's own.
  • A key not derived from the message (HISTORY[DEFAULT_ROOM]).
  • Other emitters: a connection listener on socket.io or net is not a ws server.

Known False Negatives

Handlers the rule does not follow

// NOT DETECTED: the handler is passed by reference
wss.on('connection', (ws) => ws.on('message', onMessage));

Handlers passed by reference, servers built in another file and the handleUpgrade callback form are not followed. The guard's polarity is not checked either: if (isMember(ws.user, k)) return; counts as a guard, because it proves the author compared the two, not that the comparison is right.

Mitigation: Register the message handler inline, or put the membership check in a helper the handler calls with both ws.user and the key.

Further Reading

Use this rule

npm install -D eslint-plugin-node-security
eslint.config.js
import nodeSecurity from 'eslint-plugin-node-security';export default [  {    plugins: { 'node-security': nodeSecurity },    rules: {      'node-security/require-ws-message-authorization': 'error'    },  },];
npm install -D oxlint-plugin-node-security
.oxlintrc.json
{  "jsPlugins": ["oxlint-plugin-node-security"],  "rules": {    "node-security/require-ws-message-authorization": "error"  }}
vite.config.ts
import { defineConfig } from 'vite-plus';export default defineConfig({  lint: {    jsPlugins: ['oxlint-plugin-node-security'],    rules: {      'node-security/require-ws-message-authorization': 'error',    },  },});

oxlint-plugin-node-security re-exports eslint-plugin-node-security/oxlint: the same rules under the same IDs, and no ESLint install. Why oxlint.

Did this rule catch something? Star the repo to get new CWE coverage as we ship it — or follow the AI-code-security benchmarks behind these rules.