Skip to content

Commit c21e315

Browse files
noreplyclaude
andcommitted
fork-follow: the pushing jobs run in the Environment fork-push
A repository secret can be read by a workflow file on any pushed branch. An Environment secret limited to the series branch reaches only runs from that branch, so the plan and advance jobs (the only ones that read FORK_PUSH_TOKEN) now run in the Environment fork-push. When the token is empty there, the check fails and its reason (in the run's summary) names the Environment, how to limit it to the series branch, and why the token should not stay a repository secret. A test checks that exactly the jobs that read the token use the Environment. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4f18a80 commit c21e315

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

‎.github/scripts/fork-follow-push.sh‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
# Already at SHA (a re-run) is success; moved elsewhere is a refusal.
1111
# cleanup Deletes the branch fork-follow/candidate; a failure is only a warning.
1212
#
13-
# Environment: FORK_PUSH_TOKEN, REPOSITORY (owner/name; GITHUB_REPOSITORY in a run), PUSH_URL
13+
# Environment: FORK_PUSH_TOKEN, FORK_PUSH_ENVIRONMENT (the GitHub Environment that should hold the
14+
# token; named when it is missing), REPOSITORY (owner/name; GITHUB_REPOSITORY in a run), PUSH_URL
1415
# (default https://github.com/$REPOSITORY.git), SERIES_BRANCH (default main), BASE_TAG (advance),
1516
# REPORT. Output (candidate_pushed, moved, backup_tag) goes to $GITHUB_OUTPUT, else stdout.
1617
#
@@ -37,6 +38,13 @@ die() {
3738
git_said() { printf '%s\n' '```' "$(tail -n 15 "$err")" '```'; }
3839

3940
with_token() {
41+
if [[ -z "${FORK_PUSH_TOKEN:-}" && -n "${FORK_PUSH_ENVIRONMENT:-}" ]]; then
42+
die "The secret FORK_PUSH_TOKEN is missing in the Environment $FORK_PUSH_ENVIRONMENT (or the Environment does not exist yet), so this run cannot push." \
43+
"Under Settings > Environments, create $FORK_PUSH_ENVIRONMENT (a run that names a missing Environment makes an empty one with no rules)," \
44+
"limit its deployment branches to $SERIES_BRANCH, and add the secret FORK_PUSH_TOKEN there: a fine-grained token for $REPOSITORY only, with Contents and Workflows read and write." \
45+
"Keep it out of the repository secrets: a workflow file on any pushed branch can read those, while an Environment limited to $SERIES_BRANCH gives it only to runs from $SERIES_BRANCH." \
46+
"GITHUB_TOKEN cannot replace it: it may not push commits that change workflow files."
47+
fi
4048
[[ -n "${FORK_PUSH_TOKEN:-}" ]] \
4149
|| die "The secret FORK_PUSH_TOKEN is missing, so this run cannot push." \
4250
"Add it under Settings > Secrets and variables > Actions: a fine-grained token for $REPOSITORY only, with Contents and Workflows read and write." \

‎.github/scripts/fork-follow.test.sh‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ push() { (cd "$root/work" && REPORT="$root/report.md" GITHUB_OUTPUT="$root/push.
265265
BASE_TAG=v0.1.10 bash "$dir/fork-follow-push.sh" "$@") > "$root/out" 2>&1; }
266266
rm -f "$root/report.md"
267267
check "push: a missing FORK_PUSH_TOKEN fails and says so" eval '! push check && has "FORK_PUSH_TOKEN is missing" "$root/report.md"'
268+
rm -f "$root/report.md"
269+
check "push: a missing FORK_PUSH_TOKEN names the Environment that should hold it" \
270+
eval '! FORK_PUSH_ENVIRONMENT=fork-push push check && has "missing in the Environment fork-push" "$root/report.md" && has "deployment branches to main" "$root/report.md"'
268271
export FORK_PUSH_TOKEN=test-token
269272
check "push: a token GitHub refuses (expired) fails and says so" \
270273
eval '! PUSH_URL="$root/gh/me/gone.git" push check && has "refused FORK_PUSH_TOKEN" "$root/report.md"'
@@ -384,6 +387,11 @@ check "workflow: release gets exactly the signing secrets by name, no 'secrets:
384387
eval '! grep -q "secrets: inherit" "$workflow" && [[ -n "$declared" && "$passed" == "$(sed "s/.*/&=&/" <<< "$declared")" ]]'
385388
check "workflow: FORK_PUSH_TOKEN reaches only fork-follow-push.sh steps, never a checkout" \
386389
eval '! grep -q "token: \${{ secrets.FORK_PUSH_TOKEN" "$workflow" && [[ $(grep -c "secrets.FORK_PUSH_TOKEN" "$workflow") == $(grep -cE "^\s*(run: )?bash \.github/scripts/fork-follow-push\.sh" "$workflow") ]]'
390+
# Each job that reads FORK_PUSH_TOKEN runs in the Environment that holds it, named as in env.
391+
token_jobs="$(awk '/^ [a-z_-]+:$/ { job = $1 } /secrets\.FORK_PUSH_TOKEN/ { print job }' "$workflow" | sort -u)"
392+
env_jobs="$(awk '/^ [a-z_-]+:$/ { job = $1 } /^ environment: fork-push$/ { print job }' "$workflow" | sort -u)"
393+
check "workflow: the jobs that read FORK_PUSH_TOKEN, and only they, run in the Environment fork-push" \
394+
eval '[[ -n "$token_jobs" && "$token_jobs" == "$env_jobs" ]] && grep -qx " FORK_PUSH_ENVIRONMENT: fork-push" "$workflow"'
387395
check "workflow: every checkout drops its credentials" \
388396
eval '[[ $(grep -c "actions/checkout@" "$workflow") == $(grep -c "persist-credentials: false" "$workflow") ]]'
389397
check "workflow: every action is pinned by commit" \

‎.github/workflows/fork-follow.yml‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ name: Fork follow upstream
2020
# Upstream's workflows stay off: after each push and at the end, fork-workflows.sh disables every
2121
# active workflow the series did not add and cancels their runs for the pushed commit.
2222
#
23-
# Settings: variable FORK_SERIES_BRANCH (default main). Secret FORK_PUSH_TOKEN: a fine-grained token
24-
# for this repository only, Contents and Workflows read and write (GITHUB_TOKEN may not push commits
23+
# Settings: variable FORK_SERIES_BRANCH (default main). Secret FORK_PUSH_TOKEN in the Environment
24+
# fork-push, whose deployment branches are limited to the series branch (a repository secret could
25+
# be read by a workflow file on any pushed branch; the jobs that push run in that Environment and no
26+
# other job sees the secret): a fine-grained token for this repository only, Contents and Workflows read and write (GITHUB_TOKEN may not push commits
2527
# that change .github/workflows). Every run checks it first and fails when it is missing, expired or
2628
# may not push. It reaches only the steps that run fork-follow-push.sh; the release workflow gets the
2729
# signing secrets by name and never this token. If a rule protects the series branch, the token's
@@ -43,10 +45,13 @@ permissions:
4345

4446
env:
4547
SERIES_BRANCH: ${{ vars.FORK_SERIES_BRANCH || 'main' }}
48+
# Must match `environment:` of the jobs that push (an expression there cannot read env).
49+
FORK_PUSH_ENVIRONMENT: fork-push
4650

4751
jobs:
4852
plan:
4953
name: Plan
54+
environment: fork-push
5055
runs-on: ubuntu-latest
5156
timeout-minutes: 15
5257
permissions:
@@ -173,6 +178,7 @@ jobs:
173178

174179
advance:
175180
name: Move the series branch
181+
environment: fork-push
176182
needs: [plan, release]
177183
if: >-
178184
!cancelled() && needs.plan.result == 'success' &&

0 commit comments

Comments
 (0)