mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 02:08:18 +00:00
feat(valid-detection-benchmark): validate reviewed ground truth
This commit is contained in:
@@ -88,7 +88,7 @@
|
||||
- Done when accepted repositories have exactly one canonical ecosystem and every unresolved, self-reviewed, stale, or conflicting review remains non-admissible.
|
||||
- _Requirements: 1.1, 1.5, 1.6, 1.8, 2.3_
|
||||
- _Boundary: Review Authority_
|
||||
- [ ] 3.3 Validate independently reviewed positive ground truth and exact scopes
|
||||
- [x] 3.3 Validate independently reviewed positive ground truth and exact scopes
|
||||
- Require explicit defect-present, fix-addresses, and in-repository-reachable judgments with producer/reviewer separation, policy evidence, and conflict disclosure.
|
||||
- Keep optional model assistance as a separately identified non-authoritative input that can never satisfy a missing human judgment.
|
||||
- Bind each accepted fix to one or more independently verified vulnerability-family and canonical-ecosystem scopes; record closed exclusion reasons otherwise.
|
||||
@@ -420,3 +420,4 @@
|
||||
- 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.
|
||||
- Task 3.3 revalidates exact candidate and ecosystem authorities, derives accepted/excluded ground truth from explicit independent human judgments, keeps model assistance non-authoritative, and projects each change once while preserving all verified exact scopes.
|
||||
|
||||
@@ -0,0 +1,453 @@
|
||||
"""Independent reviewed ground truth for replacement benchmark positives."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from collections.abc import Iterable, Mapping
|
||||
from dataclasses import dataclass
|
||||
from enum import StrEnum
|
||||
|
||||
from benchmarks.valid.ecosystem_review import (
|
||||
CanonicalEcosystem,
|
||||
EcosystemReviewError,
|
||||
EcosystemReviewStatus,
|
||||
RepositoryEcosystemReviewV1,
|
||||
project_accepted_ecosystems,
|
||||
repository_ecosystem_review_bytes,
|
||||
)
|
||||
from benchmarks.valid.fix_site_catalog import FixSiteCandidateV1, fix_site_candidate_bytes
|
||||
from openultrasast.core.canonical import JsonValue, canonical_bytes, content_digest
|
||||
|
||||
GROUND_TRUTH_REVIEW_SCHEMA = "openultrasast.valid-detection-benchmark.ground-truth-review.v1"
|
||||
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
||||
_FAMILY = re.compile(r"[a-z0-9][a-z0-9_]{0,127}")
|
||||
|
||||
|
||||
class GroundTruthStatus(StrEnum):
|
||||
ACCEPTED = "accepted"
|
||||
EXCLUDED = "excluded"
|
||||
|
||||
|
||||
class GroundTruthExclusionReason(StrEnum):
|
||||
MISSING_HUMAN_JUDGMENT = "missing_human_judgment"
|
||||
JUDGMENT_REJECTED = "judgment_rejected"
|
||||
SELF_REVIEW = "self_review"
|
||||
CONFLICT_DISCLOSURE_MISSING = "conflict_disclosure_missing"
|
||||
POLICY_UNSATISFIED = "policy_unsatisfied"
|
||||
MISSING_SCOPE = "missing_scope"
|
||||
SCOPE_SUBSTITUTION = "scope_substitution"
|
||||
ECOSYSTEM_NOT_ACCEPTED = "ecosystem_not_accepted"
|
||||
|
||||
|
||||
class GroundTruthReviewError(ValueError):
|
||||
"""Ground-truth review is stale, inconsistent, or non-authoritative."""
|
||||
|
||||
|
||||
@dataclass(frozen=True, order=True)
|
||||
class VerifiedScope:
|
||||
vulnerability_family: str
|
||||
ecosystem: CanonicalEcosystem
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
family = _family_name(self.vulnerability_family)
|
||||
try:
|
||||
ecosystem = CanonicalEcosystem(self.ecosystem)
|
||||
except (TypeError, ValueError) as error:
|
||||
raise GroundTruthReviewError("verified scope ecosystem is unsupported") from error
|
||||
object.__setattr__(self, "vulnerability_family", family)
|
||||
object.__setattr__(self, "ecosystem", ecosystem)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GroundTruthAttestationV1:
|
||||
candidate_digest: str
|
||||
ecosystem_review_digest: str
|
||||
producer_identity: str
|
||||
reviewer_identity: str
|
||||
conflict_disclosed: bool
|
||||
policy_digest: str
|
||||
policy_satisfied: bool
|
||||
defect_present: bool | None
|
||||
fix_addresses: bool | None
|
||||
in_repository_reachable: bool | None
|
||||
verified_scopes: tuple[VerifiedScope, ...]
|
||||
model_assistance_digest: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GroundTruthReviewV1:
|
||||
canonical_repository: str
|
||||
network_identity_digest: str
|
||||
candidate_digest: str
|
||||
ecosystem_review_digest: str
|
||||
change_identity_digest: str
|
||||
vulnerable_revision: str
|
||||
fixed_revision: str
|
||||
path: str
|
||||
function: str
|
||||
defect_present: bool | None
|
||||
fix_addresses: bool | None
|
||||
in_repository_reachable: bool | None
|
||||
reviewed_scopes: tuple[VerifiedScope, ...]
|
||||
verified_scopes: tuple[VerifiedScope, ...]
|
||||
model_assistance_digest: str | None
|
||||
producer_identity: str
|
||||
reviewer_identity: str
|
||||
conflict_disclosed: bool
|
||||
policy_digest: str
|
||||
policy_satisfied: bool
|
||||
status: GroundTruthStatus
|
||||
exclusion_reason: GroundTruthExclusionReason | None
|
||||
review_digest: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AcceptedGroundTruthV1:
|
||||
change_identity_digest: str
|
||||
candidate_digest: str
|
||||
ecosystem_review_digest: str
|
||||
ground_truth_review_digest: str
|
||||
verified_scopes: tuple[VerifiedScope, ...]
|
||||
|
||||
|
||||
def _text(value: object, field: str) -> str:
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
raise GroundTruthReviewError(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 GroundTruthReviewError(f"{field} must be a full lowercase SHA-256 digest")
|
||||
return value
|
||||
|
||||
|
||||
def _family_name(value: object) -> str:
|
||||
family = _text(value, "vulnerability family").lower()
|
||||
if _FAMILY.fullmatch(family) is None:
|
||||
raise GroundTruthReviewError("vulnerability family must be a canonical evidence label")
|
||||
return family
|
||||
|
||||
|
||||
def _judgment(value: object, field: str) -> bool | None:
|
||||
if value is not None and type(value) is not bool:
|
||||
raise GroundTruthReviewError(f"{field} judgment must be boolean or absent")
|
||||
return value
|
||||
|
||||
|
||||
def _scopes(values: Iterable[VerifiedScope]) -> tuple[VerifiedScope, ...]:
|
||||
try:
|
||||
scopes = tuple(VerifiedScope(item.vulnerability_family, item.ecosystem) for item in values)
|
||||
except (AttributeError, TypeError) as error:
|
||||
raise GroundTruthReviewError("verified scopes are malformed") from error
|
||||
return tuple(sorted(set(scopes)))
|
||||
|
||||
|
||||
def _scope_value(scope: VerifiedScope) -> dict[str, JsonValue]:
|
||||
return {"vulnerability_family": scope.vulnerability_family, "ecosystem": scope.ecosystem.value}
|
||||
|
||||
|
||||
def _change_identity(candidate: FixSiteCandidateV1) -> str:
|
||||
value: JsonValue = {
|
||||
"canonical_repository": candidate.canonical_repository,
|
||||
"network_identity_digest": candidate.network_identity_digest,
|
||||
"vulnerable_revision": candidate.vulnerable.revision,
|
||||
"fixed_revision": candidate.fixed.revision,
|
||||
"path": candidate.fixed.path,
|
||||
"function": candidate.fixed.function,
|
||||
"change_inputs_digest": candidate.change_inputs_digest,
|
||||
}
|
||||
return content_digest(value)
|
||||
|
||||
|
||||
def _review_value(review: GroundTruthReviewV1, *, include_digest: bool) -> dict[str, JsonValue]:
|
||||
value: dict[str, JsonValue] = {
|
||||
"schema_version": GROUND_TRUTH_REVIEW_SCHEMA,
|
||||
"canonical_repository": review.canonical_repository,
|
||||
"network_identity_digest": review.network_identity_digest,
|
||||
"candidate_digest": review.candidate_digest,
|
||||
"ecosystem_review_digest": review.ecosystem_review_digest,
|
||||
"change_identity_digest": review.change_identity_digest,
|
||||
"vulnerable_revision": review.vulnerable_revision,
|
||||
"fixed_revision": review.fixed_revision,
|
||||
"path": review.path,
|
||||
"function": review.function,
|
||||
"defect_present": review.defect_present,
|
||||
"fix_addresses": review.fix_addresses,
|
||||
"in_repository_reachable": review.in_repository_reachable,
|
||||
"reviewed_scopes": tuple(_scope_value(scope) for scope in review.reviewed_scopes),
|
||||
"verified_scopes": tuple(_scope_value(scope) for scope in review.verified_scopes),
|
||||
"model_assistance_digest": review.model_assistance_digest,
|
||||
"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,
|
||||
"status": review.status.value,
|
||||
"exclusion_reason": review.exclusion_reason.value if review.exclusion_reason is not None else None,
|
||||
}
|
||||
if include_digest:
|
||||
value["review_digest"] = review.review_digest
|
||||
return value
|
||||
|
||||
|
||||
def _validate_ecosystem_authority(
|
||||
candidate: FixSiteCandidateV1,
|
||||
ecosystem_review: RepositoryEcosystemReviewV1,
|
||||
expected_policy_digest: str,
|
||||
) -> CanonicalEcosystem | None:
|
||||
try:
|
||||
repository_ecosystem_review_bytes(
|
||||
ecosystem_review,
|
||||
candidate=candidate,
|
||||
expected_policy_digest=expected_policy_digest,
|
||||
)
|
||||
except EcosystemReviewError as error:
|
||||
raise GroundTruthReviewError("ecosystem review authority is invalid") from error
|
||||
if ecosystem_review.status is EcosystemReviewStatus.ACCEPTED:
|
||||
assert ecosystem_review.ecosystem is not None
|
||||
return ecosystem_review.ecosystem
|
||||
return None
|
||||
|
||||
|
||||
def _validate_attestation(
|
||||
candidate: FixSiteCandidateV1,
|
||||
ecosystem_review: RepositoryEcosystemReviewV1,
|
||||
attestation: GroundTruthAttestationV1,
|
||||
expected_ground_truth_policy_digest: str,
|
||||
) -> None:
|
||||
fix_site_candidate_bytes(candidate)
|
||||
if _digest(attestation.candidate_digest, "candidate digest") != candidate.candidate_digest:
|
||||
raise GroundTruthReviewError("ground-truth candidate digest is stale")
|
||||
if _digest(attestation.ecosystem_review_digest, "ecosystem review digest") != ecosystem_review.review_digest:
|
||||
raise GroundTruthReviewError("ground-truth ecosystem review digest is stale")
|
||||
policy = _digest(attestation.policy_digest, "ground-truth review policy digest")
|
||||
if policy != _digest(expected_ground_truth_policy_digest, "expected ground-truth policy digest"):
|
||||
raise GroundTruthReviewError("ground-truth review policy digest is stale")
|
||||
_text(attestation.producer_identity, "producer identity")
|
||||
_text(attestation.reviewer_identity, "reviewer identity")
|
||||
if type(attestation.conflict_disclosed) is not bool or type(attestation.policy_satisfied) is not bool:
|
||||
raise GroundTruthReviewError("ground-truth review policy values must be boolean")
|
||||
for value, field in (
|
||||
(attestation.defect_present, "defect-present"),
|
||||
(attestation.fix_addresses, "fix-addresses"),
|
||||
(attestation.in_repository_reachable, "in-repository-reachable"),
|
||||
):
|
||||
_judgment(value, field)
|
||||
if attestation.model_assistance_digest is not None:
|
||||
_digest(attestation.model_assistance_digest, "model assistance digest")
|
||||
|
||||
|
||||
def _classify(
|
||||
ecosystem: CanonicalEcosystem | None,
|
||||
attestation: GroundTruthAttestationV1,
|
||||
scopes: tuple[VerifiedScope, ...],
|
||||
) -> GroundTruthExclusionReason | None:
|
||||
if attestation.producer_identity == attestation.reviewer_identity:
|
||||
return GroundTruthExclusionReason.SELF_REVIEW
|
||||
if not attestation.conflict_disclosed:
|
||||
return GroundTruthExclusionReason.CONFLICT_DISCLOSURE_MISSING
|
||||
if not attestation.policy_satisfied:
|
||||
return GroundTruthExclusionReason.POLICY_UNSATISFIED
|
||||
judgments = (attestation.defect_present, attestation.fix_addresses, attestation.in_repository_reachable)
|
||||
if any(value is None for value in judgments):
|
||||
return GroundTruthExclusionReason.MISSING_HUMAN_JUDGMENT
|
||||
if not all(judgments):
|
||||
return GroundTruthExclusionReason.JUDGMENT_REJECTED
|
||||
if ecosystem is None:
|
||||
return GroundTruthExclusionReason.ECOSYSTEM_NOT_ACCEPTED
|
||||
if not scopes:
|
||||
return GroundTruthExclusionReason.MISSING_SCOPE
|
||||
if any(scope.ecosystem is not ecosystem for scope in scopes):
|
||||
return GroundTruthExclusionReason.SCOPE_SUBSTITUTION
|
||||
return None
|
||||
|
||||
|
||||
def _attestation_from_review(review: GroundTruthReviewV1) -> GroundTruthAttestationV1:
|
||||
return GroundTruthAttestationV1(
|
||||
review.candidate_digest,
|
||||
review.ecosystem_review_digest,
|
||||
review.producer_identity,
|
||||
review.reviewer_identity,
|
||||
review.conflict_disclosed,
|
||||
review.policy_digest,
|
||||
review.policy_satisfied,
|
||||
review.defect_present,
|
||||
review.fix_addresses,
|
||||
review.in_repository_reachable,
|
||||
review.reviewed_scopes,
|
||||
review.model_assistance_digest,
|
||||
)
|
||||
|
||||
|
||||
def review_ground_truth(
|
||||
candidate: FixSiteCandidateV1,
|
||||
ecosystem_review: RepositoryEcosystemReviewV1,
|
||||
attestation: GroundTruthAttestationV1,
|
||||
*,
|
||||
expected_ecosystem_policy_digest: str,
|
||||
expected_ground_truth_policy_digest: str,
|
||||
) -> GroundTruthReviewV1:
|
||||
"""Validate externally governed judgments without inventing a label."""
|
||||
ecosystem = _validate_ecosystem_authority(candidate, ecosystem_review, expected_ecosystem_policy_digest)
|
||||
_validate_attestation(candidate, ecosystem_review, attestation, expected_ground_truth_policy_digest)
|
||||
scopes = _scopes(attestation.verified_scopes)
|
||||
reason = _classify(ecosystem, attestation, scopes)
|
||||
verified = scopes if reason is None else ()
|
||||
status = GroundTruthStatus.ACCEPTED if reason is None else GroundTruthStatus.EXCLUDED
|
||||
draft = GroundTruthReviewV1(
|
||||
candidate.canonical_repository,
|
||||
candidate.network_identity_digest,
|
||||
candidate.candidate_digest,
|
||||
ecosystem_review.review_digest,
|
||||
_change_identity(candidate),
|
||||
candidate.vulnerable.revision,
|
||||
candidate.fixed.revision,
|
||||
candidate.fixed.path,
|
||||
candidate.fixed.function,
|
||||
attestation.defect_present,
|
||||
attestation.fix_addresses,
|
||||
attestation.in_repository_reachable,
|
||||
scopes,
|
||||
verified,
|
||||
attestation.model_assistance_digest,
|
||||
_text(attestation.producer_identity, "producer identity"),
|
||||
_text(attestation.reviewer_identity, "reviewer identity"),
|
||||
attestation.conflict_disclosed,
|
||||
attestation.policy_digest,
|
||||
attestation.policy_satisfied,
|
||||
status,
|
||||
reason,
|
||||
"",
|
||||
)
|
||||
review = GroundTruthReviewV1(**{**vars(draft), "review_digest": content_digest(_review_value(draft, include_digest=False))})
|
||||
ground_truth_review_bytes(
|
||||
review,
|
||||
candidate=candidate,
|
||||
ecosystem_review=ecosystem_review,
|
||||
expected_ecosystem_policy_digest=expected_ecosystem_policy_digest,
|
||||
expected_ground_truth_policy_digest=expected_ground_truth_policy_digest,
|
||||
)
|
||||
return review
|
||||
|
||||
|
||||
def _expected_identity(candidate: FixSiteCandidateV1, ecosystem_review: RepositoryEcosystemReviewV1) -> tuple[object, ...]:
|
||||
return (
|
||||
candidate.canonical_repository,
|
||||
candidate.network_identity_digest,
|
||||
candidate.candidate_digest,
|
||||
ecosystem_review.review_digest,
|
||||
_change_identity(candidate),
|
||||
candidate.vulnerable.revision,
|
||||
candidate.fixed.revision,
|
||||
candidate.fixed.path,
|
||||
candidate.fixed.function,
|
||||
)
|
||||
|
||||
|
||||
def ground_truth_review_bytes(
|
||||
review: GroundTruthReviewV1,
|
||||
*,
|
||||
candidate: FixSiteCandidateV1,
|
||||
ecosystem_review: RepositoryEcosystemReviewV1,
|
||||
expected_ecosystem_policy_digest: str,
|
||||
expected_ground_truth_policy_digest: str,
|
||||
) -> bytes:
|
||||
"""Revalidate exact authorities and encode one review canonically."""
|
||||
ecosystem = _validate_ecosystem_authority(candidate, ecosystem_review, expected_ecosystem_policy_digest)
|
||||
attestation = _attestation_from_review(review)
|
||||
_validate_attestation(candidate, ecosystem_review, attestation, expected_ground_truth_policy_digest)
|
||||
scopes = _scopes(review.reviewed_scopes)
|
||||
reason = _classify(ecosystem, attestation, scopes)
|
||||
status = GroundTruthStatus.ACCEPTED if reason is None else GroundTruthStatus.EXCLUDED
|
||||
verified = scopes if reason is None else ()
|
||||
identity = (
|
||||
review.canonical_repository,
|
||||
review.network_identity_digest,
|
||||
review.candidate_digest,
|
||||
review.ecosystem_review_digest,
|
||||
review.change_identity_digest,
|
||||
review.vulnerable_revision,
|
||||
review.fixed_revision,
|
||||
review.path,
|
||||
review.function,
|
||||
)
|
||||
if identity != _expected_identity(candidate, ecosystem_review):
|
||||
raise GroundTruthReviewError("ground-truth authority identity does not match exact inputs")
|
||||
if (review.status, review.exclusion_reason, review.verified_scopes) != (status, reason, verified):
|
||||
raise GroundTruthReviewError("ground-truth outcome or verified scope does not match reviewed inputs")
|
||||
if review.reviewed_scopes != scopes:
|
||||
raise GroundTruthReviewError("ground-truth reviewed scopes are not canonical")
|
||||
if not isinstance(review.status, GroundTruthStatus):
|
||||
raise GroundTruthReviewError("ground-truth status is unsupported")
|
||||
if review.exclusion_reason is not None and not isinstance(review.exclusion_reason, GroundTruthExclusionReason):
|
||||
raise GroundTruthReviewError("ground-truth exclusion reason is unsupported")
|
||||
expected = content_digest(_review_value(review, include_digest=False))
|
||||
if review.review_digest != expected:
|
||||
raise GroundTruthReviewError("ground-truth review digest does not match")
|
||||
return canonical_bytes(_review_value(review, include_digest=True))
|
||||
|
||||
|
||||
def project_accepted_ground_truth(
|
||||
reviews: Iterable[GroundTruthReviewV1],
|
||||
*,
|
||||
candidates_by_digest: Mapping[str, FixSiteCandidateV1],
|
||||
ecosystem_reviews_by_digest: Mapping[str, RepositoryEcosystemReviewV1],
|
||||
expected_ecosystem_policy_digest: str,
|
||||
expected_ground_truth_policy_digest: str,
|
||||
) -> tuple[AcceptedGroundTruthV1, ...]:
|
||||
"""Project every accepted change once with its exact verified scopes."""
|
||||
try:
|
||||
project_accepted_ecosystems(
|
||||
ecosystem_reviews_by_digest.values(),
|
||||
candidates_by_digest=candidates_by_digest,
|
||||
expected_policy_digest=expected_ecosystem_policy_digest,
|
||||
)
|
||||
except EcosystemReviewError as error:
|
||||
raise GroundTruthReviewError("ecosystem review identity authority is conflicting") from error
|
||||
accepted: dict[str, AcceptedGroundTruthV1] = {}
|
||||
review_bytes_by_digest: dict[str, bytes] = {}
|
||||
for review in reviews:
|
||||
candidate = candidates_by_digest.get(review.candidate_digest)
|
||||
ecosystem_review = ecosystem_reviews_by_digest.get(review.ecosystem_review_digest)
|
||||
if candidate is None or ecosystem_review is None:
|
||||
raise GroundTruthReviewError("ground-truth review authority is absent from exact input set")
|
||||
encoded = ground_truth_review_bytes(
|
||||
review,
|
||||
candidate=candidate,
|
||||
ecosystem_review=ecosystem_review,
|
||||
expected_ecosystem_policy_digest=expected_ecosystem_policy_digest,
|
||||
expected_ground_truth_policy_digest=expected_ground_truth_policy_digest,
|
||||
)
|
||||
existing_bytes = review_bytes_by_digest.get(review.review_digest)
|
||||
if existing_bytes is not None and existing_bytes != encoded:
|
||||
raise GroundTruthReviewError("same ground-truth review identity has different bytes")
|
||||
review_bytes_by_digest[review.review_digest] = encoded
|
||||
if review.status is GroundTruthStatus.EXCLUDED:
|
||||
continue
|
||||
projected = AcceptedGroundTruthV1(
|
||||
review.change_identity_digest,
|
||||
review.candidate_digest,
|
||||
review.ecosystem_review_digest,
|
||||
review.review_digest,
|
||||
review.verified_scopes,
|
||||
)
|
||||
current = accepted.get(review.change_identity_digest)
|
||||
if current is not None and current != projected:
|
||||
raise GroundTruthReviewError("one change identity has conflicting accepted ground-truth reviews")
|
||||
accepted[review.change_identity_digest] = projected
|
||||
return tuple(accepted[key] for key in sorted(accepted))
|
||||
|
||||
|
||||
__all__ = [
|
||||
"AcceptedGroundTruthV1",
|
||||
"GroundTruthAttestationV1",
|
||||
"GroundTruthExclusionReason",
|
||||
"GroundTruthReviewError",
|
||||
"GroundTruthReviewV1",
|
||||
"GroundTruthStatus",
|
||||
"VerifiedScope",
|
||||
"ground_truth_review_bytes",
|
||||
"project_accepted_ground_truth",
|
||||
"review_ground_truth",
|
||||
]
|
||||
@@ -0,0 +1,287 @@
|
||||
"""Independent positive ground-truth and exact-scope review contracts."""
|
||||
|
||||
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 ground_truth_review as ground_truth_module # noqa: E402
|
||||
from benchmarks.valid.ecosystem_review import ( # noqa: E402
|
||||
EcosystemReviewAttestationV1,
|
||||
EcosystemReviewDecision,
|
||||
review_repository_ecosystem,
|
||||
)
|
||||
from benchmarks.valid.fix_site_catalog import ( # noqa: E402
|
||||
EvidenceRef,
|
||||
FixSiteRequest,
|
||||
LicenseEvidence,
|
||||
PackageEcosystemEvidence,
|
||||
SourceEvidence,
|
||||
build_fix_site_candidate,
|
||||
)
|
||||
from benchmarks.valid.ground_truth_review import ( # noqa: E402
|
||||
GroundTruthAttestationV1,
|
||||
GroundTruthExclusionReason,
|
||||
GroundTruthReviewError,
|
||||
GroundTruthStatus,
|
||||
VerifiedScope,
|
||||
ground_truth_review_bytes,
|
||||
project_accepted_ground_truth,
|
||||
review_ground_truth,
|
||||
)
|
||||
from benchmarks.valid.reservation_identity import build_network_identity # noqa: E402
|
||||
from openultrasast.core.canonical import content_digest # noqa: E402
|
||||
|
||||
PARENT = "a" * 40
|
||||
FIXED = "b" * 40
|
||||
ECOSYSTEM_POLICY = content_digest({"policy": "ecosystem-v1"})
|
||||
GROUND_TRUTH_POLICY = content_digest({"policy": "ground-truth-v1"})
|
||||
|
||||
|
||||
class _Reader:
|
||||
def parents(self, revision: str) -> tuple[str, ...]:
|
||||
return (PARENT,) if revision == FIXED else ()
|
||||
|
||||
def blob(self, revision: str, path: str) -> str | None:
|
||||
if path != "src/app.py":
|
||||
return None
|
||||
return {
|
||||
PARENT: 'def handle():\n return "before"\n',
|
||||
FIXED: 'def handle():\n return "after"\n',
|
||||
}.get(revision)
|
||||
|
||||
|
||||
def _candidate(*, network=None):
|
||||
request = FixSiteRequest(
|
||||
repository=network or build_network_identity("https://github.com/example/application"),
|
||||
vulnerable_parent=PARENT,
|
||||
fixed_revision=FIXED,
|
||||
path="src/app.py",
|
||||
function="handle",
|
||||
observed_language="python",
|
||||
package_evidence=PackageEcosystemEvidence(
|
||||
"python",
|
||||
EvidenceRef("pyproject.toml", content_digest({"manifest": "python"}), 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(), request)
|
||||
|
||||
|
||||
def _ecosystem(candidate):
|
||||
attestation = EcosystemReviewAttestationV1(
|
||||
candidate.candidate_digest,
|
||||
candidate.source_evidence.evidence_digest,
|
||||
"[email protected]",
|
||||
"[email protected]",
|
||||
True,
|
||||
ECOSYSTEM_POLICY,
|
||||
True,
|
||||
EcosystemReviewDecision.PYTHON,
|
||||
)
|
||||
return review_repository_ecosystem(candidate, attestation, expected_policy_digest=ECOSYSTEM_POLICY)
|
||||
|
||||
|
||||
def _attestation(candidate, ecosystem, **changes):
|
||||
values = {
|
||||
"candidate_digest": candidate.candidate_digest,
|
||||
"ecosystem_review_digest": ecosystem.review_digest,
|
||||
"producer_identity": "[email protected]",
|
||||
"reviewer_identity": "[email protected]",
|
||||
"conflict_disclosed": True,
|
||||
"policy_digest": GROUND_TRUTH_POLICY,
|
||||
"policy_satisfied": True,
|
||||
"defect_present": True,
|
||||
"fix_addresses": True,
|
||||
"in_repository_reachable": True,
|
||||
"verified_scopes": (
|
||||
VerifiedScope("injection", "python"),
|
||||
VerifiedScope("authorization", "python"),
|
||||
),
|
||||
"model_assistance_digest": content_digest({"model-assistance": "non-authoritative"}),
|
||||
}
|
||||
values.update(changes)
|
||||
return GroundTruthAttestationV1(**values)
|
||||
|
||||
|
||||
def _review(candidate=None, **changes):
|
||||
candidate = candidate or _candidate()
|
||||
ecosystem = _ecosystem(candidate)
|
||||
review = review_ground_truth(
|
||||
candidate,
|
||||
ecosystem,
|
||||
_attestation(candidate, ecosystem, **changes),
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
return candidate, ecosystem, review
|
||||
|
||||
|
||||
def _encoded(candidate, ecosystem, review):
|
||||
return ground_truth_review_bytes(
|
||||
review,
|
||||
candidate=candidate,
|
||||
ecosystem_review=ecosystem,
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
|
||||
|
||||
def _rehash(review):
|
||||
draft = replace(review, review_digest="")
|
||||
return replace(
|
||||
draft,
|
||||
review_digest=content_digest(ground_truth_module._review_value(draft, include_digest=False)),
|
||||
)
|
||||
|
||||
|
||||
def test_accepts_explicit_human_judgments_and_sorted_exact_scopes_once() -> None:
|
||||
candidate, ecosystem, review = _review()
|
||||
|
||||
assert review.status is GroundTruthStatus.ACCEPTED
|
||||
assert tuple(scope.vulnerability_family for scope in review.verified_scopes) == ("authorization", "injection")
|
||||
assert len({review.change_identity_digest}) == 1
|
||||
assert review.model_assistance_digest is not None
|
||||
assert _encoded(candidate, ecosystem, review)
|
||||
projection = project_accepted_ground_truth(
|
||||
(review,),
|
||||
candidates_by_digest={candidate.candidate_digest: candidate},
|
||||
ecosystem_reviews_by_digest={ecosystem.review_digest: ecosystem},
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
assert len(projection) == 1
|
||||
assert len(projection[0].verified_scopes) == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"changes,reason",
|
||||
(
|
||||
({"defect_present": None}, GroundTruthExclusionReason.MISSING_HUMAN_JUDGMENT),
|
||||
({"fix_addresses": False}, GroundTruthExclusionReason.JUDGMENT_REJECTED),
|
||||
(
|
||||
{"in_repository_reachable": None, "model_assistance_digest": content_digest({"model": "agrees"})},
|
||||
GroundTruthExclusionReason.MISSING_HUMAN_JUDGMENT,
|
||||
),
|
||||
({"reviewer_identity": "[email protected]"}, GroundTruthExclusionReason.SELF_REVIEW),
|
||||
({"conflict_disclosed": False}, GroundTruthExclusionReason.CONFLICT_DISCLOSURE_MISSING),
|
||||
({"policy_satisfied": False}, GroundTruthExclusionReason.POLICY_UNSATISFIED),
|
||||
({"verified_scopes": ()}, GroundTruthExclusionReason.MISSING_SCOPE),
|
||||
({"verified_scopes": (VerifiedScope("injection", "javascript"),)}, GroundTruthExclusionReason.SCOPE_SUBSTITUTION),
|
||||
),
|
||||
)
|
||||
def test_missing_rejected_model_only_policy_and_scope_failures_are_excluded(changes, reason) -> None:
|
||||
_, _, review = _review(**changes)
|
||||
|
||||
assert review.status is GroundTruthStatus.EXCLUDED
|
||||
assert review.exclusion_reason is reason
|
||||
assert review.verified_scopes == ()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field", ("candidate_digest", "ecosystem_review_digest", "policy_digest"))
|
||||
def test_stale_authority_roots_are_refused(field: str) -> None:
|
||||
candidate = _candidate()
|
||||
ecosystem = _ecosystem(candidate)
|
||||
changes = {field: "0" * 64}
|
||||
with pytest.raises(GroundTruthReviewError, match="candidate|ecosystem|policy"):
|
||||
review_ground_truth(
|
||||
candidate,
|
||||
ecosystem,
|
||||
_attestation(candidate, ecosystem, **changes),
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
|
||||
|
||||
def test_scope_family_is_canonical_evidence_not_detector_vocabulary() -> None:
|
||||
_, _, review = _review(verified_scopes=(VerifiedScope("Custom_Family", "python"),))
|
||||
assert review.status is GroundTruthStatus.ACCEPTED
|
||||
assert review.verified_scopes[0].vulnerability_family == "custom_family"
|
||||
|
||||
|
||||
def test_rehashed_root_or_outcome_substitution_is_refused() -> None:
|
||||
candidate, ecosystem, review = _review()
|
||||
forged = _rehash(
|
||||
replace(
|
||||
review,
|
||||
candidate_digest="0" * 64,
|
||||
status=GroundTruthStatus.EXCLUDED,
|
||||
verified_scopes=(),
|
||||
exclusion_reason=GroundTruthExclusionReason.MISSING_HUMAN_JUDGMENT,
|
||||
)
|
||||
)
|
||||
with pytest.raises(GroundTruthReviewError, match="candidate|outcome|authority"):
|
||||
_encoded(candidate, ecosystem, forged)
|
||||
|
||||
|
||||
def test_projection_refuses_scope_substitution_and_deduplicates_equal_review() -> None:
|
||||
candidate, ecosystem, review = _review()
|
||||
forged = _rehash(replace(review, verified_scopes=(VerifiedScope("injection", "javascript"),)))
|
||||
kwargs = {
|
||||
"candidates_by_digest": {candidate.candidate_digest: candidate},
|
||||
"ecosystem_reviews_by_digest": {ecosystem.review_digest: ecosystem},
|
||||
"expected_ecosystem_policy_digest": ECOSYSTEM_POLICY,
|
||||
"expected_ground_truth_policy_digest": GROUND_TRUTH_POLICY,
|
||||
}
|
||||
with pytest.raises(GroundTruthReviewError, match="scope|outcome"):
|
||||
project_accepted_ground_truth((forged,), **kwargs)
|
||||
assert len(project_accepted_ground_truth((review, review), **kwargs)) == 1
|
||||
|
||||
|
||||
def test_conflicting_reviews_for_one_change_identity_fail_closed() -> None:
|
||||
candidate, ecosystem, review = _review()
|
||||
conflicting = review_ground_truth(
|
||||
candidate,
|
||||
ecosystem,
|
||||
_attestation(candidate, ecosystem, verified_scopes=(VerifiedScope("injection", "python"),)),
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
with pytest.raises(GroundTruthReviewError, match="conflicting"):
|
||||
project_accepted_ground_truth(
|
||||
(review, conflicting),
|
||||
candidates_by_digest={candidate.candidate_digest: candidate},
|
||||
ecosystem_reviews_by_digest={ecosystem.review_digest: ecosystem},
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
|
||||
|
||||
def test_projection_revalidates_complete_ecosystem_review_authority() -> None:
|
||||
canonical = "https://github.com/example/application"
|
||||
candidate = _candidate(network=build_network_identity(canonical))
|
||||
conflicting_candidate = _candidate(network=build_network_identity(canonical, aliases=("https://github.com/example/former",)))
|
||||
ecosystem = _ecosystem(candidate)
|
||||
conflicting_ecosystem = _ecosystem(conflicting_candidate)
|
||||
review = review_ground_truth(
|
||||
candidate,
|
||||
ecosystem,
|
||||
_attestation(candidate, ecosystem),
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
|
||||
with pytest.raises(GroundTruthReviewError, match="network|identity"):
|
||||
project_accepted_ground_truth(
|
||||
(review,),
|
||||
candidates_by_digest={
|
||||
candidate.candidate_digest: candidate,
|
||||
conflicting_candidate.candidate_digest: conflicting_candidate,
|
||||
},
|
||||
ecosystem_reviews_by_digest={
|
||||
ecosystem.review_digest: ecosystem,
|
||||
conflicting_ecosystem.review_digest: conflicting_ecosystem,
|
||||
},
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
Reference in New Issue
Block a user