Skip to content

Commit a8b8b8a

Browse files
author
hackinsacks
authored
Merge pull request MITRE-Cyber-Security-CVE-Database#4 from MITRE-Cyber-Security-CVE-Database/alert-autofix-3
Potential fix for code scanning alert no. 3: Uncontrolled data used in path expression
2 parents 4249db5 + 614966e commit a8b8b8a

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

main.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,22 @@ def hello():
135135
if not template:
136136
raise HTTPException(status_code=400, detail="Unsupported project type")
137137

138-
project_dir = os.path.join(SRC_ROOT, project.name)
138+
project_dir = os.path.normpath(os.path.join(SRC_ROOT, project.name))
139+
if not project_dir.startswith(SRC_ROOT):
140+
raise HTTPException(status_code=400, detail="Invalid project name")
139141
os.makedirs(project_dir, exist_ok=True)
140142

141143
for component in [template["frontend"], template["backend"]]:
142-
comp_dir = os.path.join(project_dir, component["path"])
144+
comp_dir = os.path.normpath(os.path.join(project_dir, component["path"]))
145+
if not comp_dir.startswith(project_dir):
146+
raise HTTPException(status_code=400, detail="Invalid component path")
143147
os.makedirs(comp_dir, exist_ok=True)
144148
for file_path, content in component["files"].items():
145-
with open(os.path.join(comp_dir, file_path), "w") as f:
149+
sanitized_file_path = os.path.normpath(file_path)
150+
if not sanitized_file_path or ".." in sanitized_file_path or sanitized_file_path.startswith("/"):
151+
raise HTTPException(status_code=400, detail="Invalid file path")
152+
full_file_path = os.path.join(comp_dir, sanitized_file_path)
153+
with open(full_file_path, "w") as f:
146154
f.write(content.format(project=project))
147155

148156
return {"message": f"Generated {project.name} at {project_dir}"}

0 commit comments

Comments
 (0)