criticalCWE-863A01:2021

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

A critical vulnerability in Next.js versions before 15.2.3 allows attackers to bypass middleware-based auth checks entirely by sending a crafted internal header.

How It Works

Next.js uses an internal header `x-middleware-subrequest` to track middleware invocation depth and avoid infinite loops. Attackers discovered they could send this header from the outside to trick the framework into skipping middleware execution, bypassing any auth guard implemented in middleware.ts.

Vulnerable Code
// BAD: Next.js < 15.2.3 — attacker sends:
// x-middleware-subrequest: middleware:middleware:middleware
// and this middleware is completely skipped
export function middleware(req: NextRequest) {
  const token = req.cookies.get('session');
  if (!token) return NextResponse.redirect('/login');
}
Secure Code
// GOOD: upgrade Next.js to >= 15.2.3
// Also: never rely solely on middleware for auth.
// Always verify the session in the route/page itself.
export async function GET() {
  const supabase = createClient();
  const { data: { user } } = await supabase.auth.getUser();
  if (!user) return Response.json({ error: 'Unauthorized' }, { status: 401 });
}

Real-World Example

CVE-2025-29927 — publicly disclosed March 2025. Affected all Next.js versions using middleware for auth. Any app where middleware was the only auth layer was fully exposed. CVSS score: 9.1 (Critical).

How to Prevent It

  • Upgrade Next.js to 15.2.3 or later immediately
  • Never use middleware as your only auth layer — always add a server-side session check in pages and API routes
  • Block the x-middleware-subrequest header at your CDN/proxy level as a temporary mitigation
  • Run Data Hogo to detect vulnerable Next.js versions in your repo

Affected Technologies

Next.js

Data Hogo detects this vulnerability automatically.

Scan Your Repo Free

Related Vulnerabilities