Zum Hauptinhalt springen
LIVE Intel Feed
"Not a Pentest" Notice: Fix guide for your own Next.js applications only.
Academy · CVE FeedCRITICAL · CVSS 9.1

CVE-2025-29927

Next.js Middleware Authorization Bypass

Impact: Attackers bypass all Next.js middleware-based authorization by adding the x-middleware-subrequest header to any request. Protected pages, API routes, and admin panels become publicly accessible. Affects all Next.js versions below 15.2.3 / 14.2.25 / 13.5.9 / 12.3.5.

9.1
CVSS Score
CRITICAL
Severity
2025-03-21
Published
15.2.3+
Fixed (v15)

How the Attack Works

# Normal request — middleware runs, redirects to /login:
GET /admin/dashboard HTTP/1.1
Host: yourapp.com
# → Middleware checks session → No session → 302 /login

# Exploit — middleware SKIPPED entirely:
GET /admin/dashboard HTTP/1.1
Host: yourapp.com
x-middleware-subrequest: middleware:middleware:middleware
# → Middleware SKIPPED → Direct access to /admin/dashboard ✅

The x-middleware-subrequest header was designed as an internal Next.js mechanism to track recursive middleware invocations and prevent infinite loops. A logic flaw caused Next.js to skip middleware execution entirely when this header was present — allowing any external attacker to include it and bypass all middleware-based security checks.

Step-by-Step Fix Runbook

1
Identify your Next.js version
# Check package.json or node_modules
cat package.json | grep '"next"'
node -e "require('next/package.json').version |> console.log(%)"

# Vulnerable versions:
# < 15.2.3  (Next.js 15.x)
# < 14.2.25 (Next.js 14.x)
# < 13.5.9  (Next.js 13.x)
# < 12.3.5  (Next.js 12.x)
2
Upgrade Next.js immediately
# npm
npm install next@latest

# yarn
yarn upgrade next

# pnpm
pnpm update next

# Verify patched version
node -e "console.log(require('next/package.json').version)"
# Must be: 15.2.3+, 14.2.25+, 13.5.9+, or 12.3.5+
3
Block x-middleware-subrequest at reverse proxy (immediate mitigation)
# Nginx — block the malicious header
# Add to server {} block:
server {
    # Strip x-middleware-subrequest header before proxying
    proxy_set_header x-middleware-subrequest "";
    # ...existing config
}

# Or explicitly deny requests containing it:
if ($http_x_middleware_subrequest) {
    return 403;
}
4
Cloudflare WAF rule (if using Cloudflare)
# Cloudflare Dashboard → Security → WAF → Custom Rules
# Rule expression:
(http.request.headers.names contains "x-middleware-subrequest")

# Action: Block
# This stops exploitation attempts before reaching your Next.js app
5
Move critical auth checks out of middleware
// VULNERABLE pattern — middleware-only auth:
// middleware.ts
export function middleware(req: NextRequest) {
  if (!req.cookies.get("session")) {
    return NextResponse.redirect("/login")
  }
}

// SECURE pattern — auth also in route handler/server component:
// app/dashboard/page.tsx
import { getServerSession } from "next-auth"
export default async function DashboardPage() {
  const session = await getServerSession()  // Server-side check
  if (!session) redirect("/login")          // Cannot be bypassed by header
  return <Dashboard />
}
6
Audit middleware.ts for security-critical logic
# Find all middleware files in your project
find . -name "middleware.ts" -o -name "middleware.js" | grep -v node_modules

# Review each for auth patterns that rely SOLELY on middleware
# Any auth check in middleware must be duplicated in the route handler
# Middleware should be treated as a "fast path" hint, not a security boundary

Architecture Lesson: Middleware is Not a Security Boundary

Key Takeaway

Next.js middleware runs at the edge and is optimized for performance — not as a security enforcement layer. Any security-critical authorization check in middleware.ts must also be present in the corresponding server component or API route handler. Treat middleware as a UX optimization ("redirect logged-out users faster"), not as the last line of defense.

Frequently Asked Questions

What exactly is the CVE-2025-29927 vulnerability?

The x-middleware-subrequest header is an internal Next.js header used to track recursive middleware calls and prevent infinite loops. A bug caused Next.js to skip middleware execution entirely when this header was present with a specific value. An attacker could add this header to any request to bypass all middleware-based authorization checks — accessing protected pages, API routes, and admin panels without authentication.

Am I affected if I don't use Next.js middleware for auth?

If your authorization checks are exclusively in server components, API route handlers, or a separate auth service — and not in middleware.ts — you are not directly vulnerable to authentication bypass. However, upgrading is still recommended as middleware may be added in future, and the header should be blocked at the proxy level as defense-in-depth.

Can I detect if CVE-2025-29927 was exploited against my app?

Check your access logs for requests containing the x-middleware-subrequest header: grep 'x-middleware-subrequest' /var/log/nginx/access.log. Also look for: access to protected routes (e.g. /admin, /dashboard, /api/admin) from IPs without valid sessions, at unusual times. Correlate with your auth service logs — if protected routes were accessed without a valid session token, investigate further.

Is this a Next.js-specific issue or affects all frameworks?

This is specific to Next.js middleware architecture. The x-middleware-subrequest header is a Next.js internal implementation detail. Other frameworks (Nuxt, SvelteKit, Remix, Express) are not affected. If you self-host Next.js behind a proxy (nginx, Cloudflare, AWS ALB), blocking the header at the proxy layer provides effective mitigation independent of the Next.js version.

Further Resources

🔒 Quantum-Resistant Mycelium Architecture
🛡️ Kuratierte Runbooks – EU-gehostet in Frankfurt
🌐 Zero Known Breaches – Powered by Living Intelligence
🏛️ DSGVO Art. 25 & 32 • SOC 2 & ISO 27001 in Vorbereitung
⚡ Real-Time Global Mycelium Network – 347 Bedrohungen in 60 Minuten
🧬 Trusted by SecOps Leaders worldwide