0. 前言

本篇是 n8n 漏洞分析系列的第三篇。在此前,我们分析了利用裸仓库特性的 CVE-2025-62726 和利用配置注入的 CVE-2025-65964。这次分析的 CVE-2026-21877,它利用 Git 节点的 Clone 操作,可以将恶意仓库的内容写入服务器的任意目录,进而导致远程命令执行漏洞。

1. 漏洞简介

n8n 的 Git 节点提供了 Clone 操作,用于将远程或本地的 Git 仓库克隆到指定路径。该功能未对用户传入的 目标路径 (Repository Path)进行有效的安全检查。

攻击者可以构造一个包含恶意文件(如 WebShell、SSH 公钥等)的 Git 仓库,并利用 Clone 操作将其克隆到服务器上的敏感目录(如 Web 根目录、.ssh 目录、或其他服务的配置目录)。

1.1 核心成因

漏洞的核心在于 n8n 允许用户在 Clone 操作中指定任意的 Repository Path(目标路径)。

同时,sourceRepository 参数支持本地文件路径(如 /tmp/git_source),这使得攻击者可以在受控的临时目录中准备恶意仓库,然后覆盖到任意敏感目标位置。

1.2 与 CVE-2025-65964 的差异

CVE-2026-21877 与前一篇分析的 CVE-2025-65964 虽同为 Git 节点漏洞,但攻击原理完全不同:

特性

CVE-2025-65964 (配置注入)

CVE-2026-21877 (任意文件写入)

漏洞类型

Git 配置操纵

文件系统写入

核心操作

addConfig

clone

攻击逻辑

修改现有仓库的 core.hooksPath 配置,使其指向仓库内的恶意脚本,诱导 Git 执行它。

利用 clone 将包含恶意文件的仓库直接写入到服务器的敏感目录(如覆盖 SSH Key、写入 Cron Job)。

利用前提

需要一个能执行 Git 命令的现有仓库环境。

需要能够指定 repositoryPath 到敏感位置(需要目标目录不存在或为空,否则会clone失败)。

2. 漏洞概述

  • 漏洞编号: CVE-2026-21877
  • CVSS: 9.8 (Critical)
  • 影响产品: n8n
  • 公开时间: 2026-01-07
  • 影响版本: 0.123.0 ≤ n8n < 1.121.3
  • 漏洞类型: Arbitrary File Write
  • 利用条件: 攻击者需要有权限创建或修改工作流。

3. 环境搭建

本复现环境基于 Docker 和 Docker Compose 构建。

3.1 靶场配置 (docker-compose.yml)

version: '3'
services:
  n8n:
    image: n8nio/n8n:1.120.4
    container_name: n8n-cve-2026-21877-python
    ports:
      - "5678:5678"
    environment:
      - N8N_ENCRYPTION_KEY=test-key
      - N8N_SECURE_COOKIE=false
    volumes:
      # 模拟生成的恶意仓库git_source,n8n 容器需要访问它进行 clone,从而触发漏洞
      - ./git_source:/tmp/git_source
    command: start

3.2 创建恶意仓库 (git_source)

在目录下创建一个用于克隆的裸仓库:

mkdir -p tmp_git_repo
cd tmp_git_repo
git init >/dev/null
echo "hacked" > pwn.txt
git add pwn.txt
git -c user.email=attacker@example.com -c user.name=Attacker commit -m "Initial commit"
cd ..
git clone --bare tmp_git_repo git_source
chmod -R 777 git_source


说明:

  • git_source 为裸仓库,n8n 的 Git 节点会从容器内路径 /tmp/git_source 读取它进行 clone
  • 上述 docker-compose.yml 已将宿主机的 ./git_source 挂载到容器的 /tmp/git_source
  • pwn.txt 仅作为示例文件,实际可替换为任意需写入目标路径的内容

3.3 Web UI 手动复现流程

  1. 访问 n8n 控制台:浏览器打开 http://ip:5678。
  2. 创建新工作流:点击页面右上角的 Create workflow 按钮。
  3. 添加 Git 节点:
  • 点击画布中央的 + 号。
  • 在搜索框输入 git。
  • 在结果列表中找到 Git 节点下的 Actions,。
    ,选择 Clone a repository,这个时候如果报错,则是浏览器的安全策略,可以直接使用vs code的端口转发功能,直接在本地访问 http://localhost:5678
  1. 配置恶意参数:
  • Operation: 选择 Clone。
  • New Repository Path: 填入目标路径,例如 /home/node/.cache/n8n/public/assets/vulnn_proof(此路径下的文件可通过 Web 访问)。
  • Source Repository: 填入挂载进容器的恶意源路径 /tmp/git_source。
    5.
    触发漏洞:
    * 点击节点面板右上角的
    Execute Node 按钮。
    * 如果执行成功,绿色对勾会出现。此时恶意文件已被写入目标路径。

    6.
    验证结果:
    * 访问
    http://127.0.0.1:5678/assets/vulnn_proof/pwn.txt。
    * 如果看到
    hacked 字样,说明任意文件写入复现成功。

另外可以研究下n8n的API 接口,直接使用python 编写POC。

4. 漏洞分析

4.1 代码层面分析

在受影响版本(如 1.120.4)中,Git.node.ts 的 clone 操作处理逻辑如下:

// packages/nodes-base/nodes/Git/Git.node.ts (Container: /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Git/Git.node.js)

// 1. 获取用户输入的目标路径 (repositoryPath)
const repositoryPath = this.getNodeParameter('repositoryPath', itemIndex, '');

// 2. 检查目录是否存在,不存在则创建 (未验证路径安全性)
if (operation === 'clone') {
    try {
        await (0, promises_1.access)(repositoryPath);
    }
    catch (error) {
        await (0, promises_1.mkdir)(repositoryPath);
    }
}

// ...

// 3. 初始化 Git 实例,baseDir 设置为 repositoryPath
const gitOptions = {
    baseDir: repositoryPath,
    config: gitConfig,
};
const git = (0, simple_git_1.default)(gitOptions)
    .env('GIT_TERMINAL_PROMPT', '0');

// ...

// 4. 执行 Clone 操作
else if (operation === 'clone') {
    // 获取源仓库地址
    let sourceRepository = this.getNodeParameter('sourceRepository', itemIndex, '');
    sourceRepository = await prepareRepository(sourceRepository);
    // 将源仓库克隆到当前 baseDir (即 repositoryPath)
    await git.clone(sourceRepository, '.');
    // ...
}

核心问题:
代码完全信任了用户提供的
repositoryPath。只要 n8n 进程(通常运行为 node 用户)对该路径有写入权限,且该路径为空(Git Clone 的要求),操作就会成功。

4.2 触发流程与危害

  1. 准备恶意源: 攻击者在本地(或远程)准备一个 Git 仓库,其中包含恶意文件(例如 authorized_keys)。
  2. 设置目标: 攻击者在 n8n 工作流中配置 Git 节点,将 repositoryPath 设置为目标服务器的敏感路径(例如 /home/node/.ssh 或 Web 服务器目录)。
  3. 执行克隆: 执行工作流。n8n 进程调用 git clone。
  4. 文件写入: Git 将恶意仓库的内容完整写入目标目录。如果目标是 .ssh 目录且写入了 authorized_keys,攻击者即可通过 SSH 免密登录服务器。

5. 补丁分析

5.1 修复逻辑

修复方案的核心是引入了路径安全检查机制。在执行任何 Git 命令(特别是 clone)之前,系统会先解析路径,然后检查 repositoryPath 是否在允许范围之内。
(
packages/nodes-base/nodes/Git/Git.node.js):

// 1. 获取并解析目标路径
const repositoryPath = this.getNodeParameter('repositoryPath', itemIndex, '');
// resolvePath 会处理相对路径并返回绝对路径
const resolvedRepositoryPath = await this.helpers.resolvePath(repositoryPath);

// 2. [修复点] 检查路径安全性
// isFilePathBlocked 会根据配置判断路径是否允许访问
const isFilePathBlocked = this.helpers.isFilePathBlocked(resolvedRepositoryPath);
if (isFilePathBlocked) {
    throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Access to the repository path is not allowed');
}

const options = this.getNodeParameter('options', itemIndex, {});

if (operation === 'clone') {
    // ... 后续 Clone 操作 ...

同时,isFilePathBlocked 的具体实现逻辑位于 n8n-core 中(execution-engine/node-execution-context/utils/file-system-helper-functions.js),其检查顺序如下:

function isFilePathBlocked(resolvedFilePath) {
    // 1. 检查是否在受限路径中 (默认开启)
    // 受限路径包括: n8nFolder (如 ~/.n8n), staticCacheDir, 配置文件路径, 二进制数据存储路径等
    const blockFileAccessToN8nFiles = process.env['N8N_BLOCK_FILE_ACCESS_TO_N8N_FILES'] !== 'false';
    const restrictedPaths = blockFileAccessToN8nFiles ? getN8nRestrictedPaths() : [];
    if (restrictedPaths.some((restrictedPath) => isContainedWithin(restrictedPath, resolvedFilePath))) {
        return true; // 命中受限路径,禁止访问
    }

    // 2. 检查文件名模式 (Block List)
    // 检查是否匹配 N8N_BLOCK_FILE_PATTERNS 定义的正则
    if (isFilePatternBlocked(resolvedFilePath)) {
        return true;
    }

    // 3. 检查白名单 (Allow List)
    // 如果配置了 N8N_ALLOW_FILE_ACCESS_TO_FILES,则必须在白名单内
    const allowedPaths = getAllowedPaths();
    if (allowedPaths.length) {
        return !allowedPaths.some((allowedPath) => isContainedWithin(allowedPath, resolvedFilePath));
    }

    return false; // 通过所有检查
}

该修复确保了用户无法将 Git 仓库克隆到 n8n 的敏感目录(如配置文件所在目录)中,并且如果管理员配置了白名单,则只能克隆到指定目录。

6. 修复建议

  1. 升级 n8n: 立即升级到更高版本。
  2. 容器隔离: 确保 n8n 运行在权限受限的容器中,避免挂载宿主机的敏感目录(如 /root, /etc, /var/run/docker.sock)。
  3. 文件系统权限: 限制 n8n 进程(通常是 node 用户)的文件写入权限,仅允许其写入必要的工作目录。

7. 参考链接

  1. GitHub Advisory GHSA-v364-rw7m-3263
  2. n8n Commit f4b009d