1515class TimeoutException (Exception ):
1616 pass
1717
18- def prepare_safe_globals ():
19- safe_globals = {
18+ def prepare_execution_globals ():
19+ """
20+ Prepare globals dictionary for Z3/SymPy code execution.
21+
22+ WARNING: This is NOT a security sandbox. The name "execution_globals" reflects
23+ that this simply provides the execution environment for solver code, not a
24+ security boundary. The code is executed via exec() with access to z3, sympy,
25+ and math libraries. Only execute trusted code.
26+ """
27+ execution_globals = {
2028 'print' : print ,
2129 '__builtins__' : {
2230 'True' : True ,
@@ -35,7 +43,7 @@ def prepare_safe_globals():
3543 }
3644
3745 # Add common math functions
38- safe_globals .update ({
46+ execution_globals .update ({
3947 'log' : math .log ,
4048 'log2' : math .log2 ,
4149 'sqrt' : math .sqrt ,
@@ -48,10 +56,10 @@ def prepare_safe_globals():
4856 })
4957
5058 # Add complex number support
51- safe_globals ['I' ] = complex (0 , 1 )
52- safe_globals ['Complex' ] = complex
59+ execution_globals ['I' ] = complex (0 , 1 )
60+ execution_globals ['Complex' ] = complex
5361
54- return safe_globals
62+ return execution_globals
5563
5664def execute_code_in_process (code : str ):
5765 import z3
@@ -60,18 +68,18 @@ def execute_code_in_process(code: str):
6068 import itertools
6169 from fractions import Fraction
6270
63- safe_globals = prepare_safe_globals ()
64-
71+ execution_globals = prepare_execution_globals ()
72+
6573 # Add Z3 specific functions
6674 z3_whitelist = set (dir (z3 ))
67- safe_globals .update ({name : getattr (z3 , name ) for name in z3_whitelist })
75+ execution_globals .update ({name : getattr (z3 , name ) for name in z3_whitelist })
6876
6977 # Add SymPy specific functions
7078 sympy_whitelist = set (dir (sympy ))
71- safe_globals .update ({name : getattr (sympy , name ) for name in sympy_whitelist })
79+ execution_globals .update ({name : getattr (sympy , name ) for name in sympy_whitelist })
7280
7381 # Ensure key Z3 and SymPy components are available
74- safe_globals .update ({
82+ execution_globals .update ({
7583 'z3' : z3 ,
7684 'sympy' : sympy ,
7785 'Solver' : z3 .Solver ,
@@ -112,22 +120,22 @@ def as_numerical(x):
112120 return x .approx (20 )
113121 return float (x )
114122
115- safe_globals ['as_numerical' ] = as_numerical
123+ execution_globals ['as_numerical' ] = as_numerical
116124
117125 def Mod (x , y ):
118126 return x % y
119127
120- safe_globals ['Mod' ] = Mod
128+ execution_globals ['Mod' ] = Mod
121129
122130 def Rational (numerator , denominator = 1 ):
123131 return z3 .Real (str (Fraction (numerator , denominator )))
124132
125- safe_globals ['Rational' ] = Rational
133+ execution_globals ['Rational' ] = Rational
126134
127135 output_buffer = io .StringIO ()
128136 with contextlib .redirect_stdout (output_buffer ):
129137 try :
130- exec (code , safe_globals , {})
138+ exec (code , execution_globals , {})
131139 except Exception :
132140 return ("error" , traceback .format_exc ())
133141 return ("success" , output_buffer .getvalue ())
0 commit comments