@@ -35,6 +35,23 @@ async function holdsBothNormalizationForms(): Promise<boolean> {
3535
3636const distinctForms = await holdsBothNormalizationForms ( ) ;
3737
38+ /** True when this file system finds "name" when asked for "NAME" (macOS and Windows by default). */
39+ async function foldsCase ( ) : Promise < boolean > {
40+ const probe = await makeTempDir ( "mcp-case-probe-" ) ;
41+ try {
42+ await fs . mkdir ( path . join ( probe , "name" ) ) ;
43+ await fs . lstat ( path . join ( probe , "NAME" ) ) ;
44+ return true ;
45+ } catch ( error ) {
46+ if ( ( error as NodeJS . ErrnoException ) . code === "ENOENT" ) return false ;
47+ throw error ;
48+ } finally {
49+ await fs . rm ( probe , { recursive : true , force : true } ) ;
50+ }
51+ }
52+
53+ const caseInsensitive = await foldsCase ( ) ;
54+
3855let dir : string ;
3956let outside : string ;
4057let conn : Connected ;
@@ -199,6 +216,75 @@ describe("symlinks", () => {
199216 expect ( await fs . readdir ( outside ) ) . toEqual ( [ "secret.txt" ] ) ;
200217 } ) ;
201218
219+ describe . skipIf ( ! caseInsensitive ) (
220+ "spelled with other letter case on a case-insensitive file system" ,
221+ ( ) => {
222+ const refusal = ( ) =>
223+ `Access denied - symlink target outside allowed directories: ${ outside } not in ${ dir } ` ;
224+
225+ it ( "refuses create_directory under a symlink that leaves" , async ( ) => {
226+ await fs . symlink ( outside , path . join ( dir , "link" ) , "junction" ) ;
227+ const result = await call ( client , "create_directory" , {
228+ path : path . join ( dir , "LINK" , "a" ) ,
229+ } ) ;
230+ expect ( textOf ( result ) ) . toBe ( refusal ( ) ) ;
231+ expect ( await fs . readdir ( outside ) ) . toEqual ( [ "secret.txt" ] ) ;
232+ } ) ;
233+
234+ it ( "refuses write_file when only an ancestor of the symlink is respelled" , async ( ) => {
235+ await fs . mkdir ( path . join ( dir , "sub" ) ) ;
236+ await fs . symlink ( outside , path . join ( dir , "sub" , "ln2" ) , "junction" ) ;
237+ const result = await call ( client , "write_file" , {
238+ path : path . join ( dir , "SUB" , "ln2" , "nested.txt" ) ,
239+ content : "x" ,
240+ } ) ;
241+ expect ( textOf ( result ) ) . toBe ( refusal ( ) ) ;
242+ expect ( await fs . readdir ( outside ) ) . toEqual ( [ "secret.txt" ] ) ;
243+ } ) ;
244+
245+ it ( "refuses a move_file destination under a symlink that leaves" , async ( ) => {
246+ await fs . symlink ( outside , path . join ( dir , "link" ) , "junction" ) ;
247+ await fs . writeFile ( path . join ( dir , "note.txt" ) , "note" ) ;
248+ const result = await call ( client , "move_file" , {
249+ source : path . join ( dir , "note.txt" ) ,
250+ destination : path . join ( dir , "lInK" , "moved.txt" ) ,
251+ } ) ;
252+ expect ( textOf ( result ) ) . toBe ( refusal ( ) ) ;
253+ expect ( await fs . readdir ( outside ) ) . toEqual ( [ "secret.txt" ] ) ;
254+ expect ( await fs . readFile ( path . join ( dir , "note.txt" ) , "utf8" ) ) . toBe (
255+ "note" ,
256+ ) ;
257+ } ) ;
258+
259+ it ( "still writes a new file under a respelled directory that stays inside" , async ( ) => {
260+ await fs . mkdir ( path . join ( dir , "sub" ) ) ;
261+ const result = await call ( client , "write_file" , {
262+ path : path . join ( dir , "SUB" , "new.txt" ) ,
263+ content : "x" ,
264+ } ) ;
265+ expect ( result . isError ) . toBeFalsy ( ) ;
266+ expect (
267+ await fs . readFile ( path . join ( dir , "sub" , "new.txt" ) , "utf8" ) ,
268+ ) . toBe ( "x" ) ;
269+ } ) ;
270+
271+ it ( "refuses to write under a respelled dangling symlink, reporting a missing parent" , async ( ) => {
272+ await fs . symlink (
273+ path . join ( outside , "not-yet" ) ,
274+ path . join ( dir , "dangling" ) ,
275+ ) ;
276+ const result = await call ( client , "write_file" , {
277+ path : path . join ( dir , "DANGLING" , "f.txt" ) ,
278+ content : "x" ,
279+ } ) ;
280+ expect ( textOf ( result ) ) . toBe (
281+ `Parent directory does not exist: ${ path . join ( dir , "DANGLING" ) } ` ,
282+ ) ;
283+ await expect ( fs . stat ( path . join ( outside , "not-yet" ) ) ) . rejects . toThrow ( ) ;
284+ } ) ;
285+ } ,
286+ ) ;
287+
202288 it ( "refuses to write through a dangling symlink, reporting a missing parent" , async ( ) => {
203289 const link = path . join ( dir , "dangling" ) ;
204290 await fs . symlink ( path . join ( outside , "not-yet" ) , link ) ;
0 commit comments