@@ -112,23 +112,32 @@ export async function uploadBlueprintAsset(cred: RequestCredentials, fileId: str
112112 assertConnectionHasOneOfPermissions ( cred , ...PERMISSIONS_FOR_MANAGE_BLUEPRINTS )
113113
114114 const storePath = getSystemStorePath ( )
115+ const assetsDir = path . resolve ( storePath , 'assets' ) + path . sep
116+ const assetPath = path . resolve ( path . join ( assetsDir , fileId ) )
117+ if ( ! assetPath . startsWith ( assetsDir ) ) {
118+ throw new Error ( 'Asset name outside of asset storage path' )
119+ }
115120
116121 // TODO: add access control here
117122 const data = Buffer . from ( body , 'base64' )
118- const parsedPath = path . parse ( fileId )
119- logger . info (
120- `Write ${ data . length } bytes to ${ path . join ( storePath , fileId ) } (storePath: ${ storePath } , fileId: ${ fileId } )`
121- )
123+ logger . info ( `Write ${ data . length } bytes to ${ assetPath } (storePath: ${ storePath } , fileId: ${ fileId } )` )
122124
123- await fsp . mkdir ( path . join ( storePath , parsedPath . dir ) , { recursive : true } )
124- await fsp . writeFile ( path . join ( storePath , fileId ) , data )
125+ const assetDirPath = path . dirname ( assetPath )
126+
127+ await fsp . mkdir ( assetDirPath , { recursive : true } )
128+ await fsp . writeFile ( assetPath , data )
125129}
126130export function retrieveBlueprintAsset ( _cred : RequestCredentials , fileId : string ) : ReadStream {
127131 check ( fileId , String )
128132
129133 const storePath = getSystemStorePath ( )
134+ const assetsDir = path . resolve ( storePath , 'assets' ) + path . sep
135+ const assetPath = path . resolve ( path . join ( assetsDir , fileId ) )
136+ if ( ! assetPath . startsWith ( assetsDir ) ) {
137+ throw new Error ( 'Requested asset outside of asset storage path' )
138+ }
130139
131- return createReadStream ( path . join ( storePath , fileId ) )
140+ return createReadStream ( assetPath )
132141}
133142/** Only to be called from internal functions */
134143export async function internalUploadBlueprint (
0 commit comments