Next.js middleware normally checks if users can access protected pages. It runs before your page loads and can block unauthorized visitors.
But malicious users can add a special header called x-middleware-subrequest
to their HTTP requests. When they do this, Next.js skips running your middleware completely. This means all your security checks get bypassed.
Here’s what a normal request looks like:
GET /admin/dashboard HTTP/1.1Host: trevorlasn.comCookie: authToken=1234
And here’s what a malicious request looks like:
GET /admin/dashboard HTTP/1.1Host: trevorlasn.comx-middleware-subrequest: middleware:middleware:middleware:middleware:middleware
That’s it. By adding this one header, they bypass all your security checks.
What is the x-middleware-subrequest Header?
The x-middleware-subrequest
header is an internal Next.js header that prevents infinite loops. When middleware calls pages that also have middleware, this header tracks the chain.
If the same middleware appears 5+ times in the chain, Next.js stops running middleware to prevent stack overflow. The header stores middleware names separated by colons.
The vulnerability exists because Next.js trusts this header in all incoming requests, not just internal ones. By adding this header with five instances of “middleware”, attackers can trick Next.js into skipping the security checks entirely.
Next.js Versions Affected
The vulnerability affects all versions of Next.js from 11.1.4 to 15.2.2. The issue was fixed in the following versions:
Version Range | Status | Action |
---|---|---|
11.1.4 to 12.3.4 | Vulnerable | Update to 12.3.5 or newer |
13.0.0 to 13.5.8 | Vulnerable | Update to 13.5.9 or newer |
14.0.0 to 14.2.24 | Vulnerable | Update to 14.2.25 or newer |
15.0.0 to 15.2.2 | Vulnerable | Update to 15.2.3 or newer |
12.3.5+ | Patched | No action needed |
13.5.9+ | Patched | No action needed |
14.2.25+ | Patched | No action needed |
15.2.3+ | Patched | No action needed |
The patched versions now validate this header properly to prevent abuse from external requests.