Personal AI, On Personal Devices
Python
security(code_interpreter): AST validation to replace bypassable substring blocklist (#1002)
clawdovil-agent-bv43e43a4bb617
diff --git a/src/openjarvis/tools/code_interpreter.py b/src/openjarvis/tools/code_interpreter.pyindex 4bc52347..79851642 100644--- a/src/openjarvis/tools/code_interpreter.py+++ b/src/openjarvis/tools/code_interpreter.py@@ -1,7 +1,25 @@-"""Code interpreter tool — safe Python code execution in subprocess."""+"""Code interpreter tool — Python execution with AST validation + hardening.++Security model (defense in depth):++1. **AST allowlist validation** (this module) rejects code *before* it runs:+ imports outside a small supported set, private-attribute walks, and calls to+ ``eval``/``exec``/``compile``/``__import__``/``open``/``getattr`` &c. This+ replaces the old substring blocklist, which was trivially bypassed (e.g.+ ``getattr(__builtins__, 'sys'+'tem')`` or a simple space: ``eval ('...')``).+2. **Isolated interpreter** — the child runs with ``-I -B -S`` (isolated mode,+ no ``.pyc``, no ``site``), a sanitized environment, and POSIX resource+ limits (CPU + address space + no new files) applied in a ``preexec_fn``.+3. **Outer sandbox** — on a server this tool should run inside the Docker+ sandbox (see ``code_interpreter_docker`` / ``deploy/docker/Dockerfile.sandbox``)+ or be disabled entirely. AST validation is a filter, not a jail: the Docker+ boundary is the real containment for untrusted code.+"""from __future__ import annotations+import ast+import osimport subprocessimport sysfrom typing import Any@@ -10,26 +28,97 @@ from openjarvis.core.registry import ToolRegistryfrom openjarvis.core.types import ToolResultfrom openjarvis.tools._stubs import BaseTool, ToolSpec-# Dangerous patterns to block-_BLOCKED_PATTERNS = [- "os.system",- "os.popen",- "subprocess.",- "shutil.rmtree",- "os.remove",- "os.unlink",- "os.rmdir",- "__import__",- "eval(",- "exec(",- "compile(",- "open(",-]+# Keep the supported import surface deliberately small. A denylist is not+# sufficient here: otherwise an apparently harmless module can re-export a+# dangerous one (for example ``platform.os.system``), and alternate file APIs+# such as ``io.open`` remain available.+_ALLOWED_IMPORTS = frozenset({"json", "math", "time"})++# Names that must never be referenced or called (escape / IO primitives).+_BLOCKED_NAMES = frozenset(+ {+ "eval",+ "exec",+ "compile",+ "__import__",+ "open",+ "input",+ "breakpoint",+ "globals",+ "locals",+ "vars",+ "getattr",+ "setattr",+ "delattr",+ "memoryview",+ "help",+ }+)+++class UnsafeCodeError(ValueError):+ """Raised when submitted code fails AST validation."""+++def _validate_ast(code: str) -> None:+ """Reject code that could escape the interpreter or perform IO.++ Raises :class:`UnsafeCodeError` (or ``SyntaxError``) on anything unsafe.+ Blocking is by *structure*, not by string matching, so obfuscation such as+ ``getattr(x, 'sys'+'tem')`` or spacing tricks cannot slip through.+ """+ tree = ast.parse(code, mode="exec")+ for node in ast.walk(tree):+ if isinstance(node, ast.Import):+ for alias in node.names:+ if alias.name not in _ALLOWED_IMPORTS:+ raise UnsafeCodeError(f"import of '{alias.name}' is not allowed")+ elif isinstance(node, ast.ImportFrom):+ if node.module not in _ALLOWED_IMPORTS or any(+ alias.name == "*" for alias in node.names+ ):+ raise UnsafeCodeError(f"import from '{node.module}' is not allowed")+ elif isinstance(node, ast.Attribute):+ # Private attributes include both dunder escape chains and module+ # implementation details that may expose imported capabilities.+ if node.attr.startswith("_"):+ raise UnsafeCodeError(f"private attribute access '{node.attr}' blocked")+ elif isinstance(node, ast.Name):+ if node.id in _BLOCKED_NAMES:+ raise UnsafeCodeError(f"use of '{node.id}' is not allowed")+ if node.id.startswith("__") and node.id.endswith("__"):+ raise UnsafeCodeError(f"dunder name '{node.id}' is not allowed")+++def _child_limits() -> None: # pragma: no cover - POSIX-only, runs in child+ """Apply resource limits in the forked child before exec (POSIX only)."""+ import resource++ # Apply each protection independently. Some platforms expose a resource+ # constant but reject changes to it (notably RLIMIT_AS on macOS); that must+ # not prevent the remaining supported limits from being installed.+ try:+ os.setsid()+ except OSError:+ pass+ try:+ resource.setrlimit(resource.RLIMIT_CPU, (10, 10))+ except (OSError, ValueError):+ pass+ try:+ _mem = 512 * 1024 * 1024+ resource.setrlimit(resource.RLIMIT_AS, (_mem, _mem))+ except (OSError, ValueError):+ pass+ try:+ resource.setrlimit(resource.RLIMIT_FSIZE, (0, 0))+ except (OSError, ValueError):+ pass@ToolRegistry.register("code_interpreter")class CodeInterpreterTool(BaseTool):- """Execute Python code in an isolated subprocess."""+ """Execute Python code after AST validation, in a hardened subprocess."""tool_id = "code_interpreter"@@ -43,7 +132,9 @@ class CodeInterpreterTool(BaseTool):name="code_interpreter",description=("Execute Python code and return the output."- " Code runs in an isolated subprocess."+ " Code is AST-validated and runs in a hardened, isolated"+ " subprocess (no imports of os/sys/subprocess/network, no"+ " file IO, no eval/exec)."),parameters={"type": "object",@@ -68,21 +159,41 @@ class CodeInterpreterTool(BaseTool):success=False,)- # Security check- for pattern in _BLOCKED_PATTERNS:- if pattern in code:- return ToolResult(- tool_name="code_interpreter",- content=f"Blocked: code contains prohibited pattern '{pattern}'",- success=False,- )+ # Security check — reject before running, by AST structure.+ try:+ _validate_ast(code)+ except SyntaxError as exc:+ return ToolResult(+ tool_name="code_interpreter",+ content=f"SyntaxError: {exc}",+ success=False,+ )+ except UnsafeCodeError as exc:+ return ToolResult(+ tool_name="code_interpreter",+ content=f"Blocked by code validation: {exc}",+ success=False,+ )++ # Sanitized environment — drop inherited secrets/tokens.+ safe_env = {+ "PATH": os.environ.get("PATH", ""),+ "LANG": os.environ.get("LANG", "C.UTF-8"),+ "PYTHONDONTWRITEBYTECODE": "1",+ "PYTHONNOUSERSITE": "1",+ }+ # ``preexec_fn`` only exists / is safe on POSIX.+ preexec = _child_limits if os.name == "posix" else Nonetry:result = subprocess.run(- [sys.executable, "-c", code],+ [sys.executable, "-I", "-B", "-S", "-c", code],capture_output=True,text=True,timeout=self._timeout,+ env=safe_env,+ cwd=os.environ.get("OPENJARVIS_CODE_CWD") or None,+ preexec_fn=preexec, # noqa: PLW1509 - intentional child hardening)output = result.stdoutif result.stderr:@@ -109,4 +220,4 @@ class CodeInterpreterTool(BaseTool):)-__all__ = ["CodeInterpreterTool"]+__all__ = ["CodeInterpreterTool", "UnsafeCodeError"]diff --git a/tests/tools/test_code_interpreter.py b/tests/tools/test_code_interpreter.pyindex be2c4eac..3b9d8090 100644--- a/tests/tools/test_code_interpreter.py+++ b/tests/tools/test_code_interpreter.py@@ -59,7 +59,9 @@ class TestCodeInterpreterTool:result = tool.execute(code="import os; os.system('ls')")assert result.success is Falseassert "Blocked" in result.content- assert "os.system" in result.content+ # AST validation blocks the dangerous *import* structurally, so the+ # message names the module rather than echoing an "os.system" substring.+ assert "os" in result.contentdef test_dangerous_subprocess_blocked(self):tool = CodeInterpreterTool()@@ -79,6 +81,24 @@ class TestCodeInterpreterTool:assert result.success is Falseassert "Blocked" in result.content+ def test_reexported_process_module_blocked(self):+ tool = CodeInterpreterTool()+ result = tool.execute(code="import platform; platform.os.system('true')")+ assert result.success is False+ assert "Blocked" in result.content++ def test_alternate_file_api_blocked(self, tmp_path):+ marker = tmp_path / "marker.txt"+ marker.write_text("keep me")+ tool = CodeInterpreterTool()++ result = tool.execute(+ code=f"import io; io.open({str(marker)!r}, 'w').write('changed')"+ )++ assert result.success is False+ assert marker.read_text() == "keep me"+def test_no_code_provided(self):tool = CodeInterpreterTool()result = tool.execute(code="")diff --git a/tests/tools/test_code_interpreter_ast.py b/tests/tools/test_code_interpreter_ast.pynew file mode 100644index 00000000..b4e1d7f9--- /dev/null+++ b/tests/tools/test_code_interpreter_ast.py@@ -0,0 +1,68 @@+"""code_interpreter: AST validation blocks the old substring bypasses."""++from __future__ import annotations++import pytest++from openjarvis.tools.code_interpreter import _validate_ast+++class TestCodeInterpreterValidation:+ @pytest.mark.parametrize(+ "code",+ [+ "import os",+ "import subprocess as s",+ "from os import system",+ "import io",+ "from io import open as reader",+ "import platform",+ "getattr(__builtins__, 'system')",+ "eval ('1+1')", # a space defeated the old substring check+ "().__class__.__base__.__subclasses__()",+ "open('/etc/passwd')",+ "__import__('os')",+ ],+ )+ def test_dangerous_code_blocked(self, code):+ # The tools package is reloaded in parts of the full suite to restore+ # registry decorators, so catch the stable public base rather than a+ # pre-reload UnsafeCodeError class object.+ with pytest.raises((ValueError, SyntaxError)):+ _validate_ast(code)++ @pytest.mark.parametrize(+ "code",+ [+ "print(sum(range(10)))",+ "import math\nprint(math.sqrt(2))",+ "import json\nprint(json.dumps({'a': 1}))",+ "xs = [i * 2 for i in range(5)]\nprint(xs)",+ ],+ )+ def test_safe_code_allowed(self, code):+ _validate_ast(code) # must not raise++ def test_limit_failure_does_not_skip_later_hardening(self, monkeypatch):+ import sys+ from types import SimpleNamespace+ from unittest.mock import MagicMock++ from openjarvis.tools.code_interpreter import _child_limits++ fake_resource = SimpleNamespace(RLIMIT_CPU=1, RLIMIT_AS=2, RLIMIT_FSIZE=3)+ fake_resource.setrlimit = MagicMock(+ side_effect=[None, ValueError("unsupported"), None]+ )+ monkeypatch.setitem(sys.modules, "resource", fake_resource)+ setsid = MagicMock()+ monkeypatch.setattr("openjarvis.tools.code_interpreter.os.setsid", setsid)++ _child_limits()++ setsid.assert_called_once_with()+ assert [call.args[0] for call in fake_resource.setrlimit.call_args_list] == [+ fake_resource.RLIMIT_CPU,+ fake_resource.RLIMIT_AS,+ fake_resource.RLIMIT_FSIZE,+ ]