fix: reject hard-linked project files and verify retained Windows proposals

This commit is contained in:
Agent Workbench Dev
2026-10-30 23:32:09 +08:00
parent f4bd0ad8dc
commit 7b1f529ed6
6 changed files with 273 additions and 3 deletions
+7 -1
View File
@@ -46,6 +46,10 @@ export function safePath(project, relative = '', allowMissing = false) {
// A project-local link or Windows alias must not turn a public path into
// access to protected Git metadata or credentials, including new files.
if (restricted(path.relative(root, real))) throw new Error('实际路径指向受保护的项目目录或凭据。');
const actual = fs.statSync(real);
// realpath cannot reveal the other names of a hard-linked file. Refuse
// ambiguous file scope before reading bytes or preparing a replacement.
if (actual.isFile() && actual.nlink !== 1) throw new Error('硬链接文件有多个路径,无法核对完整作用范围;请使用独立文件副本。');
return target;
}
@@ -65,8 +69,10 @@ export function listFiles(project, relative = '', depth = 4, limit = 700) {
const relativePath = path.relative(project.root, absolute).replaceAll('\\', '/');
if (entry.isSymbolicLink()) continue;
const directory = entry.isDirectory();
const info = directory ? null : fs.statSync(absolute);
if (!directory && (!info.isFile() || info.nlink !== 1)) continue;
files.push({ path: relativePath, name: entry.name, kind: directory ? 'directory' : 'file',
bytes: directory ? 0 : fs.statSync(absolute).size });
bytes: directory ? 0 : info.size });
if (files.length >= limit) { truncated = true; break; }
if (directory && current.depth < Math.min(12, Math.max(0, depth))) queue.push({ absolute, depth: current.depth + 1 });
}