fix(valid-detection-benchmark): normalize mixed-case .git suffixes in reservation identity

A reference such as OWNER/REPO.GIT escaped the reserved-identity check at both reservation sites because the suffix was stripped case-sensitively. One shared strip now handles the suffix case-insensitively; identities without a mixed-case suffix are byte-identical.
This commit is contained in:
Marius Ciepluch
2026-11-23 11:40:09 +01:00
parent f2093a5380
commit 7e6da2efae
4 changed files with 118 additions and 2 deletions
+2 -1
View File
@@ -23,6 +23,7 @@ from benchmarks.valid.reservation import (
census_bytes, census_bytes,
) )
from benchmarks.valid.reservation_guard_sources import PINNED_LEGACY_RESERVATION_SOURCE_DIGESTS from benchmarks.valid.reservation_guard_sources import PINNED_LEGACY_RESERVATION_SOURCE_DIGESTS
from benchmarks.valid.reservation_identity import strip_git_suffix
from openultrasast.core.canonical import JsonValue, content_digest from openultrasast.core.canonical import JsonValue, content_digest
GUARD_REPORT_SCHEMA = "openultrasast.valid-detection-benchmark.reservation-guard.v1" GUARD_REPORT_SCHEMA = "openultrasast.valid-detection-benchmark.reservation-guard.v1"
@@ -175,7 +176,7 @@ def _historical_paths(root: Path, withdrawal: HistoricalWithdrawalV1 | None) ->
def _repository_matches(content: bytes) -> tuple[str, ...]: def _repository_matches(content: bytes) -> tuple[str, ...]:
values = {match.group(1).decode("ascii").removesuffix(".git") for match in _REPOSITORY.finditer(content)} values = {strip_git_suffix(match.group(1).decode("ascii")) for match in _REPOSITORY.finditer(content)}
return tuple(sorted(f"https://github.com/{value}" for value in values)) return tuple(sorted(f"https://github.com/{value}" for value in values))
+9 -1
View File
@@ -11,6 +11,7 @@ from openultrasast.core.canonical import JsonValue, content_digest
NETWORK_SCHEMA = "openultrasast.valid-detection-benchmark.repository-network.v1" NETWORK_SCHEMA = "openultrasast.valid-detection-benchmark.repository-network.v1"
_DIGEST = re.compile(r"[0-9a-f]{64}") _DIGEST = re.compile(r"[0-9a-f]{64}")
_GIT_SUFFIX = ".git"
class ReservationError(ValueError): class ReservationError(ValueError):
@@ -52,6 +53,13 @@ def _require_digest(value: object, field: str) -> str:
return value return value
def strip_git_suffix(repository: str) -> str:
"""Drop a trailing provider clone suffix in any letter case, nothing else."""
if repository.lower().endswith(_GIT_SUFFIX):
return repository[: len(repository) - len(_GIT_SUFFIX)]
return repository
def _repository_locator(value: object) -> str: def _repository_locator(value: object) -> str:
text = _require_text(value, "repository locator") text = _require_text(value, "repository locator")
try: try:
@@ -67,7 +75,7 @@ def _repository_locator(value: object) -> str:
if len(parts) != 2: if len(parts) != 2:
raise ReservationError("repository locator must identify owner and repository") raise ReservationError("repository locator must identify owner and repository")
owner, repository = parts owner, repository = parts
repository = repository.removesuffix(".git") repository = strip_git_suffix(repository)
if not owner or not repository: if not owner or not repository:
raise ReservationError("repository locator must identify owner and repository") raise ReservationError("repository locator must identify owner and repository")
return f"https://{parsed.hostname.lower()}/{owner.lower()}/{repository.lower()}" return f"https://{parsed.hostname.lower()}/{owner.lower()}/{repository.lower()}"
+85
View File
@@ -250,3 +250,88 @@ def test_network_identity_is_deterministic_and_exact() -> None:
assert first == second assert first == second
with pytest.raises(reservation.ReservationError, match="digest"): with pytest.raises(reservation.ReservationError, match="digest"):
reservation.RepositoryIdentityResolver((replace(first, network_identity_digest="0" * 64),)) reservation.RepositoryIdentityResolver((replace(first, network_identity_digest="0" * 64),))
_MIXED_CASE_SUFFIXES = (".Git", ".gIT", ".giT", ".GIT")
_CORE_DIGEST = "730da0136fb50945132e8b281e7e28bfc0ce995fbcb2aa04fce24a816a79a0aa"
# (url, canonical member, network identity digest) captured from the unmodified
# identity function on 2026-11-23. None of these URLs carries a mixed-case ".git"
# suffix, so its identity must not move when suffix stripping becomes
# case-insensitive.
_UNCHANGED_IDENTITIES = (
("https://github.com/acme/core", "https://github.com/acme/core", _CORE_DIGEST),
("https://github.com/acme/core.git", "https://github.com/acme/core", _CORE_DIGEST),
("https://GITHUB.com/ACME/Core.git", "https://github.com/acme/core", _CORE_DIGEST),
(
"https://github.com/acme/widget.js",
"https://github.com/acme/widget.js",
"4bb775da3d25c77598ff4d984b3d1e854e4389d081c3f40fa4da3e3e0d9e2d20",
),
(
"https://github.com/acme/digest.git",
"https://github.com/acme/digest",
"c8a4e82de9913aae7c9a2b7d15b0d9d4fe6850fc92b549b37b357d4531537f18",
),
(
"https://github.com/acme/.gitlab",
"https://github.com/acme/.gitlab",
"7e713fdc633f805b965387050573427596967976e1209385d5f419bebbe1f097",
),
)
@pytest.mark.parametrize("suffix", _MIXED_CASE_SUFFIXES)
def test_mixed_case_git_suffix_shares_the_lowercase_identity(suffix: str) -> None:
plain = reservation.build_network_identity("https://github.com/acme/core")
resolver = reservation.RepositoryIdentityResolver((plain,))
legacy = reservation.legacy_reservation_source(resolver, repository=plain.canonical_repository, source_digest=LEGACY)
authority = reservation.ReservationAuthority(resolver, legacy_sources=(legacy,))
mixed_url = "https://GITHUB.com/ACME/Core" + suffix
mixed = reservation.build_network_identity(mixed_url)
assert mixed == plain
assert mixed.network_identity_digest == plain.network_identity_digest
assert resolver.resolve(mixed_url) == plain
assert authority.is_reserved(mixed_url, authority.current_census())
@pytest.mark.parametrize(
"url, canonical",
(
("https://github.com/acme/core.gitt", "https://github.com/acme/core.gitt"),
("https://github.com/acme/Core.GitHub", "https://github.com/acme/core.github"),
("https://github.com/acme/digi.t", "https://github.com/acme/digi.t"),
),
)
def test_only_the_git_suffix_is_stripped(url: str, canonical: str) -> None:
assert reservation.build_network_identity(url).canonical_repository == canonical
with pytest.raises(reservation.ReservationError, match="owner and repository"):
reservation.build_network_identity("https://GITHUB.com/ACME/.GIT")
@pytest.mark.parametrize("url, canonical, digest", _UNCHANGED_IDENTITIES)
def test_identity_without_a_mixed_case_suffix_is_byte_identical(url: str, canonical: str, digest: str) -> None:
network = reservation.build_network_identity(url)
assert (network.canonical_repository, network.network_identity_digest) == (canonical, digest)
def test_related_identity_without_a_mixed_case_suffix_is_byte_identical() -> None:
network = reservation.build_network_identity(
"https://GITHUB.com/ACME/Core.git",
aliases=("https://github.com/acme/core",),
former_names=("https://github.com/acme/old-core.git",),
redirects=("https://github.com/acme/redirected-core",),
forks=("https://github.com/Contributor/Core-Fork.git",),
)
assert network.members == (
"https://github.com/acme/core",
"https://github.com/acme/old-core",
"https://github.com/acme/redirected-core",
"https://github.com/contributor/core-fork",
)
assert network.network_identity_digest == "6b0e4a87982c368045a65c3ff1c0b3cba02b52d5d0d191aa98d38ba7c2667e36"
@@ -420,3 +420,25 @@ def test_partial_scope_report_and_replacement_acceptance_are_refused(tmp_path: P
historical_withdrawal=withdrawal, historical_withdrawal=withdrawal,
reservation_sources=(source,), reservation_sources=(source,),
) )
@pytest.mark.parametrize("suffix", (".Git", ".gIT", ".GIT"))
def test_mixed_case_git_suffix_reference_is_still_a_reserved_violation(tmp_path: Path, suffix: str) -> None:
_scan_roots(tmp_path)
authority, census, source = _authority(tmp_path)
(tmp_path / "src/leak.txt").write_text(f"see https://GITHUB.com/ACME/Reserved{suffix} for details\n")
report = reservation_guard.scan_permanent_reservations(
tmp_path,
scan_roots=("benchmarks", "src", "tests", "plane"),
authority=authority,
census=census,
reservation_sources=(source,),
)
assert report.files_read == 2 and report.bytes_read > 0 and report.reserved_network_count == 1
assert tuple((item.path, item.repository) for item in report.violations) == (("src/leak.txt", "https://github.com/ACME/Reserved"),)
reserved = authority.resolve_repository("https://github.com/acme/reserved")
assert report.violations[0].network_identity_digest == reserved.network_identity_digest
with pytest.raises(reservation_guard.ReservationGuardError, match="count=1"):
reservation_guard.require_clean_guard(report)