Skip to content

Commit b83b2c3

Browse files
Potential fix for code scanning alert no. 36: 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 41eb25f commit b83b2c3

File tree

1 file changed

+8
-3
lines changed
  • label_studio_ml/examples/deepgram

1 file changed

+8
-3
lines changed

label_studio_ml/examples/deepgram/model.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pathlib
44
from types import SimpleNamespace
55
from typing import List, Dict, Optional
6+
from werkzeug.utils import secure_filename
67
from label_studio_ml.model import LabelStudioMLBase
78
from label_studio_ml.response import ModelResponse
89
from label_studio_sdk import LabelStudio
@@ -58,9 +59,13 @@ def predict(self, tasks: List[Dict], context: Optional[Dict] = None, **kwargs) -
5859
)
5960

6061
# Generate unique filename for the audio file - task_id and user_id are unique identifiers for the task and user
61-
audio_filename = f"{task_id}_{context['user_id']}.mp3"
62-
local_audio_path = f"/tmp/{audio_filename}"
63-
62+
safe_task_id = secure_filename(str(task_id))
63+
safe_user_id = secure_filename(str(context['user_id']))
64+
audio_filename = f"{safe_task_id}_{safe_user_id}.mp3"
65+
local_audio_path = os.path.normpath(os.path.join("/tmp", audio_filename))
66+
# Ensure the final path is within /tmp
67+
if not local_audio_path.startswith(os.path.abspath("/tmp") + os.sep):
68+
raise ValueError("Invalid path: attempted directory traversal in filename")
6469
# Write audio chunks to local file
6570
with open(local_audio_path, "wb") as audio_file:
6671
for chunk in response:

0 commit comments

Comments
 (0)