From 0edc6f4e26880a40a48380ed158e68387216878c Mon Sep 17 00:00:00 2001 From: luisleo526 Date: Thu, 29 Oct 2026 06:40:43 +0800 Subject: [PATCH 1/2] release: attach the verified producer archives to the hub release At finalization, fetch the codegen npm tarball, the codegen PyPI sdist and both Linux engine tarballs from their producers, verify each against the producer's own authority (registry integrity, the version API's sha256 and the candidate's sdist digest, the companion checksum asset) before anything is uploaded, attach the original bytes to the hub release, read them back, and list them in release-manifest.json under a new `assets` key (name, size, sha256, source), sorted by name. The manifest reader accepts exactly the legacy eight keys or those plus a validated `assets`; existing manifests stay valid and are not rewritten. The dispatch payload still carries exactly its ten fields (the original eight manifest fields, event_id and the manifest reference), and both hashes still cover the raw manifest bytes including `assets`. Receipts and their evidence are unchanged. A separate binary writer refuses to overwrite an existing attachment with different bytes and checks size and sha256 on read-back; replay of an extended manifest verifies the attachments without resolving the producers again. docs/release-chain.md documents the new key and the reader rules. Co-Authored-By: Claude Sonnet 5.5 --- docs/release-chain.md | 58 +- scripts/release_artifacts.py | 40 +- scripts/release_contract.py | 61 +- scripts/release_pipeline.py | 44 +- .../fixtures/release-pair-v1.6.3/pair-body.jq | 2 +- tests/test_release_assets.py | 693 ++++++++++++++++++ tests/test_release_binding.py | 93 ++- tests/test_release_binding_workflows.py | 36 +- tests/test_release_receipts.py | 73 ++ tools/release-dry-run/ready_replay.py | 3 +- 10 files changed, 1078 insertions(+), 25 deletions(-) create mode 100644 tests/test_release_assets.py diff --git a/docs/release-chain.md b/docs/release-chain.md index 1a48e3c..0e48dd4 100644 --- a/docs/release-chain.md +++ b/docs/release-chain.md @@ -139,6 +139,23 @@ pending and creates/updates a neutral incident. All other failures are errors. through its existing evidence/PR process; this hub never fabricates evidence or receives private registry credentials. +For a candidate without a stored manifest, finalization stages the original +outer npm tarball, codegen PyPI sdist and both Linux engine tarballs. Filenames +and versions come from the candidate pins and selected producer URLs. It checks +the npm registry's mandatory SHA-512 integrity, PyPI's version-specific sdist +SHA-256 and the candidate's sdist digest, and each engine archive's mandatory +companion `.sha256` with its filename binding. Refetched producer commits must +equal the candidate. Any supplied GitHub asset digest must also agree. +All producer verification and facts binding finish before the first upload. +Public npm/PyPI downloads remain anonymous and use the existing fixed origins; +the hub token is used only for the authenticated GitHub API and upload origins. + +The separate binary writer attaches those four compressed files without +unpacking/repacking them. It reads each upload back and checks its size and +SHA-256, including when GitHub supplies no digest. Identical existing bytes are +reused; conflicting bytes are never overwritten. A partial upload leaves the +draft and its attachments for retry, with no manifest or authorized promotion. +Missing producer checksums stay pending; corruption fails finalization. Only then does finalization attach `release-manifest.json`, assign the fixed version image tag, move eligible aliases, publish the GitHub release, and fan out. Alias decisions and delivery reservations share job-level `release-finalize` @@ -150,7 +167,7 @@ The previous alias digests are preserved before the first promotion. ## Exact ready event contract The `offline`, `hosted`, `application`, `web`, `data` and `license` event type remains -**`pineforge-release`**, with unchanged payload bytes. Its ten payload fields are +**`pineforge-release`**. Its ten payload fields remain exactly section 3.2 of the design: `schema`, `event_id`, `release_version`, `prerelease`, `hub_commit`, `versions`, `image`, `facts`, `manifest`, `run_id`. The `compute` role uses **`release-pair`**, described below. @@ -163,12 +180,49 @@ Fixtures for consumer implementations: - [Strict validator](../scripts/release_contract.py) Fixture identities are synthetic, never live release evidence. The manifest is -UTF-8 JSON with sorted keys, compact separators and one final LF. It contains +UTF-8 JSON with sorted keys, compact separators and one final LF. Its legacy form contains the eight fields above other than `event_id` and `manifest`. Both omitted fields contain its SHA-256; excluding them avoids a circular hash. `run_id` is the original candidate build run, a JSON integer. Replays fetch the existing bytes; attempt IDs occur only in receipts. HPO is explicitly null before the cutoff. +New manifests also contain exactly one additional root field, `assets`: an +array sorted by unique `name`, with exactly four entries: one npm outer tarball, +one codegen sdist and engine Linux x86_64 and aarch64 tarballs. Engine names are +`pineforge-v-linux-.tar.gz`; npm and sdist names +are the selected producer URL basenames. Each entry has exactly these fields: + +| Asset field | Type and meaning | +|---|---| +| `name` | Safe original producer basename, starting with an ASCII letter or digit and otherwise containing only letters, digits, `_`, `.`, `-`; audit namespaces `release-`, `consumer-`, `facts-`, `delivery-` are reserved. | +| `size` | Positive JSON integer, at most 9007199254740991, never a boolean; length of the original compressed bytes. | +| `sha256` | Exactly 64 lowercase hexadecimal characters, computed after producer-authority verification. | +| `source` | Selected HTTPS producer download URL with the same basename: the fixed engine repository/tag on `github.com`, the `@pineforge/codegen-pyodide/-/` package path on `registry.npmjs.org`, or `files.pythonhosted.org` for the codegen sdist. No credentials, query, fragment, traversal or encoded path is accepted. | + +Consumers fetch mirrors by `name` from the matching hub release and verify +`size` and `sha256`. `source` records provenance and grants no permission to +fetch an arbitrary URL. + +Readers accept exactly the legacy eight root keys or that set plus validated +`assets`, retaining the existing schema and nested fields. The ready payload +projects only the original eight fields and adds `event_id` and `manifest`; +`assets` is obtained from the manifest, never an eleventh dispatch field. Both +hashes cover the entire original raw manifest, including `assets`. Readers +that enforce eight manifest keys, echo every manifest field into the payload, +or drop unknown keys before hashing need this update. Readers that ignore the +extension must still hash the original bytes and compare all ten payload fields. +Consumer implementations and release-record readers must confirm compatibility +before consuming newly extended releases. + +Once a manifest is stored, finalization checks its mirrored hub attachments +against every descriptor before promotion or delivery planning, without resolving +producers or newer facts again. A missing or corrupt attachment blocks replay. +Legacy manifests remain valid and are never extended or rewritten on replay; +their raw bytes, event IDs and original build run IDs remain unchanged. Receipt +fields, signatures, evidence hashes and stored receipt bytes are unchanged. +Producer binaries are release attachments; consumer receipt ZIPs still contain +only `release-receipt.json`. + `image.repository` must equal EXACTLY one of two values, the image path before and after the GitHub organization rename: `ghcr.io/pineforge-4pass/pineforge-release` or `ghcr.io/tunaxis/pineforge-release`. diff --git a/scripts/release_artifacts.py b/scripts/release_artifacts.py index 6902ff3..c693299 100644 --- a/scripts/release_artifacts.py +++ b/scripts/release_artifacts.py @@ -17,7 +17,7 @@ import release_pair as pair from release_contract import (COMMIT, Invalid, Pending, canonical, decode, - match, require, sha256) + match, require, sha256, validate_assets) PRODUCERS = {"engine": "pineforge-4pass/pineforge-engine", "codegen": "pineforge-4pass/pineforge-codegen-oss", @@ -219,7 +219,13 @@ def source_matches(client, component, commit, sources, native=None): require(expected_native == {name: git_blob(raw) for name, raw in native.items()}, "HPO native sdist differs from tagged source") -def producer(client, component, version): +def attachment(raw, source): + """Describe the original compressed bytes, only after producer verification.""" + return ({"name": urllib.parse.urlsplit(source).path.rsplit("/", 1)[-1], + "size": len(raw), "sha256": sha256(raw), "source": source}, raw) + + +def producer(client, component, version, *, attachments=None): """Return manifest pin + byte inventory, after checking even the event's own artifacts.""" v = pair.parse(pair.hpo_version(version, to_semver=True) if component == "hpo" else version) obj, commit = release(client, component, str(v)) @@ -235,9 +241,15 @@ def producer(client, component, version): for triple in triples: name = f"pineforge-v{v}-{triple}.tar.gz" raw = asset(client, obj, name, repo) - checksum = asset(client, obj, name + ".sha256", repo).decode("ascii").split() - require(len(checksum) == 2 and checksum[0] == sha256(raw) and checksum[1].lstrip("*") == name, + checksum_raw = asset(client, obj, name + ".sha256", repo) + try: + checksum = checksum_raw.decode("ascii").split() + except UnicodeError: + raise Invalid("engine tarball checksum differs") from None + require(len(checksum) == 2 and checksum[0] == sha256(raw) and checksum[1] in (name, "*" + name), "engine tarball checksum differs") + if attachments is not None and triple.startswith("linux-"): + attachments.append(attachment(raw, f"https://github.com/{repo}/releases/download/{obj['tag_name']}/{name}")) return pin, inventory if component == "codegen": meta = decode(client.download(f"https://pypi.org/pypi/pineforge-codegen/{v.pep440}/json", {"pypi.org"})) @@ -276,10 +288,12 @@ def producer(client, component, version): pin.update(wheel_sha256=sha256(wheel), sdist_sha256=sha256(sdist)) source_matches(client, component, commit, sources, native) inventory.update({name: sha256(raw) for name, raw in sources.items()}) + if component == "codegen" and attachments is not None: + attachments.append(attachment(sdist, sdists[0]["url"])) return pin, inventory -def npm(client, pin, inventory): +def npm(client, pin, inventory, *, attachments=None): v = pin["version"] obj = decode(client.download("https://registry.npmjs.org/@pineforge%2fcodegen-pyodide/" + v, {"registry.npmjs.org"})) require(obj["name"] == "@pineforge/codegen-pyodide" and obj["version"] == v, "unexpected npm identity") @@ -299,9 +313,25 @@ def npm(client, pin, inventory): require({name: sha256(data) for name, data in sources.items()} == inventory, "npm and PyPI codegen sources differ") if obj.get("gitHead"): require(obj["gitHead"] == pin["commit"], "npm git commit differs") + if attachments is not None: + attachments.append(attachment(raw, obj["dist"]["tarball"])) return {**pin, "npm_version": v, "npm_integrity": integrity} +def verified_assets(client, pins, inventory): + """Stage every mirror from candidate pins; never upload or resolve latest.""" + staged = [] + for role in ("engine", "codegen"): + resolved, _ = producer(client, role, pins[role]["version"], attachments=staged) + # Codegen includes the original candidate's sdist digest. A newly + # published API digest cannot authorize replacement of the built bytes. + require(resolved == pins[role], role + " producer identity differs from candidate") + ready = {**pins, "codegen": npm(client, pins["codegen"], inventory, attachments=staged)} + staged.sort(key=lambda item: item[0]["name"]) + validate_assets([descriptor for descriptor, _ in staged], ready) + return ready, staged + + def command(*args, input=None): result = subprocess.run(args, input=input, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) require(result.returncode == 0, "release command failed: " + args[0]) diff --git a/scripts/release_contract.py b/scripts/release_contract.py index d3f3fc4..f4a1c8a 100644 --- a/scripts/release_contract.py +++ b/scripts/release_contract.py @@ -10,6 +10,7 @@ import hashlib import json import re +from urllib.parse import urlsplit import release_pair as pair from release_sources import RELEASE_TAG @@ -95,8 +96,61 @@ def version(value): return v +def asset_descriptor(obj, versions): + """Validate one producer filename and its provenance; return its required kind.""" + keys(obj, ("name", "size", "sha256", "source")) + name = match(r"[A-Za-z0-9][A-Za-z0-9_.-]*", obj["name"]) + require(not name.startswith(("release-", "consumer-", "facts-", "delivery-")), + "reserved attachment name") + positive(obj["size"]) + match(SHA, obj["sha256"]) + source = obj["source"] + require(isinstance(source, str) and source.isascii() + and not any(c.isspace() or ord(c) < 32 or ord(c) == 127 for c in source), + "unsafe attachment source") + try: + parsed = urlsplit(source) + except ValueError: + raise Invalid("unsafe attachment source") from None + require(parsed.scheme == "https" and parsed.netloc in + ("github.com", "registry.npmjs.org", "files.pythonhosted.org") + and not parsed.query and not parsed.fragment + and source == "https://" + parsed.netloc + parsed.path, + "unexpected attachment origin") + parts = parsed.path.split("/") + require(parts[0] == "" and all(part not in ("", ".", "..") for part in parts[1:]) + and re.fullmatch(r"/[A-Za-z0-9_@./-]+", parsed.path) is not None + and parts[-1] == name, "unsafe attachment path") + if parsed.netloc == "registry.npmjs.org": + require(parsed.path == "/@pineforge/codegen-pyodide/-/" + name + and name.endswith((".tgz", ".tar.gz")), "unexpected npm attachment") + return "npm" + if parsed.netloc == "files.pythonhosted.org": + require(name.endswith(".tar.gz"), "unexpected sdist attachment") + return "sdist" + engine = versions["engine"] + for arch in ("x86_64", "aarch64"): + expected = f"pineforge-v{engine['version']}-linux-{arch}.tar.gz" + if name == expected: + require(source == f"https://github.com/pineforge-4pass/pineforge-engine/releases/download/{engine['tag']}/{name}", + "unexpected engine attachment source") + return arch + raise Invalid("unexpected engine attachment") + + +def validate_assets(assets, versions): + require(type(assets) is list and len(assets) == 4, "incomplete attachment inventory") + kinds = [asset_descriptor(obj, versions) for obj in assets] + names = [obj["name"] for obj in assets] + require(len(set(names)) == len(names) and names == sorted(names), + "attachment names must be unique and sorted") + require(set(kinds) == {"npm", "sdist", "x86_64", "aarch64"}, "unexpected attachment inventory") + return assets + + def validate(manifest): - keys(manifest, MANIFEST_KEYS) + require(type(manifest) is dict and set(manifest) in (MANIFEST_KEYS, MANIFEST_KEYS | {"assets"}), + "unexpected object fields") require(manifest["schema"] == SCHEMA, "unsupported release schema") v = version(manifest["release_version"]) require(type(manifest["prerelease"]) is bool and manifest["prerelease"] == v.prerelease, @@ -142,6 +196,8 @@ def validate(manifest): match(COMMIT, facts["commit"]) match(SHA, facts["sha256"]) require(facts["path"] == "facts/facts.json" and facts["release_key"] == str(v), "unexpected facts identity") + if "assets" in manifest: + validate_assets(manifest["assets"], versions) return manifest @@ -149,7 +205,8 @@ def payload(raw): manifest = validate(decode(raw)) require(canonical(manifest) == raw, "noncanonical manifest bytes") digest = sha256(raw) - return {**manifest, "event_id": "sha256:" + digest, "manifest": {"asset": ASSET, "sha256": digest}} + return {**{key: manifest[key] for key in MANIFEST_KEYS}, + "event_id": "sha256:" + digest, "manifest": {"asset": ASSET, "sha256": digest}} def validate_candidate(obj): diff --git a/scripts/release_pipeline.py b/scripts/release_pipeline.py index 72dfc42..55fa8f7 100644 --- a/scripts/release_pipeline.py +++ b/scripts/release_pipeline.py @@ -16,10 +16,10 @@ from urllib.parse import quote import release_pair as pair -from release_artifacts import Client, PRODUCERS, SafeRedirect, command, npm, producer +from release_artifacts import Client, PRODUCERS, SafeRedirect, command, producer, verified_assets from release_contract import (ASSET, CANDIDATE, COMMIT, DELIVERY_ONLY, EVENT_TYPES, HUB, IMAGE, RECEIPT_ROLES, ROLES, SCHEMA, SHA, Invalid, - Pending, bind_facts, canonical, decode, keys, match, + Pending, asset_descriptor, bind_facts, canonical, decode, keys, match, pair_payload, payload, positive, require, sha256, validate, validate_candidate, version) @@ -100,6 +100,36 @@ def put(self, release, name, raw): method="POST", data=raw, github=True, binary=True) require(self.read(release, name) == raw, "hub asset upload readback differs") + @staticmethod + def check_binary(raw, descriptor): + require(type(raw) is bytes and len(raw) == descriptor["size"], "hub attachment size differs") + require(sha256(raw) == descriptor["sha256"], "hub attachment sha256 differs") + + def verify_binary(self, release, descriptor, versions): + asset_descriptor(descriptor, versions) + try: + raw = self.read(release, descriptor["name"]) + except Pending: + raise Invalid("hub attachment unavailable") from None + self.check_binary(raw, descriptor) + return raw + + def put_binary(self, release, descriptor, raw, versions): + """Append only a verified producer archive; never widen the JSON writer.""" + asset_descriptor(descriptor, versions) + self.check_binary(raw, descriptor) + name = descriptor["name"] + try: + previous = self.read(release, name) + except Pending: + raise Invalid("hub attachment unavailable") from None + if previous is not None: + require(previous == raw, "immutable hub attachment already exists with different bytes") + return + self.client.request(f"https://uploads.github.com/repos/{HUB}/releases/{release['id']}/assets?name={quote(name)}", + method="POST", data=raw, github=True, binary=True) + self.verify_binary(release, descriptor, versions) + def ensure_candidate_release(self, tag, notes): try: return self.get(tag) @@ -223,15 +253,17 @@ def finalize(client, store, registry, row): require(client.tag_commit(HUB, row["tag_name"]) == candidate["hub_commit"], "hub tag source changed") raw = store.read(row, ASSET) if raw is None: - pins = dict(candidate["versions"]) - pins["codegen"] = npm(client, pins["codegen"], candidate["inventories"]["codegen"]) + pins, staged = verified_assets(client, candidate["versions"], candidate["inventories"]["codegen"]) facts_commit = client.api(f"repos/{HUB}/git/ref/heads/main")["object"]["sha"] facts_raw = client.file(HUB, facts_commit, "facts/facts.json") facts = bind_facts(facts_raw, facts_commit, str(v), pins) manifest = validate({"schema": SCHEMA, "release_version": str(v), "prerelease": v.prerelease, "hub_commit": candidate["hub_commit"], "versions": pins, "image": candidate["image"], - "facts": facts, "run_id": candidate["run_id"]}) + "facts": facts, "run_id": candidate["run_id"], + "assets": [descriptor for descriptor, _ in staged]}) raw = canonical(manifest) + for descriptor, data in staged: + store.put_binary(row, descriptor, data, pins) store.put(row, ASSET, raw) event = payload(raw) require(event["image"] == candidate["image"] and event["hub_commit"] == candidate["hub_commit"], @@ -242,6 +274,8 @@ def finalize(client, store, registry, row): if role == "codegen": actual = {key: value for key, value in actual.items() if key not in ("npm_version", "npm_integrity")} require(actual == expected, "manifest producer identity differs from candidate") + for descriptor in decode(raw).get("assets", []): + store.verify_binary(row, descriptor, event["versions"]) # An immutable manifest is the commit point. Every step below can replay. registry.point(str(v), event["image"]["digest"], fixed=True) # Preserve the existing fixed alias formats, but only after readiness. diff --git a/tests/fixtures/release-pair-v1.6.3/pair-body.jq b/tests/fixtures/release-pair-v1.6.3/pair-body.jq index f2e9c97..e859ef1 100644 --- a/tests/fixtures/release-pair-v1.6.3/pair-body.jq +++ b/tests/fixtures/release-pair-v1.6.3/pair-body.jq @@ -1,5 +1,5 @@ # Rebuilds the repository_dispatch body of the hub's release-pair send from the PUBLIC hub release assets, -# field by field as scripts/release_contract.py pair_payload() (hub main 692cbea792, lines 174-206) and +# field by field as scripts/release_contract.py::pair_payload() and # scripts/release_pipeline.py dispatch (event_type + client_payload) do. Inputs: --slurpfile m release-manifest.json, # --slurpfile c release-candidate.json, --arg msha sha256(manifest bytes), --arg csha sha256(candidate bytes). ($m[0]) as $manifest diff --git a/tests/test_release_assets.py b/tests/test_release_assets.py new file mode 100644 index 0000000..f8d0ac9 --- /dev/null +++ b/tests/test_release_assets.py @@ -0,0 +1,693 @@ +#!/usr/bin/env python3 +"""Verified attachment regressions for hub CI; all HTTP is an in-memory fixture. + +Authorities are captured before mutations. Removing a digest gate must let its +isolated negative input through, or cause a forbidden upload before refusal. +""" +from __future__ import annotations + +import base64 +import copy +import hashlib +import io +import json +import os +import sys +import tempfile +import unittest +import urllib.error +from pathlib import Path +from unittest.mock import patch +from urllib.parse import parse_qs, urlsplit + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "scripts")) +sys.path.insert(0, str(ROOT / "tests")) +import release_artifacts as artifacts # noqa: E402 +import release_contract as contract # noqa: E402 +import release_pipeline as pipeline # noqa: E402 +from test_release_binding import (MemoryRegistry, ProducerFixture, archive, # noqa: E402 + candidate, facts_for, fixture_manifest) + + +class AttachmentHTTP: + """Serve the real Client, including origin, auth, API and readback handling.""" + def __init__(self): + self.producers = ProducerFixture((ROOT / "VERSION").read_text().strip()) + self.manifest = fixture_manifest(str(self.producers.v)) + self.pypi_url = f"https://pypi.org/pypi/pineforge-codegen/{self.producers.v.pep440}/json" + self.npm_meta_url = "https://registry.npmjs.org/@pineforge%2fcodegen-pyodide/" + str(self.producers.v) + pypi = json.loads(self.producers.downloads[self.pypi_url]) + self.sdist_url = pypi["urls"][0]["url"] + self.manifest["versions"]["codegen"]["sdist_sha256"] = pypi["urls"][0]["digests"]["sha256"] + self.manifest["versions"]["codegen"]["npm_integrity"] = json.loads( + self.producers.downloads[self.npm_meta_url])["dist"]["integrity"] + self.built = candidate(self.manifest) + self.built["inventories"]["codegen"] = { + "pineforge_codegen/__init__.py": hashlib.sha256(b"VERSION='fixture'\n").hexdigest()} + self.tag = "v" + self.manifest["release_version"] + self.facts_raw = contract.canonical(facts_for(self.manifest)) + self.row = {"id": 1, "tag_name": self.tag, "draft": True, + "prerelease": self.manifest["prerelease"], + "created_at": "2020-01-01T00:00:00Z", "assets": []} + self.data, self.requests, self.uploads, self.publications = {}, [], [], [] + self.readback_problem = None + self.unavailable_reads = set() + self.fail_upload_number = None + self.forbid_producers = False + self.add_hub_asset(contract.CANDIDATE, contract.canonical(self.built)) + urls = [self.producers.npm_url, self.sdist_url] + urls += [a["browser_download_url"] for a in self.producers.releases["engine"]["assets"] + if a["name"].endswith(("-linux-x86_64.tar.gz", "-linux-aarch64.tar.gz"))] + # Freeze independent producer responses before any negative mutation. + self.originals = {url: self.producers.downloads[url] for url in urls} + self.descriptors = sorted([ + {"name": url.rsplit("/", 1)[1], "size": len(raw), + "sha256": hashlib.sha256(raw).hexdigest(), "source": url} + for url, raw in self.originals.items()], key=lambda item: item["name"]) + self.extended = {**copy.deepcopy(self.manifest), "assets": copy.deepcopy(self.descriptors)} + for role, repo in artifacts.PRODUCERS.items(): + obj = self.producers.releases[role] + self.producers.api_data[f"repos/{repo}/git/ref/tags/{obj['tag_name']}"] = { + "object": {"type": "commit", "sha": self.producers.commits[role]}} + + def add_hub_asset(self, name, raw): + asset_id = max(self.data, default=0) + 1 + self.data[asset_id] = raw + # Deliberately no optional GitHub digest: descriptor verification must work. + self.row["assets"].append({"id": asset_id, "name": name, "state": "uploaded"}) + + def hub_bytes(self, name): + found = [a for a in self.row["assets"] if a["name"] == name] + return self.data[found[0]["id"]] if found else None + + def engine_asset(self, suffix="-linux-x86_64.tar.gz"): + return next(a for a in self.producers.releases["engine"]["assets"] if a["name"].endswith(suffix)) + + def replace_sdist(self, *, update_api): + files = artifacts.tar_files(self.producers.downloads[self.sdist_url]) + files["pineforge_codegen-fixture/NOTICE"] = b"changed outer distribution\n" + raw = archive(files) + self.producers.downloads[self.sdist_url] = raw + if update_api: + meta = json.loads(self.producers.downloads[self.pypi_url]) + meta["urls"][0]["digests"]["sha256"] = hashlib.sha256(raw).hexdigest() + self.producers.downloads[self.pypi_url] = contract.canonical(meta) + + def corrupt(self, problem): + if problem == "npm-integrity": + meta = json.loads(self.producers.downloads[self.npm_meta_url]) + meta["dist"]["integrity"] = "sha512-" + base64.b64encode(bytes(64)).decode() + self.producers.downloads[self.npm_meta_url] = contract.canonical(meta) + elif problem in ("pypi-digest", "candidate-sdist"): + self.replace_sdist(update_api=problem == "candidate-sdist") + elif problem == "engine-checksum": + for item in self.producers.releases["engine"]["assets"]: + item.pop("digest", None) + item = self.engine_asset() + raw = self.producers.downloads[item["browser_download_url"]] + self.producers.downloads[item["browser_download_url"]] = bytes([raw[0] ^ 1]) + raw[1:] + elif problem == "github-digest": + self.engine_asset()["digest"] = "sha256:" + "f" * 64 + elif problem == "missing-checksum": + self.producers.releases["engine"]["assets"].remove(self.engine_asset("-linux-x86_64.tar.gz.sha256")) + elif problem in ("engine-commit", "codegen-commit"): + role = problem.removesuffix("-commit") + repo, obj = artifacts.PRODUCERS[role], self.producers.releases[role] + self.producers.api_data[f"repos/{repo}/git/ref/tags/{obj['tag_name']}"]["object"]["sha"] = "f" * 40 + if role == "codegen": + old = f"repos/{repo}/git/trees/{self.producers.commits[role]}?recursive=1" + self.producers.api_data[f"repos/{repo}/git/trees/{'f' * 40}?recursive=1"] = self.producers.api_data[old] + elif problem in ("checksum-filename", "checksum-marker", "malformed-checksum", "nonascii-checksum"): + item = self.engine_asset("-linux-x86_64.tar.gz.sha256") + original = self.producers.downloads[item["browser_download_url"]] + if problem == "checksum-filename": + raw = original.split()[0] + b" another.tar.gz\n" + elif problem == "checksum-marker": + raw = original.split()[0] + b" **" + original.split()[1] + b"\n" + elif problem == "malformed-checksum": + raw = original.split()[0] + b"\n" + else: + raw = b"\xff\n" + self.producers.downloads[item["browser_download_url"]] = raw + item.pop("digest", None) + else: + raise AssertionError("unknown negative input") + + def open(self, request, timeout): + method, url = request.get_method(), request.full_url + self.requests.append((method, url, request.data, dict(request.header_items()))) + api = f"https://api.github.com/repos/{contract.HUB}" + upload = f"https://uploads.github.com/repos/{contract.HUB}/releases/1/assets?" + if url.startswith(upload): + if method != "POST": + raise AssertionError("binary upload must be POST") + name = parse_qs(urlsplit(url).query)["name"][0] + self.uploads.append((name, request.data)) + if self.fail_upload_number == len(self.uploads): + raise urllib.error.HTTPError(url, 503, "fixture", {}, None) + raw = request.data + if self.readback_problem == "missing": + return io.BytesIO(b"{}") + if self.readback_problem == "short": + raw = raw[:-1] + elif self.readback_problem == "same-size": + raw = bytes([raw[0] ^ 1]) + raw[1:] + self.add_hub_asset(name, raw) + if self.readback_problem == "404": + self.unavailable_reads.add(self.row["assets"][-1]["id"]) + return io.BytesIO(b"{}") + if url == api + "/releases/1": + if method == "PATCH": + data = json.loads(request.data) + self.publications.append(data) + self.row.update(draft=data["draft"], prerelease=data["prerelease"]) + elif method != "GET": + raise AssertionError("unexpected hub mutation") + return io.BytesIO(contract.canonical(self.row)) + if url == api + "/releases?per_page=100&page=1": + return io.BytesIO(contract.canonical([self.row])) + if url.startswith(api + "/releases/assets/"): + asset_id = int(url.rsplit("/", 1)[1]) + if asset_id in self.unavailable_reads: + raise urllib.error.HTTPError(url, 404, "fixture", {}, None) + return io.BytesIO(self.data[asset_id]) + if url == api + "/git/ref/tags/" + self.tag: + return io.BytesIO(contract.canonical({"object": {"type": "commit", "sha": self.manifest["hub_commit"]}})) + if not self.forbid_producers: + if url == api + "/git/ref/heads/main": + return io.BytesIO(contract.canonical({"object": {"sha": "e" * 40}})) + if url == api + "/contents/facts/facts.json?ref=" + "e" * 40: + return io.BytesIO(contract.canonical({ + "encoding": "base64", "content": base64.b64encode(self.facts_raw).decode(), + "sha": artifacts.git_blob(self.facts_raw)})) + if url.startswith("https://api.github.com/"): + route = url.removeprefix("https://api.github.com/") + if route in self.producers.api_data: + return io.BytesIO(contract.canonical(self.producers.api_data[route])) + if url in self.producers.downloads: + raw = self.producers.downloads[url] + if isinstance(raw, Exception): + raise raw + return io.BytesIO(raw) + raise AssertionError("unmodeled or forbidden HTTP request: " + url) + + +class AttachmentTestCase(unittest.TestCase): + def setUp(self): + self.http = AttachmentHTTP() + self.opener = patch("urllib.request.build_opener", return_value=self.http) + self.opener.start() + self.addCleanup(self.opener.stop) + self.client = artifacts.Client(token="fixture-token") + self.store, self.registry = pipeline.Store(self.client), MemoryRegistry() + + def finish(self): + return pipeline.finalize(self.client, self.store, self.registry, self.http.row) + + def plan(self, output): + with patch.dict(os.environ, { + "RELEASE_READY_CONSUMERS": "offline", + "RELEASE_READY_EXCLUDE": ",".join(r for r in contract.ROLES if r != "offline"), + "FACTS_WEB_DISPATCH_ENABLED": "false", "FACTS_LICENSE_DISPATCH_ENABLED": "false", + "GITHUB_RUN_ID": "100", "GITHUB_RUN_ATTEMPT": "1"}), \ + patch.object(pipeline, "report_issue"), patch.object(pipeline, "age_minutes", return_value=0): + pipeline.finalize_all(self.client, self.store, self.registry, self.http.tag, output) + + def assert_blocked(self): + self.assertIsNone(self.http.hub_bytes(contract.ASSET)) + self.assertTrue(self.http.row["draft"]) + self.assertEqual(self.registry.operations, []) + self.assertEqual(self.http.publications, []) + self.assertFalse(any(a["name"].startswith("delivery-") for a in self.http.row["assets"])) + + +class ExtendedManifestTests(AttachmentTestCase): + def test_exact_assets_and_ten_field_projection_hash_all_raw_bytes(self): + raw = contract.canonical(self.http.extended) + event = contract.payload(raw) + self.assertEqual(set(event), {"schema", "release_version", "prerelease", "hub_commit", + "versions", "image", "facts", "run_id", "event_id", "manifest"}) + self.assertEqual(len(event), 10) + self.assertNotIn("assets", event) + self.assertEqual(contract.decode(raw)["assets"], self.http.descriptors) + for descriptor in contract.decode(raw)["assets"]: + self.assertEqual(set(descriptor), {"name", "size", "sha256", "source"}) + original = self.http.originals[descriptor["source"]] + self.assertEqual(descriptor["size"], len(original)) + self.assertEqual(descriptor["sha256"], hashlib.sha256(original).hexdigest()) + digest = hashlib.sha256(raw).hexdigest() + self.assertEqual(event["event_id"], "sha256:" + digest) + self.assertEqual(event["manifest"], {"asset": contract.ASSET, "sha256": digest}) + contract.validate_payload(event, raw) + actual = contract.pair_payload(raw, contract.canonical(self.http.built)) + self.assertEqual(actual["receipt_sha256"], digest) + self.assertEqual(actual["build"]["candidate_sha256"], + hashlib.sha256(contract.canonical(self.http.built)).hexdigest()) + + def test_descriptor_tampering_cannot_keep_either_old_hash(self): + raw = contract.canonical(self.http.extended) + event = contract.payload(raw) + changed = copy.deepcopy(self.http.extended) + changed["assets"][0]["size"] += 1 + changed_raw = contract.canonical(changed) + for keep in ("both", "event", "manifest"): + proposed = contract.payload(changed_raw) + if keep in ("both", "event"): + proposed["event_id"] = event["event_id"] + if keep in ("both", "manifest"): + proposed["manifest"] = event["manifest"] + with self.subTest(keep=keep), self.assertRaises(contract.Invalid): + contract.validate_payload(proposed, changed_raw) + # Valid hashes cannot mask an invalid extension. + changed["assets"][0]["size"] = True + with self.assertRaises(contract.Invalid): + contract.validate_payload(event, contract.canonical(changed)) + + def test_reject_duplicate_missing_extra_unsorted_and_nonarray_assets(self): + assets = self.http.descriptors + cases = [None, {}, [], assets[::-1], assets + [copy.deepcopy(assets[0])]] + cases += [assets[:i] + assets[i + 1:] for i in range(len(assets))] + duplicate = copy.deepcopy(assets) + duplicate[-1] = copy.deepcopy(duplicate[0]) + cases.append(sorted(duplicate, key=lambda item: item["name"])) + extra = copy.deepcopy(assets) + extra.append({**extra[0], "name": "unexpected.tgz", + "source": "https://registry.npmjs.org/@pineforge/codegen-pyodide/-/unexpected.tgz"}) + cases.append(sorted(extra, key=lambda item: item["name"])) + # Four unique names can still omit a required type/architecture. + for missing in range(len(assets)): + wrong_kind = copy.deepcopy(assets) + if urlsplit(assets[missing]["source"]).netloc == "registry.npmjs.org": + wrong_kind[missing] = {**assets[0], "name": "another.tar.gz", + "source": "https://files.pythonhosted.org/another.tar.gz"} + else: + wrong_kind[missing] = {**assets[0], "name": "another.tgz", + "source": "https://registry.npmjs.org/@pineforge/codegen-pyodide/-/another.tgz"} + cases.append(sorted(wrong_kind, key=lambda item: item["name"])) + for descriptors in cases: + with self.subTest(assets=descriptors), self.assertRaises(contract.Invalid): + contract.payload(contract.canonical({**self.http.manifest, "assets": descriptors})) + with self.assertRaises(contract.Invalid): + contract.payload(contract.canonical({**self.http.extended, "extra": []})) + + def test_reject_malformed_descriptor_fields_names_sizes_hashes_and_sources(self): + for field in ("name", "size", "sha256", "source"): + obj = copy.deepcopy(self.http.extended) + del obj["assets"][0][field] + with self.subTest(missing=field), self.assertRaises(contract.Invalid): + contract.payload(contract.canonical(obj)) + changes = [ + ("extra", True), ("size", True), ("size", False), ("size", 0), + ("size", -1), ("size", 1.5), ("size", "1"), ("size", 2**53), + ("sha256", "F" * 64), ("sha256", "0" * 63), ("sha256", None), + ("name", "../outside.tgz"), ("name", "/outside.tgz"), ("name", "a\\b.tgz"), + ("name", ".hidden.tgz"), ("name", "release-manifest.json"), + ("name", "consumer-offline.tgz"), ("name", "release-audit.tgz"), + ("name", "delivery-audit.tgz"), ("name", "facts-audit.tgz"), + ("source", "http://registry.npmjs.org/fixture.tgz"), + ("source", "https://registry.npmjs.org.evil/fixture.tgz"), + ("source", "https://user@registry.npmjs.org/fixture.tgz"), + ("source", "https://registry.npmjs.org:443/fixture.tgz"), + ("source", "https://github.com/other/releases/download/tag/fixture.tgz"), + ("source", "https://[invalid/fixture.tgz"), ("source", None), + ("source", ["https://registry.npmjs.org/fixture.tgz"]), + ] + for field, value in changes: + obj = copy.deepcopy(self.http.extended) + obj["assets"][0][field] = value + obj["assets"].sort(key=lambda item: item["name"]) + with self.subTest(field=field, value=value), self.assertRaises(contract.Invalid): + contract.payload(contract.canonical(obj)) + for descriptor in self.http.descriptors: + for source in (descriptor["source"] + "?download=1", descriptor["source"] + "#fragment", + descriptor["source"].replace("https://", "https://other@"), + descriptor["source"].replace("/" + descriptor["name"], "/../" + descriptor["name"]), + descriptor["source"].replace("/" + descriptor["name"], "/%2e%2e/" + descriptor["name"]), + descriptor["source"].replace(descriptor["name"], "different.tar.gz")): + obj = copy.deepcopy(self.http.extended) + next(a for a in obj["assets"] if a["name"] == descriptor["name"])["source"] = source + with self.subTest(source=source), self.assertRaises(contract.Invalid): + contract.payload(contract.canonical(obj)) + + def test_legacy_bytes_and_ready_fixture_are_unchanged(self): + raw = (ROOT / "tests/fixtures/release-manifest.json").read_bytes() + expected = (ROOT / "tests/fixtures/release-ready.json").read_bytes() + self.assertEqual(contract.canonical(contract.payload(raw)), expected) + self.assertEqual(contract.canonical(contract.validate(contract.decode(raw))), raw) + + +class ProducerAttachmentTests(AttachmentTestCase): + def test_collect_original_outer_bytes_with_existing_producer_authorities(self): + pins, staged = artifacts.verified_assets( + self.client, self.http.built["versions"], self.http.built["inventories"]["codegen"]) + self.assertEqual(pins, self.http.manifest["versions"]) + self.assertEqual([descriptor for descriptor, _ in staged], self.http.descriptors) + self.assertEqual({descriptor["source"]: raw for descriptor, raw in staged}, self.http.originals) + npm_raw = next(raw for descriptor, raw in staged if descriptor["source"] == self.http.producers.npm_url) + self.assertIn("package/package.json", artifacts.tar_files(npm_raw)) + self.assertNotEqual(npm_raw, artifacts.tar_files(npm_raw)[ + f"package/pineforge_codegen-{self.http.producers.v}.tar.gz"]) + for _, url, _, headers in self.http.requests: + if urlsplit(url).netloc in ("registry.npmjs.org", "pypi.org", "files.pythonhosted.org", "github.com"): + self.assertNotIn("Authorization", headers) + self.assertEqual(headers["Accept"], "*/*") + + def test_changed_sdist_against_unchanged_api_digest_is_rejected_by_real_producer(self): + # Mutation control: removing the PyPI digest comparison alone makes this succeed. + self.http.corrupt("pypi-digest") + with self.assertRaisesRegex(contract.Invalid, "^codegen sdist checksum differs$"): + artifacts.producer(self.client, "codegen", str(self.http.producers.v), attachments=[]) + + def test_changed_engine_against_unchanged_companion_without_api_digest_is_rejected(self): + # Mutation control: only the companion checksum can reject these bytes. + self.http.corrupt("engine-checksum") + with self.assertRaisesRegex(contract.Invalid, "^engine tarball checksum differs$"): + artifacts.producer(self.client, "engine", str(self.http.producers.v), attachments=[]) + + def test_wrong_npm_integrity_with_valid_tar_structure_is_rejected(self): + self.http.corrupt("npm-integrity") + self.assertIn("package/package.json", artifacts.tar_files(self.http.producers.downloads[self.http.producers.npm_url])) + with self.assertRaisesRegex(contract.Invalid, "^npm integrity differs$"): + artifacts.npm(self.client, self.http.built["versions"]["codegen"], + self.http.built["inventories"]["codegen"], attachments=[]) + + def test_updated_pypi_digest_cannot_replace_the_candidate_sdist(self): + # API verification and tagged source still pass; only candidate binding fails. + self.http.corrupt("candidate-sdist") + artifacts.producer(self.client, "codegen", str(self.http.producers.v)) + with self.assertRaisesRegex(contract.Invalid, "^codegen producer identity differs from candidate$"): + artifacts.verified_assets(self.client, self.http.built["versions"], + self.http.built["inventories"]["codegen"]) + + def test_conflicting_optional_github_digest_refuses_archive_and_checksum(self): + for suffix in ("-linux-x86_64.tar.gz", "-linux-x86_64.tar.gz.sha256"): + self.http = AttachmentHTTP() + self.http.engine_asset(suffix)["digest"] = "sha256:" + "f" * 64 + with self.subTest(suffix=suffix), patch("urllib.request.build_opener", return_value=self.http), \ + self.assertRaisesRegex(contract.Invalid, "^release asset digest differs$"): + artifacts.producer(self.client, "engine", str(self.http.producers.v), attachments=[]) + + def test_producer_commit_must_still_equal_candidate_for_each_refetched_producer(self): + for role in ("engine", "codegen"): + http = AttachmentHTTP() + repo = artifacts.PRODUCERS[role] + tag = http.producers.releases[role]["tag_name"] + http.producers.api_data[f"repos/{repo}/git/ref/tags/{tag}"]["object"]["sha"] = "f" * 40 + if role == "codegen": + old = f"repos/{repo}/git/trees/{http.producers.commits[role]}?recursive=1" + http.producers.api_data[f"repos/{repo}/git/trees/{'f' * 40}?recursive=1"] = http.producers.api_data[old] + with self.subTest(role=role), patch("urllib.request.build_opener", return_value=http), \ + self.assertRaisesRegex(contract.Invalid, "^" + role + " producer identity differs from candidate$"): + artifacts.verified_assets(self.client, http.built["versions"], http.built["inventories"]["codegen"]) + + def test_bad_checksum_format_and_filename_are_invalid_and_missing_is_pending(self): + for problem in ("checksum-filename", "checksum-marker", "malformed-checksum", "nonascii-checksum", "missing-checksum"): + http = AttachmentHTTP() + http.corrupt(problem) + kind = contract.Pending if problem == "missing-checksum" else contract.Invalid + with self.subTest(problem=problem), patch("urllib.request.build_opener", return_value=http), \ + self.assertRaises(kind): + artifacts.verified_assets(self.client, http.built["versions"], http.built["inventories"]["codegen"]) + + def test_engine_checksum_accepts_the_single_standard_binary_marker(self): + item = self.http.engine_asset("-linux-x86_64.tar.gz.sha256") + raw = self.http.producers.downloads[item["browser_download_url"]] + self.http.producers.downloads[item["browser_download_url"]] = ( + raw.split()[0] + b" *" + raw.split()[1] + b"\n") + item.pop("digest", None) + pins, staged = artifacts.verified_assets( + self.client, self.http.built["versions"], self.http.built["inventories"]["codegen"]) + self.assertEqual(pins, self.http.manifest["versions"]) + self.assertEqual([d for d, _ in staged], self.http.descriptors) + + def test_ambiguous_engine_asset_is_invalid(self): + self.http.producers.releases["engine"]["assets"].append(copy.deepcopy(self.http.engine_asset())) + with self.assertRaisesRegex(contract.Invalid, "^ambiguous or incomplete asset$"): + artifacts.verified_assets(self.client, self.http.built["versions"], + self.http.built["inventories"]["codegen"]) + + def test_real_download_origin_checks_refuse_foreign_producer_urls(self): + for role in ("npm", "sdist", "engine"): + http = AttachmentHTTP() + if role == "npm": + meta = json.loads(http.producers.downloads[http.npm_meta_url]) + meta["dist"]["tarball"] = "https://files.pythonhosted.org/fixture.tgz" + http.producers.downloads[http.npm_meta_url] = contract.canonical(meta) + elif role == "sdist": + meta = json.loads(http.producers.downloads[http.pypi_url]) + meta["urls"][0]["url"] = "https://registry.npmjs.org/fixture.tar.gz" + http.producers.downloads[http.pypi_url] = contract.canonical(meta) + else: + http.engine_asset()["browser_download_url"] = "https://github.com/other/fixture.tar.gz" + with self.subTest(role=role), patch("urllib.request.build_opener", return_value=http), \ + self.assertRaises(contract.Invalid): + artifacts.verified_assets(self.client, http.built["versions"], http.built["inventories"]["codegen"]) + self.assertEqual(http.uploads, []) + + def test_unsafe_selected_filename_blocks_finalization_even_with_valid_integrity(self): + original_url = self.http.producers.npm_url + changed_url = original_url.rsplit("/", 1)[0] + "/release-audit.tgz" + self.http.producers.downloads[changed_url] = self.http.producers.downloads[original_url] + meta = json.loads(self.http.producers.downloads[self.http.npm_meta_url]) + meta["dist"]["tarball"] = changed_url + self.http.producers.downloads[self.http.npm_meta_url] = contract.canonical(meta) + with self.assertRaisesRegex(contract.Invalid, "^reserved attachment name$"): + self.finish() + self.assertEqual(self.http.uploads, []) + self.assert_blocked() + + +class BinaryAttachmentTests(AttachmentTestCase): + def test_real_binary_writer_exact_byte_replay_and_json_writer_scope(self): + descriptor = self.http.descriptors[0] + raw = self.http.originals[descriptor["source"]] + for _ in range(2): + self.store.put_binary(self.http.row, descriptor, raw, self.http.manifest["versions"]) + self.assertEqual(self.http.uploads, [(descriptor["name"], raw)]) + self.assertEqual(self.http.hub_bytes(descriptor["name"]), raw) + self.assertEqual(self.store.verify_binary(self.http.row, descriptor, self.http.manifest["versions"]), raw) + with self.assertRaises(contract.Invalid): + self.store.put(self.http.row, descriptor["name"], raw) + + def test_different_existing_bytes_are_never_overwritten(self): + descriptor = self.http.descriptors[0] + raw = self.http.originals[descriptor["source"]] + changed = bytes([raw[0] ^ 1]) + raw[1:] + self.http.add_hub_asset(descriptor["name"], changed) + with self.assertRaises(contract.Invalid): + self.store.put_binary(self.http.row, descriptor, raw, self.http.manifest["versions"]) + self.assertEqual(self.http.uploads, []) + self.assertEqual(self.http.hub_bytes(descriptor["name"]), changed) + + def test_existing_attachment_404_is_invalid_and_never_uploaded_again(self): + descriptor = self.http.descriptors[0] + raw = self.http.originals[descriptor["source"]] + self.http.add_hub_asset(descriptor["name"], raw) + self.http.unavailable_reads.add(self.http.row["assets"][-1]["id"]) + with self.assertRaisesRegex(contract.Invalid, "^hub attachment unavailable$"): + self.store.put_binary(self.http.row, descriptor, raw, self.http.manifest["versions"]) + self.assertEqual(self.http.uploads, []) + self.assertEqual(self.http.hub_bytes(descriptor["name"]), raw) + + def test_input_size_and_digest_are_checked_before_upload(self): + descriptor = self.http.descriptors[0] + original = self.http.originals[descriptor["source"]] + for raw in (original[:-1], bytes([original[0] ^ 1]) + original[1:]): + with self.subTest(size=len(raw)), self.assertRaises(contract.Invalid): + self.store.put_binary(self.http.row, descriptor, raw, self.http.manifest["versions"]) + # Removing the input guard cannot hide behind a later readback failure. + self.assertEqual(self.http.uploads, []) + with self.assertRaisesRegex(contract.Invalid, "^hub attachment size differs$"): + self.store.put_binary(self.http.row, {**descriptor, "size": len(original) + 1}, + original, self.http.manifest["versions"]) + self.assertEqual(self.http.uploads, []) + + def test_missing_wrong_size_and_same_size_corrupt_readback_fail_without_api_digest(self): + descriptor = self.http.descriptors[0] + raw = self.http.originals[descriptor["source"]] + for problem in ("missing", "404", "short", "same-size"): + http = AttachmentHTTP() + http.readback_problem = problem + with self.subTest(problem=problem), patch("urllib.request.build_opener", return_value=http), \ + self.assertRaises(contract.Invalid): + self.store.put_binary(http.row, descriptor, raw, http.manifest["versions"]) + self.assertEqual(http.uploads, [(descriptor["name"], raw)]) + + def test_binary_writer_refuses_unsafe_descriptor_before_any_request(self): + descriptor = self.http.descriptors[0] + raw = self.http.originals[descriptor["source"]] + for changes in ({"name": "release-manifest.json"}, {"name": "consumer-offline.tgz"}, + {"source": "https://example.org/fixture.tgz"}, {"size": True}): + before = len(self.http.requests) + with self.subTest(changes=changes), self.assertRaises(contract.Invalid): + self.store.put_binary(self.http.row, {**descriptor, **changes}, raw, self.http.manifest["versions"]) + self.assertEqual(len(self.http.requests), before) + + def test_optional_hub_api_digest_cannot_disagree_with_valid_descriptor_bytes(self): + descriptor = self.http.descriptors[0] + raw = self.http.originals[descriptor["source"]] + self.http.add_hub_asset(descriptor["name"], raw) + self.http.row["assets"][-1]["digest"] = "sha256:" + "f" * 64 + with self.assertRaisesRegex(contract.Invalid, "^hub asset digest differs$"): + self.store.verify_binary(self.http.row, descriptor, self.http.manifest["versions"]) + + +class AttachmentFinalizerTests(AttachmentTestCase): + def test_finalizer_attaches_all_verified_bytes_before_manifest_and_publication(self): + event = self.finish() + stored = json.loads(self.http.hub_bytes(contract.ASSET)) + self.assertEqual(stored["assets"], self.http.descriptors) + self.assertEqual(set(stored), contract.MANIFEST_KEYS | {"assets"}) + self.assertEqual(self.http.uploads[:4], [ + (d["name"], self.http.originals[d["source"]]) for d in self.http.descriptors]) + self.assertEqual(self.http.uploads[4][0], contract.ASSET) + self.assertFalse(self.http.row["draft"]) + self.assertEqual(len(event), 10) + self.assertEqual(event["run_id"], self.http.built["run_id"]) + self.assertEqual(set(json.loads(self.http.hub_bytes("release-ready.json"))), {"event_id", "timestamp"}) + # Every producer read precedes the first upload, not just the manifest. + first_upload = next(i for i, (_, u, _, _) in enumerate(self.http.requests) + if u.startswith("https://uploads.github.com/")) + producer_reads = [i for i, (_, u, _, _) in enumerate(self.http.requests) + if u in self.http.producers.downloads] + self.assertTrue(producer_reads) + self.assertLess(max(producer_reads), first_upload) + + def test_every_wrong_authority_blocks_all_uploads_manifest_promotion_and_delivery(self): + problems = ("npm-integrity", "pypi-digest", "candidate-sdist", "engine-checksum", + "github-digest", "checksum-filename", "checksum-marker", "malformed-checksum", "nonascii-checksum", + "engine-commit", "codegen-commit") + for problem in problems: + self.http, self.registry = AttachmentHTTP(), MemoryRegistry() + self.http.corrupt(problem) + with self.subTest(problem=problem), tempfile.TemporaryDirectory() as tmp, \ + patch("urllib.request.build_opener", return_value=self.http): + output = Path(tmp) / "deliveries.json" + with self.assertRaisesRegex(contract.Invalid, "release finalization failed"): + self.plan(output) + self.assertEqual(json.loads(output.read_bytes()), []) + self.assertEqual(self.http.uploads, []) + self.assert_blocked() + + def test_missing_checksum_stays_pending_and_readiness_cannot_plan_a_delivery(self): + self.http.corrupt("missing-checksum") + with self.assertRaises(contract.Pending): + self.finish() + with tempfile.TemporaryDirectory() as tmp: + output = Path(tmp) / "deliveries.json" + self.plan(output) + self.assertEqual(json.loads(output.read_bytes()), []) + self.assertEqual(self.http.uploads, []) + self.assert_blocked() + + def test_facts_failure_precedes_every_binary_upload(self): + facts = json.loads(self.http.facts_raw) + facts["releases"].pop(self.http.manifest["release_version"]) + self.http.facts_raw = contract.canonical(facts) + with self.assertRaises(contract.Pending): + self.finish() + self.assertEqual(self.http.uploads, []) + self.assert_blocked() + + def test_partial_upload_keeps_draft_and_retry_reuses_bytes(self): + self.http.fail_upload_number = 2 + with self.assertRaises(contract.Invalid): + self.finish() + first_name, first_raw = self.http.uploads[0] + self.assertEqual(self.http.hub_bytes(first_name), first_raw) + self.assert_blocked() + self.http.fail_upload_number = None + event = self.finish() + self.assertEqual(sum(name == first_name for name, _ in self.http.uploads), 1) + self.assertEqual(event, contract.payload(self.http.hub_bytes(contract.ASSET))) + self.assertEqual(json.loads(self.http.hub_bytes(contract.ASSET))["assets"], self.http.descriptors) + + def test_corrupt_upload_readback_blocks_the_manifest_and_all_later_operations(self): + for problem in ("missing", "404", "short", "same-size"): + self.http, self.registry = AttachmentHTTP(), MemoryRegistry() + self.http.readback_problem = problem + with self.subTest(problem=problem), patch("urllib.request.build_opener", return_value=self.http), \ + self.assertRaises(contract.Invalid): + self.finish() + self.assert_blocked() + + def test_extended_replay_verifies_hub_bytes_without_producer_resolution(self): + first = self.finish() + raw = self.http.hub_bytes(contract.ASSET) + uploads = list(self.http.uploads) + self.http.forbid_producers = True + self.http.requests.clear() + with patch.dict(os.environ, {"GITHUB_RUN_ID": "999"}): + again = self.finish() + self.assertEqual(again, first) + self.assertEqual(self.http.hub_bytes(contract.ASSET), raw) + self.assertEqual(again["run_id"], self.http.built["run_id"]) + self.assertEqual(self.http.uploads, uploads) + read_ids = {int(url.rsplit("/", 1)[1]) for _, url, _, _ in self.http.requests + if "/releases/assets/" in url} + for descriptor in self.http.descriptors: + item = next(a for a in self.http.row["assets"] if a["name"] == descriptor["name"]) + self.assertIn(item["id"], read_ids) + + def test_interruption_after_manifest_commit_replays_without_resolving_producers(self): + # Fail the external registry operation at the first promotion. + with patch.object(self.registry, "point", side_effect=contract.Invalid("registry unavailable")), \ + self.assertRaisesRegex(contract.Invalid, "^registry unavailable$"): + self.finish() + raw = self.http.hub_bytes(contract.ASSET) + self.assertIsNotNone(raw) + self.assertTrue(self.http.row["draft"]) + self.http.forbid_producers = True + uploads = list(self.http.uploads) + event = self.finish() + self.assertEqual(self.http.hub_bytes(contract.ASSET), raw) + self.assertEqual(event, contract.payload(raw)) + self.assertEqual(self.http.uploads[:len(uploads)], uploads) + self.assertFalse(self.http.row["draft"]) + + def test_committed_manifest_with_missing_or_corrupt_attachment_cannot_promote_or_deliver(self): + for problem in ("missing", "404", "short", "same-size"): + self.http, self.registry = AttachmentHTTP(), MemoryRegistry() + with self.subTest(problem=problem), patch("urllib.request.build_opener", return_value=self.http): + self.finish() + raw = self.http.hub_bytes(contract.ASSET) + first = self.http.descriptors[0] + item = next(a for a in self.http.row["assets"] if a["name"] == first["name"]) + if problem == "missing": + self.http.row["assets"].remove(item) + elif problem == "404": + self.http.unavailable_reads.add(item["id"]) + else: + original = self.http.data[item["id"]] + self.http.data[item["id"]] = (original[:-1] if problem == "short" + else bytes([original[0] ^ 1]) + original[1:]) + self.registry.operations.clear() + self.http.uploads.clear() + self.http.publications.clear() + self.http.forbid_producers = True + with tempfile.TemporaryDirectory() as tmp: + output = Path(tmp) / "deliveries.json" + with self.assertRaisesRegex(contract.Invalid, "release finalization failed"): + self.plan(output) + self.assertEqual(json.loads(output.read_bytes()), []) + self.assertEqual(self.http.hub_bytes(contract.ASSET), raw) + self.assertEqual(self.registry.operations, []) + self.assertEqual(self.http.publications, []) + self.assertEqual(self.http.uploads, []) + + def test_legacy_replay_never_resolves_producers_or_extends_stored_bytes(self): + legacy_raw = contract.canonical(self.http.manifest) + self.http.add_hub_asset(contract.ASSET, legacy_raw) + self.http.forbid_producers = True + first = self.finish() + self.assertEqual(first, self.finish()) + self.assertEqual(self.http.hub_bytes(contract.ASSET), legacy_raw) + self.assertNotIn("assets", json.loads(legacy_raw)) + self.assertEqual(first["event_id"], "sha256:" + hashlib.sha256(legacy_raw).hexdigest()) + self.assertEqual(first["run_id"], self.http.built["run_id"]) + self.assertFalse(any(name.endswith((".tgz", ".tar.gz")) for name, _ in self.http.uploads)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_release_binding.py b/tests/test_release_binding.py index 8210329..b222621 100644 --- a/tests/test_release_binding.py +++ b/tests/test_release_binding.py @@ -23,6 +23,7 @@ from itertools import permutations from pathlib import Path from unittest.mock import patch +from urllib.parse import parse_qs, urlsplit ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) @@ -101,6 +102,27 @@ def candidate(manifest): "list_sha256": "a" * 64, "amd64_sha256": "b" * 64, "arm64_sha256": "c" * 64}} +def synthetic_ready_assets(client, pins, inventory): + """Producer boundary for older state-machine tests; HTTP gates have their own fixtures.""" + pins = copy.deepcopy(pins) + original = fixture_manifest(pins["codegen"]["version"])["versions"]["codegen"] + pins["codegen"].update({key: original[key] for key in ("npm_version", "npm_integrity")}) + urls = [ + "https://registry.npmjs.org/@pineforge/codegen-pyodide/-/fixture.tgz", + "https://files.pythonhosted.org/fixture.tar.gz", + ] + engine = pins["engine"] + urls += [f"https://github.com/{artifacts.PRODUCERS['engine']}/releases/download/{engine['tag']}/" + f"pineforge-v{engine['version']}-linux-{arch}.tar.gz" for arch in ("x86_64", "aarch64")] + staged = [] + for url in urls: + name = url.rsplit("/", 1)[1] + raw = ("synthetic producer boundary " + name).encode() + staged.append(({"name": name, "size": len(raw), "sha256": hashlib.sha256(raw).hexdigest(), + "source": url}, raw)) + return pins, sorted(staged, key=lambda item: item[0]["name"]) + + def expected_pair_payload(manifest_raw, candidate_raw): """Independent P04/P05 mapping; no production payload/hash helpers.""" manifest, built = json.loads(manifest_raw), json.loads(candidate_raw) @@ -176,6 +198,13 @@ def api(self, route, *, method="GET", data=None): raise AssertionError("unmodeled hub route " + route) def request(self, url, **kwargs): + upload = f"https://uploads.github.com/repos/{contract.HUB}/releases/" + if url.startswith(upload): + release_id = int(url.removeprefix(upload).split("/", 1)[0]) + row = next(row for row in self.rows.values() if row["id"] == release_id) + name = parse_qs(urlsplit(url).query)["name"][0] + self.put(row, name, kwargs["data"]) + return b"{}" prefix = f"https://api.github.com/repos/{contract.HUB}/releases/assets/" if url.startswith(prefix): asset_id = int(url.removeprefix(prefix)) @@ -204,6 +233,12 @@ def put(self, row, name, raw): "digest": "sha256:" + contract.sha256(raw)}) self.operations.append(("asset", name)) + def put_binary(self, row, descriptor, raw, versions): + return pipeline.Store(self).put_binary(row, descriptor, raw, versions) + + def verify_binary(self, row, descriptor, versions): + return pipeline.Store(self).verify_binary(row, descriptor, versions) + def publish(self, row, latest): row["draft"] = False self.operations.append(("publish", row["tag_name"], latest)) @@ -511,9 +546,9 @@ def setUp(self): self.store = MemoryStore(self.m) self.registry = MemoryRegistry() self.row = self.store.get("v1.6.0") - # Only npm transport is substituted; manifest/facts/candidate rules run. - self.npm = patch.object(pipeline, "npm", side_effect=lambda c, pin, inventory: { - **pin, "npm_version": pin["version"], "npm_integrity": self.m["versions"]["codegen"]["npm_integrity"]}) + # Substitute the producer boundary for these state-machine tests only. + # test_release_assets drives the real producer and storage paths via HTTP. + self.npm = patch.object(pipeline, "verified_assets", side_effect=synthetic_ready_assets) self.npm.start() self.addCleanup(self.npm.stop) @@ -565,7 +600,7 @@ def test_missing_npm_and_missing_facts_leave_all_aliases_and_release_unpublished for missing in ("npm", "facts"): with self.subTest(missing=missing): if missing == "npm": - context = patch.object(pipeline, "npm", side_effect=contract.Pending("npm missing")) + context = patch.object(pipeline, "verified_assets", side_effect=contract.Pending("npm missing")) else: self.client.facts["releases"].pop("1.6.0") context = patch.object(self.client, "file", return_value=contract.canonical(self.client.facts)) @@ -598,12 +633,23 @@ def test_wrong_facts_source_and_failed_architecture_verification_are_red(self): def test_duplicate_replay_keeps_manifest_bytes_event_and_original_run_id(self): first = self.finish() original = self.store.read(self.row, contract.ASSET) - with patch.object(pipeline, "npm", side_effect=AssertionError("must not re-resolve packages")), \ + with patch.object(pipeline, "verified_assets", side_effect=AssertionError("must not re-resolve packages")), \ patch.object(self.client, "file", side_effect=AssertionError("must not bind newer facts")): second = self.finish() self.assertEqual(original, self.store.read(self.row, contract.ASSET)) self.assertEqual(first, second) + def test_stored_legacy_fixture_replays_without_resolution_or_rewriting(self): + raw = (ROOT / "tests/fixtures/release-manifest.json").read_bytes() + expected = contract.decode((ROOT / "tests/fixtures/release-ready.json").read_bytes()) + self.store.put(self.row, contract.ASSET, raw) + with patch.object(pipeline, "verified_assets", side_effect=AssertionError("legacy replay resolved producers")), \ + patch.object(self.client, "file", side_effect=AssertionError("legacy replay resolved facts")): + self.assertEqual(self.finish(), expected) + self.assertEqual(self.finish(), expected) + self.assertEqual(self.store.read(self.row, contract.ASSET), raw) + self.assertFalse(any(name.endswith((".tgz", ".tar.gz")) for _, name in self.store.assets)) + def test_older_completion_and_its_receipt_cannot_move_newer_alias_or_consumer(self): newer = fixture_manifest("1.6.1") newer["image"]["digest"] = "sha256:" + "a" * 64 @@ -1515,6 +1561,43 @@ def test_application_dispatch_preserves_release_ready_v1_without_an_opt_in(self) self.assertEqual(json.loads(written)["consumer"], "application") self.assertEqual(json.loads(written)["event_id"], json.loads(ready_raw)["event_id"]) + def test_extended_manifest_dispatch_projects_ten_fields_for_every_receipt_role(self): + from test_release_assets import AttachmentHTTP + manifest = AttachmentHTTP().extended + raw = contract.canonical(manifest) + digest = hashlib.sha256(raw).hexdigest() + for role in contract.RECEIPT_ROLES: + store = self.ready_store(manifest) + with self.subTest(role=role): + result, sender, written, _, _ = self.send(store, role, "v" + manifest["release_version"]) + self.assertEqual(result, 0) + body = sender.api.call_args.kwargs["data"] + self.assertEqual(body["event_type"], "pineforge-release") + event = body["client_payload"] + self.assertEqual(set(event), contract.MANIFEST_KEYS | {"event_id", "manifest"}) + self.assertEqual(len(event), 10) + self.assertNotIn("assets", event) + self.assertEqual(event["manifest"], {"asset": contract.ASSET, "sha256": digest}) + self.assertEqual(event["event_id"], "sha256:" + digest) + self.assertEqual(json.loads(written)["event_id"], event["event_id"]) + self.assertEqual(store.read(store.get("v" + manifest["release_version"]), contract.ASSET), raw) + + def test_compute_dispatch_hashes_the_complete_extended_manifest(self): + from test_release_assets import AttachmentHTTP + manifest = AttachmentHTTP().extended + store = self.ready_store(manifest) + tag = "v" + manifest["release_version"] + result, sender, _, _, _ = self.send(store, "compute", tag) + self.assertEqual(result, 0) + body = sender.api.call_args.kwargs["data"] + self.assertEqual(body["event_type"], "release-pair") + self.assertEqual(len(body["client_payload"]), 8) + self.assertEqual(body["client_payload"]["receipt_sha256"], + hashlib.sha256(store.read(store.get(tag), contract.ASSET)).hexdigest()) + self.assertEqual(body["client_payload"], + expected_pair_payload(store.read(store.get(tag), contract.ASSET), + store.read(store.get(tag), contract.CANDIDATE))) + def test_pair_dispatch_posts_release_pair_with_exact_eight_key_payload(self): for consumer, release in (("compute", "1.6.0"), ("compute", "1.6.0-rc.1")): with self.subTest(consumer=consumer, release=release): diff --git a/tests/test_release_binding_workflows.py b/tests/test_release_binding_workflows.py index 51710bf..ce1c034 100644 --- a/tests/test_release_binding_workflows.py +++ b/tests/test_release_binding_workflows.py @@ -24,7 +24,7 @@ import release_contract as contract # noqa: E402 import test_release_workflows as wiring # noqa: E402 from test_release_binding import (FinalizeClient, MemoryRegistry, MemoryStore, # noqa: E402 - ProducerFixture, fixture_manifest) + ProducerFixture, fixture_manifest, synthetic_ready_assets) def workflow(name): @@ -52,9 +52,20 @@ def assert_ready_wiring(case, publish, finalizer): named["Image metadata"]["with"]["labels"]) case.assertEqual(publish["jobs"]["notify-consumers"]["uses"], "./.github/workflows/release-finalize.yml") finalize = finalizer["jobs"]["finalize"] - case.assertTrue(any("scripts/release_pipeline.py finalize" in row.get("run", "") for row in finalize["steps"])) + ready = [row for row in finalize["steps"] if "scripts/release_pipeline.py finalize" in row.get("run", "")] + case.assertEqual(len(ready), 1) + case.assertEqual(ready[0]["name"], "Finalize or record missing prerequisites") + case.assertEqual(ready[0]["env"]["GH_TOKEN"], "${{ github.token }}") + case.assertNotIn("continue-on-error", ready[0]) + case.assertNotIn("if", ready[0]) + case.assertEqual(set(finalizer["on"]["workflow_call"]["inputs"]), {"tag"}) + checkouts = [row for row in finalize["steps"] if row.get("uses", "").startswith("actions/checkout@")] + case.assertEqual(len(checkouts), 1) + case.assertEqual(checkouts[0]["with"]["ref"], "main") + case.assertLess(finalize["steps"].index(checkouts[0]), finalize["steps"].index(ready[0])) dispatch = finalizer["jobs"]["notify-consumers"] case.assertEqual(dispatch["needs"], "finalize") + case.assertEqual(dispatch["if"], "${{ needs.finalize.result == 'success' && needs.finalize.outputs.deliveries != '[]' }}") case.assertEqual(dispatch["strategy"]["fail-fast"], "false") case.assertEqual(dispatch["strategy"]["matrix"]["include"], "${{ fromJSON(needs.finalize.outputs.deliveries) }}") case.assertNotIn("concurrency", finalizer) @@ -121,6 +132,24 @@ def test_security_819_actual_first_refusal_and_full_aggregate_reject_non_success def test_actual_workflows_require_verified_candidate_before_ready_fanout(self): assert_ready_wiring(self, workflow("publish.yml"), workflow("release-finalize.yml")) + def test_attachment_wiring_refuses_skipped_finalizer_wrong_checkout_or_another_input(self): + for mutation in ("continue", "conditional", "checkout", "input", "fanout"): + finalizer = workflow("release-finalize.yml") + steps = finalizer["jobs"]["finalize"]["steps"] + ready = next(s for s in steps if s.get("name") == "Finalize or record missing prerequisites") + if mutation == "continue": + ready["continue-on-error"] = "true" + elif mutation == "conditional": + ready["if"] = "false" + elif mutation == "checkout": + next(s for s in steps if s.get("uses", "").startswith("actions/checkout@"))["with"]["ref"] = "${{ inputs.tag }}" + elif mutation == "input": + finalizer["on"]["workflow_call"]["inputs"]["assets"] = {"type": "boolean"} + else: + finalizer["jobs"]["notify-consumers"]["if"] = "${{ always() }}" + with self.subTest(mutation=mutation), self.assertRaises(AssertionError): + assert_ready_wiring(self, workflow("publish.yml"), finalizer) + def test_dispatch_origin_and_validated_tag_are_pinned_alongside_readiness(self): pins = wiring.ReleaseTriggerWorkflowTest() pins.assert_publish((ROOT / ".github/workflows/publish.yml").read_text()) @@ -365,8 +394,7 @@ def test_all_arrival_orders_replay_real_handler_shell_then_finalize_and_repeat(s manifest = fixture_manifest(ver) client, store, registry = FinalizeClient(manifest), MemoryStore(manifest), MemoryRegistry() tag = "v" + ver - with patch.object(pipeline, "npm", side_effect=lambda c, pin, inv: { - **pin, "npm_version": ver, "npm_integrity": manifest["versions"]["codegen"]["npm_integrity"]}): + with patch.object(pipeline, "verified_assets", side_effect=synthetic_ready_assets): event = pipeline.finalize(client, store, registry, store.get(tag)) raw = store.read(store.get(tag), contract.ASSET) self.assertEqual(event, pipeline.finalize(client, store, registry, store.get(tag))) diff --git a/tests/test_release_receipts.py b/tests/test_release_receipts.py index 3b8c455..fb57b95 100644 --- a/tests/test_release_receipts.py +++ b/tests/test_release_receipts.py @@ -358,5 +358,78 @@ def test_catch_all_diagnostic_includes_the_actual_exception_text(self): self.assertIn(str(error), output.getvalue()) +class ExtendedProductReceiptTests(unittest.TestCase): + def prepare(self): + from test_release_assets import AttachmentHTTP + from test_release_binding import MemoryStore as ProductStore + http, client = AttachmentHTTP(), HistoryClient() + manifest = copy.deepcopy(http.extended) + # Bind the fixture's measured entry for retained-facts verification. + facts = json.loads(client.file(contract.HUB, FACTS_COMMIT, "facts/facts.json")) + entry = copy.deepcopy(next(iter(facts["releases"].values()))) + entry["source"] = ( + f"https://github.com/pineforge-4pass/pineforge-engine/blob/v{manifest['release_version']}" + "/README.md#validation-scoreboard") + facts["releases"][manifest["release_version"]] = entry + facts_raw = contract.canonical(facts) + manifest["facts"].update(commit=FACTS_COMMIT, sha256=hashlib.sha256(facts_raw).hexdigest(), + release_key=manifest["release_version"]) + raw = contract.canonical(manifest) + event = contract.payload(raw) + store = ProductStore(manifest) + row = store.get("v" + manifest["release_version"]) + row.update(draft=False, prerelease=manifest["prerelease"]) + store.put(row, contract.ASSET, raw) + for descriptor in manifest["assets"]: + store.put(row, descriptor["name"], http.originals[descriptor["source"]]) + for run_id, receipt in client.documents.items(): + receipt.pop("facts") + receipt.update(schema=receipts.PRODUCT_SCHEMA, event_id=event["event_id"], + release_version=event["release_version"]) + for name in ("preview", "deployed", "previous"): + if receipt.get(name): + receipt[name].update(image_digest=event["image"]["digest"], + facts_commit=FACTS_COMMIT, facts_sha256=manifest["facts"]["sha256"]) + client.seal(run_id) + client.responses[f"repos/{contract.HUB}/compare/{FACTS_COMMIT}...{FACTS_COMMIT}"] = {"status": "identical"} + return client, store, row, raw, facts_raw, event + + def test_extended_product_collection_keeps_receipt_fields_evidence_and_binary_bytes(self): + client, store, row, raw, facts_raw, event = self.prepare() + before = copy.deepcopy(store.assets) + with patch.dict(os.environ, {"TARGET_REPOSITORY": "fixture", "RELEASE_READY_CONSUMERS": "license", + "RELEASE_RECEIPT_POLICY": json.dumps({"license": POLICY})}), \ + patch.object(client, "file", return_value=facts_raw): + receipts.collect(client, client, store, "license") + written = copy.deepcopy(store.assets) + receipts.collect(client, client, store, "license") + self.assertEqual(store.assets, written) + for key, data in before.items(): + self.assertEqual(store.assets[key], data) + self.assertEqual(store.read(row, contract.ASSET), raw) + for run_id, receipt in client.documents.items(): + name = f"consumer-license-{receipt['attempt']}-{receipt['sequence']}.json" + public = json.loads(store.read(row, name)) + legacy = json.loads((FIXTURES / ("stored-ready.json" if run_id == 1007 else "stored-verified.json")).read_bytes()) + self.assertEqual(set(public), (set(legacy) - {"facts"}) | {"event_id", "release_version"}) + self.assertEqual(public["event_id"], event["event_id"]) + self.assertEqual(public["evidence_sha256"], hashlib.sha256(contract.canonical(receipt)).hexdigest()) + self.assertNotIn("assets", public) + self.assertNotIn("assets", receipt) + + def test_extended_product_collection_rejects_wrong_event_before_writing(self): + client, store, row, _, facts_raw, _ = self.prepare() + client.documents[1007]["event_id"] = "sha256:" + "0" * 64 + client.seal(1007) + before = copy.deepcopy(store.assets) + with patch.dict(os.environ, {"TARGET_REPOSITORY": "fixture", + "RELEASE_RECEIPT_POLICY": json.dumps({"license": POLICY})}), \ + patch.object(client, "file", return_value=facts_raw), \ + self.assertRaisesRegex(contract.Invalid, "^receipt event differs$"): + receipts.collect(client, client, store, "license", 1007) + self.assertEqual(store.assets, before) + self.assertFalse(any(a["name"].startswith("consumer-") for a in row["assets"])) + + if __name__ == "__main__": unittest.main() diff --git a/tools/release-dry-run/ready_replay.py b/tools/release-dry-run/ready_replay.py index c263386..4a8d6ad 100644 --- a/tools/release-dry-run/ready_replay.py +++ b/tools/release-dry-run/ready_replay.py @@ -25,7 +25,8 @@ def main(argv=None): if args.k: loader.testNamePatterns = ["*" + args.k + "*"] suite = unittest.TestSuite(loader.loadTestsFromName(name) for name in - ("test_release_binding", "test_release_binding_workflows", "test_release_runtime_smoke")) + ("test_release_binding", "test_release_assets", "test_release_receipts", + "test_release_binding_workflows", "test_release_runtime_smoke")) if not suite.countTestCases(): parser.error("no replay cases selected") stream = sys.stderr From 7ad38d33105104b9b4343eb2c1f9c3bc5a059a74 Mon Sep 17 00:00:00 2001 From: luisleo526 <20812494+luisleo526@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:58:05 +0800 Subject: [PATCH 2/2] test: isolate release asset validation rules --- docs/release-chain.md | 5 ++ tests/test_release_assets.py | 153 ++++++++++++++++++++++++++++++++++- 2 files changed, 155 insertions(+), 3 deletions(-) diff --git a/docs/release-chain.md b/docs/release-chain.md index 77ac89d..21bea3c 100644 --- a/docs/release-chain.md +++ b/docs/release-chain.md @@ -165,6 +165,11 @@ unpacking/repacking them. It reads each upload back and checks its size and SHA-256, including when GitHub supplies no digest. Identical existing bytes are reused; conflicting bytes are never overwritten. A partial upload leaves the draft and its attachments for retry, with no manifest or authorized promotion. +If an interrupted upload leaves an attachment listed in any state other than +`uploaded`, the writer refuses it as invalid hub asset metadata and finalization +fails closed: no manifest, promotion or delivery, and nothing is overwritten or +deleted. Later runs fail the same way until an operator removes that attachment +from the draft. Missing producer checksums stay pending; corruption fails finalization. Only then does finalization attach `release-manifest.json`, assign the fixed version image tag, move eligible aliases, publish the GitHub release, and fan diff --git a/tests/test_release_assets.py b/tests/test_release_assets.py index 3a8c7b2..a6a2c5c 100644 --- a/tests/test_release_assets.py +++ b/tests/test_release_assets.py @@ -3,6 +3,10 @@ Authorities are captured before mutations. Removing a digest gate must let its isolated negative input through, or cause a forbidden upload before refusal. + +Fixtures are built at fixed releases, never from the repository's VERSION file: +one stable, parity-capable release for every case, and one explicit prerelease. +A prerelease bump of VERSION therefore cannot change what any case proves. """ from __future__ import annotations @@ -26,14 +30,22 @@ import release_artifacts as artifacts # noqa: E402 import release_contract as contract # noqa: E402 import release_pipeline as pipeline # noqa: E402 +from parity_bundle import PARITY_CAPABILITY_FROM # noqa: E402 from test_release_binding import (MemoryRegistry, ProducerFixture, archive, # noqa: E402 candidate, facts_for, fixture_manifest) +# The fixed, stable, parity-capable release every case is built at: scripts/parity_bundle.py +# defines it, and wire_case in test_release_parity_wire.py defaults to the same constant. +FIXTURE_RELEASE = PARITY_CAPABILITY_FROM +# The one explicit prerelease case: above the parity cutoff, like the pair prerelease that +# handle-upstream.yml writes into VERSION. +PRERELEASE_RELEASE = "1.12.0-rc.1" + class AttachmentHTTP: """Serve the real Client, including origin, auth, API and readback handling.""" - def __init__(self): - self.producers = ProducerFixture((ROOT / "VERSION").read_text().strip()) + def __init__(self, release=FIXTURE_RELEASE): + self.producers = ProducerFixture(release) if contract.parity_capable(str(self.producers.v)): from test_release_parity_wire import wire_case @@ -202,8 +214,10 @@ def open(self, request, timeout): class AttachmentTestCase(unittest.TestCase): + release = FIXTURE_RELEASE + def setUp(self): - self.http = AttachmentHTTP() + self.http = AttachmentHTTP(self.release) self.opener = patch("urllib.request.build_opener", return_value=self.http) self.opener.start() self.addCleanup(self.opener.stop) @@ -345,6 +359,115 @@ def test_legacy_bytes_and_ready_fixture_are_unchanged(self): self.assertEqual(contract.canonical(contract.validate(contract.decode(raw))), raw) +class AssetRuleIsolationTests(AttachmentTestCase): + # One frozen-shape rule per test. Each test replaces ONE of the four valid descriptors so that + # exactly one rule refuses it: name and source agree with each other and every other field is + # valid, so deleting that rule leaves a fully valid inventory. The expected messages are + # literals written here; the validator is never asked for them. Some rules share a message + # (the two npm checks, the two halves of the name-order check, both engine-source changes), + # so the message alone cannot tell them apart; the rule-removal rows of the proof run do. + NPM = "https://registry.npmjs.org/@pineforge/codegen-pyodide/-/" + ENGINE_RELEASES = "https://github.com/pineforge-4pass/pineforge-engine/releases/download/" + + def setUp(self): + super().setUp() + found = self.http.descriptors + self.versions = self.http.manifest["versions"] + self.valid = { + "npm": next(d for d in found if urlsplit(d["source"]).netloc == "registry.npmjs.org"), + "sdist": next(d for d in found if urlsplit(d["source"]).netloc == "files.pythonhosted.org"), + "x86_64": next(d for d in found if d["name"].endswith("-linux-x86_64.tar.gz")), + "aarch64": next(d for d in found if d["name"].endswith("-linux-aarch64.tar.gz")), + } + + def inventory(self, **replaced): + assets = {kind: copy.deepcopy(item) for kind, item in self.valid.items()} + assets.update(replaced) + return sorted(assets.values(), key=lambda item: item["name"]) + + def refused(self, assets, message): + # Control: the same four descriptors, unchanged, are accepted. + contract.validate_assets(self.inventory(), self.versions) + with self.assertRaisesRegex(contract.Invalid, message): + contract.validate_assets(assets, self.versions) + + def reserved_prefix_alone(self, prefix): + name = prefix + "fixture.tgz" + changed = {**self.valid["npm"], "name": name, "source": self.NPM + name} + self.refused(self.inventory(npm=changed), "^reserved attachment name$") + + def test_consumer_prefix_alone_is_reserved(self): + self.reserved_prefix_alone("consumer-") + + def test_facts_prefix_alone_is_reserved(self): + self.reserved_prefix_alone("facts-") + + def test_delivery_prefix_alone_is_reserved(self): + self.reserved_prefix_alone("delivery-") + + def test_name_pattern_alone_refuses_a_leading_dot(self): + # Not reserved, the source ends in the same name, right package path, right suffix. + name = ".hidden.tgz" + changed = {**self.valid["npm"], "name": name, "source": self.NPM + name} + self.refused(self.inventory(npm=changed), "^malformed identity$") + + def test_duplicate_name_of_two_kinds_alone_is_refused(self): + # The npm and the sdist descriptor both name "fixture.tar.gz", each valid for its own kind. + # All four kinds are present and the names are sorted, so only uniqueness refuses. + name = self.valid["sdist"]["name"] + changed = {**self.valid["npm"], "name": name, "source": self.NPM + name} + assets = self.inventory(npm=changed) + names = [item["name"] for item in assets] + self.assertEqual(names.count(name), 2) + self.assertEqual(names, sorted(names)) + self.refused(assets, "^attachment names must be unique and sorted$") + + def test_unsorted_names_alone_are_refused(self): + # Unique names, all four kinds, every descriptor valid; only the order is wrong. + assets = list(reversed(self.inventory())) + names = [item["name"] for item in assets] + self.assertEqual(len(set(names)), 4) + self.assertNotEqual(names, sorted(names)) + self.refused(assets, "^attachment names must be unique and sorted$") + + def test_npm_package_path_alone_is_refused(self): + # Valid suffix, name equal to the last path part, wrong package in the path. + name = self.valid["npm"]["name"] + changed = {**self.valid["npm"], + "source": "https://registry.npmjs.org/@pineforge/other-package/-/" + name} + self.refused(self.inventory(npm=changed), "^unexpected npm attachment$") + + def test_npm_suffix_alone_is_refused(self): + # Right package path and name equal to the last path part; only the suffix is wrong. + name = "fixture.zip" + changed = {**self.valid["npm"], "name": name, "source": self.NPM + name} + self.refused(self.inventory(npm=changed), "^unexpected npm attachment$") + + def test_sdist_suffix_alone_is_refused(self): + # An npm-style suffix on the PyPI host; the name is unique and keeps the order. + name = "fixture-sdist.tgz" + changed = {**self.valid["sdist"], "name": name, "source": "https://files.pythonhosted.org/" + name} + self.refused(self.inventory(sdist=changed), "^unexpected sdist attachment$") + + def engine_source_alone(self, source): + name = self.valid["x86_64"]["name"] + # Controls: the real source is the pinned repository and tag, and the changed one keeps the name. + self.assertEqual(self.valid["x86_64"]["source"], f"{self.ENGINE_RELEASES}v{FIXTURE_RELEASE}/{name}") + self.assertEqual(source.rsplit("/", 1)[1], name) + self.assertNotEqual(source, self.valid["x86_64"]["source"]) + self.refused(self.inventory(x86_64={**self.valid["x86_64"], "source": source}), + "^unexpected engine attachment source$") + + def test_engine_source_repository_alone_is_refused(self): + name = self.valid["x86_64"]["name"] + self.engine_source_alone( + f"https://github.com/pineforge-4pass/pineforge-other/releases/download/v{FIXTURE_RELEASE}/{name}") + + def test_engine_source_tag_alone_is_refused(self): + name = self.valid["x86_64"]["name"] + self.engine_source_alone(f"{self.ENGINE_RELEASES}v1.0.0/{name}") + + class ProducerAttachmentTests(AttachmentTestCase): def test_collect_original_outer_bytes_with_existing_producer_authorities(self): pins, staged = artifacts.verified_assets( @@ -699,5 +822,29 @@ def test_legacy_replay_never_resolves_producers_or_extends_stored_bytes(self): self.assertFalse(any(name.endswith((".tgz", ".tar.gz")) for name, _ in self.http.uploads)) +class PrereleaseFactsTests(AttachmentTestCase): + release = PRERELEASE_RELEASE + + def test_prerelease_binds_main_facts_without_a_release_entry(self): + # A prerelease needs no facts.releases[] entry; it binds the validated bytes of main's + # facts. A stable release without that entry stays Pending, see + # test_facts_failure_precedes_every_binary_upload. + self.assertTrue(self.http.manifest["prerelease"]) + facts = json.loads(self.http.facts_raw) + facts["releases"].pop(PRERELEASE_RELEASE) + self.http.facts_raw = contract.canonical(facts) + self.assertNotIn(PRERELEASE_RELEASE, json.loads(self.http.facts_raw)["releases"]) + try: + self.finish() + except contract.Pending as error: + self.fail("a prerelease must bind main's facts without a release entry: " + str(error)) + stored = json.loads(self.http.hub_bytes(contract.ASSET)) + # The commit is main's head as the fixture serves it; the digest is of the bytes it served. + self.assertEqual(stored["facts"], { + "commit": "e" * 40, "path": "facts/facts.json", + "sha256": hashlib.sha256(self.http.facts_raw).hexdigest(), + "release_key": PRERELEASE_RELEASE}, "prerelease facts binding differs") + + if __name__ == "__main__": unittest.main()