forked from aden-hive/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_sandbox.py
More file actions
413 lines (333 loc) · 10.9 KB
/
code_sandbox.py
File metadata and controls
413 lines (333 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""
Code Sandbox for Safe Execution of Dynamic Code.
Provides a restricted execution environment for code generated by
the external planner. This is critical for open-ended planning where
the planner can create arbitrary code actions.
Security measures:
1. Restricted builtins (no file I/O, no imports of dangerous modules)
2. Timeout enforcement
3. Memory limits (via resource module on Unix)
4. Namespace isolation
"""
import ast
import signal
import sys
from contextlib import contextmanager
from dataclasses import dataclass, field
from typing import Any
# Safe builtins whitelist
SAFE_BUILTINS = {
# Basic types
"True": True,
"False": False,
"None": None,
# Type constructors
"bool": bool,
"int": int,
"float": float,
"str": str,
"list": list,
"dict": dict,
"set": set,
"tuple": tuple,
"frozenset": frozenset,
# Basic functions
"abs": abs,
"all": all,
"any": any,
"bin": bin,
"chr": chr,
"divmod": divmod,
"enumerate": enumerate,
"filter": filter,
"format": format,
"hex": hex,
"isinstance": isinstance,
"issubclass": issubclass,
"iter": iter,
"len": len,
"map": map,
"max": max,
"min": min,
"next": next,
"oct": oct,
"ord": ord,
"pow": pow,
"range": range,
"repr": repr,
"reversed": reversed,
"round": round,
"slice": slice,
"sorted": sorted,
"sum": sum,
"zip": zip,
}
# Modules that can be imported
ALLOWED_MODULES = {
"math",
"json",
"re",
"datetime",
"collections",
"itertools",
"functools",
"operator",
"string",
"random",
"statistics",
"decimal",
"fractions",
}
# Dangerous AST nodes to block
BLOCKED_AST_NODES = {
ast.Import,
ast.ImportFrom,
ast.Global,
ast.Nonlocal,
}
class CodeSandboxError(Exception):
"""Error during sandboxed code execution."""
pass
class TimeoutError(CodeSandboxError):
"""Code execution timed out."""
pass
class SecurityError(CodeSandboxError):
"""Code contains potentially dangerous operations."""
pass
@dataclass
class SandboxResult:
"""Result of sandboxed code execution."""
success: bool
result: Any = None
error: str | None = None
stdout: str = ""
variables: dict[str, Any] = field(default_factory=dict)
execution_time_ms: int = 0
class RestrictedImporter:
"""Custom importer that only allows whitelisted modules."""
def __init__(self, allowed_modules: set[str]):
self.allowed_modules = allowed_modules
self._cache: dict[str, Any] = {}
def __call__(self, name: str, *args, **kwargs):
if name not in self.allowed_modules:
raise SecurityError(f"Import of module '{name}' is not allowed")
if name not in self._cache:
import importlib
self._cache[name] = importlib.import_module(name)
return self._cache[name]
class CodeValidator:
"""Validates code for safety before execution."""
def __init__(self, blocked_nodes: set[type] | None = None):
self.blocked_nodes = blocked_nodes or BLOCKED_AST_NODES
def validate(self, code: str) -> list[str]:
"""
Validate code and return list of issues.
Returns empty list if code is safe.
"""
issues = []
try:
tree = ast.parse(code)
except SyntaxError as e:
return [f"Syntax error: {e}"]
for node in ast.walk(tree):
# Check for blocked node types
if type(node) in self.blocked_nodes:
lineno = getattr(node, "lineno", "?")
issues.append(f"Blocked operation: {type(node).__name__} at line {lineno}")
# Check for dangerous attribute access
if isinstance(node, ast.Attribute):
if node.attr.startswith("_"):
issues.append(
f"Access to private attribute '{node.attr}' at line {node.lineno}"
)
# Check for exec/eval calls
if isinstance(node, ast.Call):
if isinstance(node.func, ast.Name):
if node.func.id in ("exec", "eval", "compile", "__import__"):
issues.append(
f"Blocked function call: {node.func.id} at line {node.lineno}"
)
return issues
class CodeSandbox:
"""
Sandboxed environment for executing dynamic code.
Usage:
sandbox = CodeSandbox(timeout_seconds=5)
result = sandbox.execute(
code="x = 1 + 2\\nresult = x * 3",
inputs={"multiplier": 2},
)
if result.success:
print(result.variables["result"]) # 6
"""
def __init__(
self,
timeout_seconds: int = 10,
allowed_modules: set[str] | None = None,
safe_builtins: dict[str, Any] | None = None,
):
self.timeout_seconds = timeout_seconds
self.allowed_modules = allowed_modules or ALLOWED_MODULES
self.safe_builtins = safe_builtins or SAFE_BUILTINS
self.validator = CodeValidator()
self.importer = RestrictedImporter(self.allowed_modules)
@contextmanager
def _timeout_context(self, seconds: int):
"""Context manager for timeout enforcement."""
def handler(signum, frame):
raise TimeoutError(f"Code execution timed out after {seconds} seconds")
# Only works on Unix-like systems
if hasattr(signal, "SIGALRM"):
old_handler = signal.signal(signal.SIGALRM, handler)
signal.alarm(seconds)
try:
yield
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, old_handler)
else:
# Windows: no timeout support, just execute
yield
def _create_namespace(self, inputs: dict[str, Any]) -> dict[str, Any]:
"""Create isolated namespace for code execution."""
namespace = {
"__builtins__": dict(self.safe_builtins),
"__import__": self.importer,
}
# Add input variables
namespace.update(inputs)
return namespace
def execute(
self,
code: str,
inputs: dict[str, Any] | None = None,
extract_vars: list[str] | None = None,
) -> SandboxResult:
"""
Execute code in sandbox.
Args:
code: Python code to execute
inputs: Variables to inject into namespace
extract_vars: Variable names to extract from namespace after execution
Returns:
SandboxResult with execution outcome
"""
import time
inputs = inputs or {}
extract_vars = extract_vars or []
# Validate code first
issues = self.validator.validate(code)
if issues:
return SandboxResult(
success=False,
error=f"Code validation failed: {'; '.join(issues)}",
)
# Create isolated namespace
namespace = self._create_namespace(inputs)
# Capture stdout
import io
old_stdout = sys.stdout
sys.stdout = captured_stdout = io.StringIO()
start_time = time.time()
try:
with self._timeout_context(self.timeout_seconds):
# Compile and execute
compiled = compile(code, "<sandbox>", "exec")
exec(compiled, namespace)
execution_time_ms = int((time.time() - start_time) * 1000)
# Extract requested variables
extracted = {}
for var in extract_vars:
if var in namespace:
extracted[var] = namespace[var]
# Also extract any new variables (not in inputs or builtins)
for key, value in namespace.items():
if key not in inputs and key not in self.safe_builtins and not key.startswith("_"):
extracted[key] = value
return SandboxResult(
success=True,
result=namespace.get("result"), # Convention: 'result' is the return value
stdout=captured_stdout.getvalue(),
variables=extracted,
execution_time_ms=execution_time_ms,
)
except TimeoutError as e:
return SandboxResult(
success=False,
error=str(e),
execution_time_ms=self.timeout_seconds * 1000,
)
except SecurityError as e:
return SandboxResult(
success=False,
error=f"Security violation: {e}",
execution_time_ms=int((time.time() - start_time) * 1000),
)
except Exception as e:
return SandboxResult(
success=False,
error=f"{type(e).__name__}: {e}",
stdout=captured_stdout.getvalue(),
execution_time_ms=int((time.time() - start_time) * 1000),
)
finally:
sys.stdout = old_stdout
def execute_expression(
self,
expression: str,
inputs: dict[str, Any] | None = None,
) -> SandboxResult:
"""
Execute a single expression and return its value.
Simpler than execute() - just evaluates one expression.
"""
inputs = inputs or {}
# Validate
try:
ast.parse(expression, mode="eval")
except SyntaxError as e:
return SandboxResult(success=False, error=f"Syntax error: {e}")
namespace = self._create_namespace(inputs)
try:
with self._timeout_context(self.timeout_seconds):
result = eval(expression, namespace)
return SandboxResult(success=True, result=result)
except Exception as e:
return SandboxResult(
success=False,
error=f"{type(e).__name__}: {e}",
)
# Singleton instance with default settings
default_sandbox = CodeSandbox()
def safe_exec(
code: str,
inputs: dict[str, Any] | None = None,
timeout_seconds: int = 10,
) -> SandboxResult:
"""
Convenience function for safe code execution.
Args:
code: Python code to execute
inputs: Variables to inject
timeout_seconds: Max execution time
Returns:
SandboxResult
"""
sandbox = CodeSandbox(timeout_seconds=timeout_seconds)
return sandbox.execute(code, inputs)
def safe_eval(
expression: str,
inputs: dict[str, Any] | None = None,
timeout_seconds: int = 5,
) -> SandboxResult:
"""
Convenience function for safe expression evaluation.
Args:
expression: Python expression to evaluate
inputs: Variables to inject
timeout_seconds: Max execution time
Returns:
SandboxResult
"""
sandbox = CodeSandbox(timeout_seconds=timeout_seconds)
return sandbox.execute_expression(expression, inputs)