11import path from 'path' ;
22
3+ /**
4+ * Checks if a path is a Windows UNC path (e.g. \\server\share).
5+ */
6+ function isUNCPath ( p : string ) : boolean {
7+ return p . startsWith ( '\\\\' ) && ! p . startsWith ( '\\\\?\\' ) ;
8+ }
9+
10+ /**
11+ * Normalizes a path, preserving UNC path prefixes on Windows.
12+ *
13+ * On Windows, path.resolve(path.normalize(uncPath)) can corrupt UNC paths
14+ * by stripping a leading backslash, turning \\server\share into \server\share
15+ * which path.resolve then interprets as drive-relative (e.g. C:\server\share).
16+ * UNC paths are always absolute, so we normalize without path.resolve.
17+ */
18+ function normalizePath ( p : string ) : string {
19+ if ( isUNCPath ( p ) ) {
20+ // UNC paths are always absolute — normalize without resolve to avoid
21+ // stripping the leading \\ prefix.
22+ const normalized = path . normalize ( p ) ;
23+ // Ensure the UNC prefix is preserved after normalization
24+ if ( ! normalized . startsWith ( '\\\\' ) ) {
25+ return '\\' + normalized ;
26+ }
27+ return normalized ;
28+ }
29+ return path . resolve ( path . normalize ( p ) ) ;
30+ }
31+
332/**
433 * Checks if an absolute path is within any of the allowed directories.
5- *
34+ *
635 * @param absolutePath - The absolute path to check (will be normalized)
736 * @param allowedDirectories - Array of absolute allowed directory paths (will be normalized)
837 * @returns true if the path is within an allowed directory, false otherwise
@@ -27,7 +56,7 @@ export function isPathWithinAllowedDirectories(absolutePath: string, allowedDire
2756 // Normalize the input path
2857 let normalizedPath : string ;
2958 try {
30- normalizedPath = path . resolve ( path . normalize ( absolutePath ) ) ;
59+ normalizedPath = normalizePath ( absolutePath ) ;
3160 } catch {
3261 return false ;
3362 }
@@ -51,7 +80,7 @@ export function isPathWithinAllowedDirectories(absolutePath: string, allowedDire
5180 // Normalize the allowed directory
5281 let normalizedDir : string ;
5382 try {
54- normalizedDir = path . resolve ( path . normalize ( dir ) ) ;
83+ normalizedDir = normalizePath ( dir ) ;
5584 } catch {
5685 return false ;
5786 }
0 commit comments