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

Heuristic:0xSteph Pentest ai Fail Closed Scope Gate

From Leeroopedia
Knowledge Sources
Domains Security_Testing, Authorization, Safety
Last Updated 2026-10-23 14:00 GMT

Overview

Fail-closed scope enforcement that denies all traffic by default when no scope is defined, preventing even a hallucinating LLM from hitting out-of-scope targets.

Description

The Fail-Closed Scope Gate is the non-negotiable safety property that the `ScopeEnforcer` enforces at the `SecurityTool.execute()` chokepoint. The design principle is simple: absence of scope = denial of access, not permission.

When `mode="strict"` (the default) and no allowed targets are configured, `check()` returns `(False, "No scope defined. Add targets to scope before scanning.")`. This means:

  • A fresh engagement with no scope configured cannot send any traffic.
  • An LLM that hallucinates a target URL cannot bypass the gate — the handler checks scope via contextvar before executing.
  • The `build_enforcer` factory seeds the allow-list from engagement targets, ensuring even a default engagement is scope-bounded to at least its own target.

The `mode="permissive"` option exists but is not the default. In permissive mode, an undefined scope allows traffic, but this is explicitly opt-in and never used in production engagements.

Usage

Apply this heuristic when:

  • Designing scope enforcement — always default to fail-closed; any change that allows scanning outside the declared target list is a security regression.
  • Reviewing handler code — every URL-taking handler must call `get_current_scope()` and `scope.check(target)` before sending any request.
  • Debugging "target not in scope" errors — verify the target was added to the engagement scope via `build_enforcer` or `--scope-allow`.
  • Testing scope bypasses — the `tests/test_scope.py` suite verifies that URL variants (ports, schemes, case) cannot bypass the enforcer.

The Insight (Rule of Thumb)

  • Action: Enforce scope at the tool execution chokepoint, not at the agent level. Deny by default when no scope is defined.
  • Value: `mode="strict"` (default). `check()` returns `(False, reason)` when `_allowed_domains` and `_allowed_cidrs` are both empty.
  • Trade-off: Fail-closed means a misconfigured engagement produces zero findings (safe) rather than scanning unintended targets (dangerous). The operator must explicitly define scope.
  • Compatibility: Hostname normalization (lowercase, port-stripped, scheme-stripped) prevents bypass via URL variants. Wildcard domains (`*.example.com`) and CIDR ranges are supported.

Reasoning

Enforcing scope at the agent level is insufficient because a hallucinating LLM can produce arbitrary URLs in its tool calls. The only safe enforcement point is the last point before the request leaves the process — the `SecurityTool.execute()` chokepoint.

Fail-closed is the correct default because the consequence of scanning an out-of-scope target (legal liability, CFAA violation) is far worse than the consequence of missing a finding. A pentester can always re-run with corrected scope; they cannot undo an unauthorized scan.

Hostname normalization is critical: without it, `API.example.com:8443` from an imported scope would never match the port-stripped, lowercased hostname that `check()` tests against — a silent scope miss that would allow the request through.

Code Evidence

Fail-closed check from `engine/scope.py:88-91`:

def check(self, target: str, tool_name: str = "") -> tuple[bool, str]:
    if not self._allowed_domains and not self._allowed_cidrs:
        if self.mode == "permissive":
            return True, "No scope defined, permissive mode"
        return False, "No scope defined. Add targets to scope before scanning."

Contextvar plumbing for handlers from `engine/scope.py:189-205`:

_current_scope: ContextVar[ScopeEnforcer | None] = ContextVar(
    "current_scope", default=None
)

def get_current_scope() -> ScopeEnforcer | None:
    return _current_scope.get()

Hostname normalization from `engine/scope.py:62-68`:

# Normalize the domain pattern the same way check() normalizes the
# hostname it tests (lowercase, no scheme/path/port), preserving a
# leading "*." wildcard. Without this, an entry like
# "API.example.com:8443" ... would be stored verbatim and never equal
# the port-stripped, lowercased hostname check() compares against —
# a silent scope miss.

Violation logging from `engine/scope.py:164`:

logger.warning(f"SCOPE VIOLATION: {tool} tried to access {target}: {reason}")

Related Pages

Page Connections

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