Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(ci): scope the release credential and stop persisting it to disk (#3062)

Tejas Chopraac8646aa3c63
diff --git a/.github/workflows/release-metadata-sync.yml b/.github/workflows/release-metadata-sync.yml
index ef775cf3b..2526b9770 100644
--- a/.github/workflows/release-metadata-sync.yml
+++ b/.github/workflows/release-metadata-sync.yml
@@ -50,15 +50,26 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
+ # Prefer a short-lived, repo-scoped GitHub App installation token over a
+ # personal PAT. Gated on the repo variable so an unconfigured app simply
+ # falls through to the existing chain instead of breaking the release.
+ - name: Mint installation token
+ id: app-token
+ if: ${{ vars.RELEASE_APP_ID != '' }}
+ continue-on-error: true
+ uses: actions/create-github-app-token@v3
+ with:
+ app-id: ${{ vars.RELEASE_APP_ID }}
+ private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}
+
- uses: actions/checkout@v7
with:
ref: ${{ github.ref_name }}
- # PAT (not GITHUB_TOKEN) for the same reason release-please.yml uses one:
- # a push made with GITHUB_TOKEN does not trigger workflows, so the release
- # PR's checks would never re-run against the synced commit and would stay
- # red. Falls back to GITHUB_TOKEN, where the sync still lands and a manual
- # re-run of the PR's checks picks it up.
- token: ${{ secrets.RELEASE_PLEASE_TOKEN || secrets.GITHUB_TOKEN }}
+ # Do NOT persist the credential into .git/config. The next step runs
+ # scripts/version-sync.py *from the checked-out branch*, and this job
+ # triggers on a push to the unprotected glob release-please--branches--**.
+ # A persisted token would be readable by that script.
+ persist-credentials: false
- uses: actions/setup-python@v6
with:
@@ -72,6 +83,14 @@ jobs:
run: python scripts/verify-versions.py
- name: Commit and push if anything changed
+ env:
+ # An app installation token if one was minted, else the existing
+ # chain. A PAT (not GITHUB_TOKEN) is still preferred here for the same
+ # reason release-please.yml wants one: a push made with GITHUB_TOKEN
+ # does not trigger workflows, so the release PR's checks would never
+ # re-run against the synced commit and would stay red. Supplied only
+ # to this step, after the branch-supplied script has already run.
+ SYNC_TOKEN: ${{ steps.app-token.outputs.token || secrets.RELEASE_PLEASE_TOKEN || secrets.GITHUB_TOKEN }}
run: |
if git diff --quiet; then
echo "Already in sync — nothing to commit."
@@ -81,7 +100,12 @@ jobs:
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "chore: sync generated version metadata"
+ # Push via an explicit remote URL because the checkout no longer
+ # persists credentials. Passed on stdin-free env expansion so the
+ # token is not written to the command line or into .git/config.
# This push re-triggers this workflow. version-sync.py is idempotent, so
# the next run finds no diff and exits above without pushing — the loop
# terminates after one no-op run.
- git push origin HEAD:"${GITHUB_REF_NAME}"
+ git push \
+ "https://x-access-token:${SYNC_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" \
+ HEAD:"${GITHUB_REF_NAME}"
diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml
index ce458bfd8..c40893be2 100644
--- a/.github/workflows/release-please.yml
+++ b/.github/workflows/release-please.yml
@@ -42,16 +42,31 @@ jobs:
release-please:
runs-on: ubuntu-latest
steps:
+ # Prefer a short-lived, repo-scoped GitHub App installation token. A
+ # personal PAT carries the maintainer's whole account — with a classic
+ # `repo` scope that reaches every other repository they can access — and
+ # this credential can tag past branch protection and reaches PyPI, npm and
+ # GHCR through the `release: published` publishes. An installation token is
+ # scoped to this repository and expires in an hour. Gated on the repo
+ # variable so an unconfigured app falls through instead of blocking a
+ # release. See #2955.
+ - name: Mint installation token
+ id: app-token
+ if: ${{ vars.RELEASE_APP_ID != '' }}
+ continue-on-error: true
+ uses: actions/create-github-app-token@v3
+ with:
+ app-id: ${{ vars.RELEASE_APP_ID }}
+ private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}
+
- uses: googleapis/release-please-action@v5
with:
- # PAT (not GITHUB_TOKEN): a release/tag created by GITHUB_TOKEN does
- # NOT emit events that trigger other workflows, so release.yml
- # (PyPI/npm) and docker.yml — which fire on `release: published` —
- # never ran, and releases had to be cut by hand. A PAT is treated as a
- # real user, so the release it creates DOES trigger those publishes; it
- # also lets the bot tag past branch/tag protection. Falls back to
- # GITHUB_TOKEN when the secret is unset (the release PR still opens; it
- # just won't trigger the downstream publishes).
- token: ${{ secrets.RELEASE_PLEASE_TOKEN || secrets.GITHUB_TOKEN }}
+ # Neither an app token nor a PAT is GITHUB_TOKEN, and that matters: a
+ # release/tag created by GITHUB_TOKEN does NOT emit events that trigger
+ # other workflows, so release.yml (PyPI/npm) and docker.yml — which fire
+ # on `release: published` — never ran, and releases had to be cut by
+ # hand. Falls back to GITHUB_TOKEN when nothing else is set (the release
+ # PR still opens; it just won't trigger the downstream publishes).
+ token: ${{ steps.app-token.outputs.token || secrets.RELEASE_PLEASE_TOKEN || secrets.GITHUB_TOKEN }}
config-file: .release-please-config.json
manifest-file: .release-please-manifest.json
diff --git a/tests/test_release_workflows.py b/tests/test_release_workflows.py
index 207827802..d43e1ce6d 100644
--- a/tests/test_release_workflows.py
+++ b/tests/test_release_workflows.py
@@ -1467,3 +1467,69 @@ def test_version_sync_covers_every_file_the_verifier_gates() -> None:
"server.json",
]:
assert fragment in sync, f"version-sync.py no longer propagates a version to {fragment}"
+
+
+def test_metadata_sync_does_not_persist_credentials_for_branch_supplied_code() -> None:
+ """The release credential must not be readable by the synced branch's code.
+
+ ``release-metadata-sync`` triggers on a push to the ``release-please--branches--**``
+ glob, which is not a protected namespace, and then runs
+ ``scripts/version-sync.py`` *from the checked-out branch*. With
+ ``actions/checkout``'s default ``persist-credentials: true`` the token is
+ written to ``.git/config`` before that script runs, so anyone able to push a
+ matching branch could read it. The credential reaches PyPI, npm and GHCR via
+ the ``release: published`` publishes, so this is not a theoretical leak.
+ """
+ workflow = yaml.safe_load(
+ (ROOT / ".github/workflows/release-metadata-sync.yml").read_text(encoding="utf-8")
+ )
+ steps = workflow["jobs"]["sync"]["steps"]
+
+ checkouts = [s for s in steps if str(s.get("uses", "")).startswith("actions/checkout")]
+ assert checkouts, "expected a checkout step"
+ for step in checkouts:
+ assert step.get("with", {}).get("persist-credentials") is False, step
+ # A token passed to checkout is exactly what persist-credentials would
+ # write to disk; the push step supplies it via env instead.
+ assert "token" not in step.get("with", {}), step
+
+
+ "workflow_path,job",
+ [
+ (".github/workflows/release-please.yml", "release-please"),
+ (".github/workflows/release-metadata-sync.yml", "sync"),
+ ],
+)
+def test_release_workflows_prefer_scoped_app_token(workflow_path: str, job: str) -> None:
+ """A repo-scoped, short-lived app token must be preferred over the PAT.
+
+ The PAT carries a maintainer's entire account and bypasses branch and tag
+ protection (#2955). The app-token step is gated on ``vars.RELEASE_APP_ID``
+ and marked ``continue-on-error`` so an unconfigured app falls back to the
+ existing chain rather than blocking a release.
+ """
+ workflow = yaml.safe_load((ROOT / workflow_path).read_text(encoding="utf-8"))
+ steps = workflow["jobs"][job]["steps"]
+
+ minters = [
+ s for s in steps if str(s.get("uses", "")).startswith("actions/create-github-app-token")
+ ]
+ assert len(minters) == 1, steps
+ minter = minters[0]
+ assert minter["id"] == "app-token"
+ assert minter["continue-on-error"] is True
+ assert "vars.RELEASE_APP_ID" in str(minter["if"])
+
+ # Whatever consumes the credential must try the app token first.
+ consumers = [
+ value
+ for step in steps
+ for value in list(step.get("with", {}).values()) + list(step.get("env", {}).values())
+ if "RELEASE_PLEASE_TOKEN" in str(value)
+ ]
+ assert consumers, "expected a step consuming the release credential"
+ for value in consumers:
+ assert str(value).index("steps.app-token.outputs.token") < str(value).index(
+ "secrets.RELEASE_PLEASE_TOKEN"
+ ), value