Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xSteph Pentest ai eval condition

From Leeroopedia
Knowledge Sources
Domains Security_Testing, Methodology
Last Updated 2026-10-23 12:00 GMT

Overview

Concrete sandboxed condition evaluator for playbook phase gating that parses expression strings through an AST walker with a function whitelist, no eval, and no attribute access.

Description

eval_condition evaluates a condition expression string against a ConditionContext containing the current findings and phase results. It uses Python's `ast` module to parse and walk the expression tree with a strict whitelist:

Allowed function calls:

  • `has_finding(severity='high')` — True if any finding matches
  • `has_finding(category='web')` — True if any finding matches
  • `count_findings(severity='high') > 2` — Count findings matching
  • `any_finding()` — True when at least one finding exists
  • `phase_ran('recon')` — True if the phase ran
  • `phase_skipped('recon')` — True if the phase was skipped

Allowed operators:

  • Boolean: `and`, `or`, `not`
  • Comparison: `>`, `>=`, `<`, `<=`, `==`, `!=`
  • Constants: True, False, strings, numbers
  • Parentheses for grouping

Forbidden: bare names (except True/False), attribute access, function calls outside the whitelist, any other AST node type. Errors bubble up as ConditionError.

Usage

Import this when you need to:

  • Evaluate playbook phase conditions
  • Build a custom condition evaluator with a different context

Code Reference

Source Location

  • Repository: pentest-ai
  • File: engine/playbook_conditions.py

Signature

class ConditionError(Exception):
    pass

@dataclass
class ConditionContext:
    findings: list[dict[str, Any]] = field(default_factory=list)
    phase_results: dict[str, bool] = field(default_factory=dict)

    def has_finding(self, severity: str = "", category: str = "") -> bool:
    def count_findings(self, severity: str = "", category: str = "") -> int:
    def any_finding(self) -> bool:
    def phase_ran(self, phase_id: str) -> bool:
    def phase_skipped(self, phase_id: str) -> bool:

def eval_condition(expr: str, ctx: ConditionContext) -> bool:
    """Evaluate a condition expression. Raises ConditionError on invalid input."""

Import

from engine.playbook_conditions import eval_condition, ConditionContext, ConditionError

I/O Contract

Inputs

Name Type Required Description
expr str Yes Condition expression string
ctx ConditionContext Yes Findings + phase results for evaluation

Outputs

Name Type Description
returns bool True if condition matches, False if not
raises ConditionError Invalid syntax or unsupported expression

Usage Examples

Evaluating Conditions

from engine.playbook_conditions import eval_condition, ConditionContext

ctx = ConditionContext(
    findings=[{"severity": "high", "category": "web"}, {"severity": "critical"}],
    phase_results={"recon": True, "exploit": False},
)

# Simple checks
assert eval_condition("any_finding()", ctx) == True
assert eval_condition("has_finding(severity='high')", ctx) == True
assert eval_condition("count_findings(severity='high') > 1", ctx) == False

# Boolean composition
assert eval_condition("has_finding(severity='critical') and phase_ran('recon')", ctx) == True
assert eval_condition("not phase_skipped('recon')", ctx) == True

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment