Skip to content

Commit a16d61c

Browse files
Potential fix for code scanning alert no. 427: Uncontrolled data used in path expression (#2362)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 2baee6e commit a16d61c

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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)