Skip to content

Commit 0f26418

Browse files
Copilotstreamich
andcommitted
fix: prevent infinite recursion in Windows path resolution for cp operations
Co-authored-by: streamich <[email protected]>
1 parent 19557fb commit 0f26418

2 files changed

Lines changed: 296 additions & 408 deletions

File tree

‎src/volume.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,9 +1302,23 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
13021302
}
13031303

13041304
private isSrcSubdir(src: string, dest: string): boolean {
1305-
const normalizedSrc = resolveCrossPlatform(src);
1306-
const normalizedDest = resolveCrossPlatform(dest);
1307-
return normalizedDest.startsWith(normalizedSrc + sep);
1305+
// Normalize paths without using resolve to avoid potential infinite recursion
1306+
const normalizePath = (p: string): string => {
1307+
// Convert to posix-style and remove trailing slashes, but keep root as '/'
1308+
const normalized = p.replace(/\\/g, '/').replace(/\/+$/, '');
1309+
return normalized || '/';
1310+
};
1311+
1312+
const normalizedSrc = normalizePath(src);
1313+
const normalizedDest = normalizePath(dest);
1314+
1315+
// Check if dest is a subdirectory of src
1316+
if (normalizedSrc === '/') {
1317+
// Special case for root: everything except root itself is a subdirectory
1318+
return normalizedDest !== '/';
1319+
}
1320+
1321+
return normalizedDest.startsWith(normalizedSrc + '/') || normalizedDest === normalizedSrc;
13081322
}
13091323

13101324
private ensureParentDir(dest: string): void {

0 commit comments

Comments
 (0)