From 9a06b0bdb92015d04447a0af70daa2c603a5c57e Mon Sep 17 00:00:00 2001 From: walsykerem Date: Sat, 21 Nov 2026 20:45:11 +0300 Subject: [PATCH 1/4] ci: keep fork pull requests off privileged runners --- .github/workflows/test.yml | 55 +++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d2a57fc18..8b1c83dca 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,35 +29,51 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'push' && github.ref != 'refs/heads/main' }} # A slash-command dispatch supplies a fork repository and immutable commit SHA. -# Normal push runs use the upstream repository and pushed ref. +# Only fork-checks checks that code out. Every other job needs a shared runner, +# passwordless sudo and registry credentials, so it runs this repository's code +# at this workflow revision and is skipped when the dispatch names a fork. env: TEST_PR_HEAD_REPO: ${{ inputs.pr_head_repo || github.repository }} - TEST_SOURCE_REPO: ${{ inputs.pr_head_repo || github.repository }} - TEST_SOURCE_REF: ${{ inputs.pr_head_sha || github.ref }} permissions: contents: read jobs: + # Untrusted fork commits run only here: a disposable hosted runner with no + # secrets, no sudo and no registry login. Nothing it builds is reused. + fork-checks: + if: ${{ github.event_name == 'workflow_dispatch' && inputs.pr_head_repo != '' && inputs.pr_head_repo != github.repository }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4.4.0 + with: + repository: ${{ inputs.pr_head_repo }} + ref: ${{ inputs.pr_head_sha }} + persist-credentials: false + - uses: actions/setup-go@v6.5.0 + with: + go-version-file: go.mod + cache: false + - name: Build + run: make build + test: + # Fork code must never reach a runner that has passwordless sudo. + if: ${{ github.event_name != 'workflow_dispatch' || inputs.pr_head_repo == '' || inputs.pr_head_repo == github.repository }} runs-on: [self-hosted, linux, x64, kvm] steps: - uses: actions/checkout@v4.4.0 with: - clean: false - repository: ${{ env.TEST_SOURCE_REPO }} - ref: ${{ env.TEST_SOURCE_REF }} persist-credentials: false - - name: Clean workspace (preserve binary caches) + # Checkout already resets the tree; this second pass exists because a + # previous run on this shared runner may have left anything behind, and + # nothing untracked (bin/, downloaded binaries) is trusted across runs. + - name: Clean workspace run: | git reset --hard HEAD - git clean -ffdx \ - -e lib/vmm/binaries/ \ - -e lib/hypervisor/firecracker/binaries/ \ - -e lib/ingress/binaries/ \ - -e bin/ - + git clean -ffdx + # Tests create a bridge and route per VM and clean up on success, but a # run that dies mid-test (crash, timeout, superseded-run cancellation) # leaks them, and enough leaked routes fail new tests on subnet @@ -284,7 +300,7 @@ jobs: report-pr-test: if: ${{ always() && github.event_name == 'workflow_dispatch' && inputs.pr_number != '' }} - needs: test + needs: [test, fork-checks] runs-on: ubuntu-latest permissions: contents: read @@ -298,7 +314,8 @@ jobs: PR_NUMBER: ${{ inputs.pr_number }} EXPECTED_REPO: ${{ inputs.pr_head_repo }} EXPECTED_SHA: ${{ inputs.pr_head_sha }} - TEST_RESULT: ${{ needs.test.result }} + # Fork dispatches skip the privileged jobs; report what actually ran. + TEST_RESULT: ${{ needs.test.result == 'skipped' && needs.fork-checks.result || needs.test.result }} run: | set -euo pipefail [[ "$PR_NUMBER" =~ ^[0-9]+$ ]] @@ -323,6 +340,8 @@ jobs: --field target_url="$GITHUB_SERVER_URL/$REPO/actions/runs/$GITHUB_RUN_ID" test-darwin: + # Same rule as test: a macOS runner is shared and holds registry credentials. + if: ${{ github.event_name != 'workflow_dispatch' || inputs.pr_head_repo == '' || inputs.pr_head_repo == github.repository }} runs-on: [self-hosted, macos, arm64] concurrency: group: macos-ci-test-${{ inputs.pr_head_sha || github.ref }} @@ -332,8 +351,6 @@ jobs: steps: - uses: actions/checkout@v4.4.0 with: - repository: ${{ env.TEST_SOURCE_REPO }} - ref: ${{ env.TEST_SOURCE_REF }} persist-credentials: false - uses: actions/setup-go@v6.5.0 with: @@ -438,6 +455,8 @@ jobs: make clean e2e-install: + # Same rule as test: a macOS runner is shared and holds registry credentials. + if: ${{ github.event_name != 'workflow_dispatch' || inputs.pr_head_repo == '' || inputs.pr_head_repo == github.repository }} runs-on: [self-hosted, macos, arm64] needs: test-darwin concurrency: @@ -446,8 +465,6 @@ jobs: steps: - uses: actions/checkout@v4.4.0 with: - repository: ${{ env.TEST_SOURCE_REPO }} - ref: ${{ env.TEST_SOURCE_REF }} persist-credentials: false - uses: actions/setup-go@v6.5.0 with: From dd0a9bd08616225addaba0329fc085a36a80e1a0 Mon Sep 17 00:00:00 2001 From: walsykerem Date: Sat, 21 Nov 2026 21:53:15 +0300 Subject: [PATCH 2/4] ci: test the dispatched commit in privileged jobs Self-hosted jobs gated on the dispatch not naming a fork still checked out the default branch, so a same-repo /test run tested main and report-pr-test posted that result onto the pull request commit. The three privileged jobs now check out ref: inputs.pr_head_sha || github.ref, so a same-repo dispatch tests the PR commit while a push run has empty inputs and falls back to the pushed ref. The test job exports the HEAD it checked out, and report-pr-test skips status when that differs from the dispatched commit, so a run of main can no longer be labelled as the PR result. Fork dispatches still skip every privileged job, and fork-checks is still the only job that checks out inputs.pr_head_repo, on a hosted runner. proof/592-privileged-checkout.sh asserts these invariants. Old shape: 20/27, failing on the three missing checkout refs and on the missing tested SHA. Fixed shape: 27/27. --- .github/workflows/test.yml | 28 ++++++++- proof/592-privileged-checkout.sh | 101 +++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 proof/592-privileged-checkout.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8b1c83dca..f5853601e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,9 +29,10 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'push' && github.ref != 'refs/heads/main' }} # A slash-command dispatch supplies a fork repository and immutable commit SHA. -# Only fork-checks checks that code out. Every other job needs a shared runner, -# passwordless sudo and registry credentials, so it runs this repository's code -# at this workflow revision and is skipped when the dispatch names a fork. +# Only fork-checks checks out a foreign repository. Every other job needs a +# shared runner, passwordless sudo and registry credentials, so it is skipped +# when the dispatch names a fork and otherwise checks out the dispatched commit +# in this repository (or the pushed ref for a push run), never plain main. env: TEST_PR_HEAD_REPO: ${{ inputs.pr_head_repo || github.repository }} @@ -61,11 +62,19 @@ jobs: # Fork code must never reach a runner that has passwordless sudo. if: ${{ github.event_name != 'workflow_dispatch' || inputs.pr_head_repo == '' || inputs.pr_head_repo == github.repository }} runs-on: [self-hosted, linux, x64, kvm] + outputs: + tested_sha: ${{ steps.tested-sha.outputs.sha }} steps: - uses: actions/checkout@v4.4.0 with: + # Test the dispatched PR commit; a push has no inputs and uses its ref. + ref: ${{ inputs.pr_head_sha || github.ref }} persist-credentials: false + - name: Record tested commit + id: tested-sha + run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" + # Checkout already resets the tree; this second pass exists because a # previous run on this shared runner may have left anything behind, and # nothing untracked (bin/, downloaded binaries) is trusted across runs. @@ -314,12 +323,21 @@ jobs: PR_NUMBER: ${{ inputs.pr_number }} EXPECTED_REPO: ${{ inputs.pr_head_repo }} EXPECTED_SHA: ${{ inputs.pr_head_sha }} + # The commit the privileged job actually checked out, so a run that + # fell back to main's code can never be reported as the PR's result. + TESTED_SHA: ${{ needs.test.outputs.tested_sha }} # Fork dispatches skip the privileged jobs; report what actually ran. TEST_RESULT: ${{ needs.test.result == 'skipped' && needs.fork-checks.result || needs.test.result }} + FORK_ONLY: ${{ needs.test.result == 'skipped' }} run: | set -euo pipefail [[ "$PR_NUMBER" =~ ^[0-9]+$ ]] [[ "$EXPECTED_SHA" =~ ^[0-9a-f]{40}$ ]] + # Fork-checks fetches EXPECTED_SHA itself; a privileged run must show it did. + if [[ "$FORK_ONLY" != "true" && "$TESTED_SHA" != "$EXPECTED_SHA" ]]; then + echo "::notice::Skipping test status because the run tested ${TESTED_SHA:-nothing}, not $EXPECTED_SHA" + exit 0 + fi pr="$(gh api "repos/$REPO/pulls/$PR_NUMBER" --jq '[.state, .base.ref, .head.repo.full_name, .head.sha] | @tsv')" read -r state base_ref head_repo head_sha <<< "$pr" if [[ "$state" != open || "$base_ref" != main || "$head_repo" != "$EXPECTED_REPO" || "$head_sha" != "$EXPECTED_SHA" ]]; then @@ -351,6 +369,8 @@ jobs: steps: - uses: actions/checkout@v4.4.0 with: + # Same commit as test: the dispatched PR commit, else the pushed ref. + ref: ${{ inputs.pr_head_sha || github.ref }} persist-credentials: false - uses: actions/setup-go@v6.5.0 with: @@ -465,6 +485,8 @@ jobs: steps: - uses: actions/checkout@v4.4.0 with: + # Same commit as test: the dispatched PR commit, else the pushed ref. + ref: ${{ inputs.pr_head_sha || github.ref }} persist-credentials: false - uses: actions/setup-go@v6.5.0 with: diff --git a/proof/592-privileged-checkout.sh b/proof/592-privileged-checkout.sh new file mode 100644 index 000000000..35342c66d --- /dev/null +++ b/proof/592-privileged-checkout.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env bash +# Asserts the privileged (self-hosted) jobs in .github/workflows/test.yml test +# the commit the dispatch names, that only fork-checks checks out a foreign +# repository, and that the fork gate still skips every privileged job. +# Usage: bash proof/592-privileged-checkout.sh [repo-root] +set -euo pipefail +root="${1:-$(cd "$(dirname "$0")/.." && pwd)}" +python3 - "$root" <<'PY' +import sys, yaml + +root = sys.argv[1].rstrip("/") +wf = yaml.safe_load(open(f"{root}/.github/workflows/test.yml")) +cmd = yaml.safe_load(open(f"{root}/.github/workflows/test-command.yml")) +jobs = wf["jobs"] +fails, checks = [], 0 + + +def check(ok, what): + global checks + checks += 1 + print(("ok " if ok else "FAIL ") + what) + if not ok: + fails.append(what) + + +def runners(job): + r = job.get("runs-on", "") + return r if isinstance(r, list) else [r] + + +def checkouts(job): + return [s for s in job.get("steps", []) if "actions/checkout" in str(s.get("uses", ""))] + + +privileged = [n for n, j in jobs.items() if "self-hosted" in runners(j)] +check(privileged == ["test", "test-darwin", "e2e-install"], + f"privileged jobs are test, test-darwin, e2e-install (got {privileged})") + +REF = "${{ inputs.pr_head_sha || github.ref }}" +for name in privileged: + cos = checkouts(jobs[name]) + check(len(cos) == 1, f"{name}: exactly one checkout") + with_ = cos[0].get("with", {}) or {} + check(with_.get("ref") == REF, f"{name}: checkout ref is '{REF}' (got {with_.get('ref')!r})") + check("inputs.pr_head_repo" not in str(with_.get("repository", "")), + f"{name}: never checks out inputs.pr_head_repo") + cond = str(jobs[name].get("if", "")) + check("github.event_name != 'workflow_dispatch'" in cond + and "inputs.pr_head_repo == ''" in cond + and "inputs.pr_head_repo == github.repository" in cond, + f"{name}: fork gate unchanged (skips a dispatch that names a fork)") + +foreign = [n for n, j in jobs.items() + if any("inputs.pr_head_repo" in str((s.get("with") or {}).get("repository", "")) + for s in checkouts(j))] +check(foreign == ["fork-checks"], f"only fork-checks checks out a foreign repo (got {foreign})") +fc = checkouts(jobs["fork-checks"])[0].get("with", {}) +check(fc.get("ref") == "${{ inputs.pr_head_sha }}", "fork-checks pins the dispatched sha") +check("self-hosted" not in runners(jobs["fork-checks"]), "fork-checks stays on a hosted runner") + +# report-pr-test must not label a run with a commit it did not test. +rep = jobs["report-pr-test"] +check("test" in rep.get("needs", []), "report-pr-test waits for test") +env = rep["steps"][0].get("env", {}) +check(env.get("TESTED_SHA") == "${{ needs.test.outputs.tested_sha }}", + "report-pr-test reads the tested commit from test's output") +script = rep["steps"][0]["run"] +check("$TESTED_SHA" in script and "$EXPECTED_SHA" in script and "!= " in script, + "report-pr-test compares tested commit with the reported commit") + +t = jobs["test"] +check(t.get("outputs", {}).get("tested_sha") == "${{ steps.tested-sha.outputs.sha }}", + "test exports the commit it checked out") +check(any(s.get("id") == "tested-sha" and "git rev-parse HEAD" in s.get("run", "") + for s in t["steps"]), "test records HEAD after checkout") + +# test-command.yml supplies the fields the binding depends on. +text = open(f"{root}/.github/workflows/test-command.yml").read() +for field in ("pr_head_repo", "pr_head_sha", "pr_number"): + check(f"--field {field}=" in text, f"test-command.yml dispatches {field}") + +# Expression shape: push -> pushed ref, same-repo dispatch -> PR commit. +def resolve(expr, inputs, github): + for part in expr.replace("${{", "").replace("}}", "").split("||"): + part = part.strip() + ns, _, key = part.partition(".") + val = {"inputs": inputs, "github": github}.get(ns, {}).get(key, "") + if val: + return val + return "" + +gh = {"ref": "refs/heads/feature"} +check(resolve(REF, {}, gh) == "refs/heads/feature", "push run resolves to the pushed ref") +check(resolve(REF, {"pr_head_sha": "a" * 40}, gh) == "a" * 40, + "same-repo dispatch resolves to the PR commit, not main") +check(resolve(REF, {"pr_head_sha": "a" * 40}, gh) != "refs/heads/main", + "same-repo dispatch is not reported from main") + +print(f"\n{checks - len(fails)}/{checks} checks passed") +sys.exit(1 if fails else 0) +PY From 9482d2bc9364c66861e288faf72dde52fe56f28c Mon Sep 17 00:00:00 2001 From: walsykerem Date: Sat, 21 Nov 2026 21:54:25 +0300 Subject: [PATCH 3/4] chore: drop local proof script from branch --- proof/592-privileged-checkout.sh | 101 ------------------------------- 1 file changed, 101 deletions(-) delete mode 100644 proof/592-privileged-checkout.sh diff --git a/proof/592-privileged-checkout.sh b/proof/592-privileged-checkout.sh deleted file mode 100644 index 35342c66d..000000000 --- a/proof/592-privileged-checkout.sh +++ /dev/null @@ -1,101 +0,0 @@ -#!/usr/bin/env bash -# Asserts the privileged (self-hosted) jobs in .github/workflows/test.yml test -# the commit the dispatch names, that only fork-checks checks out a foreign -# repository, and that the fork gate still skips every privileged job. -# Usage: bash proof/592-privileged-checkout.sh [repo-root] -set -euo pipefail -root="${1:-$(cd "$(dirname "$0")/.." && pwd)}" -python3 - "$root" <<'PY' -import sys, yaml - -root = sys.argv[1].rstrip("/") -wf = yaml.safe_load(open(f"{root}/.github/workflows/test.yml")) -cmd = yaml.safe_load(open(f"{root}/.github/workflows/test-command.yml")) -jobs = wf["jobs"] -fails, checks = [], 0 - - -def check(ok, what): - global checks - checks += 1 - print(("ok " if ok else "FAIL ") + what) - if not ok: - fails.append(what) - - -def runners(job): - r = job.get("runs-on", "") - return r if isinstance(r, list) else [r] - - -def checkouts(job): - return [s for s in job.get("steps", []) if "actions/checkout" in str(s.get("uses", ""))] - - -privileged = [n for n, j in jobs.items() if "self-hosted" in runners(j)] -check(privileged == ["test", "test-darwin", "e2e-install"], - f"privileged jobs are test, test-darwin, e2e-install (got {privileged})") - -REF = "${{ inputs.pr_head_sha || github.ref }}" -for name in privileged: - cos = checkouts(jobs[name]) - check(len(cos) == 1, f"{name}: exactly one checkout") - with_ = cos[0].get("with", {}) or {} - check(with_.get("ref") == REF, f"{name}: checkout ref is '{REF}' (got {with_.get('ref')!r})") - check("inputs.pr_head_repo" not in str(with_.get("repository", "")), - f"{name}: never checks out inputs.pr_head_repo") - cond = str(jobs[name].get("if", "")) - check("github.event_name != 'workflow_dispatch'" in cond - and "inputs.pr_head_repo == ''" in cond - and "inputs.pr_head_repo == github.repository" in cond, - f"{name}: fork gate unchanged (skips a dispatch that names a fork)") - -foreign = [n for n, j in jobs.items() - if any("inputs.pr_head_repo" in str((s.get("with") or {}).get("repository", "")) - for s in checkouts(j))] -check(foreign == ["fork-checks"], f"only fork-checks checks out a foreign repo (got {foreign})") -fc = checkouts(jobs["fork-checks"])[0].get("with", {}) -check(fc.get("ref") == "${{ inputs.pr_head_sha }}", "fork-checks pins the dispatched sha") -check("self-hosted" not in runners(jobs["fork-checks"]), "fork-checks stays on a hosted runner") - -# report-pr-test must not label a run with a commit it did not test. -rep = jobs["report-pr-test"] -check("test" in rep.get("needs", []), "report-pr-test waits for test") -env = rep["steps"][0].get("env", {}) -check(env.get("TESTED_SHA") == "${{ needs.test.outputs.tested_sha }}", - "report-pr-test reads the tested commit from test's output") -script = rep["steps"][0]["run"] -check("$TESTED_SHA" in script and "$EXPECTED_SHA" in script and "!= " in script, - "report-pr-test compares tested commit with the reported commit") - -t = jobs["test"] -check(t.get("outputs", {}).get("tested_sha") == "${{ steps.tested-sha.outputs.sha }}", - "test exports the commit it checked out") -check(any(s.get("id") == "tested-sha" and "git rev-parse HEAD" in s.get("run", "") - for s in t["steps"]), "test records HEAD after checkout") - -# test-command.yml supplies the fields the binding depends on. -text = open(f"{root}/.github/workflows/test-command.yml").read() -for field in ("pr_head_repo", "pr_head_sha", "pr_number"): - check(f"--field {field}=" in text, f"test-command.yml dispatches {field}") - -# Expression shape: push -> pushed ref, same-repo dispatch -> PR commit. -def resolve(expr, inputs, github): - for part in expr.replace("${{", "").replace("}}", "").split("||"): - part = part.strip() - ns, _, key = part.partition(".") - val = {"inputs": inputs, "github": github}.get(ns, {}).get(key, "") - if val: - return val - return "" - -gh = {"ref": "refs/heads/feature"} -check(resolve(REF, {}, gh) == "refs/heads/feature", "push run resolves to the pushed ref") -check(resolve(REF, {"pr_head_sha": "a" * 40}, gh) == "a" * 40, - "same-repo dispatch resolves to the PR commit, not main") -check(resolve(REF, {"pr_head_sha": "a" * 40}, gh) != "refs/heads/main", - "same-repo dispatch is not reported from main") - -print(f"\n{checks - len(fails)}/{checks} checks passed") -sys.exit(1 if fails else 0) -PY From 76f60e2aeda085e64a97b5f587ff27ab5666c61d Mon Sep 17 00:00:00 2001 From: walsykerem Date: Sat, 21 Nov 2026 22:05:58 +0300 Subject: [PATCH 4/4] ci: pin privileged checkouts to the triggering commit --- .github/workflows/test.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f5853601e..b405aa991 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -67,8 +67,8 @@ jobs: steps: - uses: actions/checkout@v4.4.0 with: - # Test the dispatched PR commit; a push has no inputs and uses its ref. - ref: ${{ inputs.pr_head_sha || github.ref }} + # Test the dispatched PR commit; otherwise the commit that triggered the run. + ref: ${{ inputs.pr_head_sha || github.sha }} persist-credentials: false - name: Record tested commit @@ -369,8 +369,8 @@ jobs: steps: - uses: actions/checkout@v4.4.0 with: - # Same commit as test: the dispatched PR commit, else the pushed ref. - ref: ${{ inputs.pr_head_sha || github.ref }} + # Same commit as test: the dispatched PR commit, else the triggering commit. + ref: ${{ inputs.pr_head_sha || github.sha }} persist-credentials: false - uses: actions/setup-go@v6.5.0 with: @@ -485,8 +485,8 @@ jobs: steps: - uses: actions/checkout@v4.4.0 with: - # Same commit as test: the dispatched PR commit, else the pushed ref. - ref: ${{ inputs.pr_head_sha || github.ref }} + # Same commit as test: the dispatched PR commit, else the triggering commit. + ref: ${{ inputs.pr_head_sha || github.sha }} persist-credentials: false - uses: actions/setup-go@v6.5.0 with: