Security

Fix the Next.js x-middleware-set-cookie HttpOnly cookie leak (CVE-2026-119346)

Mayur Dabhi
Mayur Dabhi
November 25, 2026
12 min read

You run curl -si against your login endpoint and the session token shows up twice. Once in set-cookie, where it belongs, and once in x-middleware-set-cookie. The cookie is marked HttpOnly, but that second copy is a plain response header, and any script on your origin can read it. This is the Next.js x-middleware-set-cookie leak, CVE-2026-119346, fixed in the November 2026 security release.

Short answer

Edge Route Handlers and Edge API routes that call NextResponse.cookies.set() copy the serialized cookie into the internal x-middleware-set-cookie header on Next.js 16.0.0 to 16.4.4 and 15.0.0 to 15.5.28. Upgrade to next@16.4.5 or next@15.5.29 (16.4.6 is the current latest). If you can't upgrade today, write cookies with res.headers.append('Set-Cookie', ...) or strip the header at your proxy with proxy_hide_header x-middleware-set-cookie;.

Which Next.js versions leak cookies through x-middleware-set-cookie?

Every next release from 16.0.0 through 16.4.4, and from 15.0.0 through 15.5.28, can leak. That's the range in the GitHub advisory GHSA-f3mh-rxg3-f5g2, published November 3, 2026. It's rated Moderate with a CVSS v4 score of 6.0. The CVE record itself only appeared on November 16, which is why the CVE pages trail the fix by almost two weeks.

LineAffectedFirst fixedNote
16.x (Active LTS)>= 16.0.0, <= 16.4.416.4.516.4.6 (Nov 9) is the current latest, no security changes of its own
15.x (Maintenance LTS)>= 15.0.0, <= 15.5.2815.5.29Published under the backport dist-tag
14.xNot listedn/aOutside the advisory ranges. I haven't seen anyone confirm how 14.x Edge routes behave.

You'll find other ranges online. A couple of articles say "16.0.0 through 16.4.2", and one CVE aggregator words it as "until 15.5.29 and 16.4.5", which reads as if the fixed versions were vulnerable. Go with the advisory. 16.4.4 is the last vulnerable 16.x release, and I reproduced the leak on exactly that version below.

The bug is narrow. The official release post limits it to App Router Edge Route Handlers and Pages Router Edge API routes that set cookies with NextResponse.cookies.set(). It says the leak "did not affect Node.js Route Handlers or routes setting cookies through cookies().set()". An attacker also needs to run JavaScript on your origin already: "This is not an independent way to execute JavaScript." So the real-world risk is an XSS bug that would normally be stopped at HttpOnly getting your session token anyway.

Why can an HttpOnly cookie leak through a response header?

Because HttpOnly only protects the Set-Cookie header, and the leaked copy lives in a different header. MDN describes HttpOnly as forbidding JavaScript from reading the cookie through document.cookie. The Fetch spec also lists Set-Cookie as a forbidden response header name, so response.headers.get('set-cookie') returns nothing in a browser. A custom header like x-middleware-set-cookie gets neither protection.

On the same origin, this is enough to read it:

Same-origin script (what an XSS payload would run)
const res = await fetch('/api/edge-login');
console.log(res.headers.get('x-middleware-set-cookie'));
// on 16.4.4: "session=s3cr3t-token; Path=/; Secure; HttpOnly; SameSite=lax"

I didn't run that in a browser (my lab server has none). Node's fetch in the check script further down read exactly that value from the header, and the browser side follows from the Fetch spec: nothing marks this header as forbidden.

Cross-origin pages can't do this. Browsers only expose CORS-safelisted response headers unless the server lists more in Access-Control-Expose-Headers. That's why the advisory needs same-origin script.

The header itself isn't a mistake. Next.js added it in PR #65008 so cookies set in Middleware show up in cookies() inside Server Components during the same request, and PR #67924 extended it to Server Actions in July 2024. If you've fought with "Setting cookies in middleware and reading in server components" (GitHub discussion #50374), this header is the answer to that problem. My post on how React Server Components work covers why that request-time cookie view matters.

Where the header gets written is the actual bug. In packages/next/src/server/web/spec-extension/response.ts, the NextResponse constructor wraps its cookie jar in a proxy. Every set or delete runs headers.set('x-middleware-set-cookie', ...) with all current cookies serialized and joined by commas. That happens on any NextResponse, not just in Middleware. For Middleware on a self-hosted server, the router moves the header onto the incoming request and doesn't send it to the browser. Edge route responses had no such step.

The fix, commit 809be72 in the v16.4.4...v16.4.5 compare, is titled "Strip internal middleware cookie headers from edge responses". It doesn't touch NextResponse. The header is still created, and then web/adapter.ts and next-server.ts delete it before the response leaves the server. Keep that in mind if you run Next.js through an adapter that doesn't use that server code, because the adapter needs its own fix.

It shows up in real apps too. auth0/nextjs-auth0 issue #2340 (Next.js 15.5.3, September 2025) has a header dump where x-middleware-set-cookie carries __session__0=eyJlbmMiOiJB..., the session cookie value, right next to the real set-cookie lines.

I reproduced the leak on Next.js 16.4.4 and 16.4.5

I built two App Router apps with identical source: one pinned to next@16.4.4, one to next@16.4.5. The server has Node v25.2.1 and npm 11.6.2, and npm installed React 19.3.1 and TypeScript 5.9.3 in both. I ran them with next build and next start, not the dev server, and set NEXT_TELEMETRY_DISABLED=1. There's no browser on this machine, so all checks are curl and Node fetch. No Nginx either, which comes up later.

The vulnerable pattern is the one you'd write without thinking twice:

app/api/edge-login/route.ts
import { NextResponse } from 'next/server';

export const runtime = 'edge';

export async function GET() {
  const res = NextResponse.json({ ok: true });
  res.cookies.set('session', 's3cr3t-token', {
    httpOnly: true,
    secure: true,
    sameSite: 'lax',
    path: '/',
  });
  return res;
}

Next to it I added controls: node-login (same code, Node runtime), edge-headers-cookies (Edge, but using cookies() from next/headers), edge-append (the workaround), edge-mixed (the workaround done wrong) and a proxy.ts that sets an HttpOnly cookie on /proxy-test.

The first build refused to finish

npm audit in the 16.4.4 app already flagged it: next 16.0.0 - 16.4.4, severity moderate, with seven November advisories, GHSA-f3mh-rxg3-f5g2 among them, and an offer to install next@16.4.6. Then npx next build compiled everything, printed the route table, and failed:

Terminal: next build on 16.4.4 (first run)
▲ Next.js 16.4.4 (Turbopack)
...
⚠ The Edge Runtime is deprecated. You can use the "nodejs" runtime instead. Learn more: https://nextjs.org/docs/messages/edge-runtime-deprecated
⚠ Using edge runtime on a page currently disables static generation for that page
...
> Build error occurred
Error [SecurityFatalError]: Your version of Next.js is affected by a known security vulnerability.

**We strongly recommend you upgrade Next.js.**

This command stopped to show the upgrade recommendation. Retry the same command to continue the original task.
...
Reference: https://registry.npmjs.org/-/npm/v1/security/advisories/bulk
    at ignore-listed frames {
  exitCode: 1
}

Running the same command again finished with exit 0. It happened a second time when I rebuilt after adding a route, and the retry passed again. The 16.4.5 build never showed it. The message also suggested next upgrade --agent=security, which I didn't run, since the whole point was to keep 16.4.4 vulnerable. If your CI builds a vulnerable version, expect this error to fail a job that passed yesterday.

curl on both versions

Terminal: the same route on 16.4.4 (:3101) and 16.4.5 (:3102)
$ curl -si http://localhost:3101/api/edge-login
HTTP/1.1 200 OK
content-type: application/json
set-cookie: session=s3cr3t-token; Path=/; Secure; HttpOnly; SameSite=lax
x-middleware-set-cookie: session=s3cr3t-token; Path=/; Secure; HttpOnly; SameSite=lax
Date: Mon, 23 Nov 2026 22:42:29 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Transfer-Encoding: chunked

{"ok":true}

$ curl -si http://localhost:3102/api/edge-login
HTTP/1.1 200 OK
content-type: application/json
set-cookie: session=s3cr3t-token; Path=/; Secure; HttpOnly; SameSite=lax
Date: Mon, 23 Nov 2026 22:42:29 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Transfer-Encoding: chunked

{"ok":true}

There it is. On 16.4.4 the full serialized cookie, flags and all, sits in x-middleware-set-cookie. On 16.4.5 the header is gone and set-cookie is unchanged. Then every route on both servers:

Route16.4.4 x-middleware-set-cookie16.4.5
Edge + NextResponse.cookies.set()present (leak)gone
Node + NextResponse.cookies.set()nonenone
Edge + cookies().set() from next/headersnonenone
Edge + headers.append('Set-Cookie')nonenone
Edge + append, then cookies.set()present, appended cookie droppedgone, appended cookie still dropped
proxy.ts on next startnonenone

That matches the release post: Node routes and cookies().set() are clean. The Proxy row confirms what the router source suggested, that self-hosted Middleware/Proxy doesn't send the header out. In Next.js 16, Proxy defaults to the Node.js runtime anyway. All of this is plain next start. On Netlify the Middleware case does leak, which I cover in the hosting section.

The workaround that quietly loses a cookie

The mixed route was the surprise. It appends the session cookie by hand, then sets a harmless theme cookie the normal way:

app/api/edge-mixed/route.ts
import { NextResponse } from 'next/server';

export const runtime = 'edge';

// Gotcha: append a Set-Cookie, then call cookies.set() on the same response
export async function GET() {
  const res = NextResponse.json({ ok: true });
  res.headers.append(
    'Set-Cookie',
    'session=s3cr3t-token; Path=/; HttpOnly; Secure; SameSite=Lax'
  );
  res.cookies.set('theme', 'dark', { path: '/' });
  return res;
}
Output: edge-mixed on 16.4.4 and 16.4.5
$ curl -si http://localhost:3101/api/edge-mixed | grep -i -E 'set-cookie|x-middleware'
set-cookie: theme=dark; Path=/
x-middleware-set-cookie: theme=dark; Path=/

$ curl -si http://localhost:3102/api/edge-mixed | grep -i -E 'set-cookie|x-middleware'
set-cookie: theme=dark; Path=/

Both versions lost the session cookie. The cookies.set() call rewrote the Set-Cookie headers from its own jar, which never knew about the appended one. The OpenNext advisory warns about exactly this, and here it is on a stock next start. Upgrading removes the leaked header but doesn't bring the cookie back. In a real app I'd expect that to look like a login that silently doesn't stick.

The version that works appends every cookie and never touches res.cookies:

app/api/edge-append-two/route.ts
import { NextResponse } from 'next/server';

export const runtime = 'edge';

// Fix for the mixed case: append EVERY cookie, never call res.cookies.set()
export async function GET() {
  const res = NextResponse.json({ ok: true });
  res.headers.append(
    'Set-Cookie',
    'session=s3cr3t-token; Path=/; HttpOnly; Secure; SameSite=Lax'
  );
  res.headers.append('Set-Cookie', 'theme=dark; Path=/');
  return res;
}
Result: edge-append-two on 16.4.4
$ curl -si http://localhost:3101/api/edge-append-two | grep -i -E 'set-cookie|x-middleware'
set-cookie: session=s3cr3t-token; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: theme=dark; Path=/

Both cookies, no leaked header, on the vulnerable version. Use append, not headers.set, for each one. The auth0 issue above shows what set does with several cookies: "only the last cookie is written to the response."

Check your own deployment with curl and a CI script

Hit every route that sets a cookie and grep for the header. Anything printed means you leak:

One-off check
curl -si https://your-app.example.com/api/login | grep -i -E 'set-cookie|x-middleware'

I used -si (a full GET, headers printed) for every check, not -I, so the handler runs exactly as it does for a real visitor. If your login route only accepts POST, add -X POST and the body it expects. A grep for runtime = 'edge' in your repo finds candidate routes, but it won't tell you what the deployed server actually sends.

For CI I wrote a small Node script. It fetches each path without following redirects and exits 1 if any response carries the header:

check-cookie-leak.mjs
// Usage: node check-cookie-leak.mjs <baseUrl> [path ...]
// Exits 1 if any response carries the internal x-middleware-set-cookie header.
const [base, ...paths] = process.argv.slice(2);
if (!base) {
  console.error('usage: node check-cookie-leak.mjs <baseUrl> [path ...]');
  process.exit(2);
}
const targets = paths.length ? paths : ['/'];
let leaks = 0;

for (const path of targets) {
  const url = new URL(path, base);
  try {
    const res = await fetch(url, {
      redirect: 'manual',
      signal: AbortSignal.timeout(10_000),
    });
    const leaked = res.headers.get('x-middleware-set-cookie');
    if (leaked) {
      leaks++;
      console.log(`LEAK: ${path} (${res.status}) x-middleware-set-cookie: ${leaked}`);
    } else {
      console.log(`ok:   ${path} (${res.status})`);
    }
  } catch (err) {
    leaks++;
    console.log(`ERROR: ${path} ${err.message}`);
  }
}

console.log(leaks ? `${leaks} problem(s) found` : 'no x-middleware-set-cookie header found');
process.exit(leaks ? 1 : 0);
Output: 16.4.4 vs 16.4.5
$ node check-cookie-leak.mjs http://localhost:3101 /api/edge-login /api/node-login /api/edge-headers-cookies /api/edge-append /api/edge-mixed /proxy-test; echo "exit=$?"
LEAK: /api/edge-login (200) x-middleware-set-cookie: session=s3cr3t-token; Path=/; Secure; HttpOnly; SameSite=lax
ok:   /api/node-login (200)
ok:   /api/edge-headers-cookies (200)
ok:   /api/edge-append (200)
LEAK: /api/edge-mixed (200) x-middleware-set-cookie: theme=dark; Path=/
ok:   /proxy-test (200)
2 problem(s) found
exit=1

$ node check-cookie-leak.mjs http://localhost:3102 /api/edge-login /api/node-login /api/edge-headers-cookies /api/edge-append /api/edge-mixed /proxy-test; echo "exit=$?"
ok:   /api/edge-login (200)
...
ok:   /proxy-test (200)
no x-middleware-set-cookie header found
exit=0

It only uses built-in fetch, so there's nothing to install. Run it after deploy against staging, with the cookie-setting paths listed explicitly. A network error counts as a failure on purpose: a check that silently passes when the server is down isn't worth having. One limitation: it sends GET with no body. For a POST login route you'd need to extend it with a method and payload.

Upgrade, headers.append or Nginx: which fix to pick

Upgrade if you can. The other options cover the gap while an upgrade waits on something else.

FixWhat it coversCatch
Upgrade to 16.4.5+ or 15.5.29Edge Route Handlers and Edge API routes, plus six other November advisoriesDoesn't fix the Middleware case on Netlify or OpenNext by itself
res.headers.append('Set-Cookie', ...)Routes you change by handOne cookies.set() on the same response drops the appended cookies
Strip at Nginx or CDNEverything behind that proxy, including code you didn't writeMust cover every path to the origin, cached responses included
Move the route to the Node runtimeThat route (no header on 16.4.4 in my test)Changes where the code runs, so test it like any runtime change

Upgrade

These are the commands from the release post, with one comment corrected:

Terminal
npm install next@15.5.29  # for 15.5
npm install next@16.4.5   # for 16.4

The official page says # for 16.5 on the second line. That's a typo, there's no 16.5 stable release. I'd go straight to 16.4.6 on the 16 line, since it's the latest and it includes the fix. I only tested 16.4.5, the first fixed version. After installing, check that the lockfile actually moved and rebuild. A container image built last week still has the old node_modules.

headers.append, if you can't upgrade today

Build the cookie string yourself and append it, as in edge-append-two above. On 16.4.4 that route sent both cookies with no leaked header. The rule is all or nothing per response: once any code calls res.cookies.set() or res.cookies.delete() on that response, your appended cookies are gone. Auth libraries that set cookies on the response you return count too, so grep for them before you trust this.

Strip the header in Nginx

If you self-host behind Nginx, one directive removes the header from every proxied response and leaves Set-Cookie alone:

nginx-snippet.conf (location block, not run)
# Self-hosted Next.js behind Nginx: drop the internal header, keep Set-Cookie
location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_hide_header x-middleware-set-cookie;
}

Nginx isn't installed on my lab server, so this exact config never ran. What I ran instead was a tiny Node reverse proxy that does the same single thing, deleting that one response header, in front of the 16.4.4 server. Through it, /api/edge-login returned only set-cookie and the check script printed no x-middleware-set-cookie header found with exit 0. The proxy_hide_header docs allow it in http, server and location. I didn't test how it inherits between levels, so I'd put it in every location that proxies to Next.js. If your Nginx setup is new to you, my Nginx configuration guide covers server blocks and reverse proxying.

Stripping it at the edge is safe for the feature the header exists for. The cookie merge for Server Components happens inside the Next.js server before the response goes out, so the browser never needed this header.

Netlify, OpenNext and other hosts need their own update

If you don't run next start yourself, bumping next may not be the whole fix. The adapters that turn a Next.js build into platform functions have their own copy of the logic.

Netlify: Middleware still leaks after the Next.js upgrade

The Netlify changelog says the leak also affects cookies set in Middleware there, and "Upgrading Next.js alone does not fix the Middleware case on Netlify." That case is fixed in @netlify/plugin-nextjs 5.16.6. An auto-installed adapter picks it up on redeploy. A pinned one has to be bumped by hand.

I couldn't test any hosted platform for this post. On those, run the check script against the deployed URL after the redeploy, and include a path covered by your Middleware or Proxy matcher, since that's the case Netlify says the Next.js upgrade alone misses.

What to check before you ship the fix

Should you rotate sessions? If you know you had an XSS bug on an affected origin while running a vulnerable version, I would. Otherwise it's a judgment call. And don't "fix" it by dropping HttpOnly, which makes the cookie readable to every script through document.cookie. If this is also your first jump to 16.4, the React 19.3 browser() API in Next.js 16.4 post covers another change on that line.

FAQ

Is Next.js 14 affected by CVE-2026-119346?

Not according to the advisory. GHSA-f3mh-rxg3-f5g2 lists affected ranges starting at 15.0.0, so 14.x isn't covered. The x-middleware-set-cookie header itself predates 15, though (PR #65008 added it and PR #67924 extended it in July 2024), and I haven't seen anyone test 14.x Edge routes. Run the curl check against your own app before you assume it's clean.

Is there a public PoC, or is CVE-2026-119346 exploited in the wild?

I couldn't find a public proof of concept beyond the tests in the Next.js fix, and the CVE isn't in CISA's Known Exploited Vulnerabilities catalog. Rapid7 lists an EPSS score of about 0%. It also needs attacker JavaScript already running on your origin, so it turns an existing XSS bug into session theft rather than opening a new way in.

Is 16.4.4 or 15.5.28 enough?

No. Those are the last vulnerable releases on each line. The first fixed versions are 16.4.5 and 15.5.29, and 16.4.6 is the current 16.x latest. Check what's actually installed with npx next --version or your lockfile, because a caret range in package.json doesn't tell you what CI resolved.

Does removing HttpOnly from the cookie fix the leak?

No, it makes things worse. Without HttpOnly any script on the page can read the cookie through document.cookie, which is the exact attack the flag exists to stop. The leak is in the extra header, so remove the header: upgrade, write cookies with headers.append, or strip x-middleware-set-cookie at your proxy.

Why does npm audit not flag Next.js 16.4.4?

It does now. When I ran npm audit on a 16.4.4 install on November 24, 2026, it reported next 16.0.0 - 16.4.4 as moderate and listed GHSA-f3mh-rxg3-f5g2. If yours stays quiet, check that your lockfile really resolves 16.4.4. Its suggested npm audit fix --force installs 16.4.6, which it warns is outside the stated dependency range, so review that change before you commit it.

Does Vercel hosting protect me if I don't upgrade?

No source I found says so. The advisory and the release post list upgrading as the fix and don't mention any platform-level mitigation. Upgrade and redeploy, then run the check script against the deployed URL to see what the platform actually sends for your cookie routes.

Next.js Security Cookies Edge Runtime CVE
Mayur Dabhi

Mayur Dabhi

Full Stack Developer with 5+ years of experience building scalable web applications with Laravel, React, and Node.js.