[P1/高] GitDiffTool pathspec 未校验工作空间内,可读取任意仓库差异 #19

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

问题类型

安全漏洞 / 高 / 工具系统

文件位置

electron/harness/tools/built-in/git.ts (GitDiffTool)

问题描述

GitDiffTool 接受 pathspec 参数,直接传给 git diff <pathspec>
未校验 pathspec 是否在工作空间内:

  • -- /etc/passwd 可尝试读取系统文件差异(如果 git 仓库覆盖该路径)
  • -- ../../../other-repo/ 可读取其他仓库
  • 绝对路径如 -- C:\\Windows\\System32\\ 可越权

影响

  • 跨工作空间信息泄露
  • 违反沙箱路径约束

建议修复

async execute(input: { pathspec?: string[] }): Promise<ToolResult> {
  const safePathspecs: string[] = [];
  if (input.pathspec) {
    for (const p of input.pathspec) {
      // 跳过 git 选项(-- 开头)
      if (p.startsWith('--')) continue;
      // realpath 校验
      const resolved = resolve(this.workspacePath, p);
      const real = realpathSync(resolved);
      if (!real.startsWith(this.workspacePath + sep) && real !== this.workspacePath) {
        return { error: `Path outside workspace: ${p}` };
      }
      safePathspecs.push(p);
    }
  }
  // ...
}

参考 file-guard.ts 的 validatePath 实现。

## 问题类型 安全漏洞 / 高 / 工具系统 ## 文件位置 `electron/harness/tools/built-in/git.ts` (GitDiffTool) ## 问题描述 GitDiffTool 接受 pathspec 参数,直接传给 `git diff <pathspec>`。 未校验 pathspec 是否在工作空间内: - `-- /etc/passwd` 可尝试读取系统文件差异(如果 git 仓库覆盖该路径) - `-- ../../../other-repo/` 可读取其他仓库 - 绝对路径如 `-- C:\\Windows\\System32\\` 可越权 ## 影响 - 跨工作空间信息泄露 - 违反沙箱路径约束 ## 建议修复 ```ts async execute(input: { pathspec?: string[] }): Promise<ToolResult> { const safePathspecs: string[] = []; if (input.pathspec) { for (const p of input.pathspec) { // 跳过 git 选项(-- 开头) if (p.startsWith('--')) continue; // realpath 校验 const resolved = resolve(this.workspacePath, p); const real = realpathSync(resolved); if (!real.startsWith(this.workspacePath + sep) && real !== this.workspacePath) { return { error: `Path outside workspace: ${p}` }; } safePathspecs.push(p); } } // ... } ``` 参考 file-guard.ts 的 validatePath 实现。
thzxx added the ??????????? labels 2026-07-21 21:55:48 +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#19