@@ -96,6 +96,46 @@ function resolveRelativePathAgainstAllowedDirectories(relativePath: string): str
9696}
9797
9898// Security & Validation Functions
99+ async function resolveUnicodeEquivalentPath ( absolutePath : string ) : Promise < string > {
100+ const allowedDirectory = [ ...allowedDirectories ]
101+ . sort ( ( left , right ) => right . length - left . length )
102+ . find ( directory => isPathWithinAllowedDirectories ( normalizePath ( absolutePath ) , [ directory ] ) ) ;
103+
104+ if ( ! allowedDirectory ) {
105+ return absolutePath ;
106+ }
107+
108+ let currentPath = await fs . realpath ( allowedDirectory ) ;
109+ const relativeParts = path . relative ( allowedDirectory , absolutePath ) . split ( path . sep ) . filter ( Boolean ) ;
110+
111+ for ( let index = 0 ; index < relativeParts . length ; index ++ ) {
112+ const requestedPart = relativeParts [ index ] ;
113+ const entries = ( await fs . readdir ( currentPath ) ) ?? [ ] ;
114+ const exactMatch = entries . find ( entry => entry === requestedPart ) ;
115+ const equivalentMatches = exactMatch
116+ ? [ exactMatch ]
117+ : entries . filter ( entry => entry . normalize ( 'NFC' ) === requestedPart . normalize ( 'NFC' ) ) ;
118+
119+ if ( equivalentMatches . length > 1 ) {
120+ throw new Error ( `Ambiguous Unicode path component: ${ requestedPart } ` ) ;
121+ }
122+
123+ if ( equivalentMatches . length === 0 ) {
124+ if ( index === relativeParts . length - 1 ) {
125+ return path . join ( currentPath , requestedPart ) ;
126+ }
127+ throw new Error ( `Parent directory does not exist: ${ path . join ( currentPath , requestedPart ) } ` ) ;
128+ }
129+
130+ currentPath = await fs . realpath ( path . join ( currentPath , equivalentMatches [ 0 ] ) ) ;
131+ if ( ! isPathWithinAllowedDirectories ( normalizePath ( currentPath ) , allowedDirectories ) ) {
132+ throw new Error ( `Access denied - symlink target outside allowed directories: ${ currentPath } not in ${ allowedDirectories . join ( ', ' ) } ` ) ;
133+ }
134+ }
135+
136+ return currentPath ;
137+ }
138+
99139export async function validatePath ( requestedPath : string ) : Promise < string > {
100140 const expandedPath = expandHome ( requestedPath ) ;
101141 const absolute = path . isAbsolute ( expandedPath )
@@ -123,16 +163,13 @@ export async function validatePath(requestedPath: string): Promise<string> {
123163 // Security: For new files that don't exist yet, verify parent directory
124164 // This ensures we can't create files in unauthorized locations
125165 if ( ( error as NodeJS . ErrnoException ) . code === 'ENOENT' ) {
126- const parentDir = path . dirname ( absolute ) ;
127166 try {
128- const realParentPath = await fs . realpath ( parentDir ) ;
129- const normalizedParent = normalizePath ( realParentPath ) ;
130- if ( ! isPathWithinAllowedDirectories ( normalizedParent , allowedDirectories ) ) {
131- throw new Error ( `Access denied - parent directory outside allowed directories : ${ realParentPath } not in ${ allowedDirectories . join ( ', ' ) } ` ) ;
167+ return await resolveUnicodeEquivalentPath ( absolute ) ;
168+ } catch ( resolutionError ) {
169+ if ( ( resolutionError as NodeJS . ErrnoException ) . code === 'ENOENT' ) {
170+ throw new Error ( `Parent directory does not exist : ${ path . dirname ( absolute ) } ` ) ;
132171 }
133- return absolute ;
134- } catch {
135- throw new Error ( `Parent directory does not exist: ${ parentDir } ` ) ;
172+ throw resolutionError ;
136173 }
137174 }
138175 throw error ;
0 commit comments