WAF Rule for Next.js CVE-2025-29927 - Authentication Bypass (2025)

March 24, 2025 (2 months ago)

Next.js just disclosed a critical authentication bypass vulnerability that affects almost all versions. This is serious stuff - it could allow attackers to bypass your authentication regardless of which auth provider you're using.

The root of CVE-2025-29927 lies in how Next.js middleware processes the x-middleware-subrequest header. If an attacker crafts a request with this header set to specific values (like middleware, src/middleware, or repeated values depending on the app structure and Next.js version), the middleware logic can be completely bypassed. This means:

For example, sending a header like:

x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware

can bypass protections in some Next.js versions (Discovered by zhero_web_security).

This makes the vulnerability extremely dangerous for any app relying on middleware for security.

📦 Affected Versions and Patches

Here are the safe versions you should upgrade to:

⚡WAF rule to protect your Next.JS APP

Create a Custom Rule in Cloudflare WAF (Free Plan)

If you're on the free plan, here's how to create a custom rule:

  1. Navigate to Security > WAF > Custom rules
  2. Create a new rule named "next-js-CVE-2025-29927"
  3. Add this expression:
(len(http.request.headers["x-middleware-subrequest"]) > 0)

This rule blocks requests that contain the x-middleware-subrequest header, which is the key vector for this vulnerability. Here's why:

The len() function checks if the header exists and has any value. Even an empty value would be blocked, which is what we want since legitimate users should never send this header directly.

  1. Set action to "Block"
  2. Deploy!

Stay safe out there, and happy coding! 🚀