Skip to content

Commit f5038d5

Browse files
ZePan110github-advanced-security[bot]
authored andcommitted
Potential fix for code scanning alert no. 427: Uncontrolled data used in path expression (opea-project#2362)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: cogniware-devops <ambarish.desai@cogniware.ai>
1 parent 29c8306 commit f5038d5

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

EdgeCraftRAG/edgecraftrag/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ def get_prompt_template(model_path, prompt_content=None, template_path=None, ena
4545
if prompt_content is not None:
4646
template = prompt_content
4747
elif template_path is not None:
48-
template = Path(template_path).read_text(encoding=None)
48+
# Safely load the template only if it is inside /templates (or other safe root)
49+
safe_root = "/templates"
50+
normalized_path = os.path.normpath(os.path.join(safe_root, template_path))
51+
if not normalized_path.startswith(safe_root):
52+
raise ValueError("Template path is outside of the allowed directory.")
53+
if not os.path.exists(normalized_path):
54+
raise FileNotFoundError("Template file does not exist.")
55+
template = Path(normalized_path).read_text(encoding=None)
4956
else:
5057
template = DEFAULT_TEMPLATE
5158
tokenizer = AutoTokenizer.from_pretrained(model_path)

0 commit comments

Comments
 (0)