feat(model-grounded-detection): 4.1 guard dominance, 4.2 the missing sink facts

4.1 is the arbiter for the class no crash oracle reaches. An access-control bug
is the ABSENCE of a guard: no flow to follow, no crash to reproduce. What the
model can establish is a consistency violation -- an obligated operation no
discharging guard governs, in a file where its siblings are governed by one.

"Governed" turned out not to be one relation, and asking pure CFG dominance of
the operation gets two of three shapes wrong. Measured against the real engine:

  identity constraint  the guard is INSIDE the operation's arguments
                       filter_by(owner_id = current_user.id)
  identity branch      abort(403).controlledBy == [note.owner_id != current_user.id]
                       -- the check runs AFTER the fetch, so it never dominates
                       the operation, but it controls whether the value escapes
  dominating guard     a discharger-naming call that dominates the operation

That is why the query uses controlledBy rather than dominatedBy for shape 2.

Two bugs found by running it, both the same class as the sanitizer bug in group
2 -- part of a fact flattened, the part carrying the signal dropped:

  * dominance_specs used only `calls` and `decorators`, omitting
    identity_sources (current_user, request.user) and constraint_params
    (owner_id, user_id) -- which is how an identity constraint is actually
    written. The arbiter reported "no guard" on handlers that plainly had one.
    request_sources and permissive_values stay excluded: they describe the
    vulnerable shape, and including them would let the arbiter read a bug as
    its own fix.
  * matching operations by code containment yielded four or five desugared rows
    per operation (the bare `tmp1.filter_by` receiver among them, carrying none
    of the guards the real call has), so a guarded operation could be flagged.
    Matching is by call name now.

Live, deterministic, zero model calls:
  leaky      -> model_entailed  no discharging guard, while 2 sibling handlers are guarded
  edit_note  -> no finding      (ownership check controls the denial)
  list_notes -> no finding      (identity constraint in the query)

4.2 authors the sink facts the go/no-go measured as its largest miss class (11
of 23): Django ORM raw SQL, and the four families that previously had no fact
routing to them by CWE at all -- path, output_encoding, untrusted_destination,
prototype. Each entry is a documented dangerous API of the named library with
its provenance recorded inline, and rule_ids left empty because a sink fact is
not a detection rule. Flagged for maintainer review as data, given the standing
"no LLM-authored patterns" constraint. The exact-set assertion written in task
1.1 to hold this gap open is updated rather than deleted, so it still fails if a
family ever loses its seed.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01T7XD7x5KonMdmGRkp63EQG
This commit is contained in:
Marius Ciepluch
2026-09-08 14:45:12 +02:00
co-authored by Claude Opus 5
parent b8f8f727c6
commit fec67253f3
9 changed files with 417 additions and 13 deletions
+117
View File
@@ -0,0 +1,117 @@
"""model-grounded-detection task 4.1: guard dominance, the arbiter for bugs that never crash (Req 6.2).
An access-control bug is the *absence* of a guard, so there is no flow to follow and no crash to reproduce.
What the model can establish is a consistency violation: an obligated operation that no discharging guard
dominates, in a file where its siblings are dominated by one. That asymmetry is the evidence.
This replaces the line-order heuristic in `semantic/obligations/dominance.py` ("the guard's line is before the
operation's line, with an early exit between") with real control-flow dominance over the CFG.
"""
from __future__ import annotations
from pathlib import Path
def _spec(): # type: ignore[no-untyped-def]
from openultrasast.model.specs import DominanceSpec
return DominanceSpec(
family="access_control",
language="python",
operations=("filter_by", "query.get", "session.delete"),
dischargers=("login_required", "current_user", "check_permission"),
)
def _cpg(rows): # type: ignore[no-untyped-def]
from openultrasast.cpg.backend import CpgResult
return CpgResult(cpg_path=Path("cpg.bin"), run=lambda q, p: rows)
def _op(method: str, guards: list[str], line: int = 10) -> dict[str, object]:
return {"operation": "Note.query.filter_by(id=nid)", "opLine": str(line), "opMethod": method,
"dominatingGuards": guards}
def test_an_unguarded_operation_among_guarded_siblings_is_entailed() -> None:
"""The consistency violation: every sibling checks the owner, this one does not."""
from openultrasast.model.dominance import verdict
from openultrasast.model.ladder import Rung
rows = [_op("leaky", []), _op("list_notes", ["current_user"]), _op("edit_note", ["current_user"])]
answer = verdict(_cpg(rows), _spec(), function="leaky")
assert answer is not None and answer.rung is Rung.ENTAILED
assert answer.family == "access_control"
assert "leaky" in answer.witness and "2 sibling" in answer.witness, "the witness names the operation and its siblings"
def test_a_method_with_two_operations_counts_once_as_a_sibling() -> None:
"""One handler holding two obligated operations is one piece of corroboration, not two."""
from openultrasast.model.dominance import verdict
rows = [_op("leaky", []), _op("listing", ["current_user"], line=3), _op("listing", ["current_user"], line=4)]
answer = verdict(_cpg(rows), _spec(), function="leaky")
assert answer is not None and "1 sibling" in answer.witness
assert answer.witness.count("listing") == 1
def test_a_guarded_operation_yields_no_finding() -> None:
from openultrasast.model.dominance import verdict
rows = [_op("safe", ["current_user"]), _op("other", ["current_user"])]
assert verdict(_cpg(rows), _spec(), function="safe") is None
def test_an_unguarded_operation_with_no_guarded_sibling_is_only_corroborated() -> None:
"""Nothing to be inconsistent with: the whole module may be unauthenticated by design, and saying
`entailed` there would assert a policy the model cannot see."""
from openultrasast.model.dominance import verdict
from openultrasast.model.ladder import Rung
rows = [_op("a", []), _op("b", [])]
answer = verdict(_cpg(rows), _spec(), function="a")
assert answer is not None and answer.rung is Rung.CORROBORATED
def test_no_obligated_operation_yields_no_verdict() -> None:
from openultrasast.model.dominance import verdict
assert verdict(_cpg([]), _spec(), function="whatever") is None
def test_a_failed_query_is_not_read_as_an_absence_of_operations() -> None:
"""`None` from the engine means "could not decide", never "there is nothing here"."""
from openultrasast.cpg.backend import CpgResult
from openultrasast.model.dominance import verdict
broken = CpgResult(cpg_path=Path("c.bin"), run=lambda q, p: None)
assert verdict(broken, _spec(), function="a") is None
def test_the_verdict_is_deterministic_over_one_cpg() -> None:
"""Req 6.4, for the absence arbiter as well as the flow one."""
from openultrasast.model.dominance import verdict
rows = [_op("leaky", []), _op("z", ["current_user"]), _op("a", ["check_permission"])]
cpg = _cpg(rows)
assert verdict(cpg, _spec(), function="leaky") == verdict(cpg, _spec(), function="leaky")
def test_the_spec_drives_the_query_parameters() -> None:
from openultrasast.cpg.backend import CpgResult
from openultrasast.model.dominance import verdict
seen: dict[str, object] = {}
def run(query: str, params: dict[str, object]) -> object:
seen["query"] = query
seen.update(params)
return []
verdict(CpgResult(cpg_path=Path("c.bin"), run=run), _spec(), function="f")
assert seen["query"] == "dominance"
assert seen["operations"] == ("filter_by", "query.get", "session.delete")
assert seen["dischargers"] == ("login_required", "current_user", "check_permission")
assert seen["function"] == "f"
+30 -8
View File
@@ -36,10 +36,9 @@ def test_the_guard_classifier_still_recognises_each_kind() -> None:
def test_the_seeded_families_and_the_named_gaps_are_both_exact() -> None:
"""What the retained fact data can seed, and what it provably cannot — Req 9.2, a gap is *named*.
`path`, `output_encoding`, `untrusted_destination` and `prototype` have no sink in `ruleset/semantic`
whose CWE routes to them, which is the same hole the entailment ceiling diagnosed ("closed literal sink
table: misses per-family sinks"). Task 4.2 authors those sinks and must update this test when it does —
the set is asserted exactly so a gap can neither appear nor close silently.
Task 4.2 closed the gap this test was written to hold open: `path`, `output_encoding`,
`untrusted_destination` and `prototype` now have sink facts routing to them by CWE. The set is still
asserted exactly, so a family can neither lose its seed nor gain one without this record moving.
"""
from openultrasast.model.specs import dominance_specs, taint_specs
from openultrasast.model.taxonomy import load_families
@@ -50,11 +49,12 @@ def test_the_seeded_families_and_the_named_gaps_are_both_exact() -> None:
seeded |= {spec.family for spec in taint_specs(language=language).values()}
seeded |= {spec.family for spec in dominance_specs(language=language).values()}
deferred = {"unknown", "memory"} # the declined bucket and the deferred execution tier
assert seeded == {"injection", "deserialization", "config_secrets", "access_control", "memory"}
assert seeded == {
"injection", "deserialization", "config_secrets", "access_control",
"path", "output_encoding", "untrusted_destination", "prototype", "memory",
}
gaps = {family.id for family in taxonomy.families} - seeded - deferred
assert gaps == {"path", "output_encoding", "untrusted_destination", "prototype"}, (
"a family lost or gained a seed without this record being updated"
)
assert gaps == set(), f"a family lost its seed: {sorted(gaps)}"
def test_the_injection_taint_spec_carries_sources_sinks_and_sanitizers() -> None:
@@ -126,3 +126,25 @@ def test_safe_shape_sinks_are_kept_for_the_shape_test() -> None:
assert "execute" not in spec.sanitizers
c_memory = taint_specs(language="c").get("memory")
assert c_memory is not None and "printf" in c_memory.safe_shape_sinks
def test_dischargers_carry_identity_and_constraint_tokens_but_not_the_negative_fields() -> None:
"""A discharger fact spreads across six fields and two of them mean the opposite.
`identity_sources` (current_user, request.user) and `constraint_params` (owner_id, user_id) are how an
identity constraint is actually written, and omitting them left the absence arbiter reporting "no guard"
on a handler that plainly had one. `request_sources` and `permissive_values` are excluded because they
describe the vulnerable shape, not the fix -- including them would let the arbiter read a bug as its fix.
"""
from openultrasast.model.specs import dominance_specs
from openultrasast.semantic.obligations import load_obligation_facts
spec = dominance_specs(language="python")["access_control"]
assert "current_user" in spec.dischargers and "owner_id" in spec.dischargers
assert "login_required" in spec.dischargers # decorators still present
facts = load_obligation_facts().for_language("python")
negative = {p for fact in facts.dischargers for p in fact.request_sources} | {
v for fact in facts.dischargers for v in fact.permissive_values
}
assert not (negative & set(spec.dischargers)), "the negative fields must never read as a discharge"