Skip to content

Commit b5e3b37

Browse files
ci(api-review-gate): build the fork detector from the default branch
The merge base is the commit the fork's author branched from, so a branch cut from an old commit selected an old detector, and a fix to the detector never reached the PRs an attacker controls. The workflow itself comes from the default branch, so the detector now does too. The head is also checked out with its links intact. Flattening them hid a CRD behind a file link from the detector while Helm still shipped it, and made the verdict differ from the one the same-repo gate reaches. Assisted-by: LLM Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent dfa2283 commit b5e3b37

2 files changed

Lines changed: 14 additions & 17 deletions

File tree

‎.github/workflows/api-review-gate-fork.yaml‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ name: API Review Gate (fork)
1010
# fork's run executes the fork's own copy of the workflow, so its conclusion is
1111
# whatever the fork wants it to be. This workflow runs from the DEFAULT branch
1212
# and reaches the verdict itself, from inputs a fork cannot forge: the detector
13-
# built from the base branch, reading the PR head as data, plus the base repo's
13+
# built from the default branch, reading the PR head as data, plus the base repo's
1414
# PR and review APIs. The fork's run is used only as a trigger.
1515
#
1616
# Fork code never executes here. The head is fetched into a worktree that only
17-
# the base-built detector reads, in a job whose token can write nothing; the jobs
17+
# the default-branch detector reads, in a job whose token can write nothing; the jobs
1818
# that can write statuses never see the head at all. That is why this is a
1919
# workflow_run and not a pull_request_target run, which would offer fork code a
2020
# privileged token for no gain.
@@ -152,8 +152,8 @@ jobs:
152152
core.setOutput('should_run', 'true');
153153
154154
# The only job that touches fork bytes, so its token can write nothing and it
155-
# reaches no secret. The detector is built from the merge base, as in the
156-
# unprivileged gate, or else from the default branch; never from the head.
155+
# reaches no secret. The detector is built from the default branch, never
156+
# from the head or the merge base.
157157
detect:
158158
name: Detect sizeable API changes
159159
needs: [resolve]
@@ -194,23 +194,20 @@ jobs:
194194
fi
195195
merge_base="$(git merge-base "${HEAD_SHA}" "origin/${BASE_REF}")"
196196
echo "Merge base: ${merge_base}"
197-
# Symlinks land as plain files, so a fork cannot point a manifest the
198-
# detector reads at a file elsewhere on the runner and have it echoed
199-
# into the public log. The detector never follows the tree's own
200-
# directory links, so the verdict is unchanged.
201-
git -c core.symlinks=false worktree add --detach "${RUNNER_TEMP}/head" "${HEAD_SHA}"
197+
# Links stay links: a checkout that flattened them would hide a CRD
198+
# behind a file link from the detector while Helm still ships it, and
199+
# the verdict would differ from the one the same-repo gate reaches.
200+
git worktree add --detach "${RUNNER_TEMP}/head" "${HEAD_SHA}"
202201
git worktree add --detach "${RUNNER_TEMP}/base" "${merge_base}"
203202
203+
# Always from the default branch, the commit this workflow and its approval
204+
# rule come from. Not from the merge base: a fork's author chooses that,
205+
# and an old one would select an old detector, so a fix to the detector
206+
# would not reach the PRs that most need it.
204207
- name: Build api-gate
205208
run: |
206209
set -euo pipefail
207-
if [ -d "${RUNNER_TEMP}/base/cmd/api-gate" ]; then
208-
build_dir="${RUNNER_TEMP}/base"
209-
else
210-
build_dir="${GITHUB_WORKSPACE}"
211-
echo "The merge base predates api-gate; building it from the default branch."
212-
fi
213-
( cd "${build_dir}" && go build -o "${RUNNER_TEMP}/api-gate" ./cmd/api-gate )
210+
( cd "${GITHUB_WORKSPACE}" && go build -o "${RUNNER_TEMP}/api-gate" ./cmd/api-gate )
214211
215212
- name: Detect sizeable API changes
216213
id: detect

‎docs/agents/e2e-testing.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Consequences to keep in mind when touching either workflow:
168168

169169
**This pattern now has a second consumer — change it in both places.** `.github/workflows/api-review-gate.yaml` publishes the `API Review Gate` context the same way for same-repo PRs, from an `api-gate-report` job, and `api-review-gate-fork.yaml` publishes it for forks. Two details measured while wiring it up are worth recording here, because they are the load-bearing reason the gate cannot be a job. First, check runs are **not** deduplicated per name: each workflow run gets its own check suite, so `GET /commits/{sha}/check-runs?filter=latest` returns the same set as `filter=all`, and two runs on one head SHA leave two verdicts standing for one context name — the aggregate is worst-of, so a cancelled run poisons the context until the next push. Commit statuses **are** deduplicated per context, newest wins. Second, the read-only-token restriction on fork PRs covers `pull_request_review` as well as `pull_request`, so an approval arriving on a fork PR cannot publish a status either. Together these give the invariant both gates rely on: a run that posts nothing preserves whatever verdict a real run last reached, and can never invent one.
170170

171-
The fork half of the API gate departs from `e2e-fork.yaml` in three ways, each forced by a measurement. It does not take a fork from `workflow_run.head_repository` or `workflow_run.pull_requests`: for a `pull_request_review` run on a fork PR the first names the base repository and the second is empty, so it resolves the PR from the base repo's open PR list by head SHA instead. It does not relay the triggering run's conclusion, because a fork's run executes the fork's copy of the gate workflow; it rebuilds the detector from the base branch, runs it over the fetched head as data, and reads the reviews API itself. And it also runs on a schedule, because the repository makes every run from an external contributor wait for a maintainer's approval and a run in `action_required` never fires `workflow_run`, so an API owner's approval on such a PR reaches the status only through the sweep, scheduled every fifteen minutes (GitHub may start a scheduled run late). The sweep moves only a status the full evaluation already set after finding a sizeable change, between "needs an approving review" and "approved"; a fork PR whose push run still awaits approval has no status for it to move, and stays unmergeable until a maintainer lets that run execute, as it does for `E2E Tests`. A status lives on the commit, not on a PR, so a head that an open same-repo PR carries is left to that PR's own reporter, however many fork PRs carry it too, and a fork PR on such a head shows whichever verdict stands there even when its base differs, as two same-repo PRs at different bases already do, because one commit status cannot hold a verdict per base. A head carried only by fork PRs fails closed, since their bases can differ: `resolve` writes the failure and the sweep turns a success already standing there into the same one, and it recovers on the next evaluation once all but one of them are closed.
171+
The fork half of the API gate departs from `e2e-fork.yaml` in three ways, each forced by a measurement. It does not take a fork from `workflow_run.head_repository` or `workflow_run.pull_requests`: for a `pull_request_review` run on a fork PR the first names the base repository and the second is empty, so it resolves the PR from the base repo's open PR list by head SHA instead. It does not relay the triggering run's conclusion, because a fork's run executes the fork's copy of the gate workflow; it builds the detector from the default branch, not from the merge base a fork's author chooses, runs it over the fetched head as data, and reads the reviews API itself. And it also runs on a schedule, because the repository makes every run from an external contributor wait for a maintainer's approval and a run in `action_required` never fires `workflow_run`, so an API owner's approval on such a PR reaches the status only through the sweep, scheduled every fifteen minutes (GitHub may start a scheduled run late). The sweep moves only a status the full evaluation already set after finding a sizeable change, between "needs an approving review" and "approved"; a fork PR whose push run still awaits approval has no status for it to move, and stays unmergeable until a maintainer lets that run execute, as it does for `E2E Tests`. A status lives on the commit, not on a PR, so a head that an open same-repo PR carries is left to that PR's own reporter, however many fork PRs carry it too, and a fork PR on such a head shows whichever verdict stands there even when its base differs, as two same-repo PRs at different bases already do, because one commit status cannot hold a verdict per base. A head carried only by fork PRs fails closed, since their bases can differ: `resolve` writes the failure and the sweep turns a success already standing there into the same one, and it recovers on the next evaluation once all but one of them are closed.
172172

173173
- **Fork TIA reads trusted inputs, but the fork still writes the selector.** `e2e-fork.yaml`'s `e2e` job runs the same `select-e2e.sh` TIA as in-tree (§7), and its inputs are hardened: the changed-file list and the `full-e2e` label come from `resolve`'s base-repo API lookups (`pulls.listFiles` / the PR's labels), the same trusted source as the docs-only decision, passed in as outputs (`changed_b64`, `full_e2e`) rather than re-derived from a `git diff` of the fork-controlled merge ref. Keep that part as it is: re-deriving the selection from the checked-out tree would add a second bypass for no gain.
174174
- **What the fork lane is, and is not.** The `e2e` job runs from the fork's tree, so `select-e2e.sh`, the suites under `hack/e2e-chainsaw/`, `hack/e2e-install-cozystack.bats` and `packages/core/testing`'s recipes are all fork-authored. A fork that wants its own suite to report green can edit any of them, and hardening the TIA inputs does not change that. Read the lane accordingly: it is a correctness gate that stops an honest contributor shipping a regression, not a boundary against a hostile one. What contains a hostile fork is that no fork run executes without maintainer approval (below), that the job holds no push credential, and that a diff editing the test harness is visible in review.

0 commit comments

Comments
 (0)