|
9 | 9 |
|
10 | 10 | import { execFileSync } from 'node:child_process'; |
11 | 11 | import assert from 'node:assert/strict'; |
| 12 | +import fs from 'node:fs'; |
| 13 | +import os from 'node:os'; |
12 | 14 | import path from 'node:path'; |
13 | 15 | import test from 'node:test'; |
14 | 16 | import { audit } from './audit.ts'; |
15 | 17 | import { LANE_ENVIRONMENTS, NON_GATE_STEPS } from './declarations.ts'; |
16 | | -import { categories, covered, loadModel, stepDigest, type Lane, type Model } from './model.ts'; |
| 18 | +import { |
| 19 | + categories, |
| 20 | + covered, |
| 21 | + loadLanes, |
| 22 | + loadModel, |
| 23 | + stepDigest, |
| 24 | + type Lane, |
| 25 | + type Model, |
| 26 | +} from './model.ts'; |
17 | 27 | import { CHECK_CATALOG } from '../check-affected/checks.ts'; |
18 | 28 |
|
19 | 29 | const repoRoot = path.resolve(import.meta.dirname, '../..'); |
@@ -44,7 +54,6 @@ function plantCommand(run: string, extras: Record<string, string> = {}): Model { |
44 | 54 | name: 'Planted', |
45 | 55 | source: 'ci.yml', |
46 | 56 | run, |
47 | | - inputs: [], |
48 | 57 | extras, |
49 | 58 | digest: stepDigest(run, extras), |
50 | 59 | }; |
@@ -82,6 +91,88 @@ function editStep( |
82 | 91 | const bypassesFor = (run: string, extras: Record<string, string> = {}) => |
83 | 92 | audit(plantCommand(run, extras)).filter((failure) => failure.assertion === 'bypass'); |
84 | 93 |
|
| 94 | +/** |
| 95 | + * Load a planted workflow with the REAL loader, resolving `./.github/actions/…` against the |
| 96 | + * real tree. The composite action under test is the one CI uses, so these cases exercise |
| 97 | + * parse, resolution and audit together rather than a hand-built lane. |
| 98 | + */ |
| 99 | +function plantedWorkflow(yaml: string): Model { |
| 100 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'gate-lane-')); |
| 101 | + try { |
| 102 | + fs.writeFileSync(path.join(dir, 'planted.yml'), yaml); |
| 103 | + const lanes = loadLanes(dir, repoRoot, base.scripts); |
| 104 | + return mutate((model) => ({ lanes: [...model.lanes, ...lanes] })); |
| 105 | + } finally { |
| 106 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +const applyRunnerCall = (command: string) => `name: Planted |
| 111 | +on: |
| 112 | + pull_request: |
| 113 | +jobs: |
| 114 | + planted: |
| 115 | + name: Planted Apple Lane |
| 116 | + runs-on: macos-15 |
| 117 | + steps: |
| 118 | + - name: Build the Apple runner |
| 119 | + uses: ./.github/actions/setup-apple-runner-build |
| 120 | + with: |
| 121 | + derived-path: /tmp/derived |
| 122 | + cache-key-prefix: planted |
| 123 | + build-command: ${command} |
| 124 | +`; |
| 125 | + |
| 126 | +test('a caller cannot smuggle code through an input a composite action executes', () => { |
| 127 | + // Review round 5: `setup-apple-runner-build` runs `${{ inputs.build-command }}`, so its |
| 128 | + // own step digest is CONSTANT and vouches for nothing — the command lives at the call |
| 129 | + // site. Both models below reach a byte-identical action; only the `with:` value differs. |
| 130 | + assert.deepEqual( |
| 131 | + messages(plantedWorkflow(applyRunnerCall('pnpm gate swift-runner-ios'))), |
| 132 | + [], |
| 133 | + 'a call site whose executable input is a gate needs no declaration', |
| 134 | + ); |
| 135 | + |
| 136 | + const smuggled = audit( |
| 137 | + plantedWorkflow(applyRunnerCall(`node -e 'import("./scripts/layering/check.ts")'`)), |
| 138 | + ).filter((failure) => failure.assertion === 'bypass'); |
| 139 | + assert.equal(smuggled.length, 1, 'and project code in the same value is a bypass'); |
| 140 | + assert.match(smuggled[0]?.message ?? '', /build-command/); |
| 141 | +}); |
| 142 | + |
| 143 | +test('an execution surface the model does not read is rejected rather than ignored', () => { |
| 144 | + // Neither shape is used today. `defaults.run` retargets the shell of every step in a |
| 145 | + // lane, and a reusable-workflow job runs steps from a file the loader never opens, so |
| 146 | + // both would make the no-bypass rule quietly incomplete. |
| 147 | + const surfaces = (yaml: string) => |
| 148 | + audit(plantedWorkflow(yaml)).filter((failure) => failure.assertion === 'surface'); |
| 149 | + |
| 150 | + const found = surfaces(`name: Planted |
| 151 | +on: |
| 152 | + pull_request: |
| 153 | +defaults: |
| 154 | + run: |
| 155 | + shell: bash -lc {0} |
| 156 | +jobs: |
| 157 | + planted: |
| 158 | + runs-on: ubuntu-latest |
| 159 | + steps: |
| 160 | + - run: pnpm gate layering |
| 161 | +`); |
| 162 | + assert.equal(found.length, 1); |
| 163 | + assert.match(found[0]?.message ?? '', /defaults:/); |
| 164 | + |
| 165 | + const reusable = surfaces(`name: Planted |
| 166 | +on: |
| 167 | + pull_request: |
| 168 | +jobs: |
| 169 | + planted: |
| 170 | + uses: ./.github/workflows/ci.yml |
| 171 | +`); |
| 172 | + assert.equal(reusable.length, 1); |
| 173 | + assert.match(reusable[0]?.message ?? '', /reusable workflow/); |
| 174 | +}); |
| 175 | + |
85 | 176 | test('the live tree is green — every planted failure below is a real difference', () => { |
86 | 177 | assert.deepEqual(messages(base), []); |
87 | 178 | }); |
@@ -175,6 +266,8 @@ test('a qualifying lane accepts nothing but a gate invocation or a listed step', |
175 | 266 | 'echo bm9kZSBzY3JpcHRzL2xheWVyaW5nL2NoZWNrLnRz | base64 -d | sh', |
176 | 267 | "bash <<'EOS'\nnode scripts/layering/check.ts\nEOS", |
177 | 268 | 'printf "node scripts/layering/check.ts" > /tmp/x.sh && sh /tmp/x.sh', |
| 269 | + // An env prefix is a `env:` block spelled in the shell, and injects the same way. |
| 270 | + 'NODE_OPTIONS=--import ./scripts/layering/check.ts pnpm gate layering', |
178 | 271 | ]; |
179 | 272 | for (const run of evasions) { |
180 | 273 | assert.equal(bypassesFor(run).length, 1, `must be rejected: ${run}`); |
@@ -307,9 +400,7 @@ test('an entry goes inert when the step it describes stops existing', () => { |
307 | 400 | })), |
308 | 401 | })); |
309 | 402 | assert.ok( |
310 | | - messages(deleted).some((message) => |
311 | | - /matches no `run:` step a qualifying lane reaches/.test(message), |
312 | | - ), |
| 403 | + messages(deleted).some((message) => /matches no shell a qualifying lane reaches/.test(message)), |
313 | 404 | 'a deleted step orphans its entry', |
314 | 405 | ); |
315 | 406 |
|
|
0 commit comments