Nextjs And The Corrupt Middleware
Next.js middleware could be bypassed by sending a crafted x-middleware-subrequest header, causing the middleware to be ignored. An attacker could bypass authorization, path rewrites, and CSP headers, potentially accessing protected routes or poisoning caches.
zhero-web-sec.github.io
Published on zhero-web-sec.github.io
Recently, Yasser Allam, known by the pseudonym inzo, and I, decided to team up for some research. We discussed potential targets and chose to begin by focusing on Next.js (130K stars on github, currently downloaded + 9,4 million times per week), a framework I know quite well and with which I already have fond…
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
The middleware trusted a client-supplied header (x-middleware-subrequest) to decide whether to run, allowing an attacker to set the header to the middleware's internal name and bypass all middleware logic.
Impact
An attacker could bypass authorization, path rewrites, and CSP headers, potentially accessing protected routes or causing cache-poisoning denial of service; however, applications on Vercel and Netlify were reportedly no longer vulnerable at the time of writing.
What the attacker needed
Next.js application using middleware for security; Ability to send HTTP requests with custom headers
The fix
The vulnerability was fixed in Next.js 15.2.3 and 14.2.25; for older versions, the workaround is to block external requests containing the x-middleware-subrequest header.
How it was found
- Identify the Next.js version and middleware location (e.g., pages/_middleware, middleware, or src/middleware).
- Craft an x-middleware-subrequest header with the correct middleware path (or repeated for newer versions).
- Send the request to a protected endpoint; the middleware is ignored and the request reaches the destination.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
Next.js is a popular framework for building websites. It has a feature called middleware that runs before a page is shown, often used to check if you are logged in. The researchers found that by adding a special header to a request, you could make the middleware skip its checks entirely, so you could reach pages you shouldn't.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.