mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-10-16 19:59:26 +00:00
99 source modules -> 81. 832 tests -> 646. The whole learning/ package (16 modules, ~4,100 lines) and six semantic/ modules removed, with the three gates byte-identical to 2026-09-06-gate-baseline.txt at every step. The sequence is what made it safe: vocabularies ported first (1.1), importers detached second (1.2-1.3), deletion third (1.4). Every import break during the deletion was therefore a detachment miss in the importer and was fixed there -- no module was restored to satisfy one. Both misses were the same shape and were fixed as a class: operations.py and shapes.py each imported trailing_name from the deleted variants.py, so it moved to semantic/ir.py, its natural home, and both importers changed in one edit. Ported before deleting: the closed guard vocabulary (verbatim, nothing else holds it) into model/specs.py, which derives TaintSpec and DominanceSpec from the retained ruleset facts rather than copying them. Re-homed: families -> model/taxonomy, candidates -> model/candidates, endpoint -> model/endpoint, plus Region, labeled_family and labeled_spans. labeled_family no longer falls back to the LLM classifier -- an unlabeled pair is honestly unknown. Four deviations, each recorded in the spec notes with its reason. improve/ evolve.py and validator.py were stripped, not deleted: each mixes the mechanism lever (gone) with the deterministic rule-status loop `ousast improve` still runs, which has no LLM in it; a test now asserts the lever's absence by symbol so it cannot creep back. The learning CLI group became `model candidates`, because deleting it outright would have orphaned the retained enumerator by removing its only entry point. thinking moved from [learning] to [models]. A gap named rather than papered over: no retained sink fact routes by CWE to path, output_encoding, untrusted_destination or prototype -- the same hole the entailment ceiling diagnosed. The test asserts the seeded set and the gap set exactly, so task 4.2 must update it when it authors those sinks. The audit found exactly one genuine orphan: vectorstore.py, a bakeoff harness with no production caller and no CLI, alive only through its own test. The five named subsystems each carry an explicit decision. Manifest committed, with a test asserting the tree still matches it. Group 2 (the Joern go/no-go) has not started. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01T7XD7x5KonMdmGRkp63EQG
125 lines
6.0 KiB
Python
125 lines
6.0 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")
|