From 819084a93a4bf17b755dabeaa2d877baff474d4c Mon Sep 17 00:00:00 2001 From: Marius Ciepluch <11855163+norandom@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:31:05 +0100 Subject: [PATCH] feat(valid-detection-benchmark): validate canonical ecosystem reviews --- .../specs/valid-detection-benchmark/tasks.md | 3 +- benchmarks/valid/ecosystem_review.py | 405 ++++++++++++++++++ .../test_valid_benchmark_ecosystem_review.py | 380 ++++++++++++++++ 3 files changed, 787 insertions(+), 1 deletion(-) create mode 100644 benchmarks/valid/ecosystem_review.py create mode 100644 tests/test_valid_benchmark_ecosystem_review.py diff --git a/.kiro/specs/valid-detection-benchmark/tasks.md b/.kiro/specs/valid-detection-benchmark/tasks.md index 6c9d8fb6..14fac464 100644 --- a/.kiro/specs/valid-detection-benchmark/tasks.md +++ b/.kiro/specs/valid-detection-benchmark/tasks.md @@ -81,7 +81,7 @@ - Done when fixture fixes reproduce the exact pre/post functions and provenance, SZZ-style methods are unreachable, and an empty or unreadable function refuses candidate creation. - _Requirements: 1.1, 1.2, 1.5, 1.8, 2.1, 2.2_ - _Boundary: Fix-Site Catalog_ -- [ ] 3.2 Validate canonical repository ecosystem review +- [x] 3.2 Validate canonical repository ecosystem review - Accept only reviewed `python` or `javascript` ecosystem identities, preserving TypeScript as an observed language within `javascript`. - Require producer/reviewer separation, review-policy satisfaction, conflict disclosure, and the source-evidence digest. - Emit bounded insufficient evidence for unresolved or conflicting ecosystem classification, including polyglot ambiguity. @@ -419,3 +419,4 @@ - Task 2.3 projects one immutable reviewed lifecycle chain from the exact withdrawal through replacement completion; retirement and development opening are terminal, with the latter emitting reviewed permanent-reservation influence for every opened network. - Task 2.4 replaces directory exceptions with a production guard over the fixed `benchmarks`/`plane`/`src`/`tests` scope; reports bind exact read receipts and authorities, and replacement acceptance reruns the complete scope to reject stale or narrowed clean reports. - Task 3.1 builds non-authoritative fix-site candidates from only the fixed revision and its exact parent, binding nonzero pre/post function bytes and repository/package/language/license/source/change provenance without blame or introducing-commit inference. +- Task 3.2 revalidates externally governed ecosystem reviews against exact candidate/evidence/policy identities, derives closed accepted/insufficient outcomes, and enforces a repository↔network bijection before projecting canonical Python or JavaScript ecosystems. diff --git a/benchmarks/valid/ecosystem_review.py b/benchmarks/valid/ecosystem_review.py new file mode 100644 index 00000000..ccfd2352 --- /dev/null +++ b/benchmarks/valid/ecosystem_review.py @@ -0,0 +1,405 @@ +"""Independent canonical ecosystem review for fix-site candidates.""" + +from __future__ import annotations + +import re +from collections.abc import Iterable, Mapping +from dataclasses import dataclass +from enum import StrEnum + +from benchmarks.valid.fix_site_catalog import FixSiteCandidateV1, fix_site_candidate_bytes +from openultrasast.core.canonical import JsonValue, canonical_bytes, content_digest + +ECOSYSTEM_REVIEW_SCHEMA = "openultrasast.valid-detection-benchmark.repository-ecosystem-review.v1" +_DIGEST = re.compile(r"[0-9a-f]{64}") + + +class CanonicalEcosystem(StrEnum): + PYTHON = "python" + JAVASCRIPT = "javascript" + + +class EcosystemReviewDecision(StrEnum): + PYTHON = "python" + JAVASCRIPT = "javascript" + UNRESOLVED = "unresolved" + CONFLICTING = "conflicting" + POLYGLOT_AMBIGUITY = "polyglot_ambiguity" + + +class EcosystemReviewStatus(StrEnum): + ACCEPTED = "accepted" + INSUFFICIENT = "insufficient" + + +class EcosystemInsufficientReason(StrEnum): + UNRESOLVED = "unresolved" + CONFLICTING = "conflicting" + POLYGLOT_AMBIGUITY = "polyglot_ambiguity" + SELF_REVIEW = "self_review" + POLICY_UNSATISFIED = "policy_unsatisfied" + CONFLICT_DISCLOSURE_MISSING = "conflict_disclosure_missing" + + +class EcosystemReviewError(ValueError): + """Ecosystem review evidence is stale, malformed, or unsupported.""" + + +@dataclass(frozen=True) +class EcosystemReviewAttestationV1: + candidate_digest: str + source_evidence_digest: str + producer_identity: str + reviewer_identity: str + conflict_disclosed: bool + policy_digest: str + policy_satisfied: bool + decision: EcosystemReviewDecision + + +@dataclass(frozen=True) +class RepositoryEcosystemReviewV1: + canonical_repository: str + network_identity_digest: str + candidate_digest: str + source_evidence_digest: str + observed_language: str + package_evidence_digest: str + decision: EcosystemReviewDecision + status: EcosystemReviewStatus + ecosystem: CanonicalEcosystem | None + insufficient_reason: EcosystemInsufficientReason | None + producer_identity: str + reviewer_identity: str + conflict_disclosed: bool + policy_digest: str + policy_satisfied: bool + review_digest: str + + +def _text(value: object, field: str) -> str: + if not isinstance(value, str) or not value.strip(): + raise EcosystemReviewError(f"{field} must be nonempty text") + return value.strip() + + +def _digest(value: object, field: str) -> str: + if not isinstance(value, str) or _DIGEST.fullmatch(value) is None: + raise EcosystemReviewError(f"{field} must be a full lowercase SHA-256 digest") + return value + + +def _decision(value: object) -> EcosystemReviewDecision: + if not isinstance(value, str): + raise EcosystemReviewError("ecosystem review decision is unsupported") + try: + return EcosystemReviewDecision(value) + except ValueError as error: + raise EcosystemReviewError("ecosystem review decision is unsupported") from error + + +def _package_evidence_digest(candidate: FixSiteCandidateV1) -> str: + manifest = candidate.package_evidence.manifest + value: JsonValue = { + "ecosystem_hint": candidate.package_evidence.ecosystem_hint, + "manifest": { + "path": manifest.path, + "content_digest": manifest.content_digest, + "byte_length": manifest.byte_length, + }, + } + return content_digest(value) + + +def _observed_ecosystem(candidate: FixSiteCandidateV1) -> CanonicalEcosystem: + if candidate.observed_language == "python": + return CanonicalEcosystem.PYTHON + if candidate.observed_language in {"javascript", "typescript"}: + return CanonicalEcosystem.JAVASCRIPT + raise EcosystemReviewError("observed language is outside the canonical ecosystem boundary") + + +def _evidence_conflicts(candidate: FixSiteCandidateV1, observed: CanonicalEcosystem) -> bool: + hint = candidate.package_evidence.ecosystem_hint + if hint == "unresolved": + return False + if hint not in {item.value for item in CanonicalEcosystem}: + raise EcosystemReviewError("package ecosystem evidence is unsupported") + return hint != observed.value + + +def _policy_reason(attestation: EcosystemReviewAttestationV1) -> EcosystemInsufficientReason | None: + producer = _text(attestation.producer_identity, "producer identity") + reviewer = _text(attestation.reviewer_identity, "reviewer identity") + if producer == reviewer: + return EcosystemInsufficientReason.SELF_REVIEW + if type(attestation.conflict_disclosed) is not bool: + raise EcosystemReviewError("conflict disclosure must be boolean") + if not attestation.conflict_disclosed: + return EcosystemInsufficientReason.CONFLICT_DISCLOSURE_MISSING + if type(attestation.policy_satisfied) is not bool: + raise EcosystemReviewError("policy satisfaction must be boolean") + if not attestation.policy_satisfied: + return EcosystemInsufficientReason.POLICY_UNSATISFIED + return None + + +def _decision_reason(decision: EcosystemReviewDecision) -> EcosystemInsufficientReason | None: + reasons = { + EcosystemReviewDecision.UNRESOLVED: EcosystemInsufficientReason.UNRESOLVED, + EcosystemReviewDecision.CONFLICTING: EcosystemInsufficientReason.CONFLICTING, + EcosystemReviewDecision.POLYGLOT_AMBIGUITY: EcosystemInsufficientReason.POLYGLOT_AMBIGUITY, + } + return reasons.get(decision) + + +def _classification( + candidate: FixSiteCandidateV1, + attestation: EcosystemReviewAttestationV1, +) -> tuple[CanonicalEcosystem | None, EcosystemInsufficientReason | None]: + decision = _decision(attestation.decision) + policy_reason = _policy_reason(attestation) + if policy_reason is not None: + return None, policy_reason + decision_reason = _decision_reason(decision) + if decision_reason is not None: + return None, decision_reason + observed = _observed_ecosystem(candidate) + selected = CanonicalEcosystem(decision.value) + if _evidence_conflicts(candidate, observed) or selected is not observed: + return None, EcosystemInsufficientReason.CONFLICTING + return selected, None + + +def _review_value(review: RepositoryEcosystemReviewV1, *, include_digest: bool) -> dict[str, JsonValue]: + ecosystem = review.ecosystem.value if review.ecosystem is not None else None + reason = review.insufficient_reason.value if review.insufficient_reason is not None else None + value: dict[str, JsonValue] = { + "schema_version": ECOSYSTEM_REVIEW_SCHEMA, + "canonical_repository": review.canonical_repository, + "network_identity_digest": review.network_identity_digest, + "candidate_digest": review.candidate_digest, + "source_evidence_digest": review.source_evidence_digest, + "observed_language": review.observed_language, + "package_evidence_digest": review.package_evidence_digest, + "decision": review.decision.value, + "status": review.status.value, + "ecosystem": ecosystem, + "insufficient_reason": reason, + "producer_identity": review.producer_identity, + "reviewer_identity": review.reviewer_identity, + "conflict_disclosed": review.conflict_disclosed, + "policy_digest": review.policy_digest, + "policy_satisfied": review.policy_satisfied, + } + if include_digest: + value["review_digest"] = review.review_digest + return value + + +def _validate_attestation( + candidate: FixSiteCandidateV1, + attestation: EcosystemReviewAttestationV1, + expected_policy_digest: str, +) -> None: + fix_site_candidate_bytes(candidate) + if _digest(attestation.candidate_digest, "candidate digest") != candidate.candidate_digest: + raise EcosystemReviewError("review candidate digest is stale") + source_digest = _digest(attestation.source_evidence_digest, "source evidence digest") + if source_digest != candidate.source_evidence.evidence_digest: + raise EcosystemReviewError("review source evidence digest is stale") + policy = _digest(attestation.policy_digest, "review policy digest") + if policy != _digest(expected_policy_digest, "expected review policy digest"): + raise EcosystemReviewError("review policy digest is stale") + _decision(attestation.decision) + + +def review_repository_ecosystem( + candidate: FixSiteCandidateV1, + attestation: EcosystemReviewAttestationV1, + *, + expected_policy_digest: str, +) -> RepositoryEcosystemReviewV1: + """Validate external review input without inventing an ecosystem judgment.""" + _validate_attestation(candidate, attestation, expected_policy_digest) + ecosystem, reason = _classification(candidate, attestation) + status = EcosystemReviewStatus.ACCEPTED if ecosystem is not None else EcosystemReviewStatus.INSUFFICIENT + draft = RepositoryEcosystemReviewV1( + candidate.canonical_repository, + candidate.network_identity_digest, + candidate.candidate_digest, + candidate.source_evidence.evidence_digest, + candidate.observed_language, + _package_evidence_digest(candidate), + _decision(attestation.decision), + status, + ecosystem, + reason, + _text(attestation.producer_identity, "producer identity"), + _text(attestation.reviewer_identity, "reviewer identity"), + attestation.conflict_disclosed, + attestation.policy_digest, + attestation.policy_satisfied, + "", + ) + review = RepositoryEcosystemReviewV1(**{**vars(draft), "review_digest": content_digest(_review_value(draft, include_digest=False))}) + repository_ecosystem_review_bytes(review, candidate=candidate, expected_policy_digest=expected_policy_digest) + return review + + +def _validate_accepted_review(review: RepositoryEcosystemReviewV1) -> None: + assert review.ecosystem is not None + if review.decision.value != review.ecosystem.value: + raise EcosystemReviewError("accepted ecosystem does not match the reviewed decision") + expected = "python" if review.observed_language == "python" else "javascript" + if review.ecosystem.value != expected: + raise EcosystemReviewError("accepted ecosystem does not match the observed language") + policy_failed = review.producer_identity == review.reviewer_identity or not review.conflict_disclosed or not review.policy_satisfied + if policy_failed: + raise EcosystemReviewError("accepted ecosystem review does not satisfy independent review policy") + + +def _validate_review_outcome(review: RepositoryEcosystemReviewV1) -> None: + if not isinstance(review.decision, EcosystemReviewDecision) or not isinstance(review.status, EcosystemReviewStatus): + raise EcosystemReviewError("ecosystem review contains unsupported closed values") + if review.ecosystem is not None and not isinstance(review.ecosystem, CanonicalEcosystem): + raise EcosystemReviewError("canonical ecosystem is unsupported") + if review.insufficient_reason is not None and not isinstance(review.insufficient_reason, EcosystemInsufficientReason): + raise EcosystemReviewError("ecosystem insufficiency reason is unsupported") + if type(review.conflict_disclosed) is not bool or type(review.policy_satisfied) is not bool: + raise EcosystemReviewError("ecosystem review policy values must be boolean") + accepted = review.status is EcosystemReviewStatus.ACCEPTED + if accepted != (review.ecosystem is not None and review.insufficient_reason is None): + raise EcosystemReviewError("ecosystem review outcome is inconsistent") + if not accepted and (review.ecosystem is not None or review.insufficient_reason is None): + raise EcosystemReviewError("insufficient ecosystem review outcome is inconsistent") + if accepted: + _validate_accepted_review(review) + + +def _attestation_from_review(review: RepositoryEcosystemReviewV1) -> EcosystemReviewAttestationV1: + return EcosystemReviewAttestationV1( + review.candidate_digest, + review.source_evidence_digest, + review.producer_identity, + review.reviewer_identity, + review.conflict_disclosed, + review.policy_digest, + review.policy_satisfied, + review.decision, + ) + + +def _validate_review_authority( + review: RepositoryEcosystemReviewV1, + candidate: FixSiteCandidateV1, + expected_policy_digest: str, +) -> None: + attestation = _attestation_from_review(review) + _validate_attestation(candidate, attestation, expected_policy_digest) + expected_ecosystem, expected_reason = _classification(candidate, attestation) + expected_status = EcosystemReviewStatus.ACCEPTED if expected_ecosystem is not None else EcosystemReviewStatus.INSUFFICIENT + expected_identity = ( + candidate.canonical_repository, + candidate.network_identity_digest, + candidate.candidate_digest, + candidate.source_evidence.evidence_digest, + candidate.observed_language, + _package_evidence_digest(candidate), + ) + recorded_identity = ( + review.canonical_repository, + review.network_identity_digest, + review.candidate_digest, + review.source_evidence_digest, + review.observed_language, + review.package_evidence_digest, + ) + if recorded_identity != expected_identity: + raise EcosystemReviewError("ecosystem review evidence identity does not match the exact candidate") + if (review.status, review.ecosystem, review.insufficient_reason) != ( + expected_status, + expected_ecosystem, + expected_reason, + ): + raise EcosystemReviewError("ecosystem review outcome does not match the exact reviewed inputs") + + +def repository_ecosystem_review_bytes( + review: RepositoryEcosystemReviewV1, + *, + candidate: FixSiteCandidateV1, + expected_policy_digest: str, +) -> bytes: + """Validate and encode one accepted or bounded-insufficient review.""" + _validate_review_outcome(review) + _text(review.canonical_repository, "canonical repository") + _text(review.producer_identity, "producer identity") + _text(review.reviewer_identity, "reviewer identity") + if review.observed_language not in {"python", "javascript", "typescript"}: + raise EcosystemReviewError("observed language is outside the canonical ecosystem boundary") + _validate_review_authority(review, candidate, expected_policy_digest) + for value, field in ( + (review.network_identity_digest, "network identity digest"), + (review.candidate_digest, "candidate digest"), + (review.source_evidence_digest, "source evidence digest"), + (review.package_evidence_digest, "package evidence digest"), + (review.policy_digest, "review policy digest"), + ): + _digest(value, field) + expected = content_digest(_review_value(review, include_digest=False)) + if review.review_digest != expected: + raise EcosystemReviewError("ecosystem review digest does not match") + return canonical_bytes(_review_value(review, include_digest=True)) + + +def project_accepted_ecosystems( + reviews: Iterable[RepositoryEcosystemReviewV1], + *, + candidates_by_digest: Mapping[str, FixSiteCandidateV1], + expected_policy_digest: str, +) -> tuple[tuple[str, CanonicalEcosystem], ...]: + """Project at most one accepted ecosystem per canonical network identity.""" + accepted_by_network: dict[str, tuple[str, CanonicalEcosystem]] = {} + network_by_repository: dict[str, str] = {} + repository_by_network: dict[str, str] = {} + for review in reviews: + candidate = candidates_by_digest.get(review.candidate_digest) + if candidate is None: + raise EcosystemReviewError("ecosystem review candidate is not in the exact reviewed input set") + repository_ecosystem_review_bytes( + review, + candidate=candidate, + expected_policy_digest=expected_policy_digest, + ) + known_network = network_by_repository.get(review.canonical_repository) + if known_network is not None and known_network != review.network_identity_digest: + raise EcosystemReviewError("canonical repository maps to conflicting network identities") + known_repository = repository_by_network.get(review.network_identity_digest) + if known_repository is not None and known_repository != review.canonical_repository: + raise EcosystemReviewError("network identity maps to conflicting canonical repositories") + network_by_repository[review.canonical_repository] = review.network_identity_digest + repository_by_network[review.network_identity_digest] = review.canonical_repository + if review.status is EcosystemReviewStatus.INSUFFICIENT: + continue + assert review.ecosystem is not None + current = (review.canonical_repository, review.ecosystem) + existing = accepted_by_network.get(review.network_identity_digest) + if existing is not None and existing != current: + raise EcosystemReviewError("repository has conflicting accepted ecosystem reviews") + accepted_by_network[review.network_identity_digest] = current + return tuple((network_digest, repository_ecosystem[1]) for network_digest, repository_ecosystem in sorted(accepted_by_network.items())) + + +__all__ = [ + "CanonicalEcosystem", + "EcosystemInsufficientReason", + "EcosystemReviewAttestationV1", + "EcosystemReviewDecision", + "EcosystemReviewError", + "EcosystemReviewStatus", + "RepositoryEcosystemReviewV1", + "project_accepted_ecosystems", + "repository_ecosystem_review_bytes", + "review_repository_ecosystem", +] diff --git a/tests/test_valid_benchmark_ecosystem_review.py b/tests/test_valid_benchmark_ecosystem_review.py new file mode 100644 index 00000000..3938ab76 --- /dev/null +++ b/tests/test_valid_benchmark_ecosystem_review.py @@ -0,0 +1,380 @@ +"""Independent canonical ecosystem review for fix-site candidates.""" + +from __future__ import annotations + +import sys +from dataclasses import replace +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from benchmarks.valid import ecosystem_review as ecosystem_review_module +from benchmarks.valid import fix_site_catalog as fix_site_catalog_module +from benchmarks.valid.ecosystem_review import ( + CanonicalEcosystem, + EcosystemInsufficientReason, + EcosystemReviewAttestationV1, + EcosystemReviewDecision, + EcosystemReviewError, + EcosystemReviewStatus, + project_accepted_ecosystems, + repository_ecosystem_review_bytes, + review_repository_ecosystem, +) +from benchmarks.valid.fix_site_catalog import ( + EvidenceRef, + FixSiteRequest, + LicenseEvidence, + PackageEcosystemEvidence, + SourceEvidence, + build_fix_site_candidate, +) +from benchmarks.valid.reservation_identity import build_network_identity +from openultrasast.core.canonical import content_digest + +PARENT = "a" * 40 +FIXED = "b" * 40 +POLICY = content_digest({"policy": "independent-ecosystem-review-v1"}) + + +class _Reader: + def __init__(self, path: str, before: str, after: str) -> None: + self._path = path + self._sources = {PARENT: before, FIXED: after} + + def parents(self, revision: str) -> tuple[str, ...]: + return (PARENT,) if revision == FIXED else () + + def blob(self, revision: str, path: str) -> str | None: + return self._sources.get(revision) if path == self._path else None + + +def _candidate(language: str = "python", package_hint: str | None = None, repository=None): + if language == "python": + path = "src/app.py" + before = 'def handle():\n return "before"\n' + after = 'def handle():\n return "after"\n' + else: + suffix = "ts" if language == "typescript" else "js" + path = f"src/app.{suffix}" + before = 'function handle() {\n return "before";\n}\n' + after = 'function handle() {\n return "after";\n}\n' + hint = package_hint or ("python" if language == "python" else "javascript") + manifest_path = "pyproject.toml" if hint == "python" else "package.json" + request = FixSiteRequest( + repository=repository or build_network_identity("https://github.com/example/application"), + vulnerable_parent=PARENT, + fixed_revision=FIXED, + path=path, + function="handle", + observed_language=language, + package_evidence=PackageEcosystemEvidence( + hint, + EvidenceRef(manifest_path, content_digest({"manifest": hint}), 17), + ), + license_evidence=LicenseEvidence( + "Apache-2.0", + EvidenceRef("LICENSE", content_digest({"license": "Apache-2.0"}), 11), + ), + source_evidence=SourceEvidence( + "security_advisory", + "fixture://advisory/1", + content_digest({"advisory": 1}), + ), + change_inputs_digest=content_digest({"parent": PARENT, "fixed": FIXED}), + ) + return build_fix_site_candidate(_Reader(path, before, after), request) + + +def _attestation(candidate, decision: EcosystemReviewDecision, **changes: object) -> EcosystemReviewAttestationV1: + values: dict[str, object] = { + "candidate_digest": candidate.candidate_digest, + "source_evidence_digest": candidate.source_evidence.evidence_digest, + "producer_identity": "producer@example.invalid", + "reviewer_identity": "reviewer@example.invalid", + "conflict_disclosed": True, + "policy_digest": POLICY, + "policy_satisfied": True, + "decision": decision, + } + values.update(changes) + return EcosystemReviewAttestationV1(**values) # type: ignore[arg-type] + + +def _rehash(review): + draft = replace(review, review_digest="") + return replace( + draft, + review_digest=content_digest(ecosystem_review_module._review_value(draft, include_digest=False)), + ) + + +def _rehash_candidate(candidate, **changes): + draft = replace(candidate, provenance_digest="", candidate_digest="", **changes) + provenance = content_digest(fix_site_catalog_module._provenance_value(draft)) + reviewed = replace(draft, provenance_digest=provenance) + return replace( + reviewed, + candidate_digest=content_digest(fix_site_catalog_module._candidate_value(reviewed, include_digest=False)), + ) + + +def _encoded(review, candidate) -> bytes: + return repository_ecosystem_review_bytes( + review, + candidate=candidate, + expected_policy_digest=POLICY, + ) + + +def _project(reviews, *candidates): + return project_accepted_ecosystems( + reviews, + candidates_by_digest={candidate.candidate_digest: candidate for candidate in candidates}, + expected_policy_digest=POLICY, + ) + + +@pytest.mark.parametrize( + "language,decision,ecosystem", + ( + ("python", EcosystemReviewDecision.PYTHON, CanonicalEcosystem.PYTHON), + ("javascript", EcosystemReviewDecision.JAVASCRIPT, CanonicalEcosystem.JAVASCRIPT), + ("typescript", EcosystemReviewDecision.JAVASCRIPT, CanonicalEcosystem.JAVASCRIPT), + ), +) +def test_review_accepts_only_canonical_ecosystems(language, decision, ecosystem) -> None: + candidate = _candidate(language) + review = review_repository_ecosystem(candidate, _attestation(candidate, decision), expected_policy_digest=POLICY) + + assert review.status is EcosystemReviewStatus.ACCEPTED + assert review.ecosystem is ecosystem + assert review.insufficient_reason is None + assert _encoded(review, candidate) + + +@pytest.mark.parametrize( + "decision,reason", + ( + (EcosystemReviewDecision.UNRESOLVED, EcosystemInsufficientReason.UNRESOLVED), + (EcosystemReviewDecision.CONFLICTING, EcosystemInsufficientReason.CONFLICTING), + (EcosystemReviewDecision.POLYGLOT_AMBIGUITY, EcosystemInsufficientReason.POLYGLOT_AMBIGUITY), + ), +) +def test_bounded_review_decisions_remain_insufficient(decision, reason) -> None: + candidate = _candidate() + review = review_repository_ecosystem(candidate, _attestation(candidate, decision), expected_policy_digest=POLICY) + + assert review.status is EcosystemReviewStatus.INSUFFICIENT + assert review.ecosystem is None + assert review.insufficient_reason is reason + + +def test_conflicting_candidate_evidence_cannot_be_accepted() -> None: + candidate = _candidate("python", package_hint="javascript") + review = review_repository_ecosystem( + candidate, + _attestation(candidate, EcosystemReviewDecision.PYTHON), + expected_policy_digest=POLICY, + ) + + assert review.status is EcosystemReviewStatus.INSUFFICIENT + assert review.insufficient_reason is EcosystemInsufficientReason.CONFLICTING + + +@pytest.mark.parametrize( + "changes,reason", + ( + ({"reviewer_identity": "producer@example.invalid"}, EcosystemInsufficientReason.SELF_REVIEW), + ({"conflict_disclosed": False}, EcosystemInsufficientReason.CONFLICT_DISCLOSURE_MISSING), + ({"policy_satisfied": False}, EcosystemInsufficientReason.POLICY_UNSATISFIED), + ), +) +def test_review_policy_failures_are_non_admissible(changes, reason) -> None: + candidate = _candidate() + review = review_repository_ecosystem( + candidate, + _attestation(candidate, EcosystemReviewDecision.PYTHON, **changes), + expected_policy_digest=POLICY, + ) + + assert review.status is EcosystemReviewStatus.INSUFFICIENT + assert review.ecosystem is None + assert review.insufficient_reason is reason + + +@pytest.mark.parametrize( + "changes,message", + ( + ({"candidate_digest": "0" * 64}, "candidate"), + ({"source_evidence_digest": "1" * 64}, "source evidence"), + ({"policy_digest": "2" * 64}, "policy"), + ), +) +def test_stale_review_evidence_is_refused(changes, message) -> None: + candidate = _candidate() + with pytest.raises(EcosystemReviewError, match=message): + review_repository_ecosystem( + candidate, + _attestation(candidate, EcosystemReviewDecision.PYTHON, **changes), + expected_policy_digest=POLICY, + ) + + +def test_review_bytes_are_deterministic_and_tamper_evident() -> None: + candidate = _candidate("typescript") + review = review_repository_ecosystem( + candidate, + _attestation(candidate, EcosystemReviewDecision.JAVASCRIPT), + expected_policy_digest=POLICY, + ) + + assert _encoded(review, candidate) == _encoded(review, candidate) + with pytest.raises(EcosystemReviewError, match="digest"): + _encoded(replace(review, review_digest="0" * 64), candidate) + + +def test_repository_cannot_have_two_accepted_canonical_ecosystems() -> None: + python_candidate = _candidate("python") + javascript_candidate = _candidate("javascript") + python_review = review_repository_ecosystem( + python_candidate, + _attestation(python_candidate, EcosystemReviewDecision.PYTHON), + expected_policy_digest=POLICY, + ) + javascript_review = review_repository_ecosystem( + javascript_candidate, + _attestation(javascript_candidate, EcosystemReviewDecision.JAVASCRIPT), + expected_policy_digest=POLICY, + ) + + with pytest.raises(EcosystemReviewError, match="conflicting accepted"): + _project((python_review, javascript_review), python_candidate, javascript_candidate) + + +def test_rehashed_candidate_source_and_package_substitution_is_refused() -> None: + candidate = _candidate() + review = review_repository_ecosystem( + candidate, + _attestation(candidate, EcosystemReviewDecision.PYTHON), + expected_policy_digest=POLICY, + ) + forged = _rehash( + replace( + review, + candidate_digest="0" * 64, + source_evidence_digest="1" * 64, + package_evidence_digest="2" * 64, + ) + ) + + with pytest.raises(EcosystemReviewError, match="candidate|evidence"): + _encoded(forged, candidate) + + +def test_projection_refuses_rehashed_candidate_identity_substitution() -> None: + candidate = _candidate() + review = review_repository_ecosystem( + candidate, + _attestation(candidate, EcosystemReviewDecision.PYTHON), + expected_policy_digest=POLICY, + ) + forged = _rehash(replace(review, candidate_digest="0" * 64)) + + with pytest.raises(EcosystemReviewError, match="candidate|evidence"): + _project((forged,), candidate) + + +@pytest.mark.parametrize("mutation", ("self_review_reason", "accepted_outcome")) +def test_rehashed_outcome_substitution_is_rederived_and_refused(mutation: str) -> None: + candidate = _candidate() + if mutation == "self_review_reason": + review = review_repository_ecosystem( + candidate, + _attestation( + candidate, + EcosystemReviewDecision.PYTHON, + reviewer_identity="producer@example.invalid", + ), + expected_policy_digest=POLICY, + ) + forged = _rehash(replace(review, insufficient_reason=EcosystemInsufficientReason.UNRESOLVED)) + else: + review = review_repository_ecosystem( + candidate, + _attestation(candidate, EcosystemReviewDecision.PYTHON), + expected_policy_digest=POLICY, + ) + forged = _rehash( + replace( + review, + status=EcosystemReviewStatus.INSUFFICIENT, + ecosystem=None, + insufficient_reason=EcosystemInsufficientReason.UNRESOLVED, + ) + ) + + with pytest.raises(EcosystemReviewError, match="outcome|review"): + _encoded(forged, candidate) + + +@pytest.mark.parametrize("second_language", ("python", "javascript")) +def test_same_repository_cannot_resolve_to_different_networks(second_language: str) -> None: + canonical = "https://github.com/example/application" + first_network = build_network_identity(canonical) + second_network = build_network_identity(canonical, aliases=("https://github.com/example/former",)) + first = _candidate("python", repository=first_network) + second = _candidate(second_language, repository=second_network) + first_review = review_repository_ecosystem( + first, + _attestation(first, EcosystemReviewDecision.PYTHON), + expected_policy_digest=POLICY, + ) + second_decision = EcosystemReviewDecision.PYTHON if second_language == "python" else EcosystemReviewDecision.JAVASCRIPT + second_review = review_repository_ecosystem( + second, + _attestation(second, second_decision), + expected_policy_digest=POLICY, + ) + + with pytest.raises(EcosystemReviewError, match="identity|network"): + _project((first_review, second_review), first, second) + + +def test_same_network_cannot_resolve_to_different_canonical_repositories() -> None: + first = _candidate() + second = _rehash_candidate(first, canonical_repository="https://github.com/other/application") + first_review = review_repository_ecosystem( + first, + _attestation(first, EcosystemReviewDecision.PYTHON), + expected_policy_digest=POLICY, + ) + second_review = review_repository_ecosystem( + second, + _attestation(second, EcosystemReviewDecision.UNRESOLVED), + expected_policy_digest=POLICY, + ) + + with pytest.raises(EcosystemReviewError, match="identity|network"): + _project((first_review, second_review), first, second) + + +def test_insufficient_review_cannot_hide_repository_network_conflict() -> None: + canonical = "https://github.com/example/application" + first = _candidate(repository=build_network_identity(canonical)) + second = _candidate(repository=build_network_identity(canonical, redirects=("https://github.com/example/redirect",))) + accepted = review_repository_ecosystem( + first, + _attestation(first, EcosystemReviewDecision.PYTHON), + expected_policy_digest=POLICY, + ) + insufficient = review_repository_ecosystem( + second, + _attestation(second, EcosystemReviewDecision.UNRESOLVED), + expected_policy_digest=POLICY, + ) + + with pytest.raises(EcosystemReviewError, match="identity|network"): + _project((accepted, insufficient), first, second)