Skip to content
Access control Write-up Beginner

Next.js Middleware Authentication Bypass (CVE-2025–29927): Exploitation, Impact, and Mitigation

Next.js middleware skipped its own execution when a request carried a specially crafted x-middleware-subrequest header, so the authorisation checks inside middleware never ran. An unauthenticated attacker could therefore reach pages and APIs that middleware was supposed to protect.

prashunbaral Follow

Published on medium.com

Next.js is a web framework created by Vercel that is open-source and supports React applications with functionalities such as server-side rendering and static generation. A critical vulnerability that allowed an unauthenticated attacker to bypass middleware-based authorization handling.

Prashun Baral, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The framework trusted a client-supplied header to decide that middleware had already executed, and skipped the middleware without verifying that the header came from an internal subrequest.

Impact

An unauthenticated attacker could access confidential data, other users' control panels, or protected APIs that relied on middleware for access control.

What the attacker needed

Target runs a vulnerable Next.js version; app uses middleware for auth checks; attacker can send custom headers

The fix

The issue was fixed in patched Next.js releases, and developers should also avoid relying solely on middleware for authentication and reject unexpected x-middleware-subrequest headers.

How it was found

  1. Identify a Next.js application that uses middleware for authentication or authorisation.
  2. Send a request to a protected route with the x-middleware-subrequest header set to the middleware name for that version.
  3. For older layouts the value includes the pages directory and file name; for newer layouts it is just the middleware name, optionally under src.
  4. On versions with a recursion limit, repeat the middleware name several times separated by colons.
  5. Observe that the middleware is skipped and the protected content is returned without authentication.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

Middleware is code that runs before a page is served, often used to check whether you are logged in. Next.js had a shortcut: if a request carried a certain header, the framework assumed the middleware had already run and skipped it. An attacker could add that header by hand and walk past the login check.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.