Skip to content

Commit f4d6fb8

Browse files
Potential fix for code scanning alert no. 9: Uncontrolled data used in path expression
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 0195fff commit f4d6fb8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

code/src/api/v1/controllers/postRemoveBackground.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function myBlobToUIntDemo(blob: Blob) {
1818

1919
export const postRemoveBackgroundController = () => {
2020
const persistenceHandler = new ImagesPersistenceHandler();
21-
21+
const baseTempDir = path.join(process.cwd(), "temp");
2222
return async (req: Request, res: Response): Promise<void> => {
2323
const roomId = req.params.roomId;
2424
const imageId = req.params.imageId;
@@ -38,10 +38,14 @@ export const postRemoveBackgroundController = () => {
3838

3939
const extension = mimeTypes.extension(contentType) || "png";
4040
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+
}
4247

4348
await saveBase64ToFile(dataBase64, filePath);
44-
4549
try {
4650
removeBackground(filePath, {
4751
publicPath: `file://${path.join(process.cwd(), "public")}/`,

0 commit comments

Comments
 (0)