A pink background with concentric circles and a white bug icon in the center. Text reads, Next.js Middleware Authentication Bypass Vulnerability (CVE-2025-29927). Upwind logo in the top right corner.

Next.js Middleware Authentication Bypass Vulnerability (CVE-2025-29927)

Eliad Mualem March 24, 2025

Next.js middleware plays a key role in securing applications by enforcing authentication, managing access control, and applying security headers. However, a newly discovered vulnerability, CVE-2025-29927, allows attackers to bypass these protections entirely using a manipulated HTTP header.

Affected Versions

This flaw affects the following versions:

  • Next.js prior to 14.2.25
  • Next.js prior to 15.2.3

The Core Issue

Next.js prevents infinite middleware loops by tracking executions using the x-middleware-subrequest header. If this header exceeds a defined threshold (MAX_RECURSION_DEPTH, default: 5), middleware execution stops. Attackers can exploit this by manually setting the header, tricking Next.js into skipping security checks.

Vulnerable code example:

const runtime <strong>=</strong> await getRuntimeContext(params)
const subreq <strong>=</strong> params<strong>.</strong>request<strong>.</strong>headers[`x<strong>-</strong>middleware<strong>-</strong>subrequest`]
const subrequests <strong>=</strong> typeof subreq <strong>===</strong> 'string' <strong>?</strong> subreq<strong>.</strong>split(':') <strong>:</strong> []

const MAX_RECURSION_DEPTH <strong>=</strong> 5
const depth <strong>=</strong> subrequests<strong>.</strong>reduce(
  (acc, curr) <strong>=></strong> (curr <strong>===</strong> params<strong>.</strong>name <strong>?</strong> acc <strong>+</strong> 1 <strong>:</strong> acc),
  0
)

<strong>if</strong> (depth <strong>>=</strong> MAX_RECURSION_DEPTH) {
  <strong>return</strong> {
    waitUntil: <strong>Promise.</strong>resolve(),
    response: <strong>new</strong> <strong>runtime.context.Response</strong>(<strong>null</strong>, {
      headers: {
        'x-middleware-next': '1',
      },
    }),
  }
}

By injecting a header like x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware, attackers force Next.js to assume middleware has already executed, leading to a full security bypass.

Real-World Security Risks

This vulnerability enables various high-impact attacks:

  1. Authentication Bypass. Attackers can gain unauthorized access to restricted areas such as admin panels or user dashboards.
  2. CSP Header Bypass. Attackers can circumvent middleware-enforced security headers, causing these critical protections to disappear.
  3. Geographic Restrictions Bypass. Attackers can spoof their location to access content that is restricted by geographic boundaries.

Mitigation Steps

To enhance the security of your application, consider these essential steps:

  1. Upgrade to a Patched Version. Ensure your application is secure by upgrading to Next.js versions 15.2.3 or later, or 14.2.25 or later, as these releases have addressed the identified vulnerability.
  2. Block Malicious Headers. If an immediate update isn’t feasible, implement a temporary measure by filtering out the x-middleware-subrequest header at the web server level to mitigate potential risks


How Upwind Protects Against This Threat

Upwind’s Runtime Security is designed to detect and flag malicious requests attempting to exploit vulnerabilities.

Our platform offers the following capabilities:

  • Live API Traffic Monitoring. Continuously monitors API traffic to detect exploitation attempts.
  • AI-based Anomaly Detection. Utilizes advanced AI algorithms to identify abnormal request patterns.
  • Real-Time Alerts. Notifies security teams immediately to prevent active attacks.
  • Exposure Overview. Offers a detailed overview of resources running vulnerable Next.js versions, enabling teams to quickly evaluate and address potential risks.

Learn More

By leveraging Upwind’s runtime security, organizations gain enhanced visibility into exploitation attempts, allowing them to dynamically protect their applications. To learn more about how Upwind protects against vulnerabilities, schedule a demo.

Contents

Further Reading

behind-the-curtain-part-02

What’s Behind the Curtain? AWS Bedrock AgentCore Runtime Tear Down – Part II

Recap In Part I, we explored the AgentCore Runtime microVM from the inside and discovered we weren't alone - four platform binaries were running alongside our code, and one of them was quietly shipping logs to an AWS-internal S3 bucket. We left off with a question: what can we learn from these internal components, and…
behind-the-curtain-part-01

What’s Behind the Curtain? AWS Bedrock AgentCore Runtime Tear Down – Part I

Introduction When you deploy an AI agent to AWS Bedrock AgentCore Runtime, your code runs inside a Firecracker microVM - but it doesn't run alone. In this three-part series, we tear down the platform internals, document what we found, and assess how well the isolation holds up. Setting the Stage AWS Bedrock AgentCore Runtime is…
upwind-code

Upwind Code Expands Enterprise Coverage to Azure DevOps and Bitbucket Cloud

Modern development organizations rarely keep all their code in one place. Teams may use different version control platforms because of acquisitions, business-unit preferences, regional requirements, or existing development workflows. But when code is spread across multiple providers, application security coverage can become fragmented too. Today, Upwind Code adds support for Azure DevOps and Bitbucket Cloud.…
Add the Upwind RSS Feed to Slack
Connect the Upwind RSS Feed to your Slack.
Follow the how-to here.
Threat RSS
Add the Upwind RSS Feed to Slack
Connect the Upwind RSS Feed to your Slack.
Follow the how-to here.
Main RSS