[P1/高] FileEditorTool regex 模式存在 ReDoS 风险,可被恶意输入攻击 #45

Open
opened 2026-07-21 21:56:09 +08:00 by thzxx · 0 comments
Owner

问题类型

安全漏洞 / 高 / 工具系统

文件位置

electron/harness/tools/built-in/file-editor.ts

问题描述

FileEditorTool 支持基于正则的查找替换:

const regex = new RegExp(input.pattern, input.flags);
const newContent = content.replace(regex, input.replacement);

用户提供的 pattern 未做 ReDoS 防护:

  • (a+)+ 等灾难性回溯模式
  • 在长字符串上执行时间指数级增长
  • 主进程阻塞

影响

  • 恶意 LLM 输入可阻塞主进程
  • DoS 攻击

建议修复

  1. 超时控制
import { setTimeout } from 'timers/promises';

async function safeRegexReplace(content: string, pattern: RegExp, replacement: string): Promise<string> {
  try {
    return await Promise.race([
      Promise.resolve(content.replace(pattern, replacement)),
      setTimeout(1000).then(() => { throw new Error('Regex timeout'); }),
    ]);
  } catch (err) {
    throw new Error(`Regex execution failed: ${err.message}`);
  }
}
  1. 使用 safe-regex 库:检测危险模式
import safeRegex from 'safe-regex';
if (!safeRegex(pattern)) {
  throw new Error('Potentially catastrophic regex');
}
  1. 限制输入长度:超长内容拒绝 regex 模式
## 问题类型 安全漏洞 / 高 / 工具系统 ## 文件位置 `electron/harness/tools/built-in/file-editor.ts` ## 问题描述 FileEditorTool 支持基于正则的查找替换: ```ts const regex = new RegExp(input.pattern, input.flags); const newContent = content.replace(regex, input.replacement); ``` 用户提供的 pattern 未做 ReDoS 防护: - `(a+)+` 等灾难性回溯模式 - 在长字符串上执行时间指数级增长 - 主进程阻塞 ## 影响 - 恶意 LLM 输入可阻塞主进程 - DoS 攻击 ## 建议修复 1. **超时控制**: ```ts import { setTimeout } from 'timers/promises'; async function safeRegexReplace(content: string, pattern: RegExp, replacement: string): Promise<string> { try { return await Promise.race([ Promise.resolve(content.replace(pattern, replacement)), setTimeout(1000).then(() => { throw new Error('Regex timeout'); }), ]); } catch (err) { throw new Error(`Regex execution failed: ${err.message}`); } } ``` 2. **使用 safe-regex 库**:检测危险模式 ```ts import safeRegex from 'safe-regex'; if (!safeRegex(pattern)) { throw new Error('Potentially catastrophic regex'); } ``` 3. **限制输入长度**:超长内容拒绝 regex 模式
thzxx added the ??????????? labels 2026-07-21 21:56:09 +08:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: MetonaTeam/metona-ai-desktop#45