use empty locals and globals in call to eval() in engine tool use

harden eval: prevent the calc tool from accessing globals and locals
This commit is contained in:
Andrej
2025-11-01 07:22:59 -07:00
committed by GitHub
+1 -1
View File
@@ -37,7 +37,7 @@ def eval_with_timeout(formula, max_time=3):
with timeout(max_time, formula): with timeout(max_time, formula):
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter("ignore", SyntaxWarning) warnings.simplefilter("ignore", SyntaxWarning)
return eval(formula) return eval(formula, {"__builtins__": {}}, {})
except Exception as e: except Exception as e:
signal.alarm(0) signal.alarm(0)
# print(f"Warning: Failed to eval {formula}, exception: {e}") # it's ok ignore wrong calculator usage # print(f"Warning: Failed to eval {formula}, exception: {e}") # it's ok ignore wrong calculator usage