安全漏洞 / 高 / 工具系统
electron/harness/tools/built-in/filesystem.ts (DeleteFileTool)
electron/harness/tools/built-in/filesystem.ts
DeleteFileTool 执行流程:
stat(path)
validatePath(path)
unlink(path)
rmdir(path)
步骤 1-2 与步骤 3 之间存在 TOCTOU(time-of-check to time-of-use)窗口:
path
open(path, O_NOFOLLOW)
import { open, unlink } from 'fs/promises'; async safeDelete(path: string) { const realBefore = realpathSync(path); if (!isInWorkspace(realBefore)) throw new Error('Path escape'); // 使用 O_NOFOLLOW 防止符号链接 const fd = await open(path, 'r'); const stat = await fd.stat(); if (stat.isDirectory()) { await fd.close(); throw new Error('Cannot delete directory'); } await fd.close(); // 再次校验(防止 fd 关闭后被替换) const realAfter = realpathSync(path); if (realAfter !== realBefore) throw new Error('TOCTOU detected'); await unlink(path); }
No dependencies set.
The note is not visible to the blocked user.
问题类型
安全漏洞 / 高 / 工具系统
文件位置
electron/harness/tools/built-in/filesystem.ts(DeleteFileTool)问题描述
DeleteFileTool 执行流程:
stat(path)检查文件存在与类型validatePath(path)校验工作空间内unlink(path)/rmdir(path)删除步骤 1-2 与步骤 3 之间存在 TOCTOU(time-of-check to time-of-use)窗口:
path校验通过后,实际 unlink 的可能是符号链接指向的任意文件影响
建议修复
open(path, O_NOFOLLOW)打开 fd,再对 fd 操作