Skip to content

Conversation

@wuchangming
Copy link
Contributor

问题描述

在 Windows 系统上读取图片文件时,会错误地抛出 "path traversal detected" 错误,即使文件在工作区目录内也无法读取。

问题原因

Windows 下 process.cwd() 返回反斜杠路径(如 C:\project),而 path.resolve() 返回正斜杠路径(如 C:/project/images/pic.png),导致字符串比较失败:

// 修复前
const resolvedPath = path.resolve(filePath);  // "C:/project/images/pic.png"
if (!resolvedPath.startsWith(process.cwd())) { // "C:\project"
  throw new Error('Invalid file path: path traversal detected');
}
// ❌ 比较失败: "C:/..." 不是以 "C:\..." 开头

失败截图

image

修复后,成功截图

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant