@@ -15,13 +15,8 @@ import {
1515
1616const isSafeMulterTempFilename = ( value : string ) : boolean => / ^ [ a - f 0 - 9 ] { 32 } $ / . test ( value ) ;
1717
18- const isPathInsideDirectory = ( candidatePath : string , rootDir : string ) : boolean => {
19- const relativePath = path . relative ( rootDir , candidatePath ) ;
20- return relativePath === "" || ( ! relativePath . startsWith ( ".." ) && ! path . isAbsolute ( relativePath ) ) ;
21- } ;
22-
2318const resolveStagedUploadPath = async (
24- file : { filename ?: unknown } ,
19+ file : { filename ?: unknown ; path ?: unknown } ,
2520 uploadRoot : string
2621) : Promise < string > => {
2722 const absoluteUploadRoot = path . resolve ( uploadRoot ) ;
@@ -33,13 +28,20 @@ const resolveStagedUploadPath = async (
3328 throw new ImportValidationError ( "Invalid upload path" ) ;
3429 }
3530
36- const filename = typeof file . filename === "string" ? file . filename : "" ;
37- if ( ! isSafeMulterTempFilename ( filename ) ) {
31+ // CodeQL path-injection: use basename extraction + strict allowlist, then enforce root containment.
32+ // Multer typically generates a server-side random filename; we still validate defensively.
33+ const rawPath = typeof file . path === "string" ? file . path : "" ;
34+ const rawFilename = typeof file . filename === "string" ? file . filename : "" ;
35+ const basename = path . basename ( rawPath || rawFilename ) ;
36+ if ( ! isSafeMulterTempFilename ( basename ) ) {
3837 throw new ImportValidationError ( "Invalid upload path" ) ;
3938 }
4039
41- const candidatePath = path . resolve ( canonicalUploadRoot , filename ) ;
42- if ( ! isPathInsideDirectory ( candidatePath , canonicalUploadRoot ) ) {
40+ const candidatePath = path . resolve ( canonicalUploadRoot , basename ) ;
41+ const rootPrefix = canonicalUploadRoot . endsWith ( path . sep )
42+ ? canonicalUploadRoot
43+ : `${ canonicalUploadRoot } ${ path . sep } ` ;
44+ if ( ! candidatePath . startsWith ( rootPrefix ) ) {
4345 throw new ImportValidationError ( "Invalid upload path" ) ;
4446 }
4547
0 commit comments