diff --git a/.github/agent/action-allowlist.json b/.github/agent/action-allowlist.json index 278f73e9..bc205384 100644 --- a/.github/agent/action-allowlist.json +++ b/.github/agent/action-allowlist.json @@ -10,5 +10,6 @@ "aquasecurity/trivy-action", "github/codeql-action", "raven-actions/actionlint" - ] + ], + "approved_unused": [] } diff --git a/changelog.d/1206.changed.md b/changelog.d/1206.changed.md new file mode 100644 index 00000000..78ab3db0 --- /dev/null +++ b/changelog.d/1206.changed.md @@ -0,0 +1 @@ +The action allowlist now carries `approved_unused`, a reviewed pre-approval for an action that is on the list while no workflow uses it yet, so the documented order — list entry first, workflow second — can be executed instead of failing in either direction. An entry names the issue that approved it and a minor release version by which it expires; the suite fails at that version and again when the exempted action is first used, and both messages name the entry, its issue and its expiry with the two legal moves: delete it, or renew it with a comment on the issue. An empty `approved_unused` stays green on any version, and a tree is bounded as the minor checkout it is (`0.5.1` as `0.5.0`), so the daily patch releases neither red an empty list nor kill a valid entry. diff --git a/docs/security.md b/docs/security.md index 026910ff..773e984d 100644 --- a/docs/security.md +++ b/docs/security.md @@ -1049,6 +1049,25 @@ different action. `readAllowlist` refuses to load a malformed file rather than returning an empty list, and a name in the list that no workflow uses is a test failure too — drift in either direction is how a list stops describing the tree. +One state is not drift. `approved_unused` in the same file holds entries of +exactly `name`, `issue` and `expires_at`: an action this project has pre-approved +by a reviewed decision while no workflow uses it yet. That is the only legal way +to land a list entry before its use, and it is bounded rather than open-ended — +`expires_at` is a minor release version (`x.y.0`), a tree whose minor is at or +past that version fails its suite, and the message names the entry, its issue and +its expiry plus both legal moves: delete it, or renew it with a comment on the +issue saying why the pre-approval still stands. The tree is read as the checkout +it is: any released `x.y.z` counts, and it is bounded as the minor it belongs to, +so `0.5.1` is bounded as `0.5.0` — a valid entry does not die on the patch +releases the daily cadence produces (`docs/decisions/0008-release-cadence.md`). +The bound only applies once there is an entry to bound: an empty +`approved_unused` stays green on any version, so the check cannot red a release +over a list it did not add to. The entry also expires on first use: a workflow +that uses an exempted action while the entry is still listed fails the suite, and +the change that adds the use deletes the entry. Until the check moves into the +base checker, this rule lives in the tree's own suite, so removing it is a +reviewed change but not yet one the base branch can compel. + CI enforces the list in the `action-pins` required job. The checker is read from the base branch, so the list it reads is the one already reviewed there; a pull request therefore cannot grant itself an action in the same commit that uses it. @@ -1080,7 +1099,9 @@ Procedure for adding an action: 2. Add one line to `.github/agent/action-allowlist.json` in its own reviewed change, with the justification in the pull request body. The suite's both-directions test must still pass, so a list entry that no workflow uses - cannot ride along. + cannot ride along — unless it is pre-approved in `approved_unused`, naming this + issue and a release version by which it expires. Pre-approval is what makes the + entry and its first use two changes by construction. 3. Land it, then open the workflow change that uses the action. The gate reads the list from the base branch, so reversing the order reds the newer pull request until the list is on `main`. diff --git a/scripts/action-pins.mjs b/scripts/action-pins.mjs index 9b1af8d1..d68a9b0b 100644 --- a/scripts/action-pins.mjs +++ b/scripts/action-pins.mjs @@ -2,7 +2,7 @@ // Supply-chain gate 1, wired into CI: scan the real workflow tree and fail the // job on any action reference that is not a 40-hex pin with its tag comment, or // that names an action outside the reviewed allowlist. -import { ALLOWLIST_FILE, defaultAllowlistPath, readAllowlist, scanActorGates, scanWorkflows } from './lib/action-pins.mjs' +import { ALLOWLIST_FILE, defaultAllowlistPath, readAllowlistFile, scanActorGates, scanWorkflows } from './lib/action-pins.mjs' // #640: the checker runs from the base branch, but the workflows it reads are // the pull request's own — they are the data under test, so the directory is an @@ -17,9 +17,15 @@ const directory = argument('--workflows', '.github/workflows') // #650: the list is read relative to this script, which in CI is the base // checkout, so a pull request cannot add its own action to the list. const allowlistPath = argument('--allowlist', defaultAllowlistPath()) +// #1206: the file carries two halves. The list admits an action; `approved_unused` +// names actions pre-approved for a follow-up change, and the report says how many +// there are so an exemption never reads as an ordinary entry. let allowlist +let approvedUnused try { - allowlist = readAllowlist(allowlistPath) + const file = readAllowlistFile(allowlistPath) + allowlist = file.allowed_actions + approvedUnused = file.approved_unused } catch (error) { console.error(`action-pins: ${error.message}`) process.exit(1) @@ -75,4 +81,5 @@ if (gates.findings.length > 0) { process.exit(1) } -console.log(`action-pins: ok (${report.checked} pinned references across ${report.files.length} workflow files, all on the ${allowlist.length}-action allowlist; ${gates.checked} actions: write job(s) gated on the actor)`) +const preApproved = approvedUnused.length === 0 ? '' : `, ${approvedUnused.length} action(s) pre-approved and not used yet` +console.log(`action-pins: ok (${report.checked} pinned references across ${report.files.length} workflow files, all on the ${allowlist.length}-action allowlist${preApproved}; ${gates.checked} actions: write job(s) gated on the actor)`) diff --git a/scripts/lib/action-pins.d.mts b/scripts/lib/action-pins.d.mts index 294d2259..e704e492 100644 --- a/scripts/lib/action-pins.d.mts +++ b/scripts/lib/action-pins.d.mts @@ -31,6 +31,24 @@ export declare function defaultAllowlistPath(): string /** Throws on a malformed file: an unreadable list must not allow everything. */ export declare function readAllowlist(path?: string): string[] +/** One reviewed pre-approval: an action on the list that no workflow uses yet. */ +export interface ApprovedUnusedEntry { + name: string + issue: number + expires_at: string +} + +export interface AllowlistFile { + allowed_actions: string[] + approved_unused: ApprovedUnusedEntry[] +} + +/** Both halves of the file; a missing `approved_unused` reads as an empty one. */ +export declare function readAllowlistFile(path?: string): AllowlistFile + +/** One message per exemption the tree's own version puts out of bounds. */ +export declare function approvedUnusedExpiryProblems(entries: ApprovedUnusedEntry[], version: string, path?: string): string[] + /** `github/codeql-action/init@sha` -> `github/codeql-action`. */ export declare function repositoryOf(value: string): string diff --git a/scripts/lib/action-pins.mjs b/scripts/lib/action-pins.mjs index 50d7c948..f197852e 100644 --- a/scripts/lib/action-pins.mjs +++ b/scripts/lib/action-pins.mjs @@ -28,6 +28,18 @@ const TAG_COMMENT = /(^|\s)#\s*\S+/ export const ALLOWLIST_FILE = '.github/agent/action-allowlist.json' const ACTION_REPOSITORY = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/ +// #1206, ADR 0027: an action may be pre-approved in one reviewed change and used +// in the next. The exemption is data — `name`, `issue`, `expires_at` — so granting +// it is a one-line reviewed diff, and a release version bounds it: the release +// pull request that reaches `expires_at` is where a person has to decide, so a +// lapse lands in front of a human instead of on a date nobody scheduled. +const RELEASE_VERSION = /^(\d+)\.(\d+)\.0$/ +// A checkout is any released tree, including the patch releases the daily cadence +// produces (ADR 0008 bumps the patch on every day but the week's first). Its bound +// is the minor it belongs to: `0.5.1` is bounded as `0.5.0`. +const TREE_VERSION = /^(\d+)\.(\d+)(?:\.\d+)?$/ +const APPROVED_UNUSED_FIELDS = 'expires_at,issue,name' + // Resolved from this file, never from the working directory: the checker runs // from the base checkout, so the list it reads is the one already reviewed on // the base branch. A pull request therefore cannot widen the list for itself in @@ -39,6 +51,14 @@ export function defaultAllowlistPath() { // Throws on a malformed file instead of returning an empty list: a gate that // silently allows everything when its data is unreadable is worse than no gate. export function readAllowlist(path = defaultAllowlistPath()) { + return readAllowlistFile(path).allowed_actions +} + +// The same file, both halves. `allowed_actions` is what admits an action; +// `approved_unused` names actions this project has pre-approved for a follow-up +// change that no workflow uses yet. The exemption is validated like the list it +// sits beside — an unvalidated exemption would be the one soft spot in the gate. +export function readAllowlistFile(path = defaultAllowlistPath()) { let parsed try { parsed = JSON.parse(readFileSync(path, 'utf8')) @@ -58,7 +78,107 @@ export function readAllowlist(path = defaultAllowlistPath()) { if (duplicates.length > 0) { throw new Error(`${path}: ${duplicates.join(', ')} appears more than once`) } - return list + return { allowed_actions: list, approved_unused: readApprovedUnused(parsed?.approved_unused, list, path) } +} + +// A missing key is an empty one: every tree that predates this rule keeps working, +// and the first exemption is the change that writes the key. +function readApprovedUnused(value, allowedActions, path) { + if (value === undefined) return [] + if (!Array.isArray(value)) { + throw new Error(`${path}: approved_unused must be an array of {name, issue, expires_at} entries`) + } + const allowed = new Set(allowedActions.map((entry) => entry.toLowerCase())) + const approved = [] + const seen = new Set() + value.forEach((entry, index) => { + const problem = approvedUnusedProblem(entry, allowed, path, index) + if (problem !== null) throw new Error(problem) + const key = entry.name.toLowerCase() + if (seen.has(key)) throw new Error(`${path}: ${entry.name} appears more than once in approved_unused`) + seen.add(key) + approved.push({ name: entry.name, issue: entry.issue, expires_at: entry.expires_at }) + }) + return approved +} + +// An entry is exactly three fields, because a field the reader ignores is a field +// a reviewer may believe was checked. `name` has to be on the list it sits beside: +// an exemption for an action the gate still refuses pre-approves nothing. +function approvedUnusedProblem(entry, allowed, path, index) { + const where = `${path}: approved_unused[${index}]` + if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) { + return `${where} must be an object with exactly name, issue and expires_at` + } + const fields = APPROVED_UNUSED_FIELDS.split(',') + const extra = Object.keys(entry).filter((key) => !fields.includes(key)) + if (extra.length > 0) { + return `${where}: ${extra.join(', ')} is not a field of an entry (exactly name, issue and expires_at)` + } + const missing = fields.filter((key) => !(key in entry)) + if (missing.length > 0) return `${where} is missing ${missing.join(', ')}` + if (typeof entry.name !== 'string' || !ACTION_REPOSITORY.test(entry.name)) { + return `${where}: ${JSON.stringify(entry.name)} is not an exact owner/repo name (wildcards are not allowed)` + } + if (!allowed.has(entry.name.toLowerCase())) { + return `${where}: ${entry.name} is not in allowed_actions, so the exemption would pre-approve an action the gate still refuses` + } + if (!Number.isInteger(entry.issue) || entry.issue <= 0) { + return `${where}.issue must be the number of the issue that pre-approved it` + } + if (typeof entry.expires_at !== 'string' || !RELEASE_VERSION.test(entry.expires_at)) { + return `${where}.expires_at must be a minor release version (x.y.0); there is no "no expiry" value` + } + return null +} + +function releaseVersion(value) { + const match = RELEASE_VERSION.exec(String(value)) + if (match === null) return null + return Number(match[1]) * 1000 + Number(match[2]) +} + +// The minor a checkout belongs to, whatever its patch. A tree that is not a +// version at all cannot bound anything, and the caller says so instead of guessing. +function treeMinor(value) { + const match = TREE_VERSION.exec(String(value)) + if (match === null) return null + return Number(match[1]) * 1000 + Number(match[2]) +} + +// The bound, as failures rather than prose. Equality fails deliberately: the +// release pull request that bumps the tree to `expires_at` is exactly the moment a +// person has to delete or renew the entry, so that pull request cannot go green +// while the exemption is still there. An expiry more than two minors ahead would +// outlive the review that justified it, and one minor would lapse before a single +// release could carry the follow-up. +export function approvedUnusedExpiryProblems(entries, version, path = ALLOWLIST_FILE) { + // With no entries there is nothing to bound, so a tree that is not a release has + // no problem to report. Failing here would red every patch release (ADR 0008 bumps + // the patch on all but the week's first day) over an empty list the project ships, + // which is how a check gets deleted instead of read. + if (entries.length === 0) return [] + const current = treeMinor(version) + if (current === null) { + return [`${path}: ${JSON.stringify(version)} is not a release version (x.y.z), so no exemption can be bounded against it`] + } + const problems = [] + for (const entry of entries) { + const expires = releaseVersion(entry.expires_at) + if (expires === null) { + problems.push(`${path}: ${entry.name} (#${entry.issue}) has expires_at ${JSON.stringify(entry.expires_at)}, which is not a minor release version (x.y.0)`) + continue + } + const moves = `Delete the entry, or renew it with a new expires_at and a comment on #${entry.issue} saying why the pre-approval still stands` + if (current >= expires) { + problems.push(`${path}: ${entry.name} was pre-approved on #${entry.issue} and expires at ${entry.expires_at}; this tree is ${version}. ${moves}.`) + continue + } + if (expires - current > 2) { + problems.push(`${path}: ${entry.name} is pre-approved until ${entry.expires_at} (#${entry.issue}), more than two minors beyond this tree's ${version}; the bound may not outlive the review that justified it. ${moves}.`) + } + } + return problems } // `github/codeql-action/init@` belongs to `github/codeql-action`: the diff --git a/test/actionPins.test.ts b/test/actionPins.test.ts index 8937d2a7..ce3931e2 100644 --- a/test/actionPins.test.ts +++ b/test/actionPins.test.ts @@ -3,7 +3,7 @@ import assert from 'node:assert/strict' import { mkdtempSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' -import { ALLOWLIST_FILE, checkActorGates, checkWorkflow, readAllowlist, repositoryOf, scanActorGates, scanWorkflows } from '../scripts/lib/action-pins.mjs' +import { ALLOWLIST_FILE, approvedUnusedExpiryProblems, checkActorGates, checkWorkflow, readAllowlist, readAllowlistFile, repositoryOf, scanActorGates, scanWorkflows } from '../scripts/lib/action-pins.mjs' // A real pin, the one this repository uses for actions/checkout. const PIN = '11d5960a326750d5838078e36cf38b85af677262' @@ -400,8 +400,10 @@ test('the workflow tree in this repository passes the allowlist, not just the pi // the tree that nobody added to the list (the gate would red the night after the // merge) and an entry nobody uses (the next reader cannot tell whether it is a // leftover or a mistake). Both are cheap to assert here and invisible otherwise. -test('the allowlist and the workflow tree name exactly the same repositories', () => { - const directory = '.github/workflows' +// #1206 adds one legal state to the second direction: a name pre-approved in +// `approved_unused`, which expires on first use — so the moment a workflow uses +// it, the entry has to be gone, and that is asserted rather than remembered. +function usedRepositories(directory: string): Set { const used = new Set() for (const name of readdirSync(directory)) { if (!/\.ya?ml$/i.test(name)) continue @@ -413,9 +415,111 @@ test('the allowlist and the workflow tree name exactly the same repositories', ( used.add(repositoryOf(value)) } } - const listed = new Set(readAllowlist()) - assert.deepEqual([...used].filter((repo) => !listed.has(repo)), [], 'a workflow uses an action the list does not name') - assert.deepEqual([...listed].filter((repo) => !used.has(repo)), [], 'the list names an action no workflow uses') + return used +} + +test('the allowlist and the workflow tree name exactly the same repositories', () => { + const { allowed_actions: listed, approved_unused: approved } = readAllowlistFile() + const used = usedRepositories('.github/workflows') + const exempt = new Set(approved.map((entry: { name: string }) => entry.name.toLowerCase())) + assert.deepEqual( + [...used].filter((repo) => !listed.some((name: string) => name.toLowerCase() === repo.toLowerCase())), + [], + 'a workflow uses an action the list does not name', + ) + assert.deepEqual( + [...listed].filter((name: string) => !used.has(name.toLowerCase()) && !exempt.has(name.toLowerCase())), + [], + 'the list names an action no workflow uses', + ) + assert.deepEqual( + approved.filter((entry: { name: string }) => used.has(entry.name.toLowerCase())).map((entry: { name: string; issue: number }) => `${entry.name} (#${entry.issue})`), + [], + 'an exemption outlived its purpose: a workflow uses the action, so the entry must be deleted in the change that adds the use', + ) +}) + +test('an exemption is validated like the list it sits beside', (t) => { + const dir = mkdtempSync(join(tmpdir(), 'action-exempt-')) + t.after(() => rmSync(dir, { recursive: true, force: true })) + const write = (name: string, body: string): string => { + const path = join(dir, name) + writeFileSync(path, body) + return path + } + let counter = 0 + const file = (approved: unknown): string => + write(`approved-${counter++}.json`, JSON.stringify({ allowed_actions: ['actions/checkout', 'actions/cache'], approved_unused: approved })) + const entry = { name: 'actions/cache', issue: 1206, expires_at: '0.6.0' } + + assert.deepEqual(readAllowlistFile(file([entry])).approved_unused, [entry]) + // A missing key is an empty one, so every tree that predates the rule keeps working. + assert.deepEqual(readAllowlistFile(write('none.json', JSON.stringify({ allowed_actions: ['actions/checkout'] }))).approved_unused, []) + // An exemption for an action the gate refuses pre-approves nothing. + assert.throws(() => readAllowlistFile(file([{ ...entry, name: 'evil/tool' }])), /not in allowed_actions/) + // An entry names a repository exactly, never a pattern. + assert.throws(() => readAllowlistFile(file([{ ...entry, name: 'actions/*' }])), /wildcards are not allowed/) + // Exactly three fields: a field the reader ignores is one a reviewer may believe. + assert.throws(() => readAllowlistFile(file([{ ...entry, approved_by: 'me' }])), /not a field of an entry/) + assert.throws(() => readAllowlistFile(file([{ name: 'actions/cache', expires_at: '0.6.0' }])), /missing issue/) + assert.throws(() => readAllowlistFile(file([{ ...entry, issue: 0 }])), /issue must be the number/) + assert.throws(() => readAllowlistFile(file([{ ...entry, expires_at: '0.6.1' }])), /minor release version/) + assert.throws(() => readAllowlistFile(file([{ ...entry, expires_at: 'latest' }])), /no "no expiry" value/) + assert.throws(() => readAllowlistFile(file([entry, entry])), /appears more than once in approved_unused/) + assert.throws(() => readAllowlistFile(write('shape.json', JSON.stringify({ allowed_actions: ['actions/checkout'], approved_unused: 'actions/cache' }))), /must be an array/) +}) + +test('an exemption lapses at its release version and cannot outrun two minors', () => { + const entries = (expires_at: string) => [{ name: 'actions/cache', issue: 1206, expires_at }] + + // One minor ahead is the designed case, and two is the far edge. + assert.deepEqual(approvedUnusedExpiryProblems(entries('0.6.0'), '0.5.0'), []) + assert.deepEqual(approvedUnusedExpiryProblems(entries('0.7.0'), '0.5.0'), []) + // A third outlives the review that justified it. + assert.match(approvedUnusedExpiryProblems(entries('0.8.0'), '0.5.0').join('\n'), /more than two minors beyond this tree's 0\.5\.0/) + + // Equality fails deliberately: the release pull request that reaches `expires_at` + // is exactly where a person has to delete or renew the entry. + const lapsed = approvedUnusedExpiryProblems(entries('0.6.0'), '0.6.0') + assert.equal(lapsed.length, 1) + assert.match(String(lapsed[0]), /actions\/cache/) + assert.match(String(lapsed[0]), /#1206/) + assert.match(String(lapsed[0]), /0\.6\.0/) + assert.match(String(lapsed[0]), /Delete the entry, or renew it with a new expires_at and a comment on #1206/) + + // An entry's own `expires_at` is a release version, because a person writes it; + // there is no "no expiry" value and no patch expiry. + assert.match(approvedUnusedExpiryProblems(entries('0.6.1'), '0.5.0').join('\n'), /not a minor release version/) + + // The tree version is the day the check runs, not the week the tree released: + // ADR 0008 bumps the patch on every day but the week's first, so `0.5.1` is a + // normal checkout and its bound is the minor it belongs to (`0.5.0`). An entry + // that is in bounds therefore has to stay green on a patch release, and lapse + // when the minor reaches its expiry. + assert.deepEqual(approvedUnusedExpiryProblems(entries('0.6.0'), '0.5.1'), []) + assert.deepEqual(approvedUnusedExpiryProblems(entries('0.7.0'), '0.5.2'), []) + assert.match(approvedUnusedExpiryProblems(entries('0.6.0'), '0.6.1').join('\n'), /expires at 0\.6\.0/) + assert.match(approvedUnusedExpiryProblems(entries('0.8.0'), '0.5.1').join('\n'), /more than two minors beyond this tree's 0\.5\.1/) + // Only a real version can bound anything. + assert.match(approvedUnusedExpiryProblems(entries('0.6.0'), 'latest').join('\n'), /not a release version/) + + // The shipped file is whatever the tree carries. What has to hold is that its + // entries are bounded by this tree's version, never that the list happens to be + // empty: #1206's acceptance case is a shipped one-name exemption, so pinning `[]` + // would red the very state this card exists to allow. + const shipped = readAllowlistFile().approved_unused + const treeVersion = JSON.parse(readFileSync('package.json', 'utf8')).version as string + assert.deepEqual( + approvedUnusedExpiryProblems(shipped, treeVersion), + [], + "the shipped approved_unused entries must be bounded by this tree's version", + ) + // Nothing to bound at any version, including a patch release of the same minor. + assert.deepEqual(approvedUnusedExpiryProblems([], '0.5.1'), []) + assert.deepEqual(approvedUnusedExpiryProblems([], '0.5.0'), []) + // `expires_at` and the tree version stay separate rules: the entry is a promise + // and the tree is a clock, so a malformed promise is refused on any tree. + assert.match(approvedUnusedExpiryProblems(entries('0.6.1'), '0.5.1').join('\n'), /not a minor release version/) }) test('a malformed allowlist refuses to load instead of allowing everything', (t) => {