Skip to content

fix(manifest): terminate reachability on recursive sums via List/tuple#17

Merged
nelsonduarte merged 2 commits into
mainfrom
fix/reachability-recursion-guard
Jun 29, 2026
Merged

fix(manifest): terminate reachability on recursive sums via List/tuple#17
nelsonduarte merged 2 commits into
mainfrom
fix/reachability-recursion-guard

Conversation

@nelsonduarte

Copy link
Copy Markdown
Owner

Problem

_contains_fun_via_structs in capa/manifest/_reachability.py (the manifest reachability walker) propagated its cycle-guard (the _seen visited-type set) only when recursing into named struct fields and named sum-variant payloads, but reset it to empty when recursing into type arguments (List<Self>) and tuple elements.

A recursive sum type whose self-reference passes through List<Self> or a tuple, for example

type Condition =
    PathExists(String, Bool)
    AllOf(List<Condition>)
    AnyOf(List<Condition>)
    NotMatch(Condition)

therefore looped forever and raised RecursionError. Because build_manifest -> compute_reachability runs for both --manifest and the Wasm backend (compile_wat), both crashed on such a program; --check was unaffected (it does not build the manifest). Raising the recursion limit does not help: the cycle is infinite.

Discovered while dogfooding --wasi on the downstream policy-eval program (AllOf(List<Condition>)).

Fix

Forward the current visited-set (_seen) through every recursive branch, including the tuple-element and type-argument branches, so the traversal terminates the first time it revisits a type name. The struct-field and sum-payload branches already forwarded it and are unchanged.

The fix only adds termination: the fun-bearing / capability result for every type that already terminated is identical, so no existing manifest or SBOM result changes. The sibling walker _caps_via_type needs no change (it does not expand named structs/sums, so it cannot cycle).

Validation

  • Downstream policy-eval now both generates a manifest (python -m capa --manifest) and compiles to Wasm (python -m capa --wasm --transpile policy_eval.capa), both exit 0. (policy-eval itself was not modified.)
  • Regression tests added in tests/test_manifest.py (TestRecursiveTypeReachability): recursive sum via List<Self>, via tuple, and directly self-referential all terminate and yield the correct (empty, not-unprovable) capability result; plus an end-to-end build_manifest case on the AllOf(List<Condition>) shape.
  • Manifest, IFC, and full suites pass; no existing manifest/SBOM result changed.

The reachability walker _contains_fun_via_structs propagated its
cycle-guard (the _seen visited-type set) only when recursing into
named struct fields and named sum-variant payloads, but reset it to
empty when recursing into type arguments (List<Self>) and tuple
elements. A recursive sum whose self-reference passed through
List<Self> or a tuple (for example type Condition = ... |
AllOf(List<Condition>)) therefore looped forever and raised
RecursionError, taking down build_manifest and everything built on it:
--manifest and the Wasm backend (compile_wat) both crashed, while
--check was unaffected because it does not build the manifest.

Forward the visited-set through every recursive branch so the
traversal terminates. The fix only adds termination: the fun-bearing /
capability result for every type that already terminated is unchanged,
and no existing manifest or SBOM result changes.

Adds regression tests for recursive sums via List, via tuple, and
directly self-referential, plus an end-to-end build_manifest case on
the policy-eval AllOf(List<Condition>) shape.
@nelsonduarte nelsonduarte merged commit 4580d5b into main Jun 29, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant