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

no-unanchored-ws-origin-check

Disallow unanchored WebSocket Origin regexes

Next step

no-unanchored-ws-origin-check 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 WebSocket handshake that admits an Origin through a regex missing its ^ or $ anchor. 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-1385 (Missing Origin Validation in WebSockets)
SeverityHigh (cross-site WebSocket hijacking of a logged-in user)
Auto-FixNone
CategorySecurity
Best Forws servers that rely on the Origin header to keep other sites out

Vulnerability and Risk

Vulnerability: Browsers attach cookies to a cross-site WebSocket handshake, and the Same-Origin Policy does not apply to WebSockets. The Origin header is the only thing that tells the application's own page apart from an attacker's page riding the user's session.

Risk: An allowlist written as /^https:\/\/console\.corp\.local/ admits https://console.corp.local.attacker.example, a host the attacker owns. A regex without ^ admits https://evilconsole.corp.local. Either way the attacker's page opens the socket with the victim's cookies and reads or drives the session. The protocol-ctf ws-2 lab is this shape.

Rule Details

The rule reports R.test(origin), R.exec(origin) or origin.match(R) where R is a regex literal (inline or through a const) with a top-level alternative that does not start with ^ or does not end with an unescaped $, and origin is the handshake's Origin header (inline or through a const; ?? '' and ?. are followed), read from:

  • req.headers.origin of an .on('upgrade', (req) => ...) listener;
  • req.headers.origin of a ws server's .on('connection', (ws, req) => ...);
  • info.origin or info.req.headers.origin of a verifyClient callback in new WebSocketServer({...}) options.

A ws server is proven by import provenance, never by spelling.

Examples

❌ Incorrect

import { WebSocketServer } from 'ws';

const TRUSTED_ORIGIN = /^https:\/\/console\.corp\.local/; // no $
const wss = new WebSocketServer({ noServer: true });

server.on('upgrade', (req, socket, head) => {
  if (!TRUSTED_ORIGIN.test(req.headers.origin ?? '')) return socket.destroy();
  wss.handleUpgrade(req, socket, head, (ws) => wss.emit('connection', ws, req));
});

✅ Correct

import { WebSocketServer } from 'ws';

const ALLOWED_ORIGINS = new Set(['https://console.corp.local']);
const wss = new WebSocketServer({
  port: 8080,
  verifyClient: (info) => ALLOWED_ORIGINS.has(info.origin),
});

// or, when a regex is needed, anchor every alternative at both ends
const TRUSTED_ORIGIN = /^https:\/\/(?:console|admin)\.corp\.local$/;

Not a finding

  • Exact comparisons: origin === 'https://console.example', ALLOWED.has(origin).
  • Every alternative anchored: /^https:\/\/a\.example$|^https:\/\/b\.example$/.
  • Regexes on other headers or on req.url.
  • Origin checks in plain HTTP handlers (CORS) are out of scope here.
  • require-ws-message-authorization — The message side of the same server: a handler that serves a record chosen by the message without checking it against the user

Known False Negatives

Regexes the rule does not evaluate

// NOT DETECTED: the pattern is built at runtime
new RegExp(`^https://${host}`).test(req.headers.origin);

new RegExp(string) is not evaluated. An alternative wrapped in its own group, (^a$)|(^b$), is read as unanchored (a false positive): write ^(?:a|b)$.

Mitigation: Compare against a Set of exact origins instead of a regex.

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/no-unanchored-ws-origin-check': 'error'    },  },];
npm install -D oxlint-plugin-node-security
.oxlintrc.json
{  "jsPlugins": ["oxlint-plugin-node-security"],  "rules": {    "node-security/no-unanchored-ws-origin-check": "error"  }}
vite.config.ts
import { defineConfig } from 'vite-plus';export default defineConfig({  lint: {    jsPlugins: ['oxlint-plugin-node-security'],    rules: {      'node-security/no-unanchored-ws-origin-check': '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.