|
1 | 1 | // Tests for `verify-main-source.mjs` (#5119): the arguments and the report, |
2 | | -// then the guard end to end against throwaway repositories shaped like the |
3 | | -// release flow: a merge PR from a branch pushed off `v2/main` passes, as does |
| 2 | +// the guard end to end against throwaway repositories shaped like the |
| 3 | +// release flow, a merge PR from a branch pushed off `v2/main` passes, as does |
4 | 4 | // one `v2/main` has since moved past; a branch with a commit of its own, or a |
5 | | -// merge of `v2/main` into something else, fails. Run via `npm run |
6 | | -// test:scripts`. |
| 5 | +// merge of `v2/main` into something else, fails. Last, the properties of |
| 6 | +// `main-source.yml` its safety rests on. Run via `npm run test:scripts`. |
7 | 7 |
|
8 | 8 | import { test } from "node:test"; |
9 | 9 | import assert from "node:assert/strict"; |
10 | 10 | import { spawnSync } from "node:child_process"; |
11 | | -import { mkdtempSync, rmSync } from "node:fs"; |
| 11 | +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; |
12 | 12 | import { devNull, tmpdir } from "node:os"; |
13 | 13 | import path from "node:path"; |
| 14 | +import { parse } from "yaml"; |
14 | 15 | import { |
15 | 16 | DEFAULT_SOURCE, |
16 | 17 | MAX_LISTED, |
@@ -162,3 +163,57 @@ test("verifyMainSource: run as a script, a bad argument exits 2", () => { |
162 | 163 | assert.equal(r.status, 2); |
163 | 164 | assert.match(r.stderr, /--head is required/); |
164 | 165 | }); |
| 166 | + |
| 167 | +// The workflow's safety rests on properties of its YAML; pin them, so an edit |
| 168 | +// that would run the PR's code, or stop wiring the guard, fails here. |
| 169 | +const workflow = parse( |
| 170 | + readFileSync( |
| 171 | + path.join(import.meta.dirname, "..", ".github/workflows/main-source.yml"), |
| 172 | + "utf8", |
| 173 | + ), |
| 174 | +); |
| 175 | +const job = workflow.jobs["main-source"]; |
| 176 | + |
| 177 | +test("main-source.yml: runs main's copy, on PRs into main only, read-only", () => { |
| 178 | + assert.deepEqual(Object.keys(workflow.on), ["pull_request_target"]); |
| 179 | + assert.deepEqual(workflow.on.pull_request_target.branches, ["main"]); |
| 180 | + assert.ok(workflow.on.pull_request_target.types.includes("edited")); |
| 181 | + assert.deepEqual(workflow.permissions, { contents: "read" }); |
| 182 | + assert.equal(job.permissions, undefined); |
| 183 | + assert.deepEqual(Object.keys(workflow.jobs), ["main-source"]); |
| 184 | + assert.equal(job.name, "Source is v2/main"); |
| 185 | + assert.ok(job["timeout-minutes"]); |
| 186 | +}); |
| 187 | + |
| 188 | +test("main-source.yml: checks out main, never the PR, and fetches the PR as objects only", () => { |
| 189 | + const checkout = job.steps.filter((s) => |
| 190 | + /actions\/checkout/.test(s.uses ?? ""), |
| 191 | + ); |
| 192 | + assert.equal(checkout.length, 1); |
| 193 | + assert.equal(checkout[0].with?.ref, undefined); |
| 194 | + assert.equal(checkout[0].with?.repository, undefined); |
| 195 | + assert.equal(checkout[0].with["persist-credentials"], false); |
| 196 | + const runs = job.steps.map((s) => s.run ?? "").join("\n"); |
| 197 | + const refspec = '"+refs/pull/${PR_NUMBER}/head:refs/remotes/pr/head"'; |
| 198 | + assert.ok(runs.includes(refspec), "the PR head is fetched"); |
| 199 | + // Past that fetch, nothing names the PR's ref, or switches to, installs or |
| 200 | + // executes a tree. |
| 201 | + const rest = runs.replace(refspec, ""); |
| 202 | + assert.doesNotMatch(rest, /pr\/head|refs\/pull/); |
| 203 | + assert.doesNotMatch(rest, /git (checkout|switch|reset|worktree)|npm |npx /); |
| 204 | + // No step reads a secret; expressions reach the shell only through env. |
| 205 | + for (const step of job.steps) { |
| 206 | + assert.doesNotMatch(JSON.stringify(step), /secrets\./); |
| 207 | + assert.doesNotMatch(step.run ?? "", /\$\{\{/); |
| 208 | + } |
| 209 | +}); |
| 210 | + |
| 211 | +test("main-source.yml: hands the event's head SHA to the guard", () => { |
| 212 | + const step = job.steps.find((s) => |
| 213 | + /verify-main-source\.mjs/.test(s.run ?? ""), |
| 214 | + ); |
| 215 | + assert.ok(step, "a step runs the guard"); |
| 216 | + assert.equal(step.env.HEAD_SHA, "${{ github.event.pull_request.head.sha }}"); |
| 217 | + assert.match(step.run, /--head "\$HEAD_SHA"/); |
| 218 | + assert.doesNotMatch(step.run, /--source/); |
| 219 | +}); |
0 commit comments