From e261ac2f56da4d9b935e34cae11433a23e878e91 Mon Sep 17 00:00:00 2001 From: Zoey Rose Date: Mon, 23 Nov 2026 06:59:33 -0600 Subject: [PATCH 1/5] fix(release): authenticate source catalog reads with the read-only job token The credential-free source job lists this repository's releases through api.github.com in two steps: version planning (crate_prepare.py pending) and asset preparation (crate_prepare.py prepare). crate_release.get() adds a token only when GH_READ_TOKEN is set, so both steps shared the hosted runner address's anonymous quota. Main's release failed twice on 2026-11-23 with "public request failed" in the planning step. Give both steps the job's GITHUB_TOKEN as GH_READ_TOKEN. The workflow is contents: read and the package job has no permissions override, so the token is read-only. Preparation already strips it before Cargo packaging and verification. The crate release policy check now requires exactly those two steps to carry the token, the package job to have no permissions block, and the workflow to keep contents: read. Re-anchor the reviewed release.yml digest. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/release.yml | 5 +++++ AGENTS.md | 6 ++++-- README.md | 6 +++++- tools/check-crate-release-policy.py | 28 +++++++++++++++++++++++++- tools/test_crate_release_policy.py | 31 +++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8c3f541..63ca006 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -54,6 +54,10 @@ jobs: run: NODE_PATH="${RUNNER_TEMP}/crate-analysis/node_modules" node scripts/release/test-source-version.mjs - name: Plan the actual source version without write credentials id: plan + env: + # Hosted runners share addresses whose anonymous API quota is often + # exhausted. This job's token is read-only (workflow contents: read). + GH_READ_TOKEN: ${{ github.token }} run: | NODE_PATH="${RUNNER_TEMP}/crate-analysis/node_modules" \ node scripts/release/source-version.mjs > "${RUNNER_TEMP}/source-plan.json" @@ -75,6 +79,7 @@ jobs: if: steps.plan.outputs.version != '' env: SOURCE_VERSION: ${{ steps.plan.outputs.version }} + GH_READ_TOKEN: ${{ github.token }} run: tools/package-release.sh build/release "${SOURCE_VERSION}" - name: Retain the complete verified release payload if: steps.plan.outputs.version != '' diff --git a/AGENTS.md b/AGENTS.md index 73e7e37..0e7acf7 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -102,8 +102,10 @@ 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`. -- Source packaging runs twice in a credential-free read-only job, with independent - extracted-package verification. No registry token, OIDC capability or GitHub write +- Source packaging runs twice in a read-only job without write credentials, with + independent extracted-package verification. Only its planning and preparation + steps get the read-only job token, for public release catalog reads; preparation + strips it before Cargo packaging. No registry token, OIDC capability or GitHub write credential may reach Cargo/build scripts. The write-enabled semantic-release job only checks and publishes the complete prebuilt inventory. It must refuse any difference in its independently analyzed source version or revision. diff --git a/README.md b/README.md index 8d0533e..089169e 100644 --- a/README.md +++ b/README.md @@ -176,7 +176,11 @@ future candidates use `reviewed-automatic-publication`. No post-release policy P is needed to copy a checksum back into source. The read-only source job plans the next repository version, runs the aggregate, -packages the public crate graph twice and builds the extracted archives. It emits +packages the public crate graph twice and builds the extracted archives. Only its +planning and preparation steps receive the job's read-only `GITHUB_TOKEN` +(`contents: read`), for public release catalog reads: hosted runners share +addresses whose anonymous GitHub API quota is often exhausted. Preparation strips +that token before Cargo packages or verifies the crate graph. It emits `crate-release.json` with actual source revision, source tag, inventory, registry metadata, archive SHA-256 and a content checksum excluding only VCS metadata. Changed package content with a reused Cargo version fails. Historical 0.1.0 and diff --git a/tools/check-crate-release-policy.py b/tools/check-crate-release-policy.py index 24fb556..387dbfc 100755 --- a/tools/check-crate-release-policy.py +++ b/tools/check-crate-release-policy.py @@ -17,7 +17,7 @@ PUBLISH_WORKFLOW = WORKFLOWS / "publish-crate.yml" BOOTSTRAP_CHECK = ROOT / "tools" / "check-crate-publication.py" SOURCE_WORKFLOW = WORKFLOWS / "release.yml" -EXPECTED_SOURCE_WORKFLOW_SHA256 = "f897401133c5d69d73edbafe020cdf80cedcabbe31f76233a9e1a9f52a19ae94" +EXPECTED_SOURCE_WORKFLOW_SHA256 = "818d6584dc464997c8fb1021243159ceac2096a8d73d186462930fc7fc904d29" EXPECTED_PUBLICATION_WORKFLOW_SHA256 = "b0491b93c39cb1b58e73afff2ac49a9aea8854a53c1de2df04a66ca639ffe58a" EXPECTED_WORKFLOWS = { "dependency-review.yml", "pr-title.yml", "release.yml", "validate.yml", @@ -33,6 +33,14 @@ "environment": "crates-io-release", } +# Shared hosted-runner addresses exhaust GitHub's anonymous API quota, so the +# source job's release catalog reads carry its read-only token, and only there. +SOURCE_READ_TOKEN_STEPS = ( + "Plan the actual source version without write credentials", + "Prepare every immutable source and crate asset", +) +SOURCE_READ_TOKEN = "\n GH_READ_TOKEN: ${{ github.token }}\n" + FORBIDDEN_WORKFLOW_FRAGMENTS = ( "CARGO_REGISTRY_BOOTSTRAP_TOKEN", "CARGO_REGISTRY_TOKEN", @@ -47,6 +55,23 @@ def load_json(path: Path) -> object: return json.loads(path.read_text(encoding="utf-8")) +def check_source_read_token(text: str) -> None: + start, end = text.find("\n package:\n"), text.find("\n attest:\n") + if start < 0 or end < start: + raise SystemExit("source package job is missing") + job = text[start:end] + if "\npermissions:\n contents: read\n\n" not in text or "permissions:" in job: + raise SystemExit("source package job must keep the read-only workflow token") + steps = job.split("\n - ")[1:] + carrying = [ + step.partition("\n")[0].removeprefix("name: ") + for step in steps + if "\n env:\n" in step and SOURCE_READ_TOKEN in step + ] + if carrying != list(SOURCE_READ_TOKEN_STEPS) or job.count("GH_READ_TOKEN") != len(carrying): + raise SystemExit("source catalog reads must use the read-only token in exactly the planning and preparation steps") + + def main() -> None: publishing = load_json(PUBLISHING_POLICY) release = load_json(RELEASE_POLICY) @@ -110,6 +135,7 @@ def main() -> None: if {path.name for path in workflow_paths} != EXPECTED_WORKFLOWS: raise SystemExit("reviewed workflow inventory changed") + check_source_read_token(SOURCE_WORKFLOW.read_text(encoding="utf-8")) for workflow in workflow_paths: text = workflow.read_text(encoding="utf-8") for fragment in FORBIDDEN_WORKFLOW_FRAGMENTS: diff --git a/tools/test_crate_release_policy.py b/tools/test_crate_release_policy.py index 1b87c4d..fd88d71 100755 --- a/tools/test_crate_release_policy.py +++ b/tools/test_crate_release_policy.py @@ -133,6 +133,37 @@ def test_rejects_oidc_permission_in_existing_workflow(self) -> None: stream.write("\n# id-token: write\n") self.assert_rejected("disabled registry capability") + def test_source_catalog_reads_use_only_the_read_only_job_token(self) -> None: + path = self.root / ".github" / "workflows" / "release.yml" + original = path.read_text(encoding="utf-8") + token = " GH_READ_TOKEN: ${{ github.token }}\n" + plan = original.index(" id: plan\n") + prepare = original.index(" SOURCE_VERSION: ${{ steps.plan.outputs.version }}\n") + placement = "source catalog reads must use the read-only token" + permission = "source package job must keep the read-only workflow token" + for case, changed, expected in ( + ("planning step without token", + original[:plan] + original[plan:].replace(token, "", 1), placement), + ("preparation step without token", + original[:prepare] + original[prepare:].replace(token, "", 1), placement), + ("token widened to the package job", + original.replace(" env:\n CARGO_HOME:", + " env:\n GH_READ_TOKEN: ${{ github.token }}\n CARGO_HOME:", 1), + placement), + ("token given to the aggregate validation step", + original.replace(" run: tools/validate.sh\n", " env:\n" + token + " run: tools/validate.sh\n", 1), + placement), + ("package job write permission", + original.replace(" timeout-minutes: 40\n", " timeout-minutes: 40\n permissions:\n contents: write\n", 1), + permission), + ("workflow write permission", + original.replace("permissions:\n contents: read\n", "permissions:\n contents: write\n", 1), permission), + ): + with self.subTest(case=case): + self.assertNotEqual(changed, original) + path.write_text(changed, encoding="utf-8") + self.assert_rejected(expected) + def test_rejects_registry_capabilities_in_dependency_review(self) -> None: path = self.root / ".github" / "workflows" / "dependency-review.yml" original = path.read_text(encoding="utf-8") From 7cb9bb9e5443fa5422257e811b70928ecc8cd86e Mon Sep 17 00:00:00 2001 From: Zoey Rose Date: Mon, 23 Nov 2026 07:09:31 -0600 Subject: [PATCH 2/5] fix(release): parse the whole workflow permissions block in the read-token check A YAML blank line does not end a block mapping, so the substring test let "contents: read", a blank line and further scopes such as id-token: write pass as "exactly contents: read". Read every entry of the single top-level permissions block instead. Also count the credential, not only the variable name: the package job may mention github.token only in the two catalog-reading steps and may not reference secrets, so a renamed variable, a secrets alias or a YAML anchor cannot carry the token to another step. Co-Authored-By: Claude Opus 5.5 --- tools/check-crate-release-policy.py | 22 ++++++++++++++++++++-- tools/test_crate_release_policy.py | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/tools/check-crate-release-policy.py b/tools/check-crate-release-policy.py index 387dbfc..01cf4f8 100755 --- a/tools/check-crate-release-policy.py +++ b/tools/check-crate-release-policy.py @@ -55,12 +55,27 @@ def load_json(path: Path) -> object: return json.loads(path.read_text(encoding="utf-8")) +def workflow_permissions(text: str) -> list[str] | None: + """Entries of the one top-level permissions block; YAML blank lines don't end it.""" + lines = text.split("\n") + heads = [index for index, line in enumerate(lines) if line.startswith("permissions:")] + if len(heads) != 1 or lines[heads[0]] != "permissions:": + return None + entries = [] + for line in lines[heads[0] + 1:]: + if line and not line[0].isspace() and not line.startswith("#"): + break + if line.strip() and not line.strip().startswith("#"): + entries.append(line.strip()) + return entries + + def check_source_read_token(text: str) -> None: start, end = text.find("\n package:\n"), text.find("\n attest:\n") if start < 0 or end < start: raise SystemExit("source package job is missing") job = text[start:end] - if "\npermissions:\n contents: read\n\n" not in text or "permissions:" in job: + if workflow_permissions(text) != ["contents: read"] or "permissions:" in job: raise SystemExit("source package job must keep the read-only workflow token") steps = job.split("\n - ")[1:] carrying = [ @@ -68,7 +83,10 @@ def check_source_read_token(text: str) -> None: for step in steps if "\n env:\n" in step and SOURCE_READ_TOKEN in step ] - if carrying != list(SOURCE_READ_TOKEN_STEPS) or job.count("GH_READ_TOKEN") != len(carrying): + # Count the credential as well as the name, so no alias, anchor or secret + # carries it to another step. + if (carrying != list(SOURCE_READ_TOKEN_STEPS) or job.count("GH_READ_TOKEN") != len(carrying) + or job.count("github.token") != len(carrying) or "secrets." in job): raise SystemExit("source catalog reads must use the read-only token in exactly the planning and preparation steps") diff --git a/tools/test_crate_release_policy.py b/tools/test_crate_release_policy.py index fd88d71..8336dd1 100755 --- a/tools/test_crate_release_policy.py +++ b/tools/test_crate_release_policy.py @@ -153,11 +153,29 @@ def test_source_catalog_reads_use_only_the_read_only_job_token(self) -> None: ("token given to the aggregate validation step", original.replace(" run: tools/validate.sh\n", " env:\n" + token + " run: tools/validate.sh\n", 1), placement), + ("token renamed for the aggregate validation step", + original.replace(" run: tools/validate.sh\n", + " env:\n GH_TOKEN: ${{ github.token }}\n run: tools/validate.sh\n", 1), + placement), + ("token aliased through secrets", + original.replace(" run: cargo fetch --locked\n", + " env:\n FETCH: ${{ secrets.GITHUB_TOKEN }}\n run: cargo fetch --locked\n", 1), + placement), + ("token anchored for reuse", + original.replace(token, token + " LEAK: &leak ${{ github.token }}\n", 1).replace( + " run: tools/validate.sh\n", " env:\n LEAK: *leak\n run: tools/validate.sh\n", 1), + placement), ("package job write permission", original.replace(" timeout-minutes: 40\n", " timeout-minutes: 40\n permissions:\n contents: write\n", 1), permission), ("workflow write permission", original.replace("permissions:\n contents: read\n", "permissions:\n contents: write\n", 1), permission), + ("workflow scopes after a blank line", + original.replace("permissions:\n contents: read\n\n", + "permissions:\n contents: read\n\n id-token: write\n actions: write\n\n", 1), + permission), + ("workflow read-all shorthand", + original.replace("permissions:\n contents: read\n", "permissions: read-all\n", 1), permission), ): with self.subTest(case=case): self.assertNotEqual(changed, original) From 4ba3972e754537d51ca72436bf02211f048c1f54 Mon Sep 17 00:00:00 2001 From: Zoey Rose Date: Mon, 23 Nov 2026 07:16:59 -0600 Subject: [PATCH 3/5] fix(release): close the read-token check's spelling and line-break gaps Delta review of the source read-token check found three cheap defense-in-depth gaps behind the reviewed release.yml digest: - Split the top-level permissions block with splitlines(), so a CR, NEL, U+2028 or U+2029 inside a comment cannot hide a further scope that YAML parsers read as its own line. - Count github.token case-insensitively and without whitespace over the workflow up to the attest job, so an anchor defined before the package job is counted; refuse any secrets reference in the job, and toJSON(github) or github[...] before the attest job. - Refuse any "permissions" key spelling in the package job, including "permissions :" and a quoted key. Each clause has its own test case. release.yml is unchanged, so its digest stays anchored. Co-Authored-By: Claude Opus 5.5 --- tools/check-crate-release-policy.py | 15 ++++++++++----- tools/test_crate_release_policy.py | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/tools/check-crate-release-policy.py b/tools/check-crate-release-policy.py index 01cf4f8..1aa7f02 100755 --- a/tools/check-crate-release-policy.py +++ b/tools/check-crate-release-policy.py @@ -57,7 +57,9 @@ def load_json(path: Path) -> object: def workflow_permissions(text: str) -> list[str] | None: """Entries of the one top-level permissions block; YAML blank lines don't end it.""" - lines = text.split("\n") + # splitlines() also splits on CR, NEL, U+2028 and U+2029, which YAML parsers + # may treat as line breaks, so a comment cannot hide a further scope. + lines = text.splitlines() heads = [index for index, line in enumerate(lines) if line.startswith("permissions:")] if len(heads) != 1 or lines[heads[0]] != "permissions:": return None @@ -75,7 +77,7 @@ def check_source_read_token(text: str) -> None: if start < 0 or end < start: raise SystemExit("source package job is missing") job = text[start:end] - if workflow_permissions(text) != ["contents: read"] or "permissions:" in job: + if workflow_permissions(text) != ["contents: read"] or "permissions" in job: raise SystemExit("source package job must keep the read-only workflow token") steps = job.split("\n - ")[1:] carrying = [ @@ -83,10 +85,13 @@ def check_source_read_token(text: str) -> None: for step in steps if "\n env:\n" in step and SOURCE_READ_TOKEN in step ] - # Count the credential as well as the name, so no alias, anchor or secret - # carries it to another step. + # Count the credential as well as the name, so no alias, anchor (including + # one defined before the job), secret or whole-context dump carries it to + # another step. Expressions are case-insensitive and allow whitespace. + expressions = "".join(text[:end].lower().split()) if (carrying != list(SOURCE_READ_TOKEN_STEPS) or job.count("GH_READ_TOKEN") != len(carrying) - or job.count("github.token") != len(carrying) or "secrets." in job): + or expressions.count("github.token") != len(carrying) or "secrets" in job.lower() + or "tojson(github" in expressions or "github[" in expressions): raise SystemExit("source catalog reads must use the read-only token in exactly the planning and preparation steps") diff --git a/tools/test_crate_release_policy.py b/tools/test_crate_release_policy.py index 8336dd1..793c8bd 100755 --- a/tools/test_crate_release_policy.py +++ b/tools/test_crate_release_policy.py @@ -141,6 +141,11 @@ def test_source_catalog_reads_use_only_the_read_only_job_token(self) -> None: prepare = original.index(" SOURCE_VERSION: ${{ steps.plan.outputs.version }}\n") placement = "source catalog reads must use the read-only token" permission = "source package job must keep the read-only workflow token" + + def validate_env(entry: str) -> str: + return original.replace(" run: tools/validate.sh\n", + " env:\n " + entry + "\n run: tools/validate.sh\n", 1) + for case, changed, expected in ( ("planning step without token", original[:plan] + original[plan:].replace(token, "", 1), placement), @@ -165,6 +170,19 @@ def test_source_catalog_reads_use_only_the_read_only_job_token(self) -> None: original.replace(token, token + " LEAK: &leak ${{ github.token }}\n", 1).replace( " run: tools/validate.sh\n", " env:\n LEAK: *leak\n run: tools/validate.sh\n", 1), placement), + ("token anchored before the package job", + original.replace(" group: protocol-semantic-release\n", + " group: protocol-semantic-release\n leak: &leak ${{ github.token }}\n", 1).replace( + " run: tools/validate.sh\n", " env:\n LEAK: *leak\n run: tools/validate.sh\n", 1), + placement), + ("token in another letter case", + validate_env("LEAK: ${{ github.TOKEN }}"), placement), + ("secret by index", + validate_env("LEAK: ${{ secrets['GITHUB_TOKEN'] }}"), placement), + ("whole github context", + validate_env("LEAK: ${{ toJSON(github) }}"), placement), + ("github context by index", + validate_env("LEAK: ${{ github [ 'token' ] }}"), placement), ("package job write permission", original.replace(" timeout-minutes: 40\n", " timeout-minutes: 40\n permissions:\n contents: write\n", 1), permission), @@ -176,6 +194,15 @@ def test_source_catalog_reads_use_only_the_read_only_job_token(self) -> None: permission), ("workflow read-all shorthand", original.replace("permissions:\n contents: read\n", "permissions: read-all\n", 1), permission), + ("workflow scope hidden by a comment line separator", + original.replace("permissions:\n contents: read\n", + "permissions:\n contents: read\n # note\u2028 actions: write\n", 1), permission), + ("package job permissions key with a space", + original.replace(" timeout-minutes: 40\n", " timeout-minutes: 40\n permissions : write-all\n", 1), + permission), + ("package job quoted permissions key", + original.replace(" timeout-minutes: 40\n", ' timeout-minutes: 40\n "permissions": write-all\n', 1), + permission), ): with self.subTest(case=case): self.assertNotEqual(changed, original) From f3ed70fcbca7e7a25953fefc38a43a628c1de2f7 Mon Sep 17 00:00:00 2001 From: Zoey Rose Date: Mon, 23 Nov 2026 07:59:13 -0600 Subject: [PATCH 4/5] fix(release): hand the read token only to crate preparation in package-release.sh The preparation step's GH_READ_TOKEN reached everything package-release.sh runs, including the historical-crate cargo package and the contract bundle tool; only crate_prepare.py's own children went through clean_environment(). README and AGENTS.md overclaimed that preparation strips it before Cargo. Copy the token into a non-exported holder (declare +x also drops an inherited export of that name), unset GH_READ_TOKEN before the first command, and pass it only to crate_prepare.py prepare. The crate release policy check now requires those lines and the token's single use, and the docs say where it goes. release.yml is unchanged, so its digest stays. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 7 ++++--- README.md | 6 ++++-- tools/check-crate-release-policy.py | 17 +++++++++++++++-- tools/package-release.sh | 10 +++++++++- tools/test_crate_release_policy.py | 27 +++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 0e7acf7..a8e9705 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -104,9 +104,10 @@ remains an owner operation, tracked in `github-settings`. - Source packaging runs twice in a read-only job without write credentials, with independent extracted-package verification. Only its planning and preparation - steps get the read-only job token, for public release catalog reads; preparation - strips it before Cargo packaging. No registry token, OIDC capability or GitHub write - credential may reach Cargo/build scripts. The write-enabled semantic-release job + steps get the read-only job token, for public release catalog reads; + `package-release.sh` passes it only to `crate_prepare.py`, which strips it before + Cargo. No registry token, OIDC capability or GitHub write credential may reach + Cargo/build scripts. The write-enabled semantic-release job only checks and publishes the complete prebuilt inventory. It must refuse any difference in its independently analyzed source version or revision. Governance enables immutable releases before activation; the source job does diff --git a/README.md b/README.md index 089169e..1ace3c6 100644 --- a/README.md +++ b/README.md @@ -179,8 +179,10 @@ The read-only source job plans the next repository version, runs the aggregate, packages the public crate graph twice and builds the extracted archives. Only its planning and preparation steps receive the job's read-only `GITHUB_TOKEN` (`contents: read`), for public release catalog reads: hosted runners share -addresses whose anonymous GitHub API quota is often exhausted. Preparation strips -that token before Cargo packages or verifies the crate graph. It emits +addresses whose anonymous GitHub API quota is often exhausted. In preparation, +`tools/package-release.sh` unsets it at once and hands it only to +`crate_prepare.py`, which strips it again before Cargo packages or verifies the +crate graph. It emits `crate-release.json` with actual source revision, source tag, inventory, registry metadata, archive SHA-256 and a content checksum excluding only VCS metadata. Changed package content with a reused Cargo version fails. Historical 0.1.0 and diff --git a/tools/check-crate-release-policy.py b/tools/check-crate-release-policy.py index 1aa7f02..cddeab3 100755 --- a/tools/check-crate-release-policy.py +++ b/tools/check-crate-release-policy.py @@ -17,6 +17,7 @@ PUBLISH_WORKFLOW = WORKFLOWS / "publish-crate.yml" BOOTSTRAP_CHECK = ROOT / "tools" / "check-crate-publication.py" SOURCE_WORKFLOW = WORKFLOWS / "release.yml" +PACKAGE_SCRIPT = ROOT / "tools" / "package-release.sh" EXPECTED_SOURCE_WORKFLOW_SHA256 = "818d6584dc464997c8fb1021243159ceac2096a8d73d186462930fc7fc904d29" EXPECTED_PUBLICATION_WORKFLOW_SHA256 = "b0491b93c39cb1b58e73afff2ac49a9aea8854a53c1de2df04a66ca639ffe58a" EXPECTED_WORKFLOWS = { @@ -40,6 +41,10 @@ "Prepare every immutable source and crate asset", ) SOURCE_READ_TOKEN = "\n GH_READ_TOKEN: ${{ github.token }}\n" +# The preparation step runs package-release.sh, which must hand the token only +# to crate preparation (which strips it before Cargo), not to its other tools. +PACKAGE_SCRIPT_TOKEN_SCOPE = "\ndeclare +x catalog_read_token=${GH_READ_TOKEN-}\nunset GH_READ_TOKEN\n" +PACKAGE_SCRIPT_TOKEN_USE = "\nGH_READ_TOKEN=${catalog_read_token} python3 tools/crate_prepare.py prepare " FORBIDDEN_WORKFLOW_FRAGMENTS = ( "CARGO_REGISTRY_BOOTSTRAP_TOKEN", @@ -72,7 +77,7 @@ def workflow_permissions(text: str) -> list[str] | None: return entries -def check_source_read_token(text: str) -> None: +def check_source_read_token(text: str, script: str) -> None: start, end = text.find("\n package:\n"), text.find("\n attest:\n") if start < 0 or end < start: raise SystemExit("source package job is missing") @@ -93,6 +98,13 @@ def check_source_read_token(text: str) -> None: or expressions.count("github.token") != len(carrying) or "secrets" in job.lower() or "tojson(github" in expressions or "github[" in expressions): raise SystemExit("source catalog reads must use the read-only token in exactly the planning and preparation steps") + # Scope the token before the script's first command; only the shebang, + # set -euo pipefail and comments may precede it. + before, scoped, _ = script.partition(PACKAGE_SCRIPT_TOKEN_SCOPE) + commands = [line for line in before.splitlines() if line.strip() and not line.lstrip().startswith("#")] + if (not scoped or commands != ["set -euo pipefail"] or PACKAGE_SCRIPT_TOKEN_USE not in script + or script.count("GH_READ_TOKEN") != 3 or script.count("catalog_read_token") != 2): + raise SystemExit("package-release.sh must pass the read token only to crate preparation") def main() -> None: @@ -158,7 +170,8 @@ def main() -> None: if {path.name for path in workflow_paths} != EXPECTED_WORKFLOWS: raise SystemExit("reviewed workflow inventory changed") - check_source_read_token(SOURCE_WORKFLOW.read_text(encoding="utf-8")) + check_source_read_token(SOURCE_WORKFLOW.read_text(encoding="utf-8"), + PACKAGE_SCRIPT.read_text(encoding="utf-8")) for workflow in workflow_paths: text = workflow.read_text(encoding="utf-8") for fragment in FORBIDDEN_WORKFLOW_FRAGMENTS: diff --git a/tools/package-release.sh b/tools/package-release.sh index afe7c46..dc2b8e5 100755 --- a/tools/package-release.sh +++ b/tools/package-release.sh @@ -1,6 +1,14 @@ #!/usr/bin/env bash set -euo pipefail +# Only crate preparation reads the release catalog with the read-only job token, +# and it strips the token again before Cargo runs. No other command below may +# inherit it (this limits accidental exposure; it is not a barrier against +# hostile code running as this user). declare +x also drops an inherited export +# of the holder's own name, so never export the holder. +declare +x catalog_read_token=${GH_READ_TOKEN-} +unset GH_READ_TOKEN + repository=$(git rev-parse --show-toplevel) cd "${repository}" @@ -130,7 +138,7 @@ prepare_options=() if [[ ${version} == *-validation || ${CRATE_RELEASE_DRY_RUN:-0} == 1 ]]; then prepare_options+=(--dry-run) fi -python3 tools/crate_prepare.py prepare "${prepare_options[@]}" \ +GH_READ_TOKEN=${catalog_read_token} python3 tools/crate_prepare.py prepare "${prepare_options[@]}" \ --source-tag "v${version}" --output "$(realpath "${output}")" ( diff --git a/tools/test_crate_release_policy.py b/tools/test_crate_release_policy.py index 793c8bd..3a5bf4b 100755 --- a/tools/test_crate_release_policy.py +++ b/tools/test_crate_release_policy.py @@ -30,6 +30,7 @@ def setUp(self) -> None: "policy/rust-crate-publishing.json", "policy/rust-crate-release.json", "tools/check-crate-release-policy.py", + "tools/package-release.sh", ): source = SOURCE_ROOT / relative target = self.root / relative @@ -209,6 +210,32 @@ def validate_env(entry: str) -> str: path.write_text(changed, encoding="utf-8") self.assert_rejected(expected) + def test_release_script_passes_read_token_only_to_crate_preparation(self) -> None: + path = self.root / "tools" / "package-release.sh" + original = path.read_text(encoding="utf-8") + scope = "declare +x catalog_read_token=${GH_READ_TOKEN-}\nunset GH_READ_TOKEN\n" + use = "GH_READ_TOKEN=${catalog_read_token} python3 tools/crate_prepare.py prepare " + cd = 'cd "${repository}"\n' + for case, changed in ( + ("token left exported", original.replace("unset GH_READ_TOKEN\n", "", 1)), + ("holder exported", original.replace("declare +x catalog_read_token", "export catalog_read_token", 1)), + ("scope after the first command", + original.replace(scope, "", 1).replace(cd, cd + scope, 1)), + ("token withheld from crate preparation", + original.replace(use, "python3 tools/crate_prepare.py prepare ", 1)), + ("token passed to cargo package", + original.replace(" cargo package --locked --offline --allow-dirty", + " GH_READ_TOKEN=${catalog_read_token} cargo package --locked --offline --allow-dirty", 1)), + ("token passed to the contract bundle", + original.replace("python3 tools/contract_release_bundle.py", + "GH_READ_TOKEN=${catalog_read_token} python3 tools/contract_release_bundle.py", 1)), + ("token re-exported", original.replace(use, "export GH_READ_TOKEN=${catalog_read_token}\n" + use, 1)), + ): + with self.subTest(case=case): + self.assertNotEqual(changed, original) + path.write_text(changed, encoding="utf-8") + self.assert_rejected("package-release.sh must pass the read token only to crate preparation") + def test_rejects_registry_capabilities_in_dependency_review(self) -> None: path = self.root / ".github" / "workflows" / "dependency-review.yml" original = path.read_text(encoding="utf-8") From 121efefd8370ae05524ca89c79174c4ce6e97738 Mon Sep 17 00:00:00 2001 From: Zoey Rose Date: Mon, 23 Nov 2026 08:08:35 -0600 Subject: [PATCH 5/5] test(release): pin every read-token scoping clause and Cargo's clean environment Delta review of f3ed70f found checker sub-conditions no test pinned and asked for the crate preparation environment guarantee the docs rely on: - Add script subtests that only the holder count (holder exported on a later line), the use line (token moved to cargo package) and the GH_READ_TOKEN count (indirect expansion) catch. Drop the separate "scope present" test: without the scope lines the partition leaves the whole script before them, which the command check already rejects. - Port content-toolkit's AST test: every non-git subprocess in crate_prepare.py must run with env=clean_environment(). - Say that only the two steps get the token in their environment; the pinned checkout and setup actions also receive it as their default token input. Don't overclaim what declare +x guards against. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 3 ++- README.md | 13 +++++++------ tools/check-crate-release-policy.py | 7 ++++--- tools/package-release.sh | 4 ++-- tools/test_crate_release.py | 20 ++++++++++++++++++++ tools/test_crate_release_policy.py | 10 ++++++++++ 6 files changed, 45 insertions(+), 12 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index a8e9705..c051853 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -104,7 +104,8 @@ remains an owner operation, tracked in `github-settings`. - Source packaging runs twice in a read-only job without write credentials, with independent extracted-package verification. Only its planning and preparation - steps get the read-only job token, for public release catalog reads; + steps get the read-only job token in their environment, for public release + catalog reads; `package-release.sh` passes it only to `crate_prepare.py`, which strips it before Cargo. No registry token, OIDC capability or GitHub write credential may reach Cargo/build scripts. The write-enabled semantic-release job diff --git a/README.md b/README.md index 1ace3c6..cbf6d23 100644 --- a/README.md +++ b/README.md @@ -177,12 +177,13 @@ is needed to copy a checksum back into source. The read-only source job plans the next repository version, runs the aggregate, packages the public crate graph twice and builds the extracted archives. Only its -planning and preparation steps receive the job's read-only `GITHUB_TOKEN` -(`contents: read`), for public release catalog reads: hosted runners share -addresses whose anonymous GitHub API quota is often exhausted. In preparation, -`tools/package-release.sh` unsets it at once and hands it only to -`crate_prepare.py`, which strips it again before Cargo packages or verifies the -crate graph. It emits +planning and preparation steps get the job's read-only `GITHUB_TOKEN` +(`contents: read`) in their environment, for public release catalog reads, because +hosted runners share addresses whose anonymous GitHub API quota is often +exhausted. The pinned checkout and setup actions also receive it through their +default `token` input. In preparation, `tools/package-release.sh` unsets it at +once and hands it only to `crate_prepare.py`, which strips it again before Cargo +packages or verifies the crate graph. It emits `crate-release.json` with actual source revision, source tag, inventory, registry metadata, archive SHA-256 and a content checksum excluding only VCS metadata. Changed package content with a reused Cargo version fails. Historical 0.1.0 and diff --git a/tools/check-crate-release-policy.py b/tools/check-crate-release-policy.py index cddeab3..352c739 100755 --- a/tools/check-crate-release-policy.py +++ b/tools/check-crate-release-policy.py @@ -99,10 +99,11 @@ def check_source_read_token(text: str, script: str) -> None: or "tojson(github" in expressions or "github[" in expressions): raise SystemExit("source catalog reads must use the read-only token in exactly the planning and preparation steps") # Scope the token before the script's first command; only the shebang, - # set -euo pipefail and comments may precede it. - before, scoped, _ = script.partition(PACKAGE_SCRIPT_TOKEN_SCOPE) + # set -euo pipefail and comments may precede it. Without the scope lines, + # the partition leaves the whole script before them, so it fails too. + before = script.partition(PACKAGE_SCRIPT_TOKEN_SCOPE)[0] commands = [line for line in before.splitlines() if line.strip() and not line.lstrip().startswith("#")] - if (not scoped or commands != ["set -euo pipefail"] or PACKAGE_SCRIPT_TOKEN_USE not in script + if (commands != ["set -euo pipefail"] or PACKAGE_SCRIPT_TOKEN_USE not in script or script.count("GH_READ_TOKEN") != 3 or script.count("catalog_read_token") != 2): raise SystemExit("package-release.sh must pass the read token only to crate preparation") diff --git a/tools/package-release.sh b/tools/package-release.sh index dc2b8e5..b2d75a4 100755 --- a/tools/package-release.sh +++ b/tools/package-release.sh @@ -4,8 +4,8 @@ set -euo pipefail # Only crate preparation reads the release catalog with the read-only job token, # and it strips the token again before Cargo runs. No other command below may # inherit it (this limits accidental exposure; it is not a barrier against -# hostile code running as this user). declare +x also drops an inherited export -# of the holder's own name, so never export the holder. +# hostile code running as this user). declare +x also drops a plain inherited +# export of the holder's own name, so never export the holder. declare +x catalog_read_token=${GH_READ_TOKEN-} unset GH_READ_TOKEN diff --git a/tools/test_crate_release.py b/tools/test_crate_release.py index dfbd275..e98a41d 100644 --- a/tools/test_crate_release.py +++ b/tools/test_crate_release.py @@ -2,6 +2,7 @@ """Negative tests for the portable exact-archive publication boundary.""" from __future__ import annotations +import ast import copy from contextlib import ExitStack import gzip @@ -611,6 +612,25 @@ def test_actions_source_ref_cannot_be_overridden_and_read_token_not_in_build(sel with patch.dict(os.environ, {'GH_READ_TOKEN': 'read-only-secret'}, clear=True): self.assertNotIn('GH_READ_TOKEN', P.clean_environment()) + def test_preparation_subprocesses_other_than_git_drop_the_read_token(self): + # The source job hands prepare() GH_READ_TOKEN for catalog reads; only + # clean_environment() keeps it from Cargo, build scripts and the verifier. + source = Path(P.__file__).read_text() + self.assertNotRegex(source, r'\bos\.(system|popen|exec|spawn|posix_spawn)|\bpty\.' + r'|from subprocess import|import subprocess as') + calls = [node for node in ast.walk(ast.parse(source)) if isinstance(node, ast.Call) + and isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name) + and node.func.value.id == 'subprocess'] + self.assertEqual(len(calls), 4) + for call in calls: + command = call.args[0] if call.args else None + if (isinstance(command, ast.List) and isinstance(command.elts[0], ast.Constant) + and command.elts[0].value == 'git'): + continue + env = [keyword.value for keyword in call.keywords if keyword.arg == 'env'] + self.assertTrue(env and isinstance(env[0], ast.Call) and isinstance(env[0].func, ast.Name) + and env[0].func.id == 'clean_environment', ast.unparse(call)) + def test_required_checks_exact_revision_name_app_and_latest_success(self): check = {'name': 'Example validation', 'head_sha': REVISION, 'app': {'slug': 'github-actions'}, 'id': 1, 'status': 'completed', 'conclusion': 'success'} diff --git a/tools/test_crate_release_policy.py b/tools/test_crate_release_policy.py index 3a5bf4b..2e3ebdd 100755 --- a/tools/test_crate_release_policy.py +++ b/tools/test_crate_release_policy.py @@ -230,6 +230,16 @@ def test_release_script_passes_read_token_only_to_crate_preparation(self) -> Non original.replace("python3 tools/contract_release_bundle.py", "GH_READ_TOKEN=${catalog_read_token} python3 tools/contract_release_bundle.py", 1)), ("token re-exported", original.replace(use, "export GH_READ_TOKEN=${catalog_read_token}\n" + use, 1)), + ("holder exported on a later line", + original.replace("unset GH_READ_TOKEN\n", "unset GH_READ_TOKEN\nexport catalog_read_token\n", 1)), + ("token passed to cargo package through indirect expansion", + original.replace(" cargo package --locked --offline --allow-dirty", + " holder=catalog_read; holder+=_token\n" + " GH_READ_TOKEN=${!holder} cargo package --locked --offline --allow-dirty", 1)), + ("token moved from crate preparation to cargo package", + original.replace(use, "python3 tools/crate_prepare.py prepare ", 1).replace( + " cargo package --locked --offline --allow-dirty", + " GH_READ_TOKEN=${catalog_read_token} cargo package --locked --offline --allow-dirty", 1)), ): with self.subTest(case=case): self.assertNotEqual(changed, original)