-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## The cap existed. fetchInternalImage was not asked for it.\n\nThe Next.js image optimizer is `/_next/image?url=...&w=...&q=...`. The handler routes to one of two functions in `packages/next/src/server/image-optimizer.ts`, depending on whether the `url` is absolute or path-relative. Absolute URLs (`https://cdn.example.com/photo.jpg`) go through `fetchExternalImage`. Path-relative URLs (`/photo.jpg`, `/api/preview/123.png`) go through `fetchInternalImage`. Both ultimately produce a `Buffer` that is handed to Sharp for re-encoding.\n\nThe capability the two functions implement is identical: fetch the upstream bytes for `href`, buffer them into a `Buffer`, return the buffer with content type and ETag. The shapes diverge only in how they reach the upstream. `fetchExternalImage` calls `fetch()` with an `AbortSignal.timeout(7_000)`. `fetchInternalImage` dispatches through Next.js's own router by handing a `MockedResponse` to `handleRequest`, then reads `mocked.res.buffers` after the response ends.\n\nOn January 6, 2026, PR #88183 landed `images.maximumResponseBody` with a 50 MB default and changed the external path to enforce it byte by byte inside the chunk loop:\n\n```typescript\n// fetchExternalImage, post-#88183\nconst chunks: Buffer[] = []\nlet totalSize = 0\n\nfor await (const c of res.body) {\n const chunk = Buffer.from(c)\n totalSize += chunk.byteLength\n if (totalSize > maximumResponseBody) {\n Log.error('upstream image response exceeded maximum size for', href, totalSize)\n throw new ImageError(\n 413,\n '\"url\" parameter is valid but upstream response is invalid'\n )\n }\n chunks.push(chunk)\n}\n\nconst buffer = Buffer.concat(chunks)\n```\n\nThe same PR did not touch `fetchInternalImage`. Its signature at the time of PR #88183:\n\n```typescript\nexport async function fetchInternalImage(\n href: string,\n _req: IncomingMessage,\n _res: ServerResponse,\n handleRequest: (\n newReq: IncomingMessage,\n newRes: ServerResponse,\n newParsedUrl?: NextUrlWithParsedQuery\n ) => Promise\n): Promise\n```\n\nThe underscore prefix on `_req` and `_res` is the TypeScript convention for \"this parameter is in the signature so the caller can pass it, but I do not use it.\" The two parameters are forwarded into `createRequestResponseMocks` for the socket and method, and never read again. The function had no concept of caller-supplied bytes. There was no slot for `maximumResponseBody` to live in.\n\n## Both branches of the ternary buffered everything into memory\n\n`next-server.ts` reaches both functions from a single ternary expression. Here is what PR #88183 wrote into that expression on January 6:\n\n```diff\n ? await fetchExternalImage(\n href,\n this.nextConfig.images.dangerouslyAllowLocalIP,\n+ this.nextConfig.images.maximumResponseBody,\n this.nextConfig.images.maximumRedirects\n )\n : await fetchInternalImage(\n href,\n req.originalRequest,\n res.originalResponse,\n handleInternalReq\n )\n```\n\nThe patched line and the unpatched alternative are inside the same `await` expression. The `+` line sits directly above `: await fetchInternalImage(`. The patch author's diff context window showed both branches at once. The branch he edited became safe. The branch he did not edit kept buffering arbitrary bytes through `MockedResponse.write`, which until then pushed every chunk onto `this.buffers` with no accumulator and no ceiling:\n\n```typescript\n// MockedResponse.write, pre-CVE-2026-44577\nwrite(chunk: Uint8Array | Buffer | string): boolean {\n if (this.resWriter) {\n return this.resWriter(chunk)\n }\n this.buffers.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))\n return true\n}\n```\n\n`mocked.res.buffers` is then read by `fetchInternalImage` after `await mocked.res.hasStreamed` resolves:\n\n```typescript\nconst buffer = Buffer.concat(mocked.res.buffers)\n```\n\n`Buffer.concat` is a memory operation. The size is whatever the upstream produced. If the upstream is a multi-gigabyte file matched by `images.localPatterns`, the process holds it in RAM until the request completes.\n\n## \"Internal\" in fetchInternalImage means \"routed through Next.js's own router\"\n\nThe function name suggests \"trusted, in-process, bounded.\" The behavior is the opposite. `fetchInternalImage` calls `handleInternalReq`, which is:\n\n```typescript\nconst handleInternalReq = async (\n newReq: IncomingMessage,\n newRes: ServerResponse\n) => {\n if (newReq.url === req.url) {\n throw new Error(`Invariant attempted to optimize _next/image itself`)\n }\n\n if (!this.routerServerHandler) {\n throw new Error(`Invariant missing routerServerHandler`)\n }\n\n await this.routerServerHandler(newReq, newRes)\n return\n}\n```\n\n`routerServerHandler` is the full Next.js router. Anything the developer's `next.config.js` can express runs here: rewrites, redirects, middleware, API routes, App Router pages, static files. The only thing the recursion guard blocks is the path `/_next/image` itself. Every other path the developer's router resolves is reachable.\n\nThe path filter is `hasLocalMatch(localPatterns, url)`. The shared library function in `match-local-pattern.ts`:\n\n```typescript\nexport function hasLocalMatch(\n localPatterns: LocalPattern[] | undefined,\n urlPathAndQuery: string\n): boolean {\n if (!localPatterns) {\n // if the user didn't define \"localPatterns\", we allow all local images\n return true\n }\n const url = new URL(urlPathAndQuery, 'http://n')\n return localPatterns.some((p) => matchLocalPattern(p, url))\n}\n```\n\nThe advisory says it plainly: \"by default, all patterns are allowed.\" A Next.js self-hoster who has not set `images.localPatterns` in `next.config.js`, which is the documented default, exposes every path their router resolves through `/_next/image`.\n\nThe exploit primitive on a default-configured self-hosted Next.js 15.5.15 or 16.2.4:\n\n```\nGET /_next/image?url=%2Flarge.bin&w=1920&q=75 HTTP/1.1\nHost: target.example\n```\n\nWhere `/large.bin` is any path the developer's router serves: a static asset, an API route that streams a database export, a rewrite that proxies to an internal backend. The router resolves the path. The handler writes the response into `MockedResponse`. `MockedResponse.write` pushes every chunk into `this.buffers`. `Buffer.concat` materializes the full size. Repeat the request from N concurrent connections; each connection holds its own copy in heap. Node hits its heap ceiling and the process exits.\n\nThe shape applies to rewrites too. A `next.config.js` rewrite that maps `/proxy/:path*` to `http://internal-bucket/:path*` does not become any safer for being matched by `localPatterns`. The image optimizer treats the rewrite the same way it treats a static file: load the entire upstream body into the in-process buffer. The \"internal\" in `fetchInternalImage` describes which router answers the request, not which network the response came from.\n\n## The patch is the parameter, threaded through four call sites\n\nCVE-2026-44577's fix lives in [commit 4e08dc7](https://github.com/vercel/next.js/commit/4e08dc7bceed20349e75ec673be2f73fa8c828d1) (PR #92920, merged April 16, 2026). The patch is mechanical. It adds `maximumResponseBody` as a parameter to `fetchInternalImage`, passes it down to `createRequestResponseMocks`, stores it on `MockedResponse`, and adds a four-line check inside `MockedResponse.write`:\n\n```diff\n+const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)\n+\n+if (this.maximumResponseBody !== undefined) {\n+ this.totalSize += buffer.byteLength\n+ if (this.totalSize > this.maximumResponseBody) {\n+ const error = new Error(\n+ `Response body exceeded maximum size of ${this.maximumResponseBody} bytes`\n+ ) as NodeJS.ErrnoException\n+ error.code = 'ERR_MAX_BODY_SIZE_EXCEEDED'\n+ this.destroy(error)\n+ return true\n+ }\n+}\n+\n+this.buffers.push(buffer)\n```\n\nAnd the call-site change in `next-server.ts`:\n\n```diff\n : await fetchInternalImage(\n href,\n req.originalRequest,\n res.originalResponse,\n+ this.nextConfig.images.maximumResponseBody,\n handleInternalReq\n )\n```\n\nThat `+` line is the line that did not exist on January 6 and existed on April 16. The PR #92920 description names the prior PR explicitly: \"In a previous PR https://github.com/vercel/next.js/pull/88183, we added `images.maximumResponseBody` but in only applied to external images. This PR ensures the same config also applies to internal images.\" The author of PR #92920 is the same Steven who authored PR #88183.\n\nThe value being threaded through, `this.nextConfig.images.maximumResponseBody`, has been on `nextConfig` since January with a 50 MB default. The integer was on the config object the whole time. The function call did not ask for it.\n\n## The fourth Next.js parallel implementation gap of this release window\n\nThis is the parallel-implementation-gap pattern. A capability is implemented by two or more parallel modules; a security check is wired to one and not the others. Three other Next.js CVEs in the same release window are the same shape:\n\n- [CVE-2026-44578](/posts/nextjs-cve-2026-44578-pinned-to-localhost): the upgrade-handler in the self-hosted router-server lacked an absolute-URL guard that the Vercel edge runtime had. Two implementations of the same upgrade contract; one was hardened.\n- [CVE-2026-44575](/posts/nextjs-cve-2026-44575-matcher-recognized-json-not-rsc): the App Router renderer stripped `.rsc` and `.segments` suffixes that `getMiddlewareMatchers` did not learn. Two URL classifiers, one of them on the old Pages-era alphabet.\n- [CVE-2026-44573](/posts/nextjs-cve-2026-44573-resolveroutes-prefixed-pages-not-data): `resolveRoutes` prefixed direct-page requests with the default locale and did not prefix data requests. Two locale-resolution sites inside one function, eleven lines re-typed apart.\n\nCVE-2026-44577 is the smallest-distance instance of the four. The two parallel implementations are not two files (Astro), not two runtimes (MLflow), not two branches of a function (Axios), not two suffix sets in a regex (CVE-2026-44575). They are two branches of one ternary expression, in the same `await`, in the same statement. The patch author had both branches open in the same editor view when the gate was wired to the first. The fix is the same value being passed to the function call on the other branch.\n\n## What the timeline says\n\nPR #88183, merged 2026-01-06, named the threat (`maximumResponseBody`), wrote the enforcement, and chose a default (50 MB). The author understood that buffering arbitrary upstream bytes into memory was a denial-of-service primitive. The PR title was `feat(next/image)!: add images.maximumResponseBody config`.\n\nPR #92920, merged 2026-04-16, threads the same value into the function call that sits four lines below the call PR #88183 patched. The PR title is `fix(next/image): ensure images.maximumResponseBody applies to local images too`.\n\nGHSA-h64f-5h5j-jqjh and CVE-2026-44577 were published 2026-05-06, twenty days after the fix. The GHSA's summary is \"Denial of Service in the Image Optimization API.\" It does not mention the parameter-threading shape. It does not mention that the value being threaded was defined three months earlier. It says only that \"We now apply response size limits consistently to internal image fetches, not just external ones.\"\n\nThe advisory framing leaves the reader with \"Vercel discovered an oversight.\" The patch framing tells you what the oversight was: a function call in a ternary, four lines below another call in the same ternary, was missing one argument. The argument was the property on `nextConfig` that the line above already named.\n\nPatched versions: Next.js 15.5.16 and 16.2.5. Versions 10.0.0 through 15.5.15 and 16.0.0 through 16.2.4 buffer any path the router resolves. Self-hosted only; Vercel-hosted deployments do not run this code path.\n\nAdvisory: [GHSA-h64f-5h5j-jqjh](https://github.com/vercel/next.js/security/advisories/GHSA-h64f-5h5j-jqjh)\nFix: [vercel/next.js@4e08dc7](https://github.com/vercel/next.js/commit/4e08dc7bceed20349e75ec673be2f73fa8c828d1) (PR #92920)\nPrior PR that introduced the cap: [vercel/next.js#88183](https://github.com/vercel/next.js/pull/88183)\n","closing_line":"The gap was the absence of a parameter in a function signature. The patch is the parameter.","hook_md":"PR #88183 landed in Next.js on January 6, 2026. It introduced `images.maximumResponseBody`, a 50 MB default cap on bytes the image optimizer would buffer before handing the result to Sharp. The cap was wired into one branch of a ternary in `next-server.ts`. The other branch of the same ternary, four lines below, was `fetchInternalImage`. It had no parameter for the cap to land in. CVE-2026-44577 is the patch from one hundred days later that adds the parameter.","post_id":421,"slug":"nextjs-cve-2026-44577-cap-wired-to-one-branch-of-the-ternary","title":"CVE-2026-44577: Next.js Wired maximumResponseBody to One Branch of the Ternary. fetchInternalImage Was the Other.","type":"initial","unreadable_sentence":"The cap was wired to one branch of the ternary on January 6. It was wired to the other branch on April 16. Both branches had been in the same expression the whole time."} -----BEGIN PGP SIGNATURE----- iHUEARYIAB0WIQRf0htP5+SjynlxywneZjl4jgkQJgUCalqKKAAKCRDeZjl4jgkQ JsXUAP44HTxAOtIC1NryJKzt5SCLuVFCKNbFew563GkNL/Gf4QEAh4eDqaiJKO/F CVG23OD1Aw5ax68zQqk6iEEwIDzXZwQ= =/7fX -----END PGP SIGNATURE-----