"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(backtest): block getattr indirection onto os in signal-engine sandbox (GHSA-jqmf F8)
Haozhe Wuec052ffab28b
diff --git a/agent/backtest/runner.py b/agent/backtest/runner.pyindex 17b711987..b2e61c4b0 100644--- a/agent/backtest/runner.py+++ b/agent/backtest/runner.py@@ -276,8 +276,10 @@ def _validate_class_body(node: ast.ClassDef) -> None:# (it does ``import module; SignalEngine().generate(data_map)``). Blocking those# imports file-wide would reject strategies generated from ~12 shipped skills, so# we block the dangerous *use* along the executed path instead of a harmless-# unused top-level import. Transitive reach via ``getattr``/indirection is a-# documented residual — see the VT-001 note; this is not a kernel-level guarantee.+# unused top-level import. Direct ``getattr``/``setattr``/``delattr`` indirection+# onto ``os`` / forbidden modules is now rejected (see _reject_forbidden_getattr);+# exotic reach via ``builtins.getattr`` or aliasing remains a documented residual+# (VT-001) — this is defense-in-depth, not a kernel-level guarantee._FORBIDDEN_IMPORT_MODULES = frozenset({"socket",@@ -319,6 +321,13 @@ _FORBIDDEN_OS_ATTRS = frozenset(_FORBIDDEN_BUILTINS = frozenset({"eval", "exec", "compile", "__import__", "globals", "locals", "vars", "breakpoint"})+# getattr/setattr/delattr can indirect around the attribute scanner+# (``getattr(os, "system")("id")``). We reject them ONLY when the target object+# is ``os`` or a forbidden module — keyed off the target, not the attribute+# string, so ``getattr(os, "sys" + "tem")`` is caught too. Legitimate dynamic+# access on user objects (``getattr(tech, name, None)``, ``getattr(self, x)``)+# is unaffected.+_GETATTR_INDIRECTION = frozenset({"getattr", "setattr", "delattr"})_OPEN_WRITE_MODE_CHARS = frozenset("wax+")_SCRUB_MSG = "is not allowed inside generated strategy code"@@ -367,6 +376,30 @@ def _reject_forbidden_open(node: ast.Call) -> None:raise ValueError(f"open() with a non-relative path {path!r} {_SCRUB_MSG}")+def _reject_forbidden_getattr(node: ast.Call) -> None:+ """Reject getattr/setattr/delattr indirection onto ``os`` / forbidden modules.++ Closes the documented bypass ``getattr(os, "system")("id")`` — and computed+ variants like ``getattr(os, "sys" + "tem")`` — by keying off the *target*+ object (first positional arg), not the attribute string. Dynamic access on+ ordinary user objects (``getattr(tech, name, None)``) is left untouched.+ """+ func = node.func+ if not (isinstance(func, ast.Name) and func.id in _GETATTR_INDIRECTION):+ return+ if not node.args:+ return+ target = node.args[0]+ if isinstance(target, ast.Name):+ root: str | None = target.id+ elif isinstance(target, ast.Attribute):+ root = _attribute_root_name(target)+ else:+ root = None+ if root == "os" or root in _FORBIDDEN_IMPORT_MODULES:+ raise ValueError(f"{func.id}() indirection onto {root!r} {_SCRUB_MSG}")++def _reject_forbidden_node(node: ast.AST) -> None:"""Raise ``ValueError`` if a single AST node performs a forbidden operation."""if isinstance(node, ast.Import):@@ -392,6 +425,7 @@ def _reject_forbidden_node(node: ast.AST) -> None:raise ValueError(f"Use of {node.id!r} {_SCRUB_MSG}")elif isinstance(node, ast.Call):_reject_forbidden_open(node)+ _reject_forbidden_getattr(node)def _scan_runtime_reachable(tree: ast.Module) -> None:diff --git a/agent/tests/test_backtest_runner_security.py b/agent/tests/test_backtest_runner_security.pyindex f5823dc2c..c1c26de84 100644--- a/agent/tests/test_backtest_runner_security.py+++ b/agent/tests/test_backtest_runner_security.py@@ -194,6 +194,68 @@ def test_signal_engine_allows_realistic_pandas_strategy(tmp_path) -> None:assert hasattr(module, "SignalEngine")+ "expr",+ [+ "getattr(os, 'system')('id')",+ "getattr(os, 'sys' + 'tem')('id')", # computed attr name; target-keyed check still catches it+ "getattr(os, 'popen')('id')",+ "setattr(os, 'x', 1)",+ ],+ ids=["getattr_system", "getattr_computed", "getattr_popen", "setattr_os"],+)+def test_signal_engine_rejects_getattr_indirection_onto_os(tmp_path, expr) -> None:+ # GHSA-jqmf F8 residual: `import os` is allowed and the attribute scanner+ # never sees ".system", so getattr(os, "system")("id") previously slipped+ # through. The target-keyed getattr/setattr/delattr guard must reject it.+ signal_file = tmp_path / "signal_engine.py"+ signal_file.write_text(+ "\n".join(+ [+ '"""x."""',+ "import os",+ "class SignalEngine:",+ " def generate(self, *args, **kwargs):",+ f" return {expr}",+ ]+ ),+ encoding="utf-8",+ )++ with pytest.raises(ValueError, match="not allowed inside generated strategy code"):+ _load_module_from_file(signal_file, _module_name())+++def test_signal_engine_allows_getattr_on_user_objects(tmp_path) -> None:+ # Dynamic attribute access on ordinary user objects (self, a DataFrame, an+ # indicator object) is legitimate and common — the bundled harmonic example+ # uses getattr(tech, name, None) — so the F8 guard must NOT reject it.+ signal_file = tmp_path / "signal_engine.py"+ signal_file.write_text(+ "\n".join(+ [+ '"""Strategy using dynamic attribute access on user objects."""',+ "from typing import Dict",+ "import pandas as pd",+ "class SignalEngine:",+ " def __init__(self, lookback: int = 20):",+ " self.lookback = lookback",+ " def generate(self, data_map: Dict[str, pd.DataFrame]):",+ " out = {}",+ " window = getattr(self, 'lookback', 20)",+ " for code, df in data_map.items():",+ " close = getattr(df, 'close', None)",+ " out[code] = close.rolling(window).mean() if close is not None else df",+ " return out",+ ]+ ),+ encoding="utf-8",+ )++ module = _load_module_from_file(signal_file, _module_name())+ assert hasattr(module, "SignalEngine")++def test_signal_engine_allows_unreachable_network_helper(tmp_path) -> None:# Mirrors the bundled skill examples: a top-level ``import requests`` plus a# standalone ``_fetch_okx`` data-fetch helper that generate() never calls.