Security rules

NGB083: Cross-site WebSocket hijacking

Reports WebSocket servers that authenticate connections with cookies or the HTTP session but never check the handshake's Origin: ws WebSocketServer without verifyClient whose connection / upgrade handlers read cookies or the session, Socket.IO servers that use cookie / session middleware without allowRequest, and Socket.IO cors: { origin: true | '*', credentials: true }..

Property Value
Rule ID NGB083
Category Angular SSR security
Default severity high
CWE CWE-1385, CWE-346
Built-in presets recommended: error, strict: error

Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.

What it checks

Reports WebSocket servers that authenticate connections with cookies or the HTTP session but never check the handshake's Origin: ws WebSocketServer without verifyClient whose connection / upgrade handlers read cookies or the session, Socket.IO servers that use cookie / session middleware without allowRequest, and Socket.IO cors: { origin: true | '*', credentials: true }.

Why it matters

The WebSocket handshake is not protected by the same-origin policy or CORS: any website can open new WebSocket('wss://your-app/ws') and the browser attaches the victim's cookies. If the server trusts those cookies and does not verify the Origin header, the attacker's page gets a fully authenticated, bidirectional channel — reading private messages and sending commands as the user (cross-site WebSocket hijacking).

Detection behavior and supported patterns

  • new WebSocketServer({ … }) / new WebSocket.Server({ … }) from ws without verifyClient, whose wss.on('connection', (ws, req) => …) or same-file server.on('upgrade', …) handler reads req.headers.cookie, req.session, req.cookies, a session parser or cookie.parse, and never mentions the origin
  • new Server(httpServer, { … }) from socket.io without allowRequest, with io.use(…) / io.engine.use(…) / io.on('connection', …) handlers that use cookies or the session (medium confidence)
  • Socket.IO cors: { origin: true | '*' | (o, cb) => cb(null, true), credentials: true } (high)

Example

TypeScript
const wss = new WebSocketServer({ server });wss.on('connection', (ws, req) => {  const user = sessions.get(cookie.parse(req.headers.cookie ?? '').sid);  ws.on('message', (m) => handle(user, m));});

Safer alternative

TypeScript
const wss = new WebSocketServer({  server,  verifyClient: ({ origin }) => ALLOWED_ORIGINS.includes(origin),});

Remediation

Verify the Origin header against an allowlist during the handshake (verifyClient for ws, allowRequest for Socket.IO) before trusting cookies, or authenticate WebSocket connections with a short-lived token that a cross-site page cannot read.

False-positive considerations

  • Servers reachable only from a private network are reported; the network boundary is not visible.

Known limitations

  • Origin checks inside helper functions called from the handler are not seen; mentioning origin anywhere in the handler counts as a check.
  • Socket.IO servers created with the call form (require('socket.io')(server)) and other WebSocket libraries (uWebSockets.js, SockJS) are not analyzed.
  • Token-based authentication sent in the first message is safe and not reported; tokens in cookies are treated as cookie authentication.

References

Run it

Shell
ngbastion explain NGB083