Update codex/sandbox.py
Browse files- codex/sandbox.py +17 -4
codex/sandbox.py
CHANGED
|
@@ -1,9 +1,21 @@
|
|
| 1 |
import ast
|
| 2 |
|
| 3 |
SAFE_NODES = (
|
| 4 |
-
ast.Module,
|
| 5 |
-
ast.
|
| 6 |
-
ast.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|