mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
204 lines
8.9 KiB
Python
204 lines
8.9 KiB
Python
"""Reject broken nested dependency provenance before producing release evidence."""
|
|
import copy
|
|
import io
|
|
import json
|
|
from urllib.parse import quote
|
|
import zipfile
|
|
|
|
import pytest
|
|
|
|
from scripts import rebuild_gradio_backport as backport
|
|
from scripts import verify_gradio_release as release
|
|
|
|
|
|
@pytest.fixture
|
|
def payload(tmp_path, monkeypatch):
|
|
root = tmp_path / "checkout"
|
|
vendor = root / "vendor/gradio"
|
|
vendor.mkdir(parents=True)
|
|
spec = backport.manifest()
|
|
for name in ("patch.json", "BACKPORT.txt"):
|
|
(vendor / name).write_bytes((backport.VENDOR / name).read_bytes())
|
|
prefix = f"gradio-{spec['version']}.dist-info/"
|
|
with zipfile.ZipFile(backport.VENDOR / spec["filename"]) as original:
|
|
members = {prefix + name: original.read(prefix + name)
|
|
for name in ("licenses/LICENSE", "BACKPORT.txt", "METADATA")}
|
|
members[prefix + "RECORD"] = backport.record_bytes(members, prefix + "RECORD")
|
|
stream = io.BytesIO()
|
|
with zipfile.ZipFile(stream, "w") as archive:
|
|
for name, data in members.items():
|
|
archive.writestr(name, data)
|
|
wheel = stream.getvalue()
|
|
(vendor / spec["filename"]).write_bytes(wheel)
|
|
spec.update(wheel_sha256=backport.sha256(wheel), wheel_bytes=len(wheel))
|
|
(vendor / "manifest.json").write_text(json.dumps(spec))
|
|
url = ("https://raw.githubusercontent.com/elder-plinius/OBLITERATUS/" + "a" * 40
|
|
+ "/vendor/gradio/" + quote(spec["filename"]))
|
|
requirement = f"gradio @ {url}#sha256={spec['wheel_sha256']}"
|
|
(root / "pyproject.toml").write_text(
|
|
'[project.optional-dependencies]\nspaces = [' + json.dumps(requirement) + ']\n')
|
|
(root / "uv.lock").write_text(
|
|
f'[[package]]\nname = "gradio"\nversion = "{spec["version"]}"\n'
|
|
f'source = {{ url = "{url}" }}\n'
|
|
f'wheels = [{{ url = "{url}", hash = "sha256:{spec["wheel_sha256"]}" }}]\n')
|
|
sbom = {"bomFormat": "CycloneDX", "specVersion": "1.5", "components": [{
|
|
"type": "library", "name": "gradio", "version": spec["version"],
|
|
"purl": f"pkg:pypi/gradio@{quote(spec['version'])}?download_url={quote(url, safe=':/')}"
|
|
}]}
|
|
monkeypatch.setattr(backport, "VENDOR", vendor)
|
|
|
|
def snapshot(*, omit=None, corrupt=None, duplicate=False):
|
|
artifact = tmp_path / "source.zip"
|
|
with zipfile.ZipFile(artifact, "w") as archive:
|
|
for path in root.rglob("*"):
|
|
if path.is_file():
|
|
relative = path.relative_to(root).as_posix()
|
|
if relative == omit:
|
|
continue
|
|
data = path.read_bytes() + (b"tampered" if relative == corrupt else b"")
|
|
archive.writestr("source/" + relative, data)
|
|
if duplicate and relative == "uv.lock":
|
|
with pytest.warns(UserWarning, match="Duplicate name"):
|
|
archive.writestr("source/" + relative, data)
|
|
return artifact
|
|
|
|
return root, spec, url, sbom, snapshot
|
|
|
|
|
|
def test_binds_verified_nested_wheel_and_reverifies_without_mutating_input(payload):
|
|
_, spec, _, sbom, snapshot = payload
|
|
original = copy.deepcopy(sbom)
|
|
artifact = snapshot()
|
|
bound = release.bind(sbom, artifact)
|
|
assert sbom == original
|
|
assert bound["components"][0]["hashes"] == [{"alg": "SHA-256", "content": spec["wheel_sha256"]}]
|
|
assert release.bind(bound, artifact, require_bound=True) == bound
|
|
|
|
|
|
@pytest.mark.parametrize("path", ["uv.lock", "pyproject.toml", "vendor/gradio/manifest.json",
|
|
"vendor/gradio/patch.json", "vendor/gradio/BACKPORT.txt", "wheel"])
|
|
@pytest.mark.parametrize("operation", ["omit", "corrupt"])
|
|
def test_source_archive_missing_or_mismatched_payload_is_rejected(payload, path, operation):
|
|
_, spec, _, sbom, snapshot = payload
|
|
if path == "wheel":
|
|
path = "vendor/gradio/" + spec["filename"]
|
|
with pytest.raises(ValueError, match="source ZIP"):
|
|
release.bind(sbom, snapshot(**{operation: path}))
|
|
|
|
|
|
def test_duplicate_archive_member_is_rejected(payload):
|
|
_, _, _, sbom, snapshot = payload
|
|
with pytest.raises(ValueError, match="duplicate"):
|
|
release.bind(sbom, snapshot(duplicate=True))
|
|
|
|
|
|
def test_additional_nested_wheel_is_not_covered_by_the_payload_exception(payload):
|
|
_, _, _, sbom, snapshot = payload
|
|
artifact = snapshot()
|
|
with zipfile.ZipFile(artifact, "a") as archive:
|
|
archive.writestr("source/vendor/unreviewed.WHL", b"unreviewed bytes")
|
|
with pytest.raises(ValueError, match="unreviewed"):
|
|
release.bind(sbom, artifact)
|
|
|
|
|
|
@pytest.mark.parametrize("corruption", ["missing", "duplicate", "version", "url", "hash", "absent-hash"])
|
|
def test_component_identity_and_existing_digest_must_match(payload, corruption):
|
|
_, _, _, sbom, snapshot = payload
|
|
component = sbom["components"][0]
|
|
if corruption == "missing":
|
|
sbom["components"] = []
|
|
elif corruption == "duplicate":
|
|
sbom["components"].append(copy.deepcopy(component))
|
|
elif corruption == "version":
|
|
component["version"] = "6.31.0"
|
|
elif corruption == "url":
|
|
component["purl"] = component["purl"].replace("elder-plinius", "other-owner")
|
|
elif corruption == "hash":
|
|
component["hashes"] = [{"alg": "SHA-256", "content": "0" * 64}]
|
|
before = copy.deepcopy(sbom)
|
|
with pytest.raises(ValueError, match="SBOM"):
|
|
release.bind(sbom, snapshot(), require_bound=corruption == "absent-hash")
|
|
assert sbom == before
|
|
|
|
|
|
@pytest.mark.parametrize("display_name", ["Gradio", "GRADIO", "downstream image component"])
|
|
@pytest.mark.parametrize("purl_name", ["gradio", "GrAdIo", "%67radio"])
|
|
@pytest.mark.parametrize("require_bound", [False, True])
|
|
def test_duplicate_pypi_identity_cannot_hide_behind_a_display_alias(
|
|
payload, display_name, purl_name, require_bound
|
|
):
|
|
_, _, _, sbom, snapshot = payload
|
|
artifact = snapshot()
|
|
bound = release.bind(sbom, artifact)
|
|
duplicate = copy.deepcopy(bound["components"][0])
|
|
duplicate.update(name=display_name, **{"bom-ref": "conflicting-alias"})
|
|
duplicate["purl"] = duplicate["purl"].replace("pypi/gradio@", f"pypi/{purl_name}@")
|
|
duplicate["hashes"] = [{"alg": "SHA-256", "content": "0" * 64}]
|
|
bound["components"].append(duplicate)
|
|
original = copy.deepcopy(bound)
|
|
with pytest.raises(ValueError, match="exactly.*Gradio"):
|
|
release.bind(bound, artifact, require_bound=require_bound)
|
|
assert bound == original
|
|
|
|
|
|
def test_single_pypi_identity_with_case_alias_gets_the_verified_digest(payload):
|
|
_, spec, _, sbom, snapshot = payload
|
|
sbom["components"][0]["name"] = "Gradio"
|
|
original = copy.deepcopy(sbom)
|
|
artifact = snapshot()
|
|
bound = release.bind(sbom, artifact)
|
|
assert sbom == original
|
|
assert bound["components"][0]["name"] == "Gradio"
|
|
assert bound["components"][0]["hashes"] == [{"alg": "SHA-256", "content": spec["wheel_sha256"]}]
|
|
assert release.bind(bound, artifact, require_bound=True) == bound
|
|
|
|
|
|
def test_distinct_gradio_client_identity_is_not_a_duplicate(payload):
|
|
_, _, _, sbom, snapshot = payload
|
|
unrelated = {"name": "gradio-client", "version": "2.7.2", "purl": "pkg:pypi/gradio-client@2.7.2"}
|
|
sbom["components"].append(unrelated)
|
|
bound = release.bind(sbom, snapshot())
|
|
assert bound["components"][1] == unrelated
|
|
|
|
|
|
@pytest.mark.parametrize("target,old,new", [
|
|
("uv.lock", "a" * 40, "main"),
|
|
("uv.lock", "elder-plinius", "other-owner"),
|
|
("uv.lock", "sha256:", "sha512:"),
|
|
("uv.lock", 'version = "6.31.0+obliteratus.1"', 'version = "6.31.0"'),
|
|
("pyproject.toml", "#sha256=", "#sha512="),
|
|
])
|
|
def test_lock_and_pip_metadata_cannot_disagree_with_manifest(payload, target, old, new):
|
|
root, _, _, sbom, snapshot = payload
|
|
path = root / target
|
|
path.write_text(path.read_text().replace(old, new))
|
|
with pytest.raises(ValueError, match="lock|metadata|commit"):
|
|
release.bind(sbom, snapshot())
|
|
|
|
|
|
def test_cli_failure_does_not_create_output(payload, tmp_path, monkeypatch):
|
|
_, _, _, sbom, snapshot = payload
|
|
input_path = tmp_path / "sbom.json"
|
|
input_path.write_text(json.dumps(sbom))
|
|
output = tmp_path / "refused.json"
|
|
monkeypatch.setattr("sys.argv", ["verify_gradio_release", "--input", str(input_path),
|
|
"--artifact", str(snapshot()), "--output", str(output),
|
|
"--require-bound"])
|
|
with pytest.raises(ValueError, match="digest missing"):
|
|
release.main()
|
|
assert not output.exists()
|
|
|
|
|
|
def test_cli_success_never_overwrites_retained_evidence(payload, tmp_path, monkeypatch):
|
|
_, _, _, sbom, snapshot = payload
|
|
input_path = tmp_path / "sbom.json"
|
|
input_path.write_text(json.dumps(sbom))
|
|
output = tmp_path / "retained.json"
|
|
monkeypatch.setattr("sys.argv", ["verify_gradio_release", "--input", str(input_path),
|
|
"--artifact", str(snapshot()), "--output", str(output)])
|
|
release.main()
|
|
retained = output.read_bytes()
|
|
with pytest.raises(FileExistsError):
|
|
release.main()
|
|
assert output.read_bytes() == retained
|