LordXido commited on
Commit
b64e0d2
·
verified ·
1 Parent(s): 19dd01b

Update codex/sandbox.py

Browse files
Files changed (1) hide show
  1. codex/sandbox.py +17 -4
codex/sandbox.py CHANGED
@@ -1,9 +1,21 @@
1
  import ast
2
 
3
  SAFE_NODES = (
4
- ast.Module, ast.Assign, ast.Expr, ast.Call, ast.Name, ast.Load,
5
- ast.Constant, ast.BinOp, ast.Add, ast.Sub, ast.Mult, ast.Div,
6
- ast.If, ast.Compare, ast.Return, ast.FunctionDef, ast.arguments
 
 
 
 
 
 
 
 
 
 
 
 
7
  )
8
 
9
  def safe_execute(code):
@@ -11,8 +23,9 @@ def safe_execute(code):
11
 
12
  for node in ast.walk(tree):
13
  if not isinstance(node, SAFE_NODES):
14
- raise ValueError("Unsafe code detected")
15
 
16
  local_env = {}
17
  exec(compile(tree, "<sandbox>", "exec"), {"__builtins__": {}}, local_env)
 
18
  return local_env
 
1
  import ast
2
 
3
  SAFE_NODES = (
4
+ ast.Module,
5
+ ast.Assign,
6
+ ast.Expr,
7
+ ast.Call,
8
+ ast.Name,
9
+ ast.Load,
10
+ ast.Constant,
11
+ ast.BinOp,
12
+ ast.Add,
13
+ ast.Sub,
14
+ ast.Mult,
15
+ ast.Div,
16
+ ast.Return,
17
+ ast.FunctionDef,
18
+ ast.arguments
19
  )
20
 
21
  def safe_execute(code):
 
23
 
24
  for node in ast.walk(tree):
25
  if not isinstance(node, SAFE_NODES):
26
+ raise ValueError(f"Unsafe code detected: {type(node).__name__}")
27
 
28
  local_env = {}
29
  exec(compile(tree, "<sandbox>", "exec"), {"__builtins__": {}}, local_env)
30
+
31
  return local_env