Files
Open_Ultra_SAST/tests/test_model_taxonomy.py
T
Marius CiepluchandClaude Opus 5 af7e33ef3e feat(reports): say what was looked for, and what cannot be decided
contributor-scan 2.16, implementing the approved Req 5.6. `c/memory`
scored zero true positives across 89k lines of C and three distinct
classes of false one, for a structural reason: it models string CALLS and
libpng overflows through arithmetic.

The task allowed retiring the family. Kept, scoped and stated instead --
`strcpy` misuse is real in plenty of C and the family earns its place for
that. Retiring it would have removed the disclosure along with the
coverage, leaving a C scan silent for a reason nobody could read.

**Its own description was the quiet-failure risk.** It claimed "bounds,
lifetime, arithmetic", which is exactly what it does not decide. It now
names the APIs it models, and a new `limits` field in families.toml says
what it cannot: an overflow expressed as an index, a pointer offset, or a
memcpy whose size is computed -- which is how most real C overflows are
written, and how every one of libpng's is.

A limit recorded in a fact table nobody reads is no better than silence, so
the report renders it. **What was analysed** lists every family offered to
a region whether or not it found anything, because a family that ran and
reported nothing is the case a reader is most likely to misread, and it
states plainly that silence means no fact table covered the code rather
than that the code is safe.

The counts are derived from the fact tables the arbiter actually used, so
they cannot drift from what the engine looked for; only the limit sentence
is authored, because "cannot decide an overflow expressed as an index" is a
statement about an abstraction and no table can produce it.

A C scan that finds nothing now says:

  | c | `memory` | 121 | 9 |
  **`memory` (c)** — Models CALLS, not arithmetic...

Gates byte-identical.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01T7XD7x5KonMdmGRkp63EQG
2026-09-09 15:08:01 +02:00

137 lines
6.6 KiB
Python

"""learning-harness task 1.1: the closed family taxonomy with a verifier per family (offline)."""
from __future__ import annotations
import tomllib
from pathlib import Path
import pytest
GOOD = (
'version = "1"\n\n'
'[[family]]\nid = "injection"\ndescription = "x"\ncwes = ["CWE-89"]\nmechanisms = ["source_reaches_sink"]\nverifier = "canary"\n'
)
def test_taxonomy_loads_ten_families_each_with_a_verifier() -> None:
from openultrasast.model.taxonomy import FAMILY_IDS, VERIFIER_KINDS, load_families
taxonomy = load_families()
assert len(FAMILY_IDS) == 10 and FAMILY_IDS[-1] == "unknown"
assert tuple(family.id for family in taxonomy.families) == FAMILY_IDS
assert taxonomy.version and all(family.verifier in VERIFIER_KINDS for family in taxonomy.families)
assert {family.id for family in taxonomy.families if family.verifier == "canary"} == {"injection", "path", "deserialization"}
assert taxonomy.by_id("access_control").verifier == "static"
assert taxonomy.by_id("memory").verifier == "none" # measured on the vendored slice, never proven by us
assert taxonomy.by_id("unknown").verifier == "none" and all(family.description for family in taxonomy.families)
def test_every_mechanism_id_resolves_to_exactly_one_family() -> None:
from openultrasast.model.taxonomy import load_families
from openultrasast.pairs import load_mechanisms
taxonomy = load_families()
_, mechanism_ids = load_mechanisms()
for mechanism in sorted(mechanism_ids):
owners = [family.id for family in taxonomy.families if mechanism in family.mechanisms]
assert len(owners) == 1, (mechanism, owners)
assert taxonomy.family_of_mechanism("missing_auth_guard").id == "access_control"
assert taxonomy.family_of_mechanism("unchecked_length_copy").id == "memory"
# a mechanism that spans families names none of them; the labeled CWE decides those rows
assert taxonomy.family_of_mechanism("source_reaches_sink").id == "unknown"
assert taxonomy.family_of_mechanism("not_a_mechanism") is None
def test_every_labeled_corpus_cwe_resolves_and_no_cwe_is_claimed_twice() -> None:
from openultrasast.model.taxonomy import load_families
taxonomy = load_families()
seen: dict[str, str] = {}
for family in taxonomy.families:
for cwe in family.cwes:
assert cwe not in seen, (cwe, seen.get(cwe), family.id)
seen[cwe] = family.id
labeled: set[str] = set()
for catalog in sorted(Path("benchmarks/pairs").rglob("catalog.toml")):
for pair in tomllib.loads(catalog.read_text()).get("pair", []):
labeled |= {str(row["cwe"]) for row in pair.get("expected", []) if row.get("cwe")}
assert sorted(cwe for cwe in labeled if taxonomy.family_of_cwe(cwe) is None) == []
assert taxonomy.family_of_cwe("cwe-89").id == "injection" # case and spacing tolerant
assert taxonomy.family_of_cwe("CWE-601").id == "untrusted_destination" # 34 open-redirect rows
assert taxonomy.family_of_cwe("CWE-835").id == "unknown" # considered and out of scope, not unresolved
assert taxonomy.family_of_cwe("CWE-99999") is None
def test_relation_answers_same_lateral_fabricated_and_parent_child_when_declared(tmp_path: Path) -> None:
from openultrasast.model.taxonomy import load_families
taxonomy = load_families()
assert taxonomy.related("injection", "injection") == "same"
assert taxonomy.related("injection", "access_control") == "lateral"
assert taxonomy.related("injection", "not_a_family") == "fabricated"
assert taxonomy.related("unknown", "injection") == "lateral" # abstention earns no partial credit
assert all(family.parent is None for family in taxonomy.families) # the shipped taxonomy is flat
(tmp_path / "families.toml").write_text(
GOOD + '\n[[family]]\nid = "path"\nparent = "injection"\ndescription = "y"\ncwes = []\nmechanisms = []\nverifier = "canary"\n'
)
scoped = load_families(tmp_path / "families.toml", require_all=False)
assert scoped.related("path", "injection") == "parent_child" and scoped.related("injection", "path") == "parent_child"
@pytest.mark.parametrize(
("text", "needle"),
[
(GOOD + 'color = "blue"\n', "color"),
(GOOD.replace('verifier = "canary"\n', ""), "verifier"),
(GOOD.replace('verifier = "canary"', 'verifier = "maybe"'), "maybe"),
(GOOD.replace('id = "injection"', 'id = "sql_injection"'), "sql_injection"),
(GOOD.replace('cwes = ["CWE-89"]', 'cwes = ["CWE-89", "CWE-89"]'), "CWE-89"),
(GOOD.replace('description = "x"\n', ""), "description"),
(GOOD + '\n[[family]]\nid = "path"\ndescription = "y"\ncwes = ["CWE-89"]\nmechanisms = []\nverifier = "canary"\n', "CWE-89"),
(
GOOD + '\n[[family]]\nid = "path"\ndescription = "y"\ncwes = []\nmechanisms = ["source_reaches_sink"]\nverifier = "canary"\n',
"source_reaches_sink",
),
(GOOD.replace('version = "1"\n', ""), "version"),
(GOOD + '\n[[family]]\nid = "path"\nparent = "nope"\ndescription = "y"\ncwes = []\nmechanisms = []\nverifier = "none"\n', "nope"),
],
ids=[
"unknown_field",
"missing_verifier",
"bad_verifier",
"fabricated_family",
"duplicate_cwe",
"no_description",
"cwe_claimed_twice",
"mechanism_claimed_twice",
"no_version",
"unknown_parent",
],
)
def test_loader_rejects_data_bugs_by_name(tmp_path: Path, text: str, needle: str) -> None:
from openultrasast.model.taxonomy import FamiliesError, load_families
(tmp_path / "families.toml").write_text(text)
with pytest.raises(FamiliesError, match=needle):
load_families(tmp_path / "families.toml", require_all=False)
def test_the_shipped_file_must_declare_the_whole_closed_set_in_order(tmp_path: Path) -> None:
from openultrasast.model.taxonomy import FamiliesError, load_families
(tmp_path / "families.toml").write_text(GOOD)
with pytest.raises(FamiliesError, match="in that order"):
load_families(tmp_path / "families.toml")
def test_the_memory_family_states_what_it_cannot_decide() -> None:
"""contributor-scan 2.16. Its description used to claim "bounds, lifetime, arithmetic", which is exactly
what it does not do -- the description was itself the quiet-failure risk."""
from openultrasast.model.taxonomy import load_families
memory = next(family for family in load_families().families if family.id == "memory")
assert "arithmetic" in memory.limits, "the gap is stated"
assert "arithmetic" not in memory.description, "and no longer claimed"
assert "strcpy" in memory.description, "what it does cover is named"