@@ -619,16 +619,39 @@ const UpdatedDatasetDetailPage: React.FC = () => {
619619 } ) ,
620620 ] ;
621621
622- // Bash script (Mac/Linux)
622+ // Shell-safe single quoting: ' → '\'' (nothing inside '…' is expanded).
623+ const shq = ( s ?: string ) => `'${ ( s ?? "" ) . replace ( / ' / g, `'\\''` ) } '` ;
624+ // Batch: % must be doubled (else %20 etc. are read as variables);
625+ // a " can't be quoted safely, so such items are skipped below.
626+ const batq = ( s ?: string ) => `"${ ( s ?? "" ) . replace ( / % / g, "%%" ) } "` ;
627+ // Only plain relative paths inside the download folder: no leading /,
628+ // no "..", no backslash, no control characters. Others → file-<n>.
629+ const safeName = ( name : string ) =>
630+ ! ! name &&
631+ ! name . startsWith ( "/" ) &&
632+ ! name . includes ( "\\" ) &&
633+ ! / [ \x00 - \x1f \x7f ] / . test ( name ) &&
634+ ! name . split ( "/" ) . some ( ( part ) => part === ".." || part === "" ) ;
635+ const safeItems = items . map ( ( it , i ) => ( {
636+ ...it ,
637+ filename : safeName ( it . filename ) ? it . filename : `file-${ i } ` ,
638+ } ) ) ;
639+
640+ // Bash script (Mac/Linux). --create-dirs makes the sub-01/anat/… folders.
623641 const sh =
624642 `#!/bin/bash\n` +
625643 `# Downloads ${ docId } from ${ dbName } \n` +
626644 `# Usage: bash ${ docId } .sh\n` +
627645 `set -e\n` +
628- `mkdir -p "${ docId } "\n` +
629- `cd "${ docId } " || exit 1\n` +
630- items
631- . map ( ( it ) => `curl -L -C - -o "${ it . filename } " "${ it . url } "` )
646+ `mkdir -p ${ shq ( docId ) } \n` +
647+ `cd ${ shq ( docId ) } || exit 1\n` +
648+ safeItems
649+ . map (
650+ ( it ) =>
651+ `curl -L -C - --create-dirs -o ${ shq ( it . filename ) } ${ shq (
652+ it . url
653+ ) } `
654+ )
632655 . join ( "\n" ) +
633656 `\necho "Done. Files saved to $(pwd)"\n` ;
634657
@@ -637,10 +660,16 @@ const UpdatedDatasetDetailPage: React.FC = () => {
637660 `@echo off\r\n` +
638661 `REM Downloads ${ docId } from ${ dbName } \r\n` +
639662 `REM Usage: double-click or run ${ docId } .bat\r\n` +
640- `if not exist "${ docId } " mkdir "${ docId } "\r\n` +
641- `cd /d "${ docId } "\r\n` +
642- items
643- . map ( ( it ) => `curl -L -C - -o "${ it . filename } " "${ it . url } "` )
663+ `if not exist ${ batq ( docId ) } mkdir ${ batq ( docId ) } \r\n` +
664+ `cd /d ${ batq ( docId ) } \r\n` +
665+ safeItems
666+ . filter ( ( it ) => ! it . filename . includes ( '"' ) && ! it . url . includes ( '"' ) )
667+ . map (
668+ ( it ) =>
669+ `curl -L -C - --create-dirs -o ${ batq ( it . filename ) } ${ batq (
670+ it . url
671+ ) } `
672+ )
644673 . join ( "\r\n" ) +
645674 `\r\necho Done. Files saved to %cd%\r\n` +
646675 `pause\r\n` ;
0 commit comments