[P1/高] CodeSearchTool ripgrep pattern 未转义,可注入命令行选项 #22

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

问题类型

安全漏洞 / 高 / 工具系统

文件位置

electron/harness/tools/built-in/code-search.ts

问题描述

CodeSearchTool 接受用户输入的 patternglob 参数,传给 ripgrep 子进程。
若未正确转义,以 - 开头的 pattern 会被 ripgrep 解释为选项:

  • --help 会输出帮助而非搜索
  • --ignore-file /etc/passwd 可读取任意文件
  • --crlf 等改变行为
  • --files 列出所有文件

影响

  • 信息泄露(任意文件读取)
  • 工具行为被劫持

建议修复

  1. 使用 execFile 数组参数(而非字符串拼接)
  2. 在 pattern 前加 -- 终止选项解析:
const args = [
  '--json',
  '--', // 终止选项解析
  pattern,
  rootPath,
];
execFile('rg', args, ...);
  1. 过滤 pattern:禁止以 - 开头,或强制转义
  2. 校验 glob 参数不包含 shell 元字符
## 问题类型 安全漏洞 / 高 / 工具系统 ## 文件位置 `electron/harness/tools/built-in/code-search.ts` ## 问题描述 CodeSearchTool 接受用户输入的 `pattern` 与 `glob` 参数,传给 ripgrep 子进程。 若未正确转义,以 `-` 开头的 pattern 会被 ripgrep 解释为选项: - `--help` 会输出帮助而非搜索 - `--ignore-file /etc/passwd` 可读取任意文件 - `--crlf` 等改变行为 - `--files` 列出所有文件 ## 影响 - 信息泄露(任意文件读取) - 工具行为被劫持 ## 建议修复 1. **使用 execFile 数组参数**(而非字符串拼接) 2. **在 pattern 前加 `--`** 终止选项解析: ```ts const args = [ '--json', '--', // 终止选项解析 pattern, rootPath, ]; execFile('rg', args, ...); ``` 3. **过滤 pattern**:禁止以 `-` 开头,或强制转义 4. 校验 glob 参数不包含 shell 元字符
thzxx added the ??????????? labels 2026-07-21 21:55:50 +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#22