fix(security): securely handle root CWD using isSubpath in isTrustedSystemPath

This commit is contained in:
Coco Sheng
2026-05-12 13:25:04 -04:00
parent ca41e1bad6
commit 5bcc400020

View File

@@ -520,9 +520,10 @@ export function isTrustedSystemPath(filePath: string): boolean {
const normPath = normalizePath(filePath);
// 1. Explicitly reject paths in current working directory to prevent RCE
// Exclude root directories to avoid inadvertently rejecting all system paths.
const normCwd = normalizePath(process.cwd());
const relative = path.relative(normCwd, normPath);
if (!relative.startsWith('..') && !path.isAbsolute(relative)) {
const isRoot = normCwd === '/' || /^[a-zA-Z]:[\\/]?$/.test(normCwd);
if (!isRoot && isSubpath(normCwd, normPath)) {
return false;
}