File tree Expand file tree Collapse file tree 1 file changed +7
-3
lines changed
code/src/api/v1/controllers Expand file tree Collapse file tree 1 file changed +7
-3
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ async function myBlobToUIntDemo(blob: Blob) {
18
18
19
19
export const postRemoveBackgroundController = ( ) => {
20
20
const persistenceHandler = new ImagesPersistenceHandler ( ) ;
21
-
21
+ const baseTempDir = path . join ( process . cwd ( ) , "temp" ) ;
22
22
return async ( req : Request , res : Response ) : Promise < void > => {
23
23
const roomId = req . params . roomId ;
24
24
const imageId = req . params . imageId ;
@@ -38,10 +38,14 @@ export const postRemoveBackgroundController = () => {
38
38
39
39
const extension = mimeTypes . extension ( contentType ) || "png" ;
40
40
const fileName = `${ roomId } /${ imageId } .${ extension } ` ;
41
- const filePath = path . join ( process . cwd ( ) , "temp" , fileName ) ;
41
+ const filePath = path . resolve ( baseTempDir , fileName ) ;
42
+ // Ensure that filePath is strictly inside baseTempDir
43
+ if ( ! filePath . startsWith ( baseTempDir + path . sep ) ) {
44
+ res . status ( 400 ) . json ( { status : "KO" , message : "Invalid path traversal attempt." } ) ;
45
+ return ;
46
+ }
42
47
43
48
await saveBase64ToFile ( dataBase64 , filePath ) ;
44
-
45
49
try {
46
50
removeBackground ( filePath , {
47
51
publicPath : `file://${ path . join ( process . cwd ( ) , "public" ) } /` ,
You can’t perform that action at this time.
0 commit comments