11import path from 'path' ;
22
3+ /**
4+ * Normalizes a path without corrupting UNC paths.
5+ * On Windows, path.resolve() converts UNC paths like \\server\share to C:\server\share,
6+ * which breaks UNC path handling. This function preserves UNC paths.
7+ *
8+ * @param p - The path to normalize
9+ * @returns Normalized path
10+ */
11+ function normalizePathSafe ( p : string ) : string {
12+ // Check if it's a UNC path (starts with \\ or //) BEFORE normalizing
13+ // We must check this first because path.normalize on macOS corrupts UNC paths
14+ if ( p . startsWith ( '\\\\' ) || p . startsWith ( '//' ) ) {
15+ // UNC paths: normalize slashes to backslashes and remove redundant slashes
16+ // but preserve the leading \\
17+ let normalized = p . replace ( / \/ / g, '\\' ) ;
18+ // Ensure exactly two leading backslashes (not more, not less)
19+ normalized = normalized . replace ( / ^ \\ + / , '\\\\' ) ;
20+ // Normalize any double backslashes in the rest of the path to single
21+ // but be careful not to break the leading \\
22+ const rest = normalized . substring ( 2 ) . replace ( / \\ + / g, '\\' ) ;
23+ return '\\\\' + rest ;
24+ }
25+
26+ // For non-UNC paths, use path.normalize then path.resolve
27+ const normalized = path . normalize ( p ) ;
28+ return path . resolve ( normalized ) ;
29+ }
30+
331/**
432 * Checks if an absolute path is within any of the allowed directories.
533 *
@@ -27,13 +55,15 @@ export function isPathWithinAllowedDirectories(absolutePath: string, allowedDire
2755 // Normalize the input path
2856 let normalizedPath : string ;
2957 try {
30- normalizedPath = path . resolve ( path . normalize ( absolutePath ) ) ;
58+ normalizedPath = normalizePathSafe ( absolutePath ) ;
3159 } catch {
3260 return false ;
3361 }
3462
3563 // Verify it's absolute after normalization
36- if ( ! path . isAbsolute ( normalizedPath ) ) {
64+ // UNC paths are always absolute, as are resolved paths
65+ const isUncPath = normalizedPath . startsWith ( '\\\\' ) ;
66+ if ( ! isUncPath && ! path . isAbsolute ( normalizedPath ) ) {
3767 throw new Error ( 'Path must be absolute after normalization' ) ;
3868 }
3969
@@ -51,13 +81,14 @@ export function isPathWithinAllowedDirectories(absolutePath: string, allowedDire
5181 // Normalize the allowed directory
5282 let normalizedDir : string ;
5383 try {
54- normalizedDir = path . resolve ( path . normalize ( dir ) ) ;
84+ normalizedDir = normalizePathSafe ( dir ) ;
5585 } catch {
5686 return false ;
5787 }
5888
5989 // Verify allowed directory is absolute after normalization
60- if ( ! path . isAbsolute ( normalizedDir ) ) {
90+ const isUncDir = normalizedDir . startsWith ( '\\\\' ) ;
91+ if ( ! isUncDir && ! path . isAbsolute ( normalizedDir ) ) {
6192 throw new Error ( 'Allowed directories must be absolute paths after normalization' ) ;
6293 }
6394
@@ -81,6 +112,18 @@ export function isPathWithinAllowedDirectories(absolutePath: string, allowedDire
81112 return pathDrive === dirDrive && normalizedPath . startsWith ( normalizedDir . replace ( / \\ ? $ / , '\\' ) ) ;
82113 }
83114
115+ // Special handling for UNC paths
116+ // Both paths must be UNC paths for a valid match
117+ if ( isUncPath && isUncDir ) {
118+ // For UNC paths, use backslash as separator
119+ return normalizedPath . startsWith ( normalizedDir + '\\' ) ;
120+ }
121+
122+ // If one is UNC and the other isn't, they can't match
123+ if ( isUncPath !== isUncDir ) {
124+ return false ;
125+ }
126+
84127 return normalizedPath . startsWith ( normalizedDir + path . sep ) ;
85128 } ) ;
86129}
0 commit comments