Skip to content

Commit

Permalink
bugfix: Added additional checks when loading template
Browse files Browse the repository at this point in the history
  • Loading branch information
GaiZhenbiao committed Sep 18, 2024
1 parent c2c2b86 commit 2836fd1
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,17 @@ def filter_history(user_name, keyword):
def load_template(filename, mode=0):
logging.debug(f"加载模板文件{filename},模式为{mode}(0为返回字典和下拉菜单,1为返回下拉菜单,2为返回字典)")
lines = []
template_file_path = os.path.join(TEMPLATES_DIR, filename)
# check if template_file_path is inside TEMPLATES_DIR
if not os.path.realpath(template_file_path).startswith(os.path.realpath(TEMPLATES_DIR)):
return "Invalid template file path"
if filename.endswith(".json"):
with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as f:
with open(template_file_path, "r", encoding="utf8") as f:
lines = json.load(f)
lines = [[i["act"], i["prompt"]] for i in lines]
else:
with open(
os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8"
template_file_path, "r", encoding="utf8"
) as csvfile:
reader = csv.reader(csvfile)
lines = list(reader)
Expand Down

0 comments on commit 2836fd1

Please sign in to comment.