@@ -269,11 +269,10 @@ describe("Unicode normalization", () => {
269269 } ,
270270 ) ;
271271
272- // KNOWN BUG #1970: pins current (wrong) behavior; the fix changes this assertion.
273272 // #1970: when the allowed directory's own name is NFD, an NFC spelling of
274- // it fails the allow-list prefix check (a string comparison) before the
275- // Unicode walk starts , on any file system.
276- it ( "refuses an NFC spelling of an NFD allowed directory (#1970)" , async ( ) => {
273+ // it used to fail the allow-list prefix check (a string comparison) before
274+ // the Unicode walk started , on any file system.
275+ it ( "accepts an NFC spelling of an NFD allowed directory (#1970)" , async ( ) => {
277276 const nfdRoot = path . join ( dir , "Capture d\u2019e\u0301cran" ) ;
278277 await fs . mkdir ( nfdRoot ) ;
279278 await fs . writeFile ( path . join ( nfdRoot , "shot.png" ) , "png" ) ;
@@ -283,11 +282,75 @@ describe("Unicode normalization", () => {
283282 const result = await call ( scoped . client , "read_text_file" , {
284283 path : requested ,
285284 } ) ;
286- expect ( textOf ( result ) ) . toBe (
287- `Access denied - path outside allowed directories: ${ requested } not in ${ nfdRoot } ` ,
288- ) ;
285+ expect ( textOf ( result ) ) . toBe ( "png" ) ;
289286 } finally {
290287 await scoped . close ( ) ;
291288 }
292289 } ) ;
290+
291+ // #1970: a respelled allowed directory must not open a path into a
292+ // different directory that merely shares its name up to normalization.
293+ it . skipIf ( ! distinctForms ) (
294+ "maps an NFD spelling of an NFC allowed directory onto it, never onto an NFD sibling (#1970)" ,
295+ async ( ) => {
296+ const nfcRoot = path . join ( dir , "caf\u00e9" ) ;
297+ const nfdSibling = path . join ( dir , "cafe\u0301" ) ;
298+ await fs . mkdir ( nfcRoot ) ;
299+ await fs . mkdir ( nfdSibling ) ;
300+ await fs . writeFile ( path . join ( nfcRoot , "f.txt" ) , "allowed" ) ;
301+ await fs . writeFile ( path . join ( nfdSibling , "f.txt" ) , "secret" ) ;
302+ const scoped = await connect ( [ nfcRoot ] ) ;
303+ try {
304+ const result = await call ( scoped . client , "read_text_file" , {
305+ path : path . join ( nfdSibling , "f.txt" ) ,
306+ } ) ;
307+ expect ( textOf ( result ) ) . toBe ( "allowed" ) ;
308+ } finally {
309+ await scoped . close ( ) ;
310+ }
311+ } ,
312+ ) ;
313+
314+ it ( "refuses a path that only shares a normalized prefix with the allowed directory's name" , async ( ) => {
315+ const result = await read ( `${ dir } \u00a0x/f.txt` ) ;
316+ expect ( textOf ( result ) ) . toMatch ( / ^ A c c e s s d e n i e d - p a t h o u t s i d e / ) ;
317+ } ) ;
318+ } ) ;
319+
320+ // #1970: macOS names screenshots "Screenshot ... at 2.40.40\u202fPM.png";
321+ // a client lists the file, then sends the name back with a plain space.
322+ describe ( "Unicode space separators (#1970)" , ( ) => {
323+ const onDisk = "Screenshot 2025-04-23 at 2.40.40\u202fPM.png" ;
324+ const typed = "Screenshot 2025-04-23 at 2.40.40 PM.png" ;
325+
326+ it ( "reads a file whose U+202F the request spells as U+0020" , async ( ) => {
327+ await fs . writeFile ( path . join ( dir , onDisk ) , "shot" ) ;
328+ expect ( textOf ( await read ( path . join ( dir , typed ) ) ) ) . toBe ( "shot" ) ;
329+ } ) ;
330+
331+ it ( "moves a file whose U+00A0 the request spells as U+0020" , async ( ) => {
332+ await fs . mkdir ( path . join ( dir , "Images" ) ) ;
333+ await fs . writeFile ( path . join ( dir , "a\u00a0b.png" ) , "x" ) ;
334+ const result = await call ( client , "move_file" , {
335+ source : path . join ( dir , "a b.png" ) ,
336+ destination : path . join ( dir , "Images" , "a b.png" ) ,
337+ } ) ;
338+ expect ( result . isError ) . toBeFalsy ( ) ;
339+ expect ( await fs . readdir ( path . join ( dir , "Images" ) ) ) . toEqual ( [ "a b.png" ] ) ;
340+ expect ( await fs . readdir ( dir ) ) . toEqual ( [ "Images" ] ) ;
341+ } ) ;
342+
343+ it ( "prefers the exact spelling when both are on disk" , async ( ) => {
344+ await fs . writeFile ( path . join ( dir , onDisk ) , "nnbsp" ) ;
345+ await fs . writeFile ( path . join ( dir , typed ) , "space" ) ;
346+ expect ( textOf ( await read ( path . join ( dir , typed ) ) ) ) . toBe ( "space" ) ;
347+ expect ( textOf ( await read ( path . join ( dir , onDisk ) ) ) ) . toBe ( "nnbsp" ) ;
348+ } ) ;
349+
350+ it ( "refuses a name that matches two entries differing only in their spaces" , async ( ) => {
351+ await fs . writeFile ( path . join ( dir , "a\u00a0b" ) , "1" ) ;
352+ await fs . writeFile ( path . join ( dir , "a\u202fb" ) , "2" ) ;
353+ const result = await read ( path . join ( dir , "a b" ) ) ;
354+ expect ( textOf ( result ) ) . toBe ( "Ambiguous Unicode path component: a b" ) ;
355+ } ) ;
293356} ) ;
0 commit comments