From 9bcccadebddf1b04d2d03e7ac31940cdf9b3340c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20Panzenb=C3=B6ck?= <134175+panzi@users.noreply.github.com> Date: Wed, 12 Feb 2025 06:15:38 +0100 Subject: [PATCH] Remove use of eval() from search.py (#4887) Use `json.loads()` instead. ### What problem does this PR solve? Using `eval()` can lead to code injections. I think this loads a JSON field, right? If yes, why is this done via `eval()` and not `json.loads()`? ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --- rag/nlp/search.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rag/nlp/search.py b/rag/nlp/search.py index 48f3d7a43..b463aa9a5 100644 --- a/rag/nlp/search.py +++ b/rag/nlp/search.py @@ -15,6 +15,7 @@ # import logging import re +import json from dataclasses import dataclass from rag.settings import TAG_FLD, PAGERANK_FLD @@ -258,7 +259,7 @@ class Dealer: q_denor = np.sqrt(np.sum([s*s for t,s in query_rfea.items() if t != PAGERANK_FLD])) for i in search_res.ids: nor, denor = 0, 0 - for t, sc in eval(search_res.field[i].get(TAG_FLD, "{}")).items(): + for t, sc in json.loads(search_res.field[i].get(TAG_FLD, "{}")).items(): if t in query_rfea: nor += query_rfea[t] * sc denor += sc * sc