From a70455453a5e35e7ef8f410e5488b1f3fca86eee Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 15:29:55 -0600 Subject: [PATCH] chore(release): remove the manual crate publication path Crate versions after 0.2.0 publish only through the automatic release contract, so the manual prepare/publish workflow operations, their tooling (crate_publication.py, crate_upload.py), the 0.2.0 next-artifact policy and their tests are removed. The packaged-crate archive check that validate.sh still needs moves to tools/crate_archive.py. The publication workflow anchor in check-crate-release-policy.py is updated for the reduced workflow, and the README and AGENTS.md describe 0.2.0 as historical lineage only. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01BPTYAokoQXfhC9D2iVv8B6 --- .github/workflows/publish-crate.yml | 108 +-------- AGENTS.md | 5 +- README.md | 100 ++------ policy/rust-crate-next.json | 12 - tools/check-crate-release-policy.py | 15 +- tools/crate_archive.py | 56 +++++ tools/crate_publication.py | 356 --------------------------- tools/crate_upload.py | 226 ----------------- tools/package-release.sh | 2 +- tools/test_crate_archive.py | 52 ++++ tools/test_crate_publication.py | 357 --------------------------- tools/test_crate_release_policy.py | 16 +- tools/test_crate_upload.py | 359 ---------------------------- tools/validate.sh | 5 +- 14 files changed, 148 insertions(+), 1521 deletions(-) delete mode 100644 policy/rust-crate-next.json create mode 100644 tools/crate_archive.py delete mode 100644 tools/crate_publication.py delete mode 100644 tools/crate_upload.py create mode 100644 tools/test_crate_archive.py delete mode 100644 tools/test_crate_publication.py delete mode 100644 tools/test_crate_upload.py diff --git a/.github/workflows/publish-crate.yml b/.github/workflows/publish-crate.yml index 97a4308..1e89515 100644 --- a/.github/workflows/publish-crate.yml +++ b/.github/workflows/publish-crate.yml @@ -1,32 +1,26 @@ -name: Prepare or publish reviewed Rust crate +name: Publish reviewed Rust crate on: workflow_dispatch: inputs: operation: - description: Prepare evidence or explicitly publish reviewed bytes + description: Publish the crates of a verified automatic source release type: choice - options: [prepare, publish, automatic] - default: prepare - required: true - source_tag: - description: Reviewed published source tag - type: string - default: v2.8.0 + options: [automatic] + default: automatic required: true source_revision: description: Exact reviewed source revision type: string - default: a47537790f5ea8a08adf7e6dffee4fa794cf3665 required: true source_release_id: description: Automatic source release ID type: string - required: false + required: true manifest_sha256: description: Exact automatic release manifest digest type: string - required: false + required: true permissions: contents: read @@ -39,96 +33,6 @@ concurrency: cancel-in-progress: false jobs: - prepare: - if: github.repository == 'atrinik/protocol' && github.repository_id == '1327106950' && github.ref == 'refs/heads/main' && github.event_name == 'workflow_dispatch' && inputs.operation == 'prepare' - runs-on: ubuntu-24.04 - timeout-minutes: 20 - env: - CARGO_HOME: /tmp/atrinik-crate-prepare-cargo - CARGO_TARGET_DIR: /tmp/atrinik-crate-prepare-target - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ github.sha }} - fetch-depth: 0 - persist-credentials: false - - run: rustup toolchain install 1.97.1 --profile minimal - - env: - SOURCE_TAG: ${{ inputs.source_tag }} - SOURCE_REVISION: ${{ inputs.source_revision }} - run: | - set -euo pipefail - python3 tools/crate_publication.py prepare --source-tag "$SOURCE_TAG" \ - --source-revision "$SOURCE_REVISION" --output "$RUNNER_TEMP/crate-prepared" - - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 - with: - name: reviewed-crate-inputs - path: | - ${{ runner.temp }}/crate-prepared/atrinik-protocol-0.2.0.crate - ${{ runner.temp }}/crate-prepared/artifact.json - if-no-files-found: error - retention-days: 7 - - verify: - if: github.repository == 'atrinik/protocol' && github.repository_id == '1327106950' && github.ref == 'refs/heads/main' && github.event_name == 'workflow_dispatch' && inputs.operation == 'publish' - runs-on: ubuntu-24.04 - timeout-minutes: 20 - outputs: - ready: ${{ steps.verify.outputs.ready_for_upload }} - env: - CARGO_HOME: /tmp/atrinik-crate-verify-cargo - CARGO_TARGET_DIR: /tmp/atrinik-crate-verify-target - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ github.sha }} - fetch-depth: 0 - persist-credentials: false - - run: rustup toolchain install 1.97.1 --profile minimal - - id: verify - env: - SOURCE_TAG: ${{ inputs.source_tag }} - SOURCE_REVISION: ${{ inputs.source_revision }} - run: | - set -euo pipefail - python3 tools/crate_publication.py verify-publish --source-tag "$SOURCE_TAG" \ - --source-revision "$SOURCE_REVISION" --output "$RUNNER_TEMP/crate-verified" - - publish: - needs: verify - if: github.repository == 'atrinik/protocol' && github.repository_id == '1327106950' && github.ref == 'refs/heads/main' && github.event_name == 'workflow_dispatch' && inputs.operation == 'publish' && needs.verify.outputs.ready == 'true' - environment: crates-io-release - runs-on: ubuntu-24.04 - timeout-minutes: 20 - permissions: - contents: read - id-token: write - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - ref: ${{ github.sha }} - fetch-depth: 0 - persist-credentials: false - - id: verify - env: - SOURCE_TAG: ${{ inputs.source_tag }} - SOURCE_REVISION: ${{ inputs.source_revision }} - run: | - set -euo pipefail - python3 tools/crate_publication.py verify-upload --source-tag "$SOURCE_TAG" \ - --source-revision "$SOURCE_REVISION" --output "$RUNNER_TEMP/crate-verified" - - id: auth - if: steps.verify.outputs.ready_for_upload == 'true' - uses: rust-lang/crates-io-auth-action@3268eb413133e2960fa4ae0e963ca76380c54cd9 # v1.0.6 - - id: upload - if: steps.verify.outputs.ready_for_upload == 'true' - env: - CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }} - CRATE_PUBLICATION_OPERATION: publish - run: python3 tools/crate_upload.py --crate "$RUNNER_TEMP/crate-verified/atrinik-protocol-0.2.0.crate" - - if: ${{ !cancelled() && (steps.upload.outcome == 'success' || steps.upload.outcome == 'failure') }} - run: python3 tools/crate_publication.py verify-registry - automatic-verify: if: github.repository == 'atrinik/protocol' && github.repository_id == '1327106950' && github.ref == 'refs/heads/main' && github.event_name == 'workflow_dispatch' && inputs.operation == 'automatic' runs-on: ubuntu-24.04 diff --git a/AGENTS.md b/AGENTS.md index 7482590..73e7e37 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -96,8 +96,9 @@ semantics and milestone gates. Do not copy M1-M6 schedules into this guide. - Treat registry publication as an irreversible external operation. Version `0.1.0` completed the one-use bootstrap; its workflow, secret and token stay - retired. Version 0.2.0 is confirmed in the registry; preserve its separately - pinned manual path and original source/archive lineage as historical evidence. Future reviewed version changes publish automatically through + retired. Version 0.2.0 is confirmed in the registry; its manual publication + path is removed and its source/archive lineage stays pinned in + `policy/crate-release.json` as historical evidence. Reviewed version changes publish only through Trusted Publishing in the main-only `crates-io-release` environment, without required reviewers or wait timers. One-time environment/registration setup remains an owner operation, tracked in `github-settings`. diff --git a/README.md b/README.md index e6e5815..8d0533e 100644 --- a/README.md +++ b/README.md @@ -112,10 +112,9 @@ Git revision. ## Rust registry publication -Protocol 0.2.0 is published. Future reviewed Cargo version bumps use the -[automatic library release contract](#automatic-rust-library-releases). The -manual procedure below records the original 0.2.0 publication and its pinned -artifact; it does not govern later versions. The manifest allows only crates.io. +Every Cargo version after 0.2.0 is published only through the +[automatic library release contract](#automatic-rust-library-releases); there is +no manual publication operation. The manifest allows only crates.io. Crate `atrinik-protocol` version `0.1.0` is registered on crates.io with SHA-256 @@ -143,82 +142,27 @@ Read [CONTRIBUTING.md](CONTRIBUTING.md) and [PROVENANCE.md](PROVENANCE.md) before proposing contract material. The cross-repository roadmap is [atrinik/atrinik#168](https://github.com/atrinik/atrinik/issues/168). -## Reviewed access-token crate candidate +## Historical 0.2.0 publication -Crate 0.2.0 was published by the separately authorized -[publication run](https://github.com/atrinik/protocol/actions/runs/38156755961). -The public API, sparse index and downloaded archive agree on the checksum below. -`policy/rust-crate-candidate.json` retains its historical preparation declaration; -`policy/rust-crate-next.json` marks the reviewed artifact `ready-for-publication` -and pins its prepared source release `v2.8.0`, -revision `a47537790f5ea8a08adf7e6dffee4fa794cf3665`, asset -`atrinik-protocol-0.2.0.crate` and SHA-256 +Crate 0.2.0 was published once by a separately authorized manual +[publication run](https://github.com/atrinik/protocol/actions/runs/38156755961) +from source release `v2.8.0`, revision +`a47537790f5ea8a08adf7e6dffee4fa794cf3665`, asset +`atrinik-protocol-0.2.0.crate`, SHA-256 `cda65c3c322993cfab378454fb9b1182df8a000216f4abd1170e53cdfdc3b3bb`. -The approved preparation [run](https://github.com/atrinik/protocol/actions/runs/37225929702) -packaged that clean source twice, and an isolated local reproduction matched. -The immutable published 0.1.0 policy remains unchanged. Later source releases omit these historical registry crates; they cannot -regenerate or replace the prepared bytes. Future reviewed version bumps use -the automatic publication contract below. - -## Preparing and publishing the reviewed Rust crate - -The manual `publish-crate.yml` workflow has explicit `prepare` and `publish` -operations. Its default is `prepare`, which requests no OIDC token and only emits -an Actions artifact. A preparation approval never authorizes publication. Inputs -for publication must match the exact reviewed tag and revision above. Both -operations run only on main in repository ID 1327106950; checkout uses the fixed -workflow-run revision, while packaging uses the separately pinned source release. -Never package activation HEAD as a substitute for the prepared source. - -The one-time publication attached the exact prepared archive to -[v2.8.0](https://github.com/atrinik/protocol/releases/tag/v2.8.0), then verify its -public asset digest. The verifier refuses a missing or mismatched asset; it cannot -substitute an Actions artifact. GitHub reports the source release as mutable, so -repeated tag, provenance and asset-digest checks detect drift rather than claiming -GitHub release immutability. Attaching a new asset also requires a separately -reviewed complete checksum-inventory update; never leave an unchecked extra asset -or silently alter an existing contract/source archive. - -A credential-free `verify` job reproduces the pinned source twice and validates -its release asset, public package/owner identity and registry API/sparse-index -state. Missing or conflicting evidence stops before the protected publication -job. An already published identical version is an idempotent success; different -bytes or an indeterminate registry state stop without upload. - -The publication job requires `operation=publish`, that successful preflight and -the `crates-io-release` environment. The automatic environment is main-only with zero required reviewers, zero wait -time, and no registry secrets or variables. Reviewed merges and successful -checks authorize each release after one-time owner setup. -Environment and crates.io Trusted Publisher setup are separately authorized owner -operations, tracked in [github-settings#86](https://github.com/atrinik/github-settings/pull/86). -Trusted Publishing binds `atrinik/protocol`, `publish-crate.yml` and that exact -environment. Only this job grants `id-token: write`, which gives every step OIDC -request authority. It never installs a toolchain or runs Cargo, package builds, -proc macros or dependency code. After environment admission, `verify-upload` fetches -the exact public release archive as inert bytes and checks its reviewed digest, -inventory and provenance. It freshly checks the workflow checkout, pinned source -ancestry, public tag/release, complete release inventory, owner and registry state -before token exchange. It writes the same checked archive snapshot for upload. -All reproduction remains in the credential-free jobs; packaging also refuses -OIDC request authority at runtime. Only the upload step receives the short-lived -token from the pinned official action; its post step revokes the token. No long-lived registry credential is allowed. - -The bounded uploader verifies the archive snapshot and public state again, then -sends those exact archive bytes using the documented -[Cargo registry publish API](https://doc.rust-lang.org/cargo/reference/registry-web-api.html#publish). -It does not invoke `cargo publish`, which would repackage the source. The request -uses a fixed HTTPS endpoint, refuses redirects, never retries an uncertain write, -and never logs the token or response body. After an attempted upload, the -credential-free verifier checks public package/owner identity and agreement -between the registry API and sparse-index checksum, including when the uploader -fails. It skips cancelled runs and skipped uploads; a matching checksum does not -turn a failed upload step into workflow success. An uncertain result requires -inspection before another separately authorized publication attempt. - -The historical 0.2.0 publication is confirmed by the registry evidence above. -Consumers still require the production registry release and normal dependency-lock -validation; source workflow success alone does not establish registry readiness. -See the [official token action](https://github.com/rust-lang/crates-io-auth-action). +`policy/crate-release.json` keeps that coordinate as immutable historical +lineage. The manual `prepare` and `publish` workflow operations and their tooling +have since been removed; later source releases omit the historical registry +crates and cannot regenerate or replace their bytes. + +The `crates-io-release` environment is main-only with zero required reviewers, +zero wait time, and no registry secrets or variables. Trusted Publishing binds +`atrinik/protocol`, `publish-crate.yml` and that exact environment; environment +and Trusted Publisher setup are separately authorized owner operations, tracked +in [github-settings#86](https://github.com/atrinik/github-settings/pull/86). The +upload step receives only a short-lived token from the pinned +[official token action](https://github.com/rust-lang/crates-io-auth-action); no +long-lived registry credential is allowed. ## Automatic Rust library releases diff --git a/policy/rust-crate-next.json b/policy/rust-crate-next.json deleted file mode 100644 index 439ac4c..0000000 --- a/policy/rust-crate-next.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "schema_version": 1, - "name": "atrinik-protocol", - "version": "0.2.0", - "status": "ready-for-publication", - "artifact": { - "repository_release": "2.8.0", - "revision": "a47537790f5ea8a08adf7e6dffee4fa794cf3665", - "asset": "atrinik-protocol-0.2.0.crate", - "sha256": "cda65c3c322993cfab378454fb9b1182df8a000216f4abd1170e53cdfdc3b3bb" - } -} diff --git a/tools/check-crate-release-policy.py b/tools/check-crate-release-policy.py index bdce1d5..628a3ed 100755 --- a/tools/check-crate-release-policy.py +++ b/tools/check-crate-release-policy.py @@ -18,7 +18,7 @@ BOOTSTRAP_CHECK = ROOT / "tools" / "check-crate-publication.py" SOURCE_WORKFLOW = WORKFLOWS / "release.yml" EXPECTED_SOURCE_WORKFLOW_SHA256 = "13b592fd367f87ebc8c4926a514f110faadf4804206b8a2fd1e19c57b07d17c9" -EXPECTED_PUBLICATION_WORKFLOW_SHA256 = "e1e02758989a4c8432dbcf8422e440041c7604980bb99fb04c003cfb01524a3b" +EXPECTED_PUBLICATION_WORKFLOW_SHA256 = "b0491b93c39cb1b58e73afff2ac49a9aea8854a53c1de2df04a66ca639ffe58a" EXPECTED_WORKFLOWS = { "dependency-review.yml", "pr-title.yml", "release.yml", "validate.yml", "publish-crate.yml", "crate-release-reconcile.yml", @@ -63,7 +63,7 @@ def main() -> None: expected_candidate = { "schema_version": 1, "name": release["name"], "version": workspace_version, "base_published_version": release["version"], - "status": "unpublished", "publication": ("reviewed-manual-publication" if workspace_version == "0.2.0" else "reviewed-automatic-publication"), + "status": "unpublished", "publication": "reviewed-automatic-publication", } if candidate != expected_candidate or not re.fullmatch(r"[0-9]+\.[0-9]+\.[0-9]+", workspace_version): raise SystemExit("unpublished crate candidate policy changed") @@ -92,17 +92,6 @@ def main() -> None: if publishing != expected: raise SystemExit("reviewed Rust registry policy changed") - next_policy = load_json(ROOT / "policy/rust-crate-next.json") - expected_next = {"schema_version": 1, "name": release["name"], - "version": "0.2.0", "status": "ready-for-publication", - "artifact": { - "repository_release": "2.8.0", - "revision": "a47537790f5ea8a08adf7e6dffee4fa794cf3665", - "asset": "atrinik-protocol-0.2.0.crate", - "sha256": "cda65c3c322993cfab378454fb9b1182df8a000216f4abd1170e53cdfdc3b3bb", - }} - if next_policy != expected_next: - raise SystemExit("reviewed next-crate artifact pins changed") if not PUBLISH_WORKFLOW.is_file(): raise SystemExit("reviewed publication workflow is required") # Fence the complete reviewed job/step/permission graph, not substrings that diff --git a/tools/crate_archive.py b/tools/crate_archive.py new file mode 100644 index 0000000..ee54e86 --- /dev/null +++ b/tools/crate_archive.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +"""Check a packaged crate archive against the reviewed inventory and sources.""" +import io +import json +from pathlib import Path +import tarfile +import tomllib + +NAME = 'atrinik-protocol' +MAX_BYTES = 16 * 1024 * 1024 + + +def require(condition, message): + if not condition: + raise ValueError(message) + + +def verify_crate(path, revision, inventory, version): + with (tarfile.open(fileobj=io.BytesIO(path), mode='r:gz') if isinstance(path, bytes) else tarfile.open(path, 'r:gz')) as archive: + members = [] + total = 0 + for member in archive: + total += member.size + require(len(members) < 256 and total <= MAX_BYTES, 'crate archive exceeds bound') + members.append(member) + prefix = f'{NAME}-{version}/' + names = [] + for member in members: + require(member.isfile() and member.name.startswith(prefix), 'unexpected archive member') + name = member.name[len(prefix):] + require(name and '..' not in Path(name).parts and not name.startswith('/'), 'unsafe archive path') + names.append(name) + require(len(names) == len(set(names)) and set(names) == set(inventory), 'crate file inventory mismatch') + def read(name): + return archive.extractfile(prefix + name).read() + vcs = json.loads(read('.cargo_vcs_info.json')) + require(vcs == {'git': {'sha1': revision}, 'path_in_vcs': 'crates/atrinik-protocol'}, 'crate VCS provenance mismatch') + manifest = tomllib.loads(read('Cargo.toml').decode()) + package = manifest['package'] + require(package['name'] == NAME and package['version'] == version and package.get('publish') == ['crates-io'], 'crate is not the publishable reviewed version') + def no_source_override(value): + if isinstance(value, dict): + require('path' not in value and 'git' not in value, 'unapproved crate dependency source') + for child in value.values(): + no_source_override(child) + elif isinstance(value, list): + for child in value: + no_source_override(child) + for key in ('dependencies', 'dev-dependencies', 'build-dependencies', 'target'): + no_source_override(manifest.get(key, {})) + lock = tomllib.loads(read('Cargo.lock').decode()) + for package in lock['package']: + if package['name'] == NAME and package['version'] == version: + require('source' not in package, 'unexpected root crate source') + else: + require(package.get('source') == 'registry+https://github.com/rust-lang/crates.io-index', 'unapproved locked dependency source') diff --git a/tools/crate_publication.py b/tools/crate_publication.py deleted file mode 100644 index f554602..0000000 --- a/tools/crate_publication.py +++ /dev/null @@ -1,356 +0,0 @@ -#!/usr/bin/env python3 -"""Isolated preparation and inert publication verification; never uploads.""" -from __future__ import annotations - -import argparse -import hashlib -import json -import io -import os -from pathlib import Path -import re -import shutil -import subprocess -import tarfile -import time -import tomllib -import urllib.error -import urllib.request - -ROOT = Path(__file__).resolve().parent.parent -NAME = 'atrinik-protocol' -VERSION = '0.2.0' -ASSET = f'{NAME}-{VERSION}.crate' -MAX_BYTES = 16 * 1024 * 1024 -API = 'https://api.github.com/repos/atrinik/protocol' - - -def require(condition, message): - if not condition: - raise ValueError(message) - - -def load_policy(path): - policy = json.loads(Path(path).read_text()) - require(set(policy) == {'schema_version', 'name', 'version', 'status', 'artifact'}, 'unexpected policy fields') - require(policy['schema_version'] == 1 and policy['name'] == NAME and policy['version'] == VERSION, 'unexpected crate identity') - if policy['status'] == 'awaiting-source-release': - require(policy['artifact'] is None, 'pending policy must not invent artifact pins') - else: - require(policy['status'] == 'ready-for-publication', 'unknown publication state') - validate_artifact(policy['artifact']) - return policy - - -def validate_artifact(value): - require(isinstance(value, dict) and set(value) == {'repository_release', 'revision', 'asset', 'sha256'}, 'incomplete artifact pins') - require(re.fullmatch(r'[0-9]+\.[0-9]+\.[0-9]+', value['repository_release']) is not None, 'invalid release version') - require(re.fullmatch(r'[0-9a-f]{40}', value['revision']) is not None, 'invalid source revision') - require(value['asset'] == ASSET and re.fullmatch(r'[0-9a-f]{64}', value['sha256']) is not None, 'invalid artifact identity') - - -def execution_boundary(): - require(not any(os.environ.get(key) for key in ('CARGO_REGISTRY_TOKEN', 'CARGO_REGISTRIES_CRATES_IO_TOKEN', 'CARGO_REGISTRY_BOOTSTRAP_TOKEN')), 'preparation must run without registry credentials') - if os.environ.get('GITHUB_ACTIONS') == 'true': - expected = {'GITHUB_REPOSITORY': 'atrinik/protocol', 'GITHUB_REPOSITORY_ID': '1327106950', - 'GITHUB_REF': 'refs/heads/main', 'GITHUB_EVENT_NAME': 'workflow_dispatch', - 'GITHUB_WORKFLOW_REF': 'atrinik/protocol/.github/workflows/publish-crate.yml@refs/heads/main'} - require(all(os.environ.get(k) == v for k, v in expected.items()), 'unexpected publication workflow identity') - - -class SafeRedirect(urllib.request.HTTPRedirectHandler): - def redirect_request(self, req, fp, code, msg, headers, newurl): - from urllib.parse import urlparse - parsed = urlparse(newurl) - require(parsed.scheme == 'https' and parsed.hostname in {'github.com', 'release-assets.githubusercontent.com', 'objects.githubusercontent.com', 'crates.io', 'index.crates.io'}, 'unexpected download redirect') - return super().redirect_request(req, fp, code, msg, headers, newurl) - - -def download(url, absent=False): - request = urllib.request.Request(url, headers={'User-Agent': 'atrinik-protocol-release-verifier (https://github.com/atrinik/protocol)', 'Accept': 'application/json'}) - try: - with urllib.request.build_opener(SafeRedirect).open(request, timeout=20) as response: - data = response.read(MAX_BYTES + 1) - require(len(data) <= MAX_BYTES, 'response exceeds bound') - return data - except urllib.error.HTTPError as error: - if absent and error.code == 404: - return None - raise - - -def git(*arguments, cwd=ROOT): - return subprocess.check_output(['git', *arguments], cwd=cwd, text=True, timeout=60).strip() - - -def release(tag, revision): - require(re.fullmatch(r'v[0-9]+\.[0-9]+\.[0-9]+', tag) is not None, 'invalid source tag') - require(re.fullmatch(r'[0-9a-f]{40}', revision) is not None, 'invalid source revision') - require(git('rev-parse', '--verify', f'{tag}^{{commit}}') == revision, 'source tag drift') - subprocess.run(['git', 'merge-base', '--is-ancestor', revision, 'origin/main'], cwd=ROOT, check=True, timeout=60) - remote = json.loads(download(f'{API}/git/ref/tags/{tag}'))['object'] - for _ in range(2): - if remote['type'] != 'tag': - break - remote = json.loads(download(f"{API}/git/tags/{remote['sha']}"))['object'] - require(remote['type'] == 'commit' and remote['sha'] == revision, 'remote source tag drift') - value = json.loads(download(f'{API}/releases/tags/{tag}')) - require(value['tag_name'] == tag and not value['draft'] and not value['prerelease'] and value.get('published_at'), 'source release is not published') - provenance = json.loads(release_asset(value, tag, 'provenance.json')) - require(provenance['revision'] == revision and provenance['version'] == tag[1:], 'release provenance drift') - return value - - -def release_asset(value, tag, name): - assets = [asset for asset in value['assets'] if asset['name'] == name] - require(len(assets) == 1 and assets[0]['state'] == 'uploaded', 'missing or duplicate release asset') - asset = assets[0] - url = f'https://github.com/atrinik/protocol/releases/download/{tag}/{name}' - require(asset['browser_download_url'] == url and asset['size'] <= MAX_BYTES, 'unexpected release asset location or bound') - data = download(url) - digest = 'sha256:' + hashlib.sha256(data).hexdigest() - require(asset.get('digest') == digest and len(data) == asset['size'], 'release API asset digest mismatch') - return data - - -def verify_release_inventory(value, tag): - assets = value['assets'] - require(isinstance(assets, list) and 1 <= len(assets) <= 128, - 'release asset inventory exceeds bound') - expected = {} - for asset in assets: - name = asset['name'] - require(re.fullmatch(r'[A-Za-z0-9_.+-]+', name) is not None and - name not in expected and asset.get('state') == 'uploaded' and - isinstance(asset.get('size'), int) and 0 < asset['size'] <= MAX_BYTES, - 'unsafe or duplicate release asset') - digest = asset.get('digest', '') - require(re.fullmatch(r'sha256:[0-9a-f]{64}', digest) is not None, - 'release asset digest is missing') - expected[name] = digest[7:] - manifest = release_asset(value, tag, 'SHA256SUMS') - require(len(manifest) <= 32768, 'release checksum manifest exceeds bound') - listed = {} - for line in manifest.decode('ascii').splitlines(): - match = re.fullmatch(r'([0-9a-f]{64}) ([A-Za-z0-9_.+-]+)', line) - require(match is not None, 'malformed release checksum entry') - digest, name = match.groups() - require(name not in listed and name != 'SHA256SUMS', 'duplicate or recursive release checksum') - listed[name] = digest - del expected['SHA256SUMS'] - require(listed == expected, 'release checksums do not cover exact asset inventory') - - -def verify_crate(path, revision, inventory, version=VERSION): - with (tarfile.open(fileobj=io.BytesIO(path), mode='r:gz') if isinstance(path, bytes) else tarfile.open(path, 'r:gz')) as archive: - members = [] - total = 0 - for member in archive: - total += member.size - require(len(members) < 256 and total <= MAX_BYTES, 'crate archive exceeds bound') - members.append(member) - prefix = f'{NAME}-{version}/' - names = [] - for member in members: - require(member.isfile() and member.name.startswith(prefix), 'unexpected archive member') - name = member.name[len(prefix):] - require(name and '..' not in Path(name).parts and not name.startswith('/'), 'unsafe archive path') - names.append(name) - require(len(names) == len(set(names)) and set(names) == set(inventory), 'crate file inventory mismatch') - def read(name): - return archive.extractfile(prefix + name).read() - vcs = json.loads(read('.cargo_vcs_info.json')) - require(vcs == {'git': {'sha1': revision}, 'path_in_vcs': 'crates/atrinik-protocol'}, 'crate VCS provenance mismatch') - manifest = tomllib.loads(read('Cargo.toml').decode()) - package = manifest['package'] - require(package['name'] == NAME and package['version'] == version and package.get('publish') == ['crates-io'], 'crate is not the publishable reviewed version') - def no_source_override(value): - if isinstance(value, dict): - require('path' not in value and 'git' not in value, 'unapproved crate dependency source') - for child in value.values(): - no_source_override(child) - elif isinstance(value, list): - for child in value: - no_source_override(child) - for key in ('dependencies', 'dev-dependencies', 'build-dependencies', 'target'): - no_source_override(manifest.get(key, {})) - lock = tomllib.loads(read('Cargo.lock').decode()) - for package in lock['package']: - if package['name'] == NAME and package['version'] == version: - require('source' not in package, 'unexpected root crate source') - else: - require(package.get('source') == 'registry+https://github.com/rust-lang/crates.io-index', 'unapproved locked dependency source') - - -def verify_registry_identity(): - package = json.loads(download(f'https://crates.io/api/v1/crates/{NAME}'))['crate'] - require(package.get('id') == NAME and package.get('name') == NAME and - package.get('repository') == 'https://github.com/atrinik/protocol', - 'public registry package identity changed') - owners = json.loads(download(f'https://crates.io/api/v1/crates/{NAME}/owners'))['users'] - require(isinstance(owners, list) and 1 <= len(owners) <= 100 and any( - owner.get('kind') == 'user' and owner.get('id') == 437663 and - owner.get('login') == 'zoeyrose' and owner.get('github_username_matches') is True - for owner in owners), 'public registry owner identity changed') - - -def registry_state(digest): - api = download(f'https://crates.io/api/v1/crates/{NAME}/{VERSION}', absent=True) - index = download(f'https://index.crates.io/at/ri/{NAME}', absent=True) - version = None if api is None else json.loads(api)['version'] - if version is not None: - require(version.get('crate') == NAME and version.get('num') == VERSION and - version.get('yanked') is False, 'registry version identity or availability changed') - api_digest = None if version is None else version['checksum'] - entries = [] if index is None else [json.loads(line) for line in index.splitlines() if line] - matches = [entry for entry in entries if entry['vers'] == VERSION] - require(len(matches) <= 1, 'duplicate registry index version') - require(not matches or (matches[0].get('name') == NAME and matches[0].get('yanked') is False), 'registry index identity or availability changed') - index_digest = None if not matches else matches[0]['cksum'] - require(all(value in (None, digest) for value in (api_digest, index_digest)), 'registry checksum conflict; never republish') - if api_digest == digest and index_digest == digest: - return 'present' - if api_digest is None and index_digest is None: - return 'absent' - return 'indeterminate' - - -def prepare(tag, revision, output, policy, publishing=False): - execution_boundary() - require(not any(os.environ.get(key) for key in - ('ACTIONS_ID_TOKEN_REQUEST_URL', 'ACTIONS_ID_TOKEN_REQUEST_TOKEN')), - 'packaging must run without OIDC request authority') - require(not any(char in str(output) for char in '\r\n'), 'invalid output path') - require(not output.exists() and not output.is_relative_to(ROOT), 'output must be absent and outside source checkout') - value = release(tag, revision) - require(subprocess.check_output(['rustc', '+1.97.1', '--version'], text=True, timeout=60).startswith('rustc 1.97.1 '), 'wrong Rust toolchain') - output.mkdir(parents=True) - source = output / 'source' - subprocess.run(['git', 'worktree', 'add', '--detach', str(source), revision], cwd=ROOT, check=True, timeout=60) - require(not git('status', '--porcelain', cwd=source), 'dirty source worktree') - manifest = tomllib.loads((source / 'crates/atrinik-protocol/Cargo.toml').read_text()) - require(manifest['package'].get('publish') == ['crates-io'], 'source release has publication disabled; prepare a reviewed publishable source release first') - inventory = (source / 'policy/rust-crate-files.txt').read_text().splitlines() - packages = [] - for number in (1, 2): - target = output / f'package-{number}' - subprocess.run(['cargo', '+1.97.1', 'package', '--locked', '--manifest-path', str(source / 'crates/atrinik-protocol/Cargo.toml'), '--target-dir', str(target)], cwd=source, check=True, timeout=600) - package = target / 'package' / ASSET - verify_crate(package, revision, inventory) - packages.append(package) - require(not git('status', '--porcelain', cwd=source), 'packaging changed source') - first = packages[0].read_bytes() - require(first == packages[1].read_bytes(), 'crate reproduction mismatch') - digest = hashlib.sha256(first).hexdigest() - artifact = {'repository_release': tag[1:], 'revision': revision, 'asset': ASSET, 'sha256': digest} - ready = False - if publishing: - require(artifact == policy['artifact'], 'reproduced artifact differs from reviewed pins') - require(release_asset(value, tag, ASSET) == first, 'published release asset differs from reproduced crate') - verify_release_inventory(value, tag) - verify_registry_identity() - state = registry_state(digest) - require(state != 'indeterminate', 'registry result indeterminate; re-read before any retry') - ready = state == 'absent' - # Re-read public release and local ref after expensive preparation. - current = release(tag, revision) - require(current['id'] == value['id'], 'release identity changed during preparation') - if publishing: - require(release_asset(current, tag, ASSET) == first, 'release asset changed during preparation') - verify_release_inventory(current, tag) - shutil.copyfile(packages[0], output / ASSET) - (output / 'artifact.json').write_text(json.dumps(artifact, indent=2) + '\n') - if os.environ.get('GITHUB_OUTPUT'): - with open(os.environ['GITHUB_OUTPUT'], 'a') as stream: - stream.write(f'ready_for_upload={str(ready).lower()}\nsource_directory={source}\n') - print(json.dumps({'artifact': artifact, 'ready_for_upload': ready, 'github_release_immutable': current.get('immutable', False)})) - - -def verify_upload_boundary(policy_path, policy): - """Only reviewed workflow code and inert source data may run with OIDC.""" - execution_boundary() - require(os.environ.get('GITHUB_ACTIONS') == 'true', 'protected verification requires Actions') - require(policy_path.resolve() == ROOT / 'policy/rust-crate-next.json', 'unexpected publication policy path') - sha = os.environ.get('GITHUB_SHA', '') - require(re.fullmatch(r'[0-9a-f]{40}', sha) is not None and - os.environ.get('GITHUB_WORKFLOW_SHA') == sha, 'unexpected workflow revision') - require(git('rev-parse', 'HEAD') == sha and not git('status', '--porcelain') and - load_policy(policy_path) == policy, 'publication checkout or policy changed') - - -def verify_upload(tag, revision, output, policy, policy_path): - """Recheck public evidence after approval without executing package code. - - Git reads pinned objects only. Never create a source worktree, install a - toolchain, run Cargo, or extract/execute archive members in the OIDC job. - """ - verify_upload_boundary(policy_path, policy) - require(not any(char in str(output) for char in '\r\n'), 'invalid output path') - require(not output.exists() and not output.is_relative_to(ROOT), 'output must be absent and outside source checkout') - artifact = policy['artifact'] - value = release(tag, revision) - data = release_asset(value, tag, artifact['asset']) - require(hashlib.sha256(data).hexdigest() == artifact['sha256'], 'release archive differs from reviewed pins') - inventory = git('show', revision + ':policy/rust-crate-files.txt').splitlines() - verify_crate(data, revision, inventory) - verify_release_inventory(value, tag) - # Re-read the mutable source release; validate the same in-memory snapshot - # that is written for the uploader, whose own digest check runs again. - current = release(tag, revision) - require(current['id'] == value['id'], 'release identity changed during verification') - require(release_asset(current, tag, artifact['asset']) == data, 'release asset changed during verification') - verify_release_inventory(current, tag) - verify_upload_boundary(policy_path, policy) - verify_registry_identity() - state = registry_state(artifact['sha256']) - require(state != 'indeterminate', 'registry result indeterminate; re-read before any retry') - ready = state == 'absent' - output.mkdir(parents=True) - with (output / ASSET).open('xb') as stream: - stream.write(data) - if os.environ.get('GITHUB_OUTPUT'): - with open(os.environ['GITHUB_OUTPUT'], 'a') as stream: - stream.write(f'ready_for_upload={str(ready).lower()}\n') - print(json.dumps({'artifact': artifact, 'ready_for_upload': ready, - 'github_release_immutable': current.get('immutable', False)})) - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument('operation', choices=('prepare', 'verify-publish', 'verify-upload', 'verify-registry')) - parser.add_argument('--policy', type=Path, default=ROOT / 'policy/rust-crate-next.json') - parser.add_argument('--source-tag') - parser.add_argument('--source-revision') - parser.add_argument('--output', type=Path) - args = parser.parse_args() - execution_boundary() - policy = load_policy(args.policy) - if args.operation == 'prepare': - require(args.source_tag is not None and args.source_revision is not None and args.output is not None, 'prepare requires exact source coordinates and output') - prepare(args.source_tag, args.source_revision, args.output.resolve(), policy) - else: - require(policy['status'] == 'ready-for-publication', 'publication disabled until actual source-release artifact pins are reviewed') - artifact = policy['artifact'] - if args.operation in ('verify-publish', 'verify-upload'): - require(args.output is not None, 'verification output required') - require(args.source_tag == 'v' + artifact['repository_release'] and args.source_revision == artifact['revision'], 'publication inputs differ from reviewed artifact pins') - if args.operation == 'verify-publish': - prepare(args.source_tag, args.source_revision, args.output.resolve(), policy, True) - else: - verify_upload(args.source_tag, args.source_revision, args.output.resolve(), policy, args.policy) - else: - for attempt in range(6): - verify_registry_identity() - if registry_state(artifact['sha256']) == 'present': - print('Public registry API and sparse-index checksums match reviewed artifact') - return - if attempt < 5: - time.sleep(5) - raise ValueError('registry verification indeterminate; inspect public state before any retry') - - -if __name__ == '__main__': - try: - main() - except (ValueError, KeyError, TypeError, OSError, subprocess.SubprocessError) as error: - raise SystemExit(f'crate verification failed: {error}') from error diff --git a/tools/crate_upload.py b/tools/crate_upload.py deleted file mode 100644 index c673ee7..0000000 --- a/tools/crate_upload.py +++ /dev/null @@ -1,226 +0,0 @@ -#!/usr/bin/env python3 -"""Upload only the exact reviewed archive from the dedicated publication job. - -Framing and metadata follow the Cargo registry API, independently implemented: -https://doc.rust-lang.org/cargo/reference/registry-web-api.html#publish -A successful response is acceptance, not public checksum verification. The -workflow must verify public registry state in a separate credential-free step. -""" -from __future__ import annotations - -import argparse -import hashlib -import io -import json -import os -from pathlib import Path -import re -import stat -import struct -import sys -import tarfile -import time -import tomllib -import urllib.request -import urllib.error - -# Importing the verifier must not dirty the checked-out publication source. -sys.dont_write_bytecode = True - -if __package__: - from . import crate_publication as P -else: - import crate_publication as P - -ENDPOINT = 'https://crates.io/api/v1/crates/new' -MAX_RESPONSE = 64 * 1024 -MAX_METADATA = 256 * 1024 - - -def execution_boundary(policy_path): - """The credential-bearing stage has its own mandatory workflow boundary.""" - expected = { - 'GITHUB_ACTIONS': 'true', 'GITHUB_REPOSITORY': 'atrinik/protocol', - 'GITHUB_REPOSITORY_ID': '1327106950', 'GITHUB_REF': 'refs/heads/main', - 'GITHUB_EVENT_NAME': 'workflow_dispatch', - 'GITHUB_WORKFLOW_REF': 'atrinik/protocol/.github/workflows/publish-crate.yml@refs/heads/main', - 'CRATE_PUBLICATION_OPERATION': 'publish', - } - P.require(all(os.environ.get(k) == v for k, v in expected.items()), 'unexpected upload workflow identity') - P.require(not any(os.environ.get(k) for k in ('CARGO_REGISTRIES_CRATES_IO_TOKEN', 'CARGO_REGISTRY_BOOTSTRAP_TOKEN')), 'unexpected registry credential source') - P.require(policy_path.resolve() == P.ROOT / 'policy/rust-crate-next.json', 'unexpected publication policy path') - sha = os.environ.get('GITHUB_SHA', '') - P.require(re.fullmatch(r'[0-9a-f]{40}', sha) is not None and os.environ.get('GITHUB_WORKFLOW_SHA') == sha, 'unexpected workflow revision') - - -def text(value): - P.require(isinstance(value, str) and 0 < len(value) <= 8192, 'unsupported manifest text') - return value - - -def strings(value, maximum): - P.require(isinstance(value, list) and len(value) <= maximum, 'unsupported manifest list') - return [text(item) for item in value] - - -def metadata(data): - """Map the reviewed normalized manifest; fail closed on new constructs.""" - with tarfile.open(fileobj=io.BytesIO(data), mode='r:gz') as archive: - prefix = f'{P.NAME}-{P.VERSION}/' - raw_manifest = archive.extractfile(prefix + 'Cargo.toml').read(MAX_METADATA + 1) - P.require(len(raw_manifest) <= MAX_METADATA, 'manifest exceeds bound') - manifest = tomllib.loads(raw_manifest.decode()) - P.require(set(manifest) == {'package', 'lib', 'dependencies'}, 'unsupported manifest sections') - package = manifest['package'] - P.require(isinstance(package, dict), 'unsupported package metadata') - allowed = {'name', 'version', 'edition', 'rust-version', 'build', 'publish', - 'autolib', 'autobins', 'autoexamples', 'autotests', 'autobenches', - 'description', 'homepage', 'documentation', 'readme', 'keywords', - 'license', 'repository', 'authors', 'categories'} - P.require(set(package) <= allowed, 'unsupported package fields') - P.require(package.get('name') == P.NAME and package.get('version') == P.VERSION - and package.get('publish') == ['crates-io'] and package.get('edition') == '2024', 'unexpected package identity') - P.require(all(package.get(key) is False for key in ('build', 'autolib', 'autobins', 'autoexamples', 'autotests', 'autobenches')), 'unsupported package targets') - P.require(manifest['lib'] == {'name': 'atrinik_protocol', 'path': 'src/lib.rs'}, 'unsupported library target') - P.require(package.get('readme') == 'README.md' and package.get('license') == 'MIT' - and package.get('repository') == 'https://github.com/atrinik/protocol', 'unexpected package metadata') - dependencies = manifest['dependencies'] - P.require(isinstance(dependencies, dict) and set(dependencies) == {'bytes', 'idna', 'prost'}, 'unsupported dependencies') - deps = [] - for name, dependency in sorted(dependencies.items()): - P.require(isinstance(dependency, dict) and set(dependency) == {'version'}, 'unsupported dependency fields') - version = text(dependency['version']) - P.require(re.fullmatch(r'[0-9]+\.[0-9]+\.[0-9]+', version) is not None, 'unsupported dependency requirement') - deps.append({'name': name, 'version_req': '^' + version, 'features': [], - 'optional': False, 'default_features': True, 'target': None, - 'kind': 'normal', 'registry': None, 'explicit_name_in_toml': None}) - readme = archive.extractfile(prefix + 'README.md').read(MAX_METADATA + 1) - P.require(len(readme) <= MAX_METADATA, 'readme exceeds bound') - result = { - 'name': P.NAME, 'vers': P.VERSION, 'deps': deps, 'features': {}, - 'authors': strings(package.get('authors', []), 16), - 'description': text(package['description']), - 'documentation': text(package['documentation']) if 'documentation' in package else None, - 'homepage': text(package['homepage']) if 'homepage' in package else None, - 'readme': readme.decode(), 'readme_file': 'README.md', - 'keywords': strings(package.get('keywords', []), 5), - 'categories': strings(package.get('categories', []), 5), - 'license': 'MIT', 'license_file': None, 'repository': package['repository'], - 'badges': {}, 'links': None, 'rust_version': text(package['rust-version']), - } - encoded = json.dumps(result, separators=(',', ':'), ensure_ascii=True).encode() - P.require(len(encoded) <= MAX_METADATA, 'metadata exceeds bound') - return encoded - - -class NoRedirect(urllib.request.HTTPRedirectHandler): - def redirect_request(self, req, fp, code, msg, headers, newurl): - raise ValueError('upload redirect refused') - - -def put_once(data, encoded, token): - """No retries, no redirects, and no untrusted response or exception output.""" - body = struct.pack(' MAX_RESPONSE: - return 'indeterminate' - result = json.loads(raw) - if not isinstance(result, dict) or 'errors' in result: - return 'indeterminate' - return 'success' - except urllib.error.HTTPError as error: - # Close the response without reading or formatting its untrusted reason. - error.close() - return 'indeterminate' - except Exception: - # Even an HTTP error can follow server-side acceptance. Recheck the - # public registry in the credential-free verifier before any new run. - return 'indeterminate' - - -def upload(crate_path, policy_path): - execution_boundary(policy_path) - # This dedicated boundary permits the action's short-lived token. Remove - # it from the environment before Git subprocesses; never invoke or weaken - # the separate credential-free preparation boundary. - token = os.environ.pop('CARGO_REGISTRY_TOKEN', None) - P.require(P.git('rev-parse', 'HEAD') == os.environ['GITHUB_SHA'] - and not P.git('status', '--porcelain'), 'unreviewed publication checkout') - policy = P.load_policy(policy_path) - P.require(policy['status'] == 'ready-for-publication', 'publication policy is not ready') - artifact = policy['artifact'] - descriptor = os.open(crate_path, os.O_RDONLY | os.O_NONBLOCK | os.O_NOFOLLOW) - with os.fdopen(descriptor, 'rb') as stream: - info = os.fstat(stream.fileno()) - P.require(stat.S_ISREG(info.st_mode) and info.st_size <= P.MAX_BYTES, 'archive is not a bounded regular file') - data = stream.read(P.MAX_BYTES + 1) - P.require(len(data) <= P.MAX_BYTES and hashlib.sha256(data).hexdigest() == artifact['sha256'], 'archive digest mismatch') - inventory = P.git('show', artifact['revision'] + ':policy/rust-crate-files.txt').splitlines() - P.verify_crate(data, artifact['revision'], inventory) - encoded = metadata(data) - tag = 'v' + artifact['repository_release'] - released = P.release(tag, artifact['revision']) - P.require(P.release_asset(released, tag, artifact['asset']) == data, 'release archive mismatch') - P.verify_release_inventory(released, tag) - execution_boundary(policy_path) - P.require(P.git('rev-parse', 'HEAD') == os.environ['GITHUB_SHA'] - and not P.git('status', '--porcelain') - and P.load_policy(policy_path) == policy, 'publication policy changed during preflight') - P.verify_registry_identity() - state = P.registry_state(artifact['sha256']) - if state == 'present': - return 'skipped' - P.require(state == 'absent', 'registry state indeterminate') - P.require(isinstance(token, str) and 0 < len(token) <= 8192 - and all(33 <= ord(character) <= 126 for character in token), 'missing or invalid upload credential') - outcome = put_once(data, encoded, token) - if outcome == 'indeterminate': - return resolve_indeterminate(artifact['sha256']) - return outcome - - -def resolve_indeterminate(digest): - """Resolve an uncertain write with bounded reads only, never another PUT.""" - try: - P.verify_registry_identity() - # The caller supplies the same policy digest used for the upload. - for attempt in range(6): - state = P.registry_state(digest) - if state == 'present': - return 'success' - if attempt < 5: - time.sleep(5) - except Exception: - pass - return 'indeterminate' - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument('--crate', required=True, type=Path) - parser.add_argument('--policy', type=Path, default=P.ROOT / 'policy/rust-crate-next.json') - args = parser.parse_args() - try: - outcome = upload(args.crate, args.policy) - except Exception: - # Public artifacts, paths, HTTP errors, and credentials never become - # diagnostics here. A failed preflight cannot authorize a PUT. - outcome = 'indeterminate' - print(json.dumps({'outcome': outcome})) - return 0 if outcome in ('success', 'skipped') else 1 - - -if __name__ == '__main__': - raise SystemExit(main()) diff --git a/tools/package-release.sh b/tools/package-release.sh index 09fb1be..afe7c46 100755 --- a/tools/package-release.sh +++ b/tools/package-release.sh @@ -39,7 +39,7 @@ metadata_crate_version=$(cargo metadata --locked --offline --no-deps \ | jq -er --arg name "${crate_name}" \ '.packages[] | select(.name == $name) | .version') # Source-release packaging never uploads or replaces the immutable registry -# artifact; reviewed manual publication is a separate workflow operation. +# artifact; registry publication is a separate automatic workflow operation. python3 tools/check-crate-release-policy.py if [[ ${metadata_crate_version} != "${crate_version}" ]]; then test "$(jq -er '.version' policy/rust-crate-candidate.json)" = "${metadata_crate_version}" diff --git a/tools/test_crate_archive.py b/tools/test_crate_archive.py new file mode 100644 index 0000000..5c36165 --- /dev/null +++ b/tools/test_crate_archive.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +import importlib.util +import io +import json +from pathlib import Path +import tarfile +import tempfile +import unittest + +SPEC = importlib.util.spec_from_file_location('crate_archive', Path(__file__).with_name('crate_archive.py')) +A = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(A) +REVISION = 'a' * 40 +VERSION = '9.8.7' + + +class CrateArchiveTests(unittest.TestCase): + def test_crate_identity_inventory_and_dependency_checks(self): + entries = { + '.cargo_vcs_info.json': json.dumps({'git': {'sha1': REVISION}, 'path_in_vcs': 'crates/atrinik-protocol'}).encode(), + 'Cargo.toml': f'[package]\nname="atrinik-protocol"\nversion="{VERSION}"\npublish=["crates-io"]\n'.encode(), + 'Cargo.lock': f'version=4\n[[package]]\nname="atrinik-protocol"\nversion="{VERSION}"\n'.encode(), + } + with tempfile.TemporaryDirectory() as temporary: + path = Path(temporary) / f'{A.NAME}-{VERSION}.crate' + def write(data, version=VERSION): + with tarfile.open(path, 'w:gz') as archive: + for name, content in data.items(): + member = tarfile.TarInfo(f'{A.NAME}-{version}/{name}') + member.size = len(content) + archive.addfile(member, io.BytesIO(content)) + write(entries) + A.verify_crate(path, REVISION, entries, VERSION) + A.verify_crate(path.read_bytes(), REVISION, entries, VERSION) + with self.assertRaisesRegex(ValueError, 'unexpected archive member'): + A.verify_crate(path, REVISION, entries, '9.8.6') + for name, replacement, error in [ + ('.cargo_vcs_info.json', b'{"git":{"sha1":"wrong"}}', 'VCS'), + ('Cargo.toml', entries['Cargo.toml'].replace(b'["crates-io"]', b'false'), 'publishable'), + ('Cargo.toml', entries['Cargo.toml'] + b'[dependencies.x]\npath="../x"\n', 'dependency'), + ('Cargo.lock', entries['Cargo.lock'] + b'[[package]]\nname="x"\nversion="1"\nsource="git+evil"\n', 'dependency'), + ]: + write({**entries, name: replacement}) + with self.subTest(name=name, error=error), self.assertRaisesRegex(ValueError, error): + A.verify_crate(path, REVISION, entries, VERSION) + write({**entries, 'unexpected': b'x'}) + with self.assertRaisesRegex(ValueError, 'inventory'): + A.verify_crate(path, REVISION, entries, VERSION) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/test_crate_publication.py b/tools/test_crate_publication.py deleted file mode 100644 index 08342b0..0000000 --- a/tools/test_crate_publication.py +++ /dev/null @@ -1,357 +0,0 @@ -"""Network-free tests for isolated builds and inert publication verification.""" -import importlib.util -import io -import json -import os -from pathlib import Path -import tarfile -import tempfile -import unittest -from unittest.mock import patch - -SPEC = importlib.util.spec_from_file_location('publication', Path(__file__).with_name('crate_publication.py')) -P = importlib.util.module_from_spec(SPEC) -SPEC.loader.exec_module(P) -REVISION = 'a' * 40 -DIGEST = 'b' * 64 -ARTIFACT = {'repository_release': '2.7.0', 'revision': REVISION, - 'asset': P.ASSET, 'sha256': DIGEST} - - -class PublicationTests(unittest.TestCase): - def policy(self, artifact=None): - return {'schema_version': 1, 'name': P.NAME, 'version': P.VERSION, - 'status': 'awaiting-source-release' if artifact is None else 'ready-for-publication', - 'artifact': artifact} - - def test_pending_policy_cannot_publish(self): - with tempfile.TemporaryDirectory() as temporary: - path = Path(temporary) / 'policy.json' - path.write_text(json.dumps(self.policy())) - with patch.dict(os.environ, {}, clear=True), patch('sys.argv', ['checker', 'verify-publish', '--policy', str(path)]), patch.object(P, 'prepare') as prepare: - with self.assertRaisesRegex(ValueError, 'publication disabled'): - P.main() - prepare.assert_not_called() - - def test_pending_policy_rejects_fake_pins(self): - with tempfile.TemporaryDirectory() as temporary: - path = Path(temporary) / 'policy.json' - value = self.policy() - value['artifact'] = ARTIFACT - path.write_text(json.dumps(value)) - with self.assertRaisesRegex(ValueError, 'must not invent'): - P.load_policy(path) - - def test_artifact_requires_complete_exact_pins(self): - P.validate_artifact(ARTIFACT) - for key, value in [('revision', 'main'), ('sha256', 'unknown'), ('asset', '../x'), ('repository_release', 'latest')]: - with self.subTest(key=key), self.assertRaises(ValueError): - P.validate_artifact({**ARTIFACT, key: value}) - with self.assertRaises(ValueError): - P.validate_artifact({}) - - def test_registry_states_and_conflict(self): - def values(api, index): - return [None if api is None else json.dumps({'version': {'crate': P.NAME, 'num': P.VERSION, 'yanked': False, 'checksum': api}}).encode(), - None if index is None else json.dumps({'name': P.NAME, 'vers': P.VERSION, 'yanked': False, 'cksum': index}).encode()] - for api, index, expected in [(None, None, 'absent'), (DIGEST, DIGEST, 'present'), (DIGEST, None, 'indeterminate')]: - with patch.object(P, 'download', side_effect=values(api, index)): - self.assertEqual(P.registry_state(DIGEST), expected) - with patch.object(P, 'download', side_effect=values('c' * 64, DIGEST)), self.assertRaisesRegex(ValueError, 'conflict'): - P.registry_state(DIGEST) - - def test_registry_package_and_owner_identity(self): - package = {'crate': {'id': P.NAME, 'name': P.NAME, - 'repository': 'https://github.com/atrinik/protocol'}} - owners = {'users': [{'kind': 'user', 'id': 437663, 'login': 'zoeyrose', - 'github_username_matches': True}]} - with patch.object(P, 'download', side_effect=[json.dumps(package).encode(), json.dumps(owners).encode()]): - P.verify_registry_identity() - for bad_package, bad_owners in ( - ({'crate': {**package['crate'], 'repository': 'https://example.invalid'}}, owners), - (package, {'users': [{**owners['users'][0], 'id': 1}]}), - (package, {'users': []}), - ): - with self.subTest(package=bad_package, owners=bad_owners), patch.object(P, 'download', side_effect=[json.dumps(bad_package).encode(), json.dumps(bad_owners).encode()]): - with self.assertRaisesRegex(ValueError, 'identity changed'): - P.verify_registry_identity() - - def test_publish_inputs_must_match_reviewed_pins(self): - with tempfile.TemporaryDirectory() as temporary: - path = Path(temporary) / 'policy.json' - path.write_text(json.dumps(self.policy(ARTIFACT))) - for tag, revision in [('v9.0.0', REVISION), ('v2.7.0', 'c' * 40)]: - argv = ['checker', 'verify-publish', '--policy', str(path), '--output', str(Path(temporary) / 'out'), '--source-tag', tag, '--source-revision', revision] - with patch.dict(os.environ, {}, clear=True), patch('sys.argv', argv), patch.object(P, 'prepare') as prepare: - with self.assertRaisesRegex(ValueError, 'inputs differ'): - P.main() - prepare.assert_not_called() - - def test_rejects_registry_credentials_and_wrong_workflow(self): - with patch.dict(os.environ, {'CARGO_REGISTRY_TOKEN': 'synthetic-test-only'}, clear=True), self.assertRaisesRegex(ValueError, 'without registry credentials'): - P.execution_boundary() - with patch.dict(os.environ, {'GITHUB_ACTIONS': 'true', 'GITHUB_REPOSITORY': 'other/protocol'}, clear=True), self.assertRaisesRegex(ValueError, 'workflow identity'): - P.execution_boundary() - - def test_accepts_exact_manual_workflow_identity(self): - environment = { - 'GITHUB_ACTIONS': 'true', 'GITHUB_REPOSITORY': 'atrinik/protocol', - 'GITHUB_REPOSITORY_ID': '1327106950', 'GITHUB_REF': 'refs/heads/main', - 'GITHUB_EVENT_NAME': 'workflow_dispatch', - 'GITHUB_WORKFLOW_REF': 'atrinik/protocol/.github/workflows/publish-crate.yml@refs/heads/main', - } - with patch.dict(os.environ, environment, clear=True): - P.execution_boundary() - - def test_tag_drift_fails_before_packaging(self): - with patch.object(P, 'git', return_value='c' * 40), patch.object(P, 'download') as download: - with self.assertRaisesRegex(ValueError, 'source tag drift'): - P.release('v2.7.0', REVISION) - download.assert_not_called() - with patch.object(P, 'git', return_value=REVISION), patch.object(P.subprocess, 'run'), patch.object(P, 'download', return_value=json.dumps({'object': {'type': 'commit', 'sha': 'c' * 40}}).encode()): - with self.assertRaisesRegex(ValueError, 'remote source tag drift'): - P.release('v2.7.0', REVISION) - - def test_asset_mismatch_and_redirect_are_rejected(self): - value = {'assets': [{'name': P.ASSET, 'state': 'uploaded', 'size': 3, - 'browser_download_url': f'https://github.com/atrinik/protocol/releases/download/v2.7.0/{P.ASSET}', 'digest': 'sha256:' + DIGEST}]} - with patch.object(P, 'download', return_value=b'bad'), self.assertRaisesRegex(ValueError, 'digest mismatch'): - P.release_asset(value, 'v2.7.0', P.ASSET) - with self.assertRaisesRegex(ValueError, 'redirect'): - P.SafeRedirect().redirect_request(None, None, 302, '', {}, 'http://attacker.invalid/') - - def test_release_manifest_covers_exact_flat_asset_inventory(self): - manifest = (DIGEST + ' ' + P.ASSET + '\n').encode() - def asset(name, digest): - return {'name': name, 'digest': 'sha256:' + digest, 'state': 'uploaded', 'size': 1} - release = {'assets': [asset('SHA256SUMS', 'c' * 64), asset(P.ASSET, DIGEST)]} - with patch.object(P, 'release_asset', return_value=manifest): - P.verify_release_inventory(release, 'v2.8.0') - for changed in (b'', manifest + manifest, manifest.replace(DIGEST.encode(), b'd' * 64), manifest.replace(P.ASSET.encode(), b'nested/file'), manifest + ('a' * 64 + ' extra\n').encode()): - with self.subTest(manifest=changed), patch.object(P, 'release_asset', return_value=changed): - with self.assertRaises(ValueError): - P.verify_release_inventory(release, 'v2.8.0') - - def test_crate_identity_inventory_and_dependency_checks(self): - entries = { - '.cargo_vcs_info.json': json.dumps({'git': {'sha1': REVISION}, 'path_in_vcs': 'crates/atrinik-protocol'}).encode(), - 'Cargo.toml': b'[package]\nname="atrinik-protocol"\nversion="0.2.0"\npublish=["crates-io"]\n', - 'Cargo.lock': b'version=4\n[[package]]\nname="atrinik-protocol"\nversion="0.2.0"\n', - } - with tempfile.TemporaryDirectory() as temporary: - path = Path(temporary) / P.ASSET - def write(data): - with tarfile.open(path, 'w:gz') as archive: - for name, content in data.items(): - member = tarfile.TarInfo(f'{P.NAME}-{P.VERSION}/{name}') - member.size = len(content) - archive.addfile(member, io.BytesIO(content)) - write(entries) - P.verify_crate(path, REVISION, entries) - P.verify_crate(path.read_bytes(), REVISION, entries) - for name, replacement, error in [ - ('.cargo_vcs_info.json', b'{"git":{"sha1":"wrong"}}', 'VCS'), - ('Cargo.toml', entries['Cargo.toml'].replace(b'["crates-io"]', b'false'), 'publishable'), - ('Cargo.toml', entries['Cargo.toml'] + b'[dependencies.x]\npath="../x"\n', 'dependency'), - ('Cargo.lock', entries['Cargo.lock'] + b'[[package]]\nname="x"\nversion="1"\nsource="git+evil"\n', 'dependency'), - ]: - write({**entries, name: replacement}) - with self.subTest(name=name, error=error), self.assertRaisesRegex(ValueError, error): - P.verify_crate(path, REVISION, entries) - write({**entries, 'unexpected': b'x'}) - with self.assertRaisesRegex(ValueError, 'inventory'): - P.verify_crate(path, REVISION, entries) - -class ProtectedVerificationTests(unittest.TestCase): - """Exercise the protected CLI with real archive/network parsers, no builds.""" - - def setUp(self): - self.temporary = tempfile.TemporaryDirectory() - self.addCleanup(self.temporary.cleanup) - self.root = Path(self.temporary.name) / 'checkout' - self.root.mkdir() - self.output = self.root.parent / 'verified' - self.github_output = self.root.parent / 'github-output' - self.tag = 'v2.7.0' - self.sha = 'd' * 40 - entries = { - '.cargo_vcs_info.json': json.dumps({'git': {'sha1': REVISION}, 'path_in_vcs': 'crates/atrinik-protocol'}).encode(), - 'Cargo.toml': b'[package]\nname="atrinik-protocol"\nversion="0.2.0"\npublish=["crates-io"]\n', - 'Cargo.lock': b'version=4\n[[package]]\nname="atrinik-protocol"\nversion="0.2.0"\n', - } - stream = io.BytesIO() - with tarfile.open(fileobj=stream, mode='w:gz') as archive: - for name, data in entries.items(): - member = tarfile.TarInfo(f'{P.NAME}-{P.VERSION}/{name}') - member.size = len(data) - archive.addfile(member, io.BytesIO(data)) - self.data = stream.getvalue() - self.inventory = '\n'.join(entries) - self.artifact = {**ARTIFACT, 'sha256': P.hashlib.sha256(self.data).hexdigest()} - self.policy_path = self.root / 'policy/rust-crate-next.json' - self.policy_path.parent.mkdir() - self.policy_path.write_text(json.dumps(PublicationTests().policy(self.artifact))) - payloads = {P.ASSET: self.data, 'provenance.json': json.dumps({'revision': REVISION, 'version': self.tag[1:]}).encode()} - payloads['SHA256SUMS'] = ''.join(P.hashlib.sha256(data).hexdigest() + ' ' + name + '\n' for name, data in payloads.items()).encode() - base = f'https://github.com/atrinik/protocol/releases/download/{self.tag}/' - self.released = {'id': 42, 'tag_name': self.tag, 'draft': False, - 'prerelease': False, 'published_at': '2026-01-01', 'assets': [ - {'name': name, 'state': 'uploaded', 'size': len(data), - 'digest': 'sha256:' + P.hashlib.sha256(data).hexdigest(), - 'browser_download_url': base + name} - for name, data in payloads.items()]} - self.responses = {base + name: data for name, data in payloads.items()} - self.ref_url = f'{P.API}/git/ref/tags/{self.tag}' - self.release_url = f'{P.API}/releases/tags/{self.tag}' - self.responses.update({ - self.ref_url: json.dumps({'object': {'type': 'commit', 'sha': REVISION}}).encode(), - self.release_url: json.dumps(self.released).encode(), - f'https://crates.io/api/v1/crates/{P.NAME}': json.dumps({'crate': {'id': P.NAME, 'name': P.NAME, 'repository': 'https://github.com/atrinik/protocol'}}).encode(), - f'https://crates.io/api/v1/crates/{P.NAME}/owners': json.dumps({'users': [{'kind': 'user', 'id': 437663, 'login': 'zoeyrose', 'github_username_matches': True}]}).encode(), - f'https://crates.io/api/v1/crates/{P.NAME}/{P.VERSION}': None, - f'https://index.crates.io/at/ri/{P.NAME}': None, - }) - environment = { - 'GITHUB_ACTIONS': 'true', 'GITHUB_REPOSITORY': 'atrinik/protocol', - 'GITHUB_REPOSITORY_ID': '1327106950', 'GITHUB_REF': 'refs/heads/main', - 'GITHUB_EVENT_NAME': 'workflow_dispatch', - 'GITHUB_WORKFLOW_REF': 'atrinik/protocol/.github/workflows/publish-crate.yml@refs/heads/main', - 'GITHUB_SHA': self.sha, 'GITHUB_WORKFLOW_SHA': self.sha, - 'GITHUB_OUTPUT': str(self.github_output), - 'ACTIONS_ID_TOKEN_REQUEST_URL': 'https://synthetic.invalid', - 'ACTIONS_ID_TOKEN_REQUEST_TOKEN': 'synthetic-test-only', - } - self.enterContext(patch.dict(os.environ, environment, clear=True)) - self.enterContext(patch.object(P, 'ROOT', self.root)) - self.enterContext(patch('sys.argv', ['checker', 'verify-upload', '--source-tag', self.tag, - '--source-revision', REVISION, '--output', str(self.output)])) - # An allowlist at the actual process boundary fails on Cargo, rustup, - # worktree creation, shell execution, or any other executable operation. - self.git_outputs = { - ('git', 'rev-parse', 'HEAD'): self.sha, - ('git', 'status', '--porcelain'): '', - ('git', 'rev-parse', '--verify', self.tag + '^{commit}'): REVISION, - ('git', 'show', REVISION + ':policy/rust-crate-files.txt'): self.inventory, - } - self.enterContext(patch.object(P.subprocess, 'check_output', side_effect=lambda command, **kwargs: self.git_outputs[tuple(command)])) - def run(command, **kwargs): - self.assertEqual(command, ['git', 'merge-base', '--is-ancestor', REVISION, 'origin/main']) - self.enterContext(patch.object(P.subprocess, 'run', side_effect=run)) - self.real_prepare = P.prepare - self.prepare = self.enterContext(patch.object(P, 'prepare', side_effect=AssertionError('protected job must never reproduce'))) - self.archive_check = self.enterContext(patch.object(P, 'verify_crate', wraps=P.verify_crate)) - self.second_read = lambda: None - self.ref_reads = 0 - def download(url, absent=False): - if url == self.ref_url: - self.ref_reads += 1 - if self.ref_reads == 2: - self.second_read() - return self.responses[url] - self.enterContext(patch.object(P, 'download', side_effect=download)) - - def test_protected_cli_only_reads_inert_bytes_with_oidc_authority(self): - P.main() - self.assertEqual((self.output / P.ASSET).read_bytes(), self.data) - self.archive_check.assert_called_once_with(self.data, REVISION, self.inventory.splitlines()) - self.assertIs(self.archive_check.call_args.args[0], self.data) - self.assertEqual(self.github_output.read_text(), 'ready_for_upload=true\n') - self.assertEqual(self.ref_reads, 2) - self.prepare.assert_not_called() - - def test_protected_cli_fails_closed_on_fresh_evidence_drift(self): - changes = { - 'remote source tag drift': lambda: self.responses.update({self.ref_url: json.dumps({'object': {'type': 'commit', 'sha': 'e' * 40}}).encode()}), - 'release identity changed': lambda: self.responses.update({self.release_url: json.dumps({**self.released, 'id': 43}).encode()}), - 'release API asset digest mismatch': lambda: self.responses.update({f'https://github.com/atrinik/protocol/releases/download/{self.tag}/{P.ASSET}': b'changed'}), - 'public registry owner identity changed': lambda: self.responses.update({f'https://crates.io/api/v1/crates/{P.NAME}/owners': b'{"users":[]}'}), - 'release checksums do not cover exact asset inventory': lambda: self.responses.update({self.release_url: json.dumps({**self.released, 'assets': self.released['assets'] + [{'name': 'extra', 'state': 'uploaded', 'size': 1, 'digest': 'sha256:' + DIGEST}]}).encode()}), - 'publication checkout or policy changed': lambda: self.git_outputs.update({('git', 'status', '--porcelain'): ' M tools/crate_publication.py'}), - } - original = dict(self.responses) - for error, change in changes.items(): - with self.subTest(error=error): - self.responses = dict(original) - self.git_outputs[('git', 'status', '--porcelain')] = '' - self.ref_reads = 0 - self.second_read = change - with self.assertRaisesRegex(ValueError, error): - P.main() - self.assertFalse(self.output.exists()) - self.assertFalse(self.github_output.exists()) - - def test_protected_cli_rejects_unreviewed_archive_even_with_matching_release_metadata(self): - self.policy_path.write_text(json.dumps(PublicationTests().policy(ARTIFACT))) - with self.assertRaisesRegex(ValueError, 'differs from reviewed pins'): - P.main() - self.assertFalse(self.output.exists()) - - def test_protected_cli_rejects_registry_uncertainty(self): - self.responses[f'https://crates.io/api/v1/crates/{P.NAME}/{P.VERSION}'] = json.dumps({'version': {'crate': P.NAME, 'num': P.VERSION, 'yanked': False, 'checksum': self.artifact['sha256']}}).encode() - with self.assertRaisesRegex(ValueError, 'registry result indeterminate'): - P.main() - self.assertFalse(self.github_output.exists()) - - def test_protected_cli_rejects_policy_drift_before_readiness(self): - def change_policy(): - self.policy_path.write_text(json.dumps(PublicationTests().policy(ARTIFACT))) - self.second_read = change_policy - with self.assertRaisesRegex(ValueError, 'publication checkout or policy changed'): - P.main() - self.assertFalse(self.github_output.exists()) - - def test_protected_cli_rejects_wrong_workflow_revision(self): - os.environ['GITHUB_WORKFLOW_SHA'] = 'e' * 40 - with self.assertRaisesRegex(ValueError, 'unexpected workflow revision'): - P.main() - self.assertEqual(self.ref_reads, 0) - - def test_public_registry_verification_still_works_in_oidc_job(self): - digest = self.artifact['sha256'] - self.responses[f'https://crates.io/api/v1/crates/{P.NAME}/{P.VERSION}'] = json.dumps({'version': {'crate': P.NAME, 'num': P.VERSION, 'yanked': False, 'checksum': digest}}).encode() - self.responses[f'https://index.crates.io/at/ri/{P.NAME}'] = json.dumps({'name': P.NAME, 'vers': P.VERSION, 'yanked': False, 'cksum': digest}).encode() - with patch('sys.argv', ['checker', 'verify-registry']): - P.main() - self.prepare.assert_not_called() - self.assertFalse(self.output.exists()) - - def test_credential_free_reproduction_still_packages_twice_and_checks_release(self): - self.git_outputs[('rustc', '+1.97.1', '--version')] = 'rustc 1.97.1 synthetic' - packages = [] - def run(command, **kwargs): - if command[:3] == ['git', 'worktree', 'add']: - source = Path(command[4]) - (source / 'crates/atrinik-protocol').mkdir(parents=True) - (source / 'crates/atrinik-protocol/Cargo.toml').write_text('[package]\npublish=["crates-io"]\n') - (source / 'policy').mkdir() - (source / 'policy/rust-crate-files.txt').write_text(self.inventory) - elif command[:3] == ['cargo', '+1.97.1', 'package']: - self.assertIn('--locked', command) - self.assertFalse(os.environ.get('ACTIONS_ID_TOKEN_REQUEST_URL')) - self.assertFalse(os.environ.get('ACTIONS_ID_TOKEN_REQUEST_TOKEN')) - target = Path(command[-1]) / 'package' / P.ASSET - target.parent.mkdir(parents=True) - target.write_bytes(self.data) - packages.append(target) - else: - self.assertEqual(command, ['git', 'merge-base', '--is-ancestor', REVISION, 'origin/main']) - with patch.dict(os.environ, {}, clear=True), patch.object(P.subprocess, 'run', side_effect=run): - self.real_prepare(self.tag, REVISION, self.output, P.load_policy(self.policy_path), True) - self.assertEqual(len(packages), 2) - self.assertNotEqual(packages[0], packages[1]) - self.assertEqual((self.output / P.ASSET).read_bytes(), self.data) - self.assertEqual(self.archive_check.call_count, 2) - self.assertEqual(self.ref_reads, 2) - - def test_packaging_rejects_oidc_before_any_source_or_build_operation(self): - # Call the real implementation hidden by the protected-path tripwire. - with patch.object(P, 'release', side_effect=AssertionError('source reached')): - for variable in ('ACTIONS_ID_TOKEN_REQUEST_URL', 'ACTIONS_ID_TOKEN_REQUEST_TOKEN'): - environment = {variable: 'synthetic-test-only'} - with self.subTest(variable=variable), patch.dict(os.environ, environment, clear=True): - with self.assertRaisesRegex(ValueError, 'without OIDC request authority'): - self.real_prepare(self.tag, REVISION, self.output, {}, False) - - -if __name__ == '__main__': - unittest.main() diff --git a/tools/test_crate_release_policy.py b/tools/test_crate_release_policy.py index 54896ee..8f283de 100755 --- a/tools/test_crate_release_policy.py +++ b/tools/test_crate_release_policy.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Regression tests for the reviewed manual registry boundary.""" +"""Regression tests for the reviewed registry publication boundary.""" from __future__ import annotations @@ -26,7 +26,6 @@ def setUp(self) -> None: "Cargo.toml", "crates/atrinik-protocol/Cargo.toml", "policy/rust-crate-candidate.json", - "policy/rust-crate-next.json", "README.md", "policy/rust-crate-publishing.json", "policy/rust-crate-release.json", @@ -99,23 +98,16 @@ def test_rejects_publish_capability_before_artifact_review(self) -> None: stream.write("\n# id-token: write\n") self.assert_rejected("reviewed publication workflow changed") - def test_rejects_unreviewed_artifact_pins(self) -> None: - path = self.root / "policy/rust-crate-next.json" - value = json.loads(path.read_text()) - value["artifact"] = {"revision": "a" * 40} - path.write_text(json.dumps(value)) - self.assert_rejected("reviewed next-crate artifact pins changed") - def test_rejects_publication_workflow_guard_drift(self) -> None: path = self.root / ".github/workflows/publish-crate.yml" original = path.read_text() for old, new in ( - ("default: prepare", "default: publish"), - ("crate_publication.py verify-upload", "crate_publication.py verify-publish"), + ("options: [automatic]", "options: [automatic, publish]"), + ("crate_release.py upload", "crate_release.py confirm"), (" - id: auth", " - run: cargo package --locked\n - id: auth"), (" - id: auth", " - run: rustup toolchain install 1.97.1\n - id: auth"), ("environment: crates-io-release", "environment: other"), - ("needs.verify.outputs.ready == 'true'", "always()"), + ("needs: automatic-verify", "needs: []"), ("persist-credentials: false", "persist-credentials: true"), ("ref: ${{ github.sha }}", "ref: main"), ("3268eb413133e2960fa4ae0e963ca76380c54cd9", "v1"), diff --git a/tools/test_crate_upload.py b/tools/test_crate_upload.py deleted file mode 100644 index 165eaae..0000000 --- a/tools/test_crate_upload.py +++ /dev/null @@ -1,359 +0,0 @@ -"""Network-free tests for the exact-byte uploader and its fail-closed boundary.""" -from contextlib import ExitStack -import hashlib -import io -import json -import os -from pathlib import Path -import struct -import tarfile -import tempfile -import unittest -from unittest.mock import Mock, patch -import urllib.error - -from tools import crate_upload as U - -P = U.P -REVISION = 'a' * 40 -WORKFLOW_REVISION = 'b' * 40 -TOKEN = 'synthetic-upload-token-for-tests' -ENVIRONMENT = { - 'GITHUB_ACTIONS': 'true', 'GITHUB_REPOSITORY': 'atrinik/protocol', - 'GITHUB_REPOSITORY_ID': '1327106950', 'GITHUB_REF': 'refs/heads/main', - 'GITHUB_EVENT_NAME': 'workflow_dispatch', - 'GITHUB_WORKFLOW_REF': 'atrinik/protocol/.github/workflows/publish-crate.yml@refs/heads/main', - 'GITHUB_SHA': WORKFLOW_REVISION, 'GITHUB_WORKFLOW_SHA': WORKFLOW_REVISION, - 'CRATE_PUBLICATION_OPERATION': 'publish', 'CARGO_REGISTRY_TOKEN': TOKEN, -} -MANIFEST = b'''[package] -name = "atrinik-protocol" -version = "0.2.0" -edition = "2024" -rust-version = "1.97.1" -build = false -publish = ["crates-io"] -autolib = false -autobins = false -autoexamples = false -autotests = false -autobenches = false -description = "Test fixture" -readme = "README.md" -license = "MIT" -repository = "https://github.com/atrinik/protocol" -[lib] -name = "atrinik_protocol" -path = "src/lib.rs" -[dependencies.bytes] -version = "1.12.1" -[dependencies.idna] -version = "1.1.0" -[dependencies.prost] -version = "0.14.4" -''' - - -def fixture(manifest=MANIFEST, vcs=None): - entries = { - 'Cargo.toml': manifest, - 'Cargo.lock': b'version=4\n[[package]]\nname="atrinik-protocol"\nversion="0.2.0"\n', - '.cargo_vcs_info.json': json.dumps(vcs or {'git': {'sha1': REVISION}, 'path_in_vcs': 'crates/atrinik-protocol'}).encode(), - 'README.md': b'Independent test fixture README.\n', - 'src/lib.rs': b'// Independent test fixture.\n', - } - stream = io.BytesIO() - with tarfile.open(fileobj=stream, mode='w:gz') as archive: - for name, content in entries.items(): - member = tarfile.TarInfo(f'{P.NAME}-{P.VERSION}/{name}') - member.size = len(content) - archive.addfile(member, io.BytesIO(content)) - return stream.getvalue(), list(entries) - - -class Response: - def __init__(self, body=b'{}', status=200, url=U.ENDPOINT): - self.body, self.status, self.url = body, status, url - self.read_bounds = [] - - def __enter__(self): - return self - - def __exit__(self, *args): - pass - - def geturl(self): - return self.url - - def read(self, bound): - self.read_bounds.append(bound) - return self.body[:bound] - - -class UploadTests(unittest.TestCase): - def setUp(self): - self.stack = ExitStack() - self.addCleanup(self.stack.close) - self.directory = Path(self.stack.enter_context(tempfile.TemporaryDirectory())) - self.stack.enter_context(patch.dict(os.environ, ENVIRONMENT, clear=True)) - self.stack.enter_context(patch.object(P, 'ROOT', self.directory)) - self.sleep = self.stack.enter_context(patch.object(U.time, 'sleep')) - self.opener = Mock() - self.response = Response() - self.opener.open.return_value = self.response - self.build_opener = self.stack.enter_context(patch.object(U.urllib.request, 'build_opener', return_value=self.opener)) - self.release = self.stack.enter_context(patch.object(P, 'release', return_value={'id': 123})) - self.release_asset = self.stack.enter_context(patch.object(P, 'release_asset')) - self.release_inventory = self.stack.enter_context(patch.object(P, 'verify_release_inventory')) - self.identity = self.stack.enter_context(patch.object(P, 'verify_registry_identity')) - self.registry = self.stack.enter_context(patch.object(P, 'registry_state', return_value='absent')) - self.git = self.stack.enter_context(patch.object(P, 'git', side_effect=self.git_result)) - self.policy_path = self.directory / 'policy/rust-crate-next.json' - self.policy_path.parent.mkdir() - self.crate_path = self.directory / P.ASSET - self.write_fixture() - - def git_result(self, *args): - self.assertNotIn('CARGO_REGISTRY_TOKEN', os.environ) - if args == ('rev-parse', 'HEAD'): - return WORKFLOW_REVISION - if args == ('status', '--porcelain'): - return '' - self.assertEqual(args, ('show', REVISION + ':policy/rust-crate-files.txt')) - return '\n'.join(self.inventory) - - def write_fixture(self, manifest=MANIFEST, vcs=None): - self.data, self.inventory = fixture(manifest, vcs) - self.crate_path.write_bytes(self.data) - self.artifact = {'repository_release': '2.8.0', 'revision': REVISION, - 'asset': P.ASSET, 'sha256': hashlib.sha256(self.data).hexdigest()} - self.policy = {'schema_version': 1, 'name': P.NAME, 'version': P.VERSION, - 'status': 'ready-for-publication', 'artifact': self.artifact} - self.policy_path.write_text(json.dumps(self.policy)) - self.release_asset.return_value = self.data - - def upload(self): - return U.upload(self.crate_path, self.policy_path) - - def test_exact_original_archive_framing_and_metadata(self): - self.assertEqual(self.upload(), 'success') - self.opener.open.assert_called_once() - request = self.opener.open.call_args.args[0] - self.assertEqual(request.full_url, U.ENDPOINT) - self.assertEqual(request.method, 'PUT') - self.assertEqual(request.get_header('Authorization'), TOKEN) - self.assertEqual(request.get_header('Content-type'), 'application/octet-stream') - payload = request.data - metadata_length, = struct.unpack('