Implementation:0xSteph Pentest ai ScopeEnforcer
| Knowledge Sources | |
|---|---|
| Domains | Security_Testing, Authorization |
| Last Updated | 2026-10-23 12:00 GMT |
Overview
Concrete scope enforcement engine that prevents any tool from hitting targets outside the declared allow-list, enforced at the SecurityTool.execute() chokepoint.
Description
The ScopeEnforcer class is the non-negotiable safety component of pentest-ai. It maintains an allow-list of domains, wildcards, and CIDR ranges, plus an exclude list. Every tool invocation passes through `check()` before executing.
Key features:
- Wildcard domains: `*.example.com` matches the apex and all subdomains
- CIDR ranges: IP networks (IPv4 and IPv6) for IP-based targets
- Exclusions: Explicit deny entries override allow entries (exclusions win)
- Hostname normalization: Lowercase, port-stripped, scheme-stripped — prevents bypass via URL variants
- IP resolution: Hostnames are DNS-resolved and checked against CIDR allow-lists
- Violation logging: Every blocked request is recorded with target, tool, reason, and timestamp
The build_enforcer factory seeds the allow-list from engagement targets, then merges explicit allow/exclude entries and config scope. Fail-closed: if nothing resolves, the enforcer denies by default.
Contextvar plumbing (`set_current_scope` / `get_current_scope` / `reset_current_scope`) exposes scope to agent_loop handlers without threading it through every function argument.
Usage
Import this when you need to:
- Enforce scope at the tool execution chokepoint
- Build a scope enforcer from CLI/MCP engagement parameters
- Expose scope to agent loop handlers via contextvar
Code Reference
Source Location
- Repository: pentest-ai
- File: engine/scope.py
Signature
class ScopeEnforcer:
def __init__(
self,
allowed_targets: list[str] | None = None,
excluded_targets: list[str] | None = None,
allowed_ports: list[int] | None = None,
mode: str = "strict",
):
def check(self, target: str, tool_name: str = "") -> tuple[bool, str]:
"""Returns (allowed, reason). Records violations."""
def check_port(self, port: int) -> tuple[bool, str]:
"""Check if a port is in the allowed set."""
def summary(self) -> dict[str, Any]:
"""Human-readable view for pre-engagement confirmation."""
@property
def violations(self) -> list[ScopeViolation]:
"""List of all recorded scope violations."""
def build_enforcer(
targets: str | list[str] | None = None,
*,
allowed: list[str] | None = None,
excluded: list[str] | None = None,
allowed_ports: list[int] | None = None,
config_scope: dict[str, Any] | None = None,
mode: str = "strict",
) -> ScopeEnforcer:
"""Build a ScopeEnforcer for a CLI/MCP engagement."""
def get_current_scope() -> ScopeEnforcer | None:
def set_current_scope(scope: ScopeEnforcer | None) -> Any:
def reset_current_scope(token: Any) -> None:
Import
from engine.scope import ScopeEnforcer, build_enforcer, get_current_scope, set_current_scope, reset_current_scope
I/O Contract
Inputs (check)
| Name | Type | Required | Description |
|---|---|---|---|
| target | str | Yes | URL, hostname, or IP to check |
| tool_name | str | No | Name of the tool requesting access (for violation log) |
Outputs (check)
| Name | Type | Description |
|---|---|---|
| returns | tuple[bool, str] | (allowed, reason) |
Usage Examples
Basic Scope Enforcement
from engine.scope import ScopeEnforcer
enforcer = ScopeEnforcer(
allowed_targets=["example.com", "*.internal.corp", "10.0.0.0/8"],
excluded_targets=["admin.example.com"],
mode="strict",
)
# In-scope
allowed, reason = enforcer.check("https://app.example.com/login")
assert allowed # True
# Out-of-scope (excluded)
allowed, reason = enforcer.check("https://admin.example.com")
assert not allowed # False — explicitly excluded
# Out-of-scope (not in allow-list)
allowed, reason = enforcer.check("https://evil.com")
assert not allowed # False — not in scope
Building from CLI Parameters
from engine.scope import build_enforcer
enforcer = build_enforcer(
targets="https://app.example.com",
allowed=["*.example.com"],
excluded=["admin.example.com"],
mode="strict",
)
Contextvar for Agent Loop Handlers
from engine.scope import set_current_scope, get_current_scope, reset_current_scope
token = set_current_scope(enforcer)
try:
# Handlers can read scope without threading it through args
scope = get_current_scope()
if scope:
allowed, _ = scope.check(url)
finally:
reset_current_scope(token)