test(valid-detection-benchmark): pin each fix-site key element and non-integer closure totals

- one sourcing-queue pair per fix-site key element (path-only,
  function-only), each proven red by dropping that key line alone
- the closure decode test covers a self-consistent bool total next to
  the float total
- the _request_order docstring names the two ties that remain open
  (fix revisions; shared candidate digest with another family or CWE)
This commit is contained in:
Marius Ciepluch
2026-11-23 13:31:01 +01:00
parent b82b52f976
commit 364b4c0777
3 changed files with 58 additions and 16 deletions
+5 -2
View File
@@ -97,8 +97,11 @@ def _request_order(request: SourcingCandidateRequest) -> tuple[str, ...]:
used to be ordered by input position, so cap selection was
permutation-sensitive; the advisory, package-manifest, and license digests
make the order a function of content only. The fix-site path and function
are the non-digest scalars two otherwise identical requests can still
differ on; appending them keeps every existing order intact.
are the non-digest fix-site scalars; appending them keeps every existing
order intact. Two ties remain. Requests identical on all nine keys can
still differ in their fix revisions, which no key element carries. Two
requests can also share one candidate digest while differing in family or
CWE; DUPLICATE_CANDIDATE then resolves that pair by input order.
"""
digest = request.model_assistance_digest or ""
return (
@@ -246,15 +246,23 @@ def test_closure_refuses_float_and_bool_totals_closed():
assert error.value.reason is PositiveSetClosureStateReason.STATE_MISMATCH
def test_closure_decode_refuses_a_self_consistent_float_total():
encoded = positive_set_closure_bytes(positive_set_closure(**_CLOSED_BASE))
@pytest.mark.parametrize(
"based, forged_total, encoded_total",
(
(_CLOSED_BASE, 45.0, b'"total_accepted_positives":45.0'),
({**_CLOSED_BASE, **_SINGLE_POSITIVE}, True, b'"total_accepted_positives":true'),
),
ids=("float", "bool"),
)
def test_closure_decode_refuses_a_self_consistent_non_integer_total(based, forged_total, encoded_total):
encoded = positive_set_closure_bytes(positive_set_closure(**based))
forged = {key: tuple(value) if isinstance(value, list) else value for key, value in json.loads(encoded).items()}
assert forged["total_accepted_positives"] == 45
forged["total_accepted_positives"] = 45.0
assert type(forged["total_accepted_positives"]) is int
forged["total_accepted_positives"] = forged_total
forged["closure_digest"] = content_digest({key: value for key, value in forged.items() if key != "closure_digest"})
content = canonical_bytes(forged)
assert b'"total_accepted_positives":45.0' in content
assert decode_positive_set_closure(encoded).total_accepted_positives == 45
assert encoded_total in content
assert decode_positive_set_closure(encoded).total_accepted_positives == based["total_accepted_positives"]
with pytest.raises(PositiveSetClosureStateError) as error:
decode_positive_set_closure(content)
+39 -8
View File
@@ -409,6 +409,26 @@ class _TwinPathReader(FakeReader):
self._blobs[(fix, "twin.py")] = SECOND_FIXED
class _TwinFunctionReader(FakeReader):
"""app.py at the py-app revisions carries a second function the fix changes."""
def __init__(self) -> None:
super().__init__()
parent, fix = _REVISIONS["py-app"]
self._blobs[(parent, "app.py")] = PYTHON_VULNERABLE + "\n\n" + SECOND_VULNERABLE
self._blobs[(fix, "app.py")] = PYTHON_FIXED + "\n\n" + SECOND_FIXED
class _SameFunctionReader(FakeReader):
"""The py-two revisions also carry the same function under a different path."""
def __init__(self) -> None:
super().__init__()
parent, fix = _REVISIONS["py-two"]
self._blobs[(parent, "worker.py")] = SECOND_VULNERABLE
self._blobs[(fix, "worker.py")] = SECOND_FIXED
def _build_with_reader(reader, requests, policy):
resolver = _resolver()
return build_sourcing_queue(
@@ -422,9 +442,18 @@ def _build_with_reader(reader, requests, policy):
)
def test_fix_site_scalars_break_otherwise_tied_candidates_under_a_cap():
first = _request("py-app")
second = _request("py-app", path="twin.py", function="load")
@pytest.mark.parametrize(
"reader_type, name, changes, differing",
(
(_TwinPathReader, "py-app", {"path": "twin.py", "function": "load"}, ("path", "function")),
(_TwinFunctionReader, "py-app", {"function": "load"}, ("function",)),
(_SameFunctionReader, "py-two", {"path": "worker.py"}, ("path",)),
),
ids=("path-and-function", "function-only", "path-only"),
)
def test_fix_site_scalars_break_otherwise_tied_candidates_under_a_cap(reader_type, name, changes, differing):
first = _request(name)
second = _request(name, **changes)
requests = [first, second, _request("js-app", family="output_encoding", cwe="CWE-79")]
policy = _policy(per_ecosystem_cap=1)
assert (first.family, first.reachability, first.package_evidence, first.license_evidence) == (
@@ -434,18 +463,20 @@ def test_fix_site_scalars_break_otherwise_tied_candidates_under_a_cap():
second.license_evidence,
)
assert (first.case["url"], first.model_assistance_digest) == (second.case["url"], second.model_assistance_digest)
assert (first.case["path"], first.case["function"]) == ("app.py", "handler")
assert (second.case["path"], second.case["function"]) == ("twin.py", "load")
sites = tuple((request.case["path"], request.case["function"]) for request in (first, second))
for element, index in (("path", 0), ("function", 1)):
assert (sites[0][index] != sites[1][index]) is (element in differing)
assert sites[0] < sites[1]
digests = set()
for order in itertools.permutations(requests):
state = _build_with_reader(_TwinPathReader(), list(order), policy)
state = _build_with_reader(reader_type(), list(order), policy)
(queued,) = state.python_queue.entries
assert (queued.fix_site.path, queued.fix_site.function) == ("app.py", "handler")
assert (queued.fix_site.path, queued.fix_site.function) == sites[0]
digests.add((state.state_digest, pending_queue_identities(state)))
assert len(digests) == 1
state = _build_with_reader(_TwinPathReader(), requests, policy)
state = _build_with_reader(reader_type(), requests, policy)
(capped,) = [item for item in state.exclusions if item.reason is SourcingQueueReason.QUEUE_CAP_EXCEEDED]
assert capped.candidate_digest != state.python_queue.entries[0].candidate_digest
assert len(state.javascript_queue.entries) == 1