Skip to content

Personal AI, On Personal Devices

Python

security(workflow): replace condition eval() with allowlist AST interpreter (#1004)

clawdovil-agent-bv6c1f1c2cf161
diff --git a/src/openjarvis/workflow/engine.py b/src/openjarvis/workflow/engine.py
index 58f49827..66b1c4a6 100644
--- a/src/openjarvis/workflow/engine.py
+++ b/src/openjarvis/workflow/engine.py
@@ -274,10 +274,18 @@ class WorkflowEngine:
success=True,
output="true",
)
- # Simple expression evaluation — check if key exists and is truthy
- # Supports: "node_id.success", "node_id.output contains 'text'"
+ # Simple expression evaluation — check if key exists and is truthy.
+ # A bare eval() (even with empty __builtins__) is escapable via
+ # attribute/subclass walks and allows unbounded-CPU expressions like
+ # (9**9)**9. Use the shared allowlist AST interpreter instead: it has
+ # no attribute access, no dunder names, and no reachable callables
+ # beyond a fixed set of safe builtins, so escape vectors are
+ # unreachable by construction. Anything it cannot evaluate raises and
+ # is treated as a false condition (unchanged fail-safe behavior).
+ from openjarvis.tools.templates.loader import safe_eval_expr
+
try:
- result = str(eval(expr, {"__builtins__": {}}, {"outputs": outputs})) # noqa: S307
+ result = str(safe_eval_expr(expr, {"outputs": outputs}))
except Exception:
result = "false"
return WorkflowStepResult(
diff --git a/tests/workflow/test_condition_safe_eval.py b/tests/workflow/test_condition_safe_eval.py
new file mode 100644
index 00000000..5d42b1b4
--- /dev/null
+++ b/tests/workflow/test_condition_safe_eval.py
@@ -0,0 +1,17 @@
+"""Workflow condition evaluation uses the allowlist AST interpreter, not eval()."""
+
+from __future__ import annotations
+
+import pytest
+
+from openjarvis.tools.templates.loader import safe_eval_expr
+
+
+class TestWorkflowSafeEval:
+ def test_subclasses_escape_rejected(self):
+ with pytest.raises(Exception):
+ safe_eval_expr("().__class__.__base__.__subclasses__()", {"outputs": {}})
+
+ def test_ordinary_condition_works(self):
+ assert safe_eval_expr("'a' in outputs", {"outputs": {"a": "x"}}) is True
+ assert safe_eval_expr("'b' in outputs", {"outputs": {"a": "x"}}) is False