mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
107 lines
5.0 KiB
Python
107 lines
5.0 KiB
Python
"""Bind the reviewed nested Gradio wheel, source ZIP, lock and CycloneDX component."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import copy
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import tomllib
|
|
from urllib.parse import parse_qs, quote, unquote
|
|
import zipfile
|
|
|
|
if __package__:
|
|
from . import rebuild_gradio_backport as backport
|
|
else:
|
|
import rebuild_gradio_backport as backport
|
|
|
|
|
|
def is_gradio_component(component: dict) -> bool:
|
|
"""Count PyPI identity independently of a component's presentation name."""
|
|
names = [component.get("name", "")]
|
|
purl = re.match(r"(?i:pkg:pypi)/([^/@?#]+)(?:[@?#]|$)", component.get("purl", ""))
|
|
if purl:
|
|
names.append(unquote(purl[1]))
|
|
# PyPA name normalization; the name fallback also refuses malformed aliases.
|
|
return any(re.sub(r"[-_.]+", "-", name).lower() == "gradio" for name in names)
|
|
|
|
|
|
def bind(sbom: dict, artifact: Path, *, require_bound: bool = False) -> dict:
|
|
"""Validate every binding before adding the verified dependency digest."""
|
|
root = backport.VENDOR.parents[1]
|
|
spec = backport.manifest()
|
|
backport.verify(backport.VENDOR / spec["filename"])
|
|
paths = ["pyproject.toml", "uv.lock", *(
|
|
"vendor/gradio/" + name for name in
|
|
("manifest.json", "patch.json", "BACKPORT.txt", spec["filename"])
|
|
)]
|
|
with zipfile.ZipFile(artifact) as archive:
|
|
names = archive.namelist()
|
|
prefixes = {name.split("/", 1)[0] for name in names}
|
|
if len(prefixes) != 1 or len(names) != len(set(names)):
|
|
raise ValueError("source ZIP must have one root and no duplicate members")
|
|
prefix = prefixes.pop() + "/"
|
|
wheels = [name for name in names if name.lower().endswith(".whl")]
|
|
if wheels != [prefix + "vendor/gradio/" + spec["filename"]]:
|
|
raise ValueError("source ZIP contains an unreviewed or missing dependency wheel")
|
|
for path in paths:
|
|
if prefix + path not in names:
|
|
raise ValueError(f"source ZIP missing {path}")
|
|
if archive.read(prefix + path) != (root / path).read_bytes():
|
|
raise ValueError(f"source ZIP differs from reviewed checkout: {path}")
|
|
|
|
lock = tomllib.loads((root / "uv.lock").read_text())
|
|
packages = [p for p in lock["package"] if p["name"] == "gradio"]
|
|
if len(packages) != 1 or packages[0]["version"] != spec["version"]:
|
|
raise ValueError("lock must contain exactly the reviewed Gradio version")
|
|
package = packages[0]
|
|
url = package.get("source", {}).get("url", "")
|
|
pattern = (r"https://raw\.githubusercontent\.com/elder-plinius/OBLITERATUS/"
|
|
r"[0-9a-f]{40}/vendor/gradio/" + re.escape(quote(spec["filename"])))
|
|
if not re.fullmatch(pattern, url):
|
|
raise ValueError("Gradio URL must pin the first-party artifact commit")
|
|
digest = spec["wheel_sha256"]
|
|
if package.get("wheels") != [{"url": url, "hash": "sha256:" + digest}]:
|
|
raise ValueError("lock wheel URL/hash differs from reviewed manifest")
|
|
project = tomllib.loads((root / "pyproject.toml").read_text())["project"]
|
|
requirement = f"gradio @ {url}#sha256={digest}"
|
|
if project["optional-dependencies"]["spaces"] != [requirement]:
|
|
raise ValueError("spaces metadata must carry the same portable URL and digest")
|
|
|
|
if sbom.get("bomFormat") != "CycloneDX" or sbom.get("specVersion") != "1.5":
|
|
raise ValueError("expected CycloneDX 1.5")
|
|
components = [(i, c) for i, c in enumerate(sbom.get("components", []))
|
|
if is_gradio_component(c)]
|
|
if len(components) != 1 or components[0][1].get("version") != spec["version"]:
|
|
raise ValueError("SBOM must contain exactly the reviewed Gradio component")
|
|
index, component = components[0]
|
|
purl, separator, query = component.get("purl", "").partition("?")
|
|
if (not separator or unquote(purl) != f"pkg:pypi/gradio@{spec['version']}"
|
|
or parse_qs(query, strict_parsing=True) != {"download_url": [url]}):
|
|
raise ValueError("SBOM Gradio download URL differs from lock")
|
|
hashes = [{"alg": "SHA-256", "content": digest}]
|
|
if (require_bound or "hashes" in component) and component.get("hashes") != hashes:
|
|
raise ValueError("SBOM Gradio digest missing or different")
|
|
# Do not mutate or write partial evidence on a failed check.
|
|
result = copy.deepcopy(sbom)
|
|
result["components"][index]["hashes"] = hashes
|
|
return result
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--input", type=Path, required=True)
|
|
parser.add_argument("--artifact", type=Path, required=True)
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
parser.add_argument("--require-bound", action="store_true")
|
|
args = parser.parse_args()
|
|
result = bind(json.loads(args.input.read_text()), args.artifact,
|
|
require_bound=args.require_bound)
|
|
with args.output.open("x") as stream:
|
|
stream.write(json.dumps(result, indent=2) + "\n")
|
|
print("verified embedded Gradio wheel/manifest/license, lock URL/hash and SBOM component")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|