From cd35883d2fb0af7b1f60b3a1c6acc1c36b640487 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Wed, 25 Nov 2026 02:01:24 +1100 Subject: [PATCH 1/8] ci(codeql): run the analysis step again Commit ed08dce88, on 12 May 2026, moved both CodeQL steps to a newer version. It also changed the analysis step from codeql-action/analyze to codeql-action/init. Since then, the job has prepared a database and stopped. No query has run on main. No result has reached code scanning. The job still passed, with only a warning about the unexpected category input. The step now uses codeql-action/analyze@v4.38.0. A new test fails when a job that starts CodeQL does not run init and then analyze. The time limit goes from 10 to 15 minutes. The rule from eab9ffd09 is about twice the longest successful run, rounded up to 5 minutes. With the analysis running, the longest of 240 successful runs in the 30 days before 12 May took 371 seconds. --- .github/workflows/codeql.yml | 4 +- .../workflow-codeql-analyze.test.mjs | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 scripts/__tests__/workflow-codeql-analyze.test.mjs diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 9a5b5072cf..7e4ed12cb0 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -19,7 +19,7 @@ jobs: analyze: name: Analyze runs-on: blacksmith-4vcpu-ubuntu-2404 - timeout-minutes: 10 + timeout-minutes: 15 strategy: fail-fast: false @@ -37,6 +37,6 @@ jobs: languages: ${{ matrix.language }} - name: Perform CodeQL Analysis - uses: github/codeql-action/init@v4.38.0 + uses: github/codeql-action/analyze@v4.38.0 with: category: "/language:${{ matrix.language }}" diff --git a/scripts/__tests__/workflow-codeql-analyze.test.mjs b/scripts/__tests__/workflow-codeql-analyze.test.mjs new file mode 100644 index 0000000000..51edfb17b4 --- /dev/null +++ b/scripts/__tests__/workflow-codeql-analyze.test.mjs @@ -0,0 +1,41 @@ +import { describe, expect, it } from 'vitest' +import { readWorkflow, workflowFiles } from './lib/workflows.mjs' + +/** + * A job that starts CodeQL must also run the analysis. + * + * `codeql-action/init` only prepares the database. `codeql-action/analyze` + * runs the queries and uploads the results. From 12 May to November 2026 the + * analysis step in `codeql.yml` named `init` by mistake. The job still + * passed, with only a warning about the unexpected `category` input, so + * nothing showed that no code on `main` was analysed. + */ +const CODEQL_STEP = /^github\/codeql-action\/(init|analyze)@/ + +const codeqlJobs = workflowFiles().flatMap((file) => + Object.entries(readWorkflow(file)?.jobs ?? {}) + .map(([id, job]) => ({ + ref: `${file}#${id}`, + actions: (job?.steps ?? []) + .map((step) => step?.uses) + .filter((uses) => typeof uses === 'string' && CODEQL_STEP.test(uses)) + .map((uses) => uses.match(CODEQL_STEP)[1]), + })) + .filter(({ actions }) => actions.length > 0), +) + +describe('CodeQL jobs run the analysis', () => { + it('finds the CodeQL job', () => { + // An empty scan would pass the check below. + expect(codeqlJobs.map(({ ref }) => ref)).toContain( + '.github/workflows/codeql.yml#analyze', + ) + }) + + it('runs init once, then analyze, in every CodeQL job', () => { + const wrong = codeqlJobs + .filter(({ actions }) => actions.join(',') !== 'init,analyze') + .map(({ ref, actions }) => `${ref}: ${actions.join(', ')}`) + expect(wrong).toEqual([]) + }) +}) From a2686a79f3f9f15eb06e2ea108bd8895f942d5ac Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Thu, 26 Nov 2026 17:53:39 +1100 Subject: [PATCH 2/8] test(ci): name the commit that dropped the CodeQL analysis The comment in the CodeQL guard gave the date of the mistake, but not the commit that made it. Other guards in this folder name the commit behind the failure they check for, so a reader can look at it. The comment now names ed08dce88. That commit changed the analysis step from codeql-action/analyze to codeql-action/init. --- scripts/__tests__/workflow-codeql-analyze.test.mjs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/__tests__/workflow-codeql-analyze.test.mjs b/scripts/__tests__/workflow-codeql-analyze.test.mjs index 51edfb17b4..1cfe62c878 100644 --- a/scripts/__tests__/workflow-codeql-analyze.test.mjs +++ b/scripts/__tests__/workflow-codeql-analyze.test.mjs @@ -5,10 +5,11 @@ import { readWorkflow, workflowFiles } from './lib/workflows.mjs' * A job that starts CodeQL must also run the analysis. * * `codeql-action/init` only prepares the database. `codeql-action/analyze` - * runs the queries and uploads the results. From 12 May to November 2026 the - * analysis step in `codeql.yml` named `init` by mistake. The job still - * passed, with only a warning about the unexpected `category` input, so - * nothing showed that no code on `main` was analysed. + * runs the queries and uploads the results. Commit `ed08dce88`, on 12 May + * 2026, changed the analysis step in `codeql.yml` to name `init` by mistake. + * Until November 2026 the job still passed, with only a warning about the + * unexpected `category` input, so nothing showed that no code on `main` was + * analysed. */ const CODEQL_STEP = /^github\/codeql-action\/(init|analyze)@/ From 0cef28b5f79ff8eb16cdf6bffaf578f3ac4ec19c Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Thu, 26 Nov 2026 17:54:05 +1100 Subject: [PATCH 3/8] test(ci): match each CodeQL step against the pattern once The CodeQL guard tested each step against its pattern in a filter. A map then matched the same step again to read the action name. The second match repeated work that the first had already done. One flatMap now matches each step once. The flatMap keeps the action name when the step matches, and drops the step when it does not. The result is the same. --- scripts/__tests__/workflow-codeql-analyze.test.mjs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/__tests__/workflow-codeql-analyze.test.mjs b/scripts/__tests__/workflow-codeql-analyze.test.mjs index 1cfe62c878..a1ae4eb0d9 100644 --- a/scripts/__tests__/workflow-codeql-analyze.test.mjs +++ b/scripts/__tests__/workflow-codeql-analyze.test.mjs @@ -17,10 +17,11 @@ const codeqlJobs = workflowFiles().flatMap((file) => Object.entries(readWorkflow(file)?.jobs ?? {}) .map(([id, job]) => ({ ref: `${file}#${id}`, - actions: (job?.steps ?? []) - .map((step) => step?.uses) - .filter((uses) => typeof uses === 'string' && CODEQL_STEP.test(uses)) - .map((uses) => uses.match(CODEQL_STEP)[1]), + actions: (job?.steps ?? []).flatMap((step) => { + const match = + typeof step?.uses === 'string' ? step.uses.match(CODEQL_STEP) : null + return match ? [match[1]] : [] + }), })) .filter(({ actions }) => actions.length > 0), ) From 55cdf400b91aff456403a499020c559da572c080 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Thu, 26 Nov 2026 17:55:34 +1100 Subject: [PATCH 4/8] test(ci): move the composite action walker into the shared helpers The node-gyp guard opens each local composite action that a job uses. The guard then puts the steps of that action in the place of the step that calls it. The CodeQL guard needs the same walk, so that a CodeQL step inside a composite action is not missed. The rule in shared-test-helpers.test.mjs is that a helper used by more than one guard goes in scripts/__tests__/lib/. A second copy that drifts does not fail: it finds less, and the guard still passes. flattenSteps, resolveRepoComposite, and usesOf move from the node-gyp guard into lib/workflows.mjs without change. The node-gyp guard imports them. The node-gyp tests of the walk stay in place and still pass. --- scripts/__tests__/lib/workflows.mjs | 93 ++++++++++++++++++- scripts/__tests__/workflow-node-gyp.test.mjs | 97 ++------------------ 2 files changed, 97 insertions(+), 93 deletions(-) diff --git a/scripts/__tests__/lib/workflows.mjs b/scripts/__tests__/lib/workflows.mjs index e8f933176a..c771fe3619 100644 --- a/scripts/__tests__/lib/workflows.mjs +++ b/scripts/__tests__/lib/workflows.mjs @@ -1,4 +1,4 @@ -import { readdirSync, readFileSync } from 'node:fs' +import { existsSync, readdirSync, readFileSync } from 'node:fs' import { join } from 'node:path' import yaml from 'js-yaml' import { REPO_ROOT } from './repo-root.mjs' @@ -44,8 +44,8 @@ export function workflowFiles() { /** * Parse one repo-relative YAML file from the workflow graph — a workflow, or a * composite action manifest that a workflow reaches through `uses: ./...`. - * `workflow-node-gyp.test.mjs` follows those `uses:` edges and needs to parse - * both kinds with the same reader. + * `flattenSteps` below follows those `uses:` edges and needs to parse both + * kinds with the same reader. * * Note for callers: `on:` parses as the boolean `true` under YAML 1.1 (the * "Norway problem"), so read triggers as `wf.on ?? wf[true]`. @@ -53,3 +53,90 @@ export function workflowFiles() { export function readWorkflow(relPath) { return yaml.load(readFileSync(join(REPO_ROOT, relPath), 'utf8')) } + +export function usesOf(step) { + return typeof step?.uses === 'string' ? step.uses.trim() || null : null +} + +const LOCAL_USES = /^\.{1,2}\// + +function stepLabel(step, index) { + return step?.name || usesOf(step) || `step #${index + 1}` +} + +/** + * Flattens a step list, splicing a local composite action's own steps in at the + * position of the `uses:` that reaches it. The result is the sequence GitHub + * actually executes, which is the only sequence in which "before" means + * anything. + * + * `resolveComposite` is injected so a guard can exercise its checker against + * synthetic composites without writing fixture files — an in-repo scan is not + * a test of the traversal, it is a test of the repo. The repo's own resolver is + * `resolveRepoComposite`. + * + * `visited` breaks the `A uses B uses A` cycle. Scoped per job, matching + * `lint-no-workflow-caching.mjs`: a composite shared by two jobs is flattened + * into each. + */ +export function flattenSteps( + steps, + resolveComposite, + trail = '', + visited = new Set(), +) { + const flat = [] + const unresolved = [] + + ;(Array.isArray(steps) ? steps : []).forEach((step, index) => { + const label = `${trail}${stepLabel(step, index)}` + const uses = usesOf(step) + + if (uses === null || !LOCAL_USES.test(uses)) { + flat.push({ step, label }) + return + } + + const composite = resolveComposite(uses) + if (composite === null) { + // Fail closed. A local `uses:` pointing at nothing fails the job on + // GitHub anyway; passing it here would make "rename the action" a way to + // leave a check with nothing to read. + unresolved.push(`${label}: \`uses: ${uses}\` — no action manifest there`) + flat.push({ step, label }) + return + } + if (visited.has(composite.id)) return + visited.add(composite.id) + + const nested = flattenSteps( + composite.steps, + resolveComposite, + `${label} -> ${composite.id} step "`, + visited, + ) + for (const entry of nested.flat) { + flat.push({ step: entry.step, label: `${entry.label}"` }) + } + unresolved.push(...nested.unresolved) + }) + + return { flat, unresolved } +} + +/** + * GitHub resolves `uses: ./x` against the checkout root, not the calling file. + * Both manifest spellings are valid, so probing only `action.yml` would stop + * the traversal dead at a composite that used the other one. + */ +export function resolveRepoComposite(usesPath) { + const dir = usesPath.replace(LOCAL_USES, '') + for (const name of ['action.yml', 'action.yaml']) { + const rel = `${dir}/${name}` + if (existsSync(join(REPO_ROOT, rel))) { + const doc = readWorkflow(rel) + return { id: rel, steps: doc?.runs?.steps ?? [] } + } + } + return null +} diff --git a/scripts/__tests__/workflow-node-gyp.test.mjs b/scripts/__tests__/workflow-node-gyp.test.mjs index 3cb65bb2b4..97b75bf301 100644 --- a/scripts/__tests__/workflow-node-gyp.test.mjs +++ b/scripts/__tests__/workflow-node-gyp.test.mjs @@ -1,8 +1,11 @@ -import { existsSync } from 'node:fs' -import { join } from 'node:path' import { describe, expect, it } from 'vitest' -import { REPO_ROOT } from './lib/repo-root.mjs' -import { readWorkflow, workflowFiles } from './lib/workflows.mjs' +import { + flattenSteps, + readWorkflow, + resolveRepoComposite, + usesOf, + workflowFiles, +} from './lib/workflows.mjs' /** * Every job that runs `pnpm install` on a Linux runner must install `node-gyp` @@ -137,10 +140,6 @@ const IGNORE_SCRIPTS = /--ignore-scripts\b/ */ const PREBUILT_RUNNER = /(?:^|[\s-])(?:macos|windows|win32|darwin)(?:[\s-]|$)/i -function usesOf(step) { - return typeof step?.uses === 'string' ? step.uses.trim() || null : null -} - function runOf(step) { return typeof step?.run === 'string' ? step.run : null } @@ -176,71 +175,6 @@ export function needsNodeGyp(runsOn) { // Traversal // --------------------------------------------------------------------------- -const LOCAL_USES = /^\.{1,2}\// - -function stepLabel(step, index) { - return step?.name || usesOf(step) || `step #${index + 1}` -} - -/** - * Flattens a step list, splicing a local composite action's own steps in at the - * position of the `uses:` that reaches it. The result is the sequence GitHub - * actually executes, which is the only sequence in which "before" means - * anything. - * - * `resolveComposite` is injected so the checker can be exercised against - * synthetic composites below without writing fixture files — an in-repo scan is - * not a test of the traversal, it is a test of the repo. - * - * `visited` breaks the `A uses B uses A` cycle. Scoped per job, matching - * `lint-no-workflow-caching.mjs`: a composite shared by two jobs is flattened - * into each. - */ -export function flattenSteps( - steps, - resolveComposite, - trail = '', - visited = new Set(), -) { - const flat = [] - const unresolved = [] - - ;(Array.isArray(steps) ? steps : []).forEach((step, index) => { - const label = `${trail}${stepLabel(step, index)}` - const uses = usesOf(step) - - if (uses === null || !LOCAL_USES.test(uses)) { - flat.push({ step, label }) - return - } - - const composite = resolveComposite(uses) - if (composite === null) { - // Fail closed. A local `uses:` pointing at nothing fails the job on - // GitHub anyway; passing it here would make "rename the action" a way to - // leave this check with nothing to read. - unresolved.push(`${label}: \`uses: ${uses}\` — no action manifest there`) - flat.push({ step, label }) - return - } - if (visited.has(composite.id)) return - visited.add(composite.id) - - const nested = flattenSteps( - composite.steps, - resolveComposite, - `${label} -> ${composite.id} step "`, - visited, - ) - for (const entry of nested.flat) { - flat.push({ step: entry.step, label: `${entry.label}"` }) - } - unresolved.push(...nested.unresolved) - }) - - return { flat, unresolved } -} - /** * The rule itself, over an already-flattened sequence: for each install step, * some EARLIER step must have put node-gyp on PATH. @@ -270,23 +204,6 @@ export function auditFlattened(flat) { // The repo scan // --------------------------------------------------------------------------- -/** - * GitHub resolves `uses: ./x` against the checkout root, not the calling file. - * Both manifest spellings are valid, so probing only `action.yml` would stop - * the traversal dead at a composite that used the other one. - */ -function resolveRepoComposite(usesPath) { - const dir = usesPath.replace(LOCAL_USES, '') - for (const name of ['action.yml', 'action.yaml']) { - const rel = `${dir}/${name}` - if (existsSync(join(REPO_ROOT, rel))) { - const doc = readWorkflow(rel) - return { id: rel, steps: doc?.runs?.steps ?? [] } - } - } - return null -} - function scan() { const jobs = [] for (const relPath of workflowFiles()) { From e3d054164baf28d4a07fdd08991d7ac9fd903dd5 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Thu, 26 Nov 2026 17:56:46 +1100 Subject: [PATCH 5/8] test(ci): follow local composite actions in the CodeQL guard The CodeQL guard read only the steps written in each job. The guard did not open a local composite action that a job calls with uses: ./. That caused two wrong results: - A job that ran init inside a composite action and never ran analyze was not seen as a CodeQL job, so the guard passed. - A correct job that ran init inside a composite action and analyze in its own steps was reported as running analyze only. The guard now opens each local composite action with flattenSteps from lib/workflows.mjs. A CodeQL step inside one counts at the place where the job calls it. A local action that cannot be opened fails a new check, because a CodeQL step could be inside it. New tests give the guard synthetic composite actions, so they test the walk itself and do not depend on the workflows in the repo. The test name "in every CodeQL job" is now true, so it stays. --- .../workflow-codeql-analyze.test.mjs | 79 ++++++++++++++++--- 1 file changed, 66 insertions(+), 13 deletions(-) diff --git a/scripts/__tests__/workflow-codeql-analyze.test.mjs b/scripts/__tests__/workflow-codeql-analyze.test.mjs index a1ae4eb0d9..dbe7d8b6a1 100644 --- a/scripts/__tests__/workflow-codeql-analyze.test.mjs +++ b/scripts/__tests__/workflow-codeql-analyze.test.mjs @@ -1,5 +1,10 @@ import { describe, expect, it } from 'vitest' -import { readWorkflow, workflowFiles } from './lib/workflows.mjs' +import { + flattenSteps, + readWorkflow, + resolveRepoComposite, + workflowFiles, +} from './lib/workflows.mjs' /** * A job that starts CodeQL must also run the analysis. @@ -13,18 +18,25 @@ import { readWorkflow, workflowFiles } from './lib/workflows.mjs' */ const CODEQL_STEP = /^github\/codeql-action\/(init|analyze)@/ -const codeqlJobs = workflowFiles().flatMap((file) => - Object.entries(readWorkflow(file)?.jobs ?? {}) - .map(([id, job]) => ({ - ref: `${file}#${id}`, - actions: (job?.steps ?? []).flatMap((step) => { - const match = - typeof step?.uses === 'string' ? step.uses.match(CODEQL_STEP) : null - return match ? [match[1]] : [] - }), - })) - .filter(({ actions }) => actions.length > 0), +// A local action that cannot be opened is reported, because a CodeQL step +// could be inside it. +function codeqlSteps(steps, resolveComposite) { + const { flat, unresolved } = flattenSteps(steps, resolveComposite) + const actions = flat.flatMap(({ step }) => { + const match = + typeof step?.uses === 'string' ? step.uses.match(CODEQL_STEP) : null + return match ? [match[1]] : [] + }) + return { actions, unresolved } +} + +const scanned = workflowFiles().flatMap((file) => + Object.entries(readWorkflow(file)?.jobs ?? {}).map(([id, job]) => ({ + ref: `${file}#${id}`, + ...codeqlSteps(job?.steps, resolveRepoComposite), + })), ) +const codeqlJobs = scanned.filter(({ actions }) => actions.length > 0) describe('CodeQL jobs run the analysis', () => { it('finds the CodeQL job', () => { @@ -34,10 +46,51 @@ describe('CodeQL jobs run the analysis', () => { ) }) + it('opens every local action that a job uses', () => { + const unresolved = scanned.flatMap(({ ref, unresolved }) => + unresolved.map((entry) => `${ref}: ${entry}`), + ) + expect(unresolved).toEqual([]) + }) + it('runs init once, then analyze, in every CodeQL job', () => { const wrong = codeqlJobs - .filter(({ actions }) => actions.join(',') !== 'init,analyze') + .filter(({ actions }) => actions.join(', ') !== 'init, analyze') .map(({ ref, actions }) => `${ref}: ${actions.join(', ')}`) expect(wrong).toEqual([]) }) }) + +describe('the checker this guard depends on', () => { + const init = { uses: 'github/codeql-action/init@v4.38.0' } + const analyze = { uses: 'github/codeql-action/analyze@v4.38.0' } + const callComposite = { uses: './.github/actions/codeql' } + const noComposites = () => null + const composite = (steps) => (uses) => + uses === callComposite.uses ? { id: 'codeql/action.yml', steps } : null + + it('reproduces the defect: init named twice', () => { + expect(codeqlSteps([init, init], noComposites).actions).toEqual([ + 'init', + 'init', + ]) + }) + + it('finds init inside a composite action', () => { + expect( + codeqlSteps([callComposite, analyze], composite([init])).actions, + ).toEqual(['init', 'analyze']) + }) + + it('finds a composite action that runs init with no analysis', () => { + expect(codeqlSteps([callComposite], composite([init])).actions).toEqual([ + 'init', + ]) + }) + + it('reports a local action it cannot open', () => { + expect(codeqlSteps([callComposite], noComposites).unresolved).toHaveLength( + 1, + ) + }) +}) From 6bd2b5c198bc12dd3074da585ffa896d698735af Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Thu, 26 Nov 2026 17:58:13 +1100 Subject: [PATCH 6/8] test(ci): refuse a CodeQL analysis that never runs or never uploads The CodeQL guard counted every analyze step, whatever its if: or with: said. Two settings let the guard pass while no result reaches code scanning. That is the failure the guard exists to catch. - An if: that is always false. GitHub skips the step on every run. The guard refuses false, 'false', and ${{ false }}. - An upload input other than always. The analyze action uploads only when upload is always. Always is also the default. With never, failure-only, or false, a run that succeeds sends nothing to code scanning. A new check refuses an analyze step with either setting. The message says what to change. An if: that can be true is still accepted. --- .../workflow-codeql-analyze.test.mjs | 79 ++++++++++++++++++- 1 file changed, 76 insertions(+), 3 deletions(-) diff --git a/scripts/__tests__/workflow-codeql-analyze.test.mjs b/scripts/__tests__/workflow-codeql-analyze.test.mjs index dbe7d8b6a1..03e07d944a 100644 --- a/scripts/__tests__/workflow-codeql-analyze.test.mjs +++ b/scripts/__tests__/workflow-codeql-analyze.test.mjs @@ -22,12 +22,36 @@ const CODEQL_STEP = /^github\/codeql-action\/(init|analyze)@/ // could be inside it. function codeqlSteps(steps, resolveComposite) { const { flat, unresolved } = flattenSteps(steps, resolveComposite) - const actions = flat.flatMap(({ step }) => { + const found = flat.flatMap(({ step, label }) => { const match = typeof step?.uses === 'string' ? step.uses.match(CODEQL_STEP) : null - return match ? [match[1]] : [] + return match ? [{ action: match[1], step, label }] : [] }) - return { actions, unresolved } + return { + actions: found.map(({ action }) => action), + analyses: found.filter(({ action }) => action === 'analyze'), + unresolved, + } +} + +// `if: false`, `if: 'false'` and `if: ${{ false }}` all skip the step. +const ALWAYS_FALSE = /^\s*(?:\$\{\{\s*false\s*\}\}|false)\s*$/ + +// The analyze action uploads to code scanning only when `upload` is +// `always`, which is its default. +function analysisProblem(step) { + const condition = step?.if + if ( + condition === false || + (typeof condition === 'string' && ALWAYS_FALSE.test(condition)) + ) { + return '`if:` is always false, so the analysis never runs' + } + const upload = step?.with?.upload + if (upload !== undefined && upload !== 'always') { + return `\`upload: ${upload}\` keeps the results out of code scanning. Remove \`upload\`, or set it to \`always\`.` + } + return null } const scanned = workflowFiles().flatMap((file) => @@ -59,6 +83,16 @@ describe('CodeQL jobs run the analysis', () => { .map(({ ref, actions }) => `${ref}: ${actions.join(', ')}`) expect(wrong).toEqual([]) }) + + it('runs every CodeQL analysis and uploads its results', () => { + const wrong = codeqlJobs.flatMap(({ ref, analyses }) => + analyses.flatMap(({ step, label }) => { + const problem = analysisProblem(step) + return problem ? [`${ref}: ${label}: ${problem}`] : [] + }), + ) + expect(wrong).toEqual([]) + }) }) describe('the checker this guard depends on', () => { @@ -93,4 +127,43 @@ describe('the checker this guard depends on', () => { 1, ) }) + + it('finds an analysis inside a composite action, to check its settings', () => { + const skipped = { ...analyze, if: false } + const { analyses } = codeqlSteps( + [init, callComposite], + composite([skipped]), + ) + expect(analyses.map(({ step }) => step)).toEqual([skipped]) + }) + + // biome-ignore lint/suspicious/noTemplateCurlyInString: a literal Actions expression, as written in a workflow. + it.each([false, 'false', '${{ false }}'])( + 'refuses an analysis with `if: %j`', + (condition) => { + expect(analysisProblem({ ...analyze, if: condition })).toMatch(/`if:`/) + }, + ) + + it('accepts an analysis whose `if:` can be true', () => { + expect( + analysisProblem({ ...analyze, if: "github.event_name == 'push'" }), + ).toBeNull() + }) + + it.each(['never', 'failure-only', false])( + 'refuses an analysis with `upload: %j`', + (upload) => { + expect(analysisProblem({ ...analyze, with: { upload } })).toMatch( + /`upload: /, + ) + }, + ) + + it('accepts an analysis that leaves `upload` unset, or sets it to always', () => { + expect(analysisProblem(analyze)).toBeNull() + expect( + analysisProblem({ ...analyze, with: { upload: 'always' } }), + ).toBeNull() + }) }) From 6d074be6b16824233d58af025d6a428dafbe9b07 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Thu, 26 Nov 2026 19:32:23 +1100 Subject: [PATCH 7/8] ci(codeql): pin both CodeQL steps to the commit of v4.38.0 A review asked for the analyze step to name a full commit SHA instead of a tag. A tag can be moved to other code. A commit SHA cannot, so the scan runs the code that was reviewed. Both steps now name b96794f015dfd88f77b49b1c93e0fa7110f94c63, the commit that the v4.38.0 tag points to. GitHub's compare of that commit with the tag reports them as identical. The init step is pinned too, so the two steps stay on the same version. The comment after each SHA names the tag, as the other SHA-pinned actions in this repository do. --- .github/workflows/codeql.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 7e4ed12cb0..21a2425fe3 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -32,11 +32,11 @@ jobs: uses: actions/checkout@v6 - name: Initialize CodeQL - uses: github/codeql-action/init@v4.38.0 + uses: github/codeql-action/init@b96794f015dfd88f77b49b1c93e0fa7110f94c63 # v4.38.0 with: languages: ${{ matrix.language }} - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v4.38.0 + uses: github/codeql-action/analyze@b96794f015dfd88f77b49b1c93e0fa7110f94c63 # v4.38.0 with: category: "/language:${{ matrix.language }}" From 6bfbada86e91ab57b5cb15ad686e8a9ba6de8098 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Thu, 26 Nov 2026 19:41:46 +1100 Subject: [PATCH 8/8] test(ci): count repeated composite calls and their conditions CodeRabbit found two gaps in flattenSteps, the helper that puts the steps of a local composite action in place of the step that calls it. - A second call to the same composite action in a job was dropped. The set that stops the `A uses B uses A` loop held every action the job had opened, not only the ones on the current call path. A job that called an action containing `init` twice, then ran `analyze`, read as "init, analyze", so the CodeQL guard passed it. The set now holds only the actions being expanded. An action leaves it when its expansion ends. The node-gyp cycle test still passes. - The `if:` of a composite call was lost. An `analyze` step inside an action called with `if: false` never runs on GitHub, yet the guard accepted it. Each flattened step now carries the `if:` of every call around it. The CodeQL guard refuses an analysis when one of them is always false. Both new tests failed before the change and pass after it. --- scripts/__tests__/lib/workflows.mjs | 28 ++++++---- .../workflow-codeql-analyze.test.mjs | 52 ++++++++++++++----- 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/scripts/__tests__/lib/workflows.mjs b/scripts/__tests__/lib/workflows.mjs index c771fe3619..9d9ec7975e 100644 --- a/scripts/__tests__/lib/workflows.mjs +++ b/scripts/__tests__/lib/workflows.mjs @@ -75,15 +75,21 @@ function stepLabel(step, index) { * a test of the traversal, it is a test of the repo. The repo's own resolver is * `resolveRepoComposite`. * - * `visited` breaks the `A uses B uses A` cycle. Scoped per job, matching - * `lint-no-workflow-caching.mjs`: a composite shared by two jobs is flattened - * into each. + * `expanding` holds only the composites on the current call path, which is + * enough to break the `A uses B uses A` cycle. A composite leaves it once its + * expansion ends, so a second call to it is expanded again: GitHub runs it + * twice, and a guard that counts steps must see both runs. + * + * Each entry's `conditions` lists the `if:` of every composite call that + * encloses the step, outermost first. GitHub skips the whole composite when + * one of them is false, whatever the step's own `if:` says. */ export function flattenSteps( steps, resolveComposite, trail = '', - visited = new Set(), + expanding = new Set(), + conditions = [], ) { const flat = [] const unresolved = [] @@ -93,7 +99,7 @@ export function flattenSteps( const uses = usesOf(step) if (uses === null || !LOCAL_USES.test(uses)) { - flat.push({ step, label }) + flat.push({ step, label, conditions }) return } @@ -103,20 +109,22 @@ export function flattenSteps( // GitHub anyway; passing it here would make "rename the action" a way to // leave a check with nothing to read. unresolved.push(`${label}: \`uses: ${uses}\` — no action manifest there`) - flat.push({ step, label }) + flat.push({ step, label, conditions }) return } - if (visited.has(composite.id)) return - visited.add(composite.id) + if (expanding.has(composite.id)) return + expanding.add(composite.id) const nested = flattenSteps( composite.steps, resolveComposite, `${label} -> ${composite.id} step "`, - visited, + expanding, + step?.if === undefined ? conditions : [...conditions, step.if], ) + expanding.delete(composite.id) for (const entry of nested.flat) { - flat.push({ step: entry.step, label: `${entry.label}"` }) + flat.push({ ...entry, label: `${entry.label}"` }) } unresolved.push(...nested.unresolved) }) diff --git a/scripts/__tests__/workflow-codeql-analyze.test.mjs b/scripts/__tests__/workflow-codeql-analyze.test.mjs index 03e07d944a..48432ea193 100644 --- a/scripts/__tests__/workflow-codeql-analyze.test.mjs +++ b/scripts/__tests__/workflow-codeql-analyze.test.mjs @@ -22,10 +22,10 @@ const CODEQL_STEP = /^github\/codeql-action\/(init|analyze)@/ // could be inside it. function codeqlSteps(steps, resolveComposite) { const { flat, unresolved } = flattenSteps(steps, resolveComposite) - const found = flat.flatMap(({ step, label }) => { + const found = flat.flatMap(({ step, label, conditions }) => { const match = typeof step?.uses === 'string' ? step.uses.match(CODEQL_STEP) : null - return match ? [{ action: match[1], step, label }] : [] + return match ? [{ action: match[1], step, label, conditions }] : [] }) return { actions: found.map(({ action }) => action), @@ -36,17 +36,20 @@ function codeqlSteps(steps, resolveComposite) { // `if: false`, `if: 'false'` and `if: ${{ false }}` all skip the step. const ALWAYS_FALSE = /^\s*(?:\$\{\{\s*false\s*\}\}|false)\s*$/ - -// The analyze action uploads to code scanning only when `upload` is -// `always`, which is its default. -function analysisProblem(step) { - const condition = step?.if - if ( - condition === false || - (typeof condition === 'string' && ALWAYS_FALSE.test(condition)) - ) { +const alwaysFalse = (condition) => + condition === false || + (typeof condition === 'string' && ALWAYS_FALSE.test(condition)) + +// `enclosing` is the `if:` of each composite call around the step, from +// `flattenSteps`. The analyze action uploads to code scanning only when +// `upload` is `always`, which is its default. +function analysisProblem(step, enclosing = []) { + if (alwaysFalse(step?.if)) { return '`if:` is always false, so the analysis never runs' } + if (enclosing.some(alwaysFalse)) { + return 'the composite action around it is called with an `if:` that is always false, so the analysis never runs' + } const upload = step?.with?.upload if (upload !== undefined && upload !== 'always') { return `\`upload: ${upload}\` keeps the results out of code scanning. Remove \`upload\`, or set it to \`always\`.` @@ -86,8 +89,8 @@ describe('CodeQL jobs run the analysis', () => { it('runs every CodeQL analysis and uploads its results', () => { const wrong = codeqlJobs.flatMap(({ ref, analyses }) => - analyses.flatMap(({ step, label }) => { - const problem = analysisProblem(step) + analyses.flatMap(({ step, label, conditions }) => { + const problem = analysisProblem(step, conditions) return problem ? [`${ref}: ${label}: ${problem}`] : [] }), ) @@ -122,6 +125,13 @@ describe('the checker this guard depends on', () => { ]) }) + it('counts each call to the same composite action', () => { + expect( + codeqlSteps([callComposite, callComposite, analyze], composite([init])) + .actions, + ).toEqual(['init', 'init', 'analyze']) + }) + it('reports a local action it cannot open', () => { expect(codeqlSteps([callComposite], noComposites).unresolved).toHaveLength( 1, @@ -145,6 +155,22 @@ describe('the checker this guard depends on', () => { }, ) + it('refuses an analysis inside a composite action called with `if: false`', () => { + const [analysis] = codeqlSteps( + [init, { ...callComposite, if: false }], + composite([analyze]), + ).analyses + expect(analysisProblem(analysis.step, analysis.conditions)).toMatch(/`if:`/) + }) + + it('accepts an analysis inside a composite action called with no `if:`', () => { + const [analysis] = codeqlSteps( + [init, callComposite], + composite([analyze]), + ).analyses + expect(analysisProblem(analysis.step, analysis.conditions)).toBeNull() + }) + it('accepts an analysis whose `if:` can be true', () => { expect( analysisProblem({ ...analyze, if: "github.event_name == 'push'" }),