Skip to content

Commit 08ac995

Browse files
authored
Replace unsafe eval() with ast.literal_eval() in ParseStrArgsAction (#1221)
1 parent 364cc22 commit 08ac995

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

evalscope/arguments.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# flake8: noqa: E501
22
import argparse
3+
import ast
34
import json
45

56
from evalscope.constants import EvalBackend, JudgeStrategy, ModelTask
@@ -23,10 +24,10 @@ def __call__(self, parser, namespace, values, option_string=None):
2324
for arg in values.strip().split(','):
2425
key, value = map(str.strip, arg.split('=', 1)) # Use maxsplit=1 to handle multiple '='
2526
try:
26-
# Safely evaluate the value using eval
27-
arg_dict[key] = eval(value)
28-
except Exception:
29-
# If eval fails, check if it's a boolean value
27+
# Safely evaluate the value using ast.literal_eval
28+
arg_dict[key] = ast.literal_eval(value)
29+
except (ValueError, SyntaxError):
30+
# If ast.literal_eval fails, check if it's a boolean value
3031
value_lower = value.lower()
3132
if value_lower == 'true':
3233
arg_dict[key] = True

0 commit comments

Comments
 (0)