CVE-2024-24750 (published 2024-02-16T22:15Z)

Undici is an HTTP/1.1 client, written from scratch for Node.js. In affected versions calling `fetch(url)` and not consuming the incoming body ((or consuming it very slowing) will lead to a memory leak. This issue has been addressed in version 6.6.1. Users are advised to upgrade. Users unable to upgrade should make sure to always consume the incoming body.


Weakness information:
cwe_id: CWE-400
cwe_name: Uncontrolled Resource Consumption
description: The product does not properly control the allocation and maintenance of a limited resource, thereby enabling an actor to influence the amount of resources consumed, eventually leading to the exhaustion of available resources.
extended_description: 
            Limited resources include memory, file system storage, database connection pool entries, and CPU. If an attacker can trigger the allocation of these limited resources, but the number or size of the resources is not controlled, then the attacker could cause a denial of service that consumes all available resources. This would prevent valid users from accessing the product, and it could potentially have an impact on the surrounding environment. For example, a memory exhaustion attack against an application could slow down the application as well as its host operating system.
            There are at least three distinct scenarios which can commonly lead to resource exhaustion:
               
                  Lack of throttling for the number of allocated resources
                  Losing all references to a resource before reaching the shutdown stage
                  Not closing/returning a resource after processing
               
            Resource exhaustion problems are often result due to an incorrect implementation of the following situations:
               
                  Error conditions and other exceptional circumstances.
                  Confusion over which part of the program is responsible for releasing the resource.
               
         
         

Fix information:
repo_url: https://github.com/nodejs/undici
commit_details: repo_url: https://github.com/nodejs/undici
author: Matteo Collina
author_date: 2024-02-05 09:35:45+01:00
msg: Merge pull request from GHSA-9f24-jqhm-jfcw

* fetch: pull don't push

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* added tests

Signed-off-by: Matteo Collina <hello@matteocollina.com>

---------

Signed-off-by: Matteo Collina <hello@matteocollina.com>
repository: repo_url: https://github.com/nodejs/undici
repo_name: nodejs/undici
description: An HTTP/1.1 client, written from scratch for Node.js
repo_language: JavaScript
owner: nodejs
file_changes: filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -1099,10 +1099,10 @@ function fetchFinale (fetchParams, response) {
 
     const byteStream = new ReadableStream({
       readableStream: transformStream.readable,
-      async start (controller) {
+      async pull (controller) {
         const reader = this.readableStream.getReader()
 
-        while (true) {
+        while (controller.desiredSize >= 0) {
           const { done, value } = await reader.read()
 
           if (done) {
@@ -1113,6 +1113,7 @@ function fetchFinale (fetchParams, response) {
           controller.enqueue(value)
         }
       },
+      queuingStrategy: new ByteLengthQueuingStrategy({ highWaterMark: 16384 }),
       type: 'bytes'
     })
 
@@ -1927,6 +1928,7 @@ async function httpNetworkFetch (
   //     cancelAlgorithm set to cancelAlgorithm.
   const stream = new ReadableStream(
     {
+      highWaterMark: 16384,
       async start (controller) {
         fetchParams.controller.controller = controller
       },
@@ -1936,7 +1938,8 @@ async function httpNetworkFetch (
       async cancel (reason) {
         await cancelAlgorithm(reason)
       },
-      type: 'bytes'
+      type: 'bytes',
+      queuingStrategy: new ByteLengthQueuingStrategy({ highWaterMark: 16384 })
     }
   )
 

programming_language: JavaScript
filename: pull-dont-push.js
change_type: ModificationType.ADD
diff: @@ -0,0 +1,53 @@
+'use strict'
+
+const { test } = require('node:test')
+const assert = require('node:assert')
+const { fetch } = require('../..')
+const { createServer } = require('http')
+const { once } = require('events')
+const { Readable, pipeline } = require('stream')
+const { setTimeout: sleep } = require('timers/promises')
+
+const { closeServerAsPromise } = require('../utils/node-http')
+
+test('Allow the usage of custom implementation of AbortController', async (t) => {
+  let count = 0
+  let socket
+  const server = createServer((req, res) => {
+    res.statusCode = 200
+    socket = res.socket
+
+    // infinite stream
+    const stream = new Readable({
+      read () {
+        this.push('a')
+        if (count++ > 1000000) {
+          this.push(null)
+        }
+      }
+    })
+
+    pipeline(stream, res, () => {})
+  })
+
+  t.after(closeServerAsPromise(server))
+
+  server.listen(0)
+  await once(server, 'listening')
+
+  t.diagnostic('server listening on port %d', server.address().port)
+  const res = await fetch(`http://localhost:${server.address().port}`)
+  t.diagnostic('fetched')
+
+  // Some time is needed to fill the buffer
+  await sleep(1000)
+
+  assert.strictEqual(socket.bytesWritten < 1024 * 1024, true) // 1 MB
+  socket.destroy()
+
+  // consume the  stream
+  try {
+    /* eslint-disable-next-line no-empty, no-unused-vars */
+    for await (const chunk of res.body) {}
+  } catch {}
+})

programming_language: JavaScript