"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(sandbox): reject forbidden modules reached through a renamed binding
Haozhe Wufe25fc36be7a
diff --git a/agent/backtest/runner.py b/agent/backtest/runner.pyindex e929ed036..79c1beb9a 100644--- a/agent/backtest/runner.py+++ b/agent/backtest/runner.py@@ -501,6 +501,53 @@ def _reject_forbidden_node(node: ast.AST) -> None:_reject_forbidden_getattr(node)+def _module_level_forbidden_aliases(tree: ast.Module) -> dict[str, str]:+ """Map module-level names that are bound to a forbidden module.++ Module-level imports are deliberately not rejected outright (see the+ ``_FORBIDDEN_IMPORT_MODULES`` comment): the shipped skill examples carry an+ unused ``import requests`` beside a helper the runner never reaches, and a+ file-wide import block would reject strategies generated from them. The+ compensating check is on the *use* along the executed path — but that check+ matches dotted chains rooted in the module's own name, so a binding that+ renames it slipped past both::++ from socket import socket as S -> S()+ import socket as sk -> sk.socket()+ from src.trading.service import place_order -> place_order(1)++ This recovers the binding so the use site can be judged by what the name+ actually refers to, which closes the aliasing half of the VT-001 residual+ without touching the harmless unused import.++ Args:+ tree: Parsed signal engine module.++ Returns:+ Bound name -> the dotted path it refers to, for forbidden targets only.+ """+ aliases: dict[str, str] = {}+ for node in tree.body:+ if isinstance(node, ast.Import):+ # A plain ``import socket`` needs no entry: its use is a dotted+ # chain rooted in ``socket``, which the attribute check already+ # rejects. Only a rename hides that root.+ for alias in node.names:+ if alias.asname and _is_forbidden_module_path(alias.name):+ aliases[alias.asname] = alias.name+ elif isinstance(node, ast.ImportFrom):+ module = node.module or ""+ for alias in node.names:+ target = f"{module}.{alias.name}" if module else alias.name+ if (+ _is_forbidden_module_path(module)+ or _is_forbidden_module_path(target)+ or (module.split(".")[0] == "os" and _is_forbidden_os_attr(alias.name))+ ):+ aliases[alias.asname or alias.name] = target+ return aliases++def _scan_runtime_reachable(tree: ast.Module) -> None:"""Reject forbidden ops in ``SignalEngine`` methods + their transitive callees.@@ -526,6 +573,7 @@ def _scan_runtime_reachable(tree: ast.Module) -> None:worklist: list[ast.AST] = [m for m in engine_cls.body if isinstance(m, (ast.FunctionDef, ast.AsyncFunctionDef))]+ aliases = _module_level_forbidden_aliases(tree)visited: set[int] = set()while worklist:fn = worklist.pop()@@ -534,6 +582,11 @@ def _scan_runtime_reachable(tree: ast.Module) -> None:visited.add(id(fn))for node in ast.walk(fn):_reject_forbidden_node(node)+ if isinstance(node, ast.Name) and node.id in aliases:+ raise ValueError(+ f"Use of {node.id!r}, bound at module level to "+ f"{aliases[node.id]!r}, {_SCRUB_MSG}"+ )if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):target = module_funcs.get(node.func.id)if target is not None:diff --git a/agent/tests/test_backtest_runner_security.py b/agent/tests/test_backtest_runner_security.pyindex 6dc65f8b0..e047fd173 100644--- a/agent/tests/test_backtest_runner_security.py+++ b/agent/tests/test_backtest_runner_security.py@@ -202,6 +202,103 @@ def test_signal_engine_still_allows_project_math_imports(tmp_path, import_line)_load_module_from_file(signal_file, _module_name())+# Module-level imports are deliberately left unrejected so an unused+# ``import requests`` beside an unreachable helper does not fail the shipped+# skill examples; the compensating check is on the *use* along the executed+# path. That check matches dotted chains rooted in the module's own name, so+# every binding below renamed the root and reached the payload anyway — the+# aliasing half of the VT-001 residual.+_MODULE_LEVEL_ALIAS_BYPASSES = [+ ("from_socket_alias", "from socket import socket as S", " return S()"),+ ("from_subprocess_alias", "from subprocess import run as R", " return R(['ls'])"),+ ("from_os_system", "from os import system", " return system('id')"),+ ("from_ctypes", "from ctypes import CDLL", " return CDLL('x')"),+ ("import_socket_alias", "import socket as sk", " return sk.socket()"),+ ("import_subprocess_alias", "import subprocess as sp", " return sp.run(['ls'])"),+ (+ "from_trading_place_order",+ "from src.trading.service import place_order",+ " return place_order(1)",+ ),+ (+ "from_live_order_gate",+ "from src.live.sdk_order_gate import check",+ " return check()",+ ),+ (+ "from_trading_submodule_alias",+ "from src.trading import service as sv",+ " return sv.place_order(1)",+ ),+ (+ "import_trading_alias",+ "import src.trading.service as t",+ " return t.place_order(1)",+ ),+]+++ "case_id,import_line,call_line",+ _MODULE_LEVEL_ALIAS_BYPASSES,+ ids=[c[0] for c in _MODULE_LEVEL_ALIAS_BYPASSES],+)+def test_signal_engine_rejects_module_level_alias_of_forbidden_module(+ tmp_path, case_id, import_line, call_line,+) -> None:+ signal_file = tmp_path / "signal_engine.py"+ signal_file.write_text(+ "\n".join(+ [+ '"""Generated signal engine."""',+ import_line,+ "class SignalEngine:",+ " def generate(self, *args, **kwargs):",+ call_line,+ ]+ ),+ encoding="utf-8",+ )++ with pytest.raises(ValueError, match="not allowed inside generated strategy code"):+ _load_module_from_file(signal_file, _module_name())+++ "import_line,body_line",+ [+ # The reason module-level imports are not rejected outright: the shipped+ # skill examples carry these beside helpers the runner never reaches.+ ("import requests", " return []"),+ ("from requests import get", " return []"),+ # Ordinary strategy imports, used on the executed path.+ ("import pandas as pd", " return pd.DataFrame()"),+ ("from src.quantlib.options import bs_price", " return bs_price(1, 1, 1, 1, 1)"),+ ("from os import path", " return path.join('a', 'b')"),+ ],+ ids=["unused_requests", "unused_from_requests", "pandas_alias", "quantlib", "os_path"],+)+def test_signal_engine_alias_check_leaves_legitimate_imports_alone(+ tmp_path, import_line, body_line,+) -> None:+ """The alias check must cost neither the unused import nor the math layer."""+ signal_file = tmp_path / "signal_engine.py"+ signal_file.write_text(+ "\n".join(+ [+ '"""Generated signal engine."""',+ import_line,+ "class SignalEngine:",+ " def generate(self, *args, **kwargs):",+ body_line,+ ]+ ),+ encoding="utf-8",+ )++ _load_module_from_file(signal_file, _module_name())++def test_signal_engine_rejects_forbidden_op_in_transitively_called_helper(tmp_path) -> None:# Payload hidden in a module-level helper that generate() calls — the# reachability walk must follow the call and reject it.