Skip to content

Commit 4daf4a7

Browse files
cliffhallclaude
andcommitted
fix(scripts): verify:action-pins matches contexts and job ids case-insensitively
GitHub resolves expression contexts, property names and job ids without regard to case, so `SECRETS.X`, `NEEDS.detect.outputs` and `needs.DETECT.outputs` slipped past the guard. Match `secrets`, the GITHUB_TOKEN exemption and `needs` case-insensitively, and resolve every job id (in `needs:` and in expressions) to its key in `jobs:` in any case. Closes #4934 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent b0d865d commit 4daf4a7

2 files changed

Lines changed: 118 additions & 13 deletions

File tree

‎scripts/verify-action-pins.mjs‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
// name and version an artifact must match before it is published, so a
4646
// moved tag there could approve a tampered artifact.
4747
//
48+
// GitHub reads expression contexts, their property names and job ids without
49+
// regard to case, so `SECRETS.X`, `NEEDS.detect.outputs` and
50+
// `needs.DETECT.outputs` are matched here as their lowercase spellings are, and
51+
// a job id named anywhere (`needs:` or an expression) resolves to the job in
52+
// `jobs:` it names in any case (#4934).
53+
//
4854
// `GITHUB_TOKEN` alone does not count: every job holds one, so counting it
4955
// would turn this into "pin everything", which #4873 does not ask. That is why
5056
// `version-packages.yml` and `prepare-python-release.yml` are out of scope
@@ -81,11 +87,11 @@ const repoRoot = path.resolve(
8187
"..",
8288
);
8389

84-
// `GITHUB_TOKEN` in either accessor spelling; removed before looking for any
85-
// other `secrets` reference, so every spelling of every other name, a dynamic
86-
// index included, still reads as a secret.
90+
// `GITHUB_TOKEN` in either accessor spelling, in any case; removed before
91+
// looking for any other `secrets` reference, so every spelling of every other
92+
// name, a dynamic index included, still reads as a secret.
8793
const DEFAULT_TOKEN =
88-
/\bsecrets\s*(?:\.\s*GITHUB_TOKEN\b|\[\s*(['"])GITHUB_TOKEN\1\s*\])/g;
94+
/\bsecrets\s*(?:\.\s*GITHUB_TOKEN\b|\[\s*(['"])GITHUB_TOKEN\1\s*\])/gi;
8995
// Where the first expression in a string opens. Everything from there on is
9096
// searched, rather than each `${{ … }}` body: finding where an expression ENDS
9197
// needs a parser, since a quoted string inside one may itself contain `}}`
@@ -124,7 +130,7 @@ function handedSecret(job, inherited) {
124130
const open = text.indexOf(EXPRESSION_OPEN);
125131
return (
126132
open !== -1 &&
127-
/\bsecrets\b/.test(text.slice(open).replace(DEFAULT_TOKEN, ""))
133+
/\bsecrets\b/i.test(text.slice(open).replace(DEFAULT_TOKEN, ""))
128134
);
129135
});
130136
}
@@ -182,6 +188,11 @@ function parseWorkflow(yaml, file = "<workflow>") {
182188
export function credentialedJobs(yaml, file) {
183189
const workflow = parseWorkflow(yaml, file).toJS() ?? {};
184190
const jobs = Object.entries(workflow.jobs ?? {});
191+
// A job id as written anywhere (a `needs:` entry, `needs.<job>`) → its key
192+
// in `jobs:`, or undefined when no job has that id in any case.
193+
const byId = new Map(jobs.map(([name]) => [name.toLowerCase(), name]));
194+
const resolve = (id) => byId.get(id.toLowerCase());
195+
const needed = (job) => needsOf(job).map(resolve).filter(Boolean);
185196
const held = new Set();
186197
for (const [name, job] of jobs) {
187198
const permissions =
@@ -197,12 +208,12 @@ export function credentialedJobs(yaml, file) {
197208
// about the chain.
198209
const upstreamOf = (name) => {
199210
const seen = new Set();
200-
const queue = [...needsOf(workflow.jobs[name])];
211+
const queue = needed(workflow.jobs[name]);
201212
while (queue.length > 0) {
202213
const next = queue.pop();
203-
if (seen.has(next) || !workflow.jobs[next]) continue;
214+
if (seen.has(next)) continue;
204215
seen.add(next);
205-
queue.push(...needsOf(workflow.jobs[next]));
216+
queue.push(...needed(workflow.jobs[next]));
206217
}
207218
return seen;
208219
};
@@ -213,7 +224,7 @@ export function credentialedJobs(yaml, file) {
213224
// reference that names no job (bare `needs`, `needs.*`, a dynamic index)
214225
// counts every job the reader needs.
215226
const NEEDS_USE =
216-
/\bneeds\b\s*(\.\s*([\w-]+|\*)|\[\s*(?:'([^']*)'|"([^"]*)"|[^\]]*)\s*\])?\s*(?:\.\s*(\w+)|\[\s*['"](\w+)['"]\s*\])?/g;
227+
/\bneeds\b\s*(\.\s*([\w-]+|\*)|\[\s*(?:'([^']*)'|"([^"]*)"|[^\]]*)\s*\])?\s*(?:\.\s*(\w+)|\[\s*['"](\w+)['"]\s*\])?/gi;
217228
const outputsReadBy = (job) => {
218229
const read = new Set();
219230
for (const text of stringsIn(job)) {
@@ -222,10 +233,11 @@ export function credentialedJobs(yaml, file) {
222233
for (const m of text.slice(open).matchAll(NEEDS_USE)) {
223234
const named = m[2] ?? m[3] ?? m[4];
224235
const property = m[5] ?? m[6];
225-
if (m[1] !== undefined && property === "result") continue;
236+
if (m[1] !== undefined && property?.toLowerCase() === "result")
237+
continue;
226238
if (named === undefined || named === "*")
227-
for (const n of needsOf(job)) read.add(n);
228-
else read.add(named);
239+
for (const n of needed(job)) read.add(n);
240+
else if (resolve(named)) read.add(resolve(named));
229241
}
230242
}
231243
return read;
@@ -235,7 +247,7 @@ export function credentialedJobs(yaml, file) {
235247
for (const [name, job] of jobs) {
236248
if (!held.has(name)) continue;
237249
for (const source of outputsReadBy(job)) {
238-
if (workflow.jobs[source] && !held.has(source)) {
250+
if (!held.has(source)) {
239251
held.add(source);
240252
grew = true;
241253
}

‎scripts/verify-action-pins.test.mjs‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,99 @@ test("reading only a needed job's result does not make it credentialed", () => {
401401
assert.deepEqual([...credentialedJobs(yaml)], ["publish"]);
402402
});
403403

404+
test("a secret in a mixed-case context counts; GITHUB_TOKEN in any case does not (#4934)", () => {
405+
const yaml = wf(
406+
"jobs:",
407+
" publish:",
408+
" steps:",
409+
" - uses: actions/checkout@v6",
410+
" - env:",
411+
" KEY: ${{ SECRETS.DEPLOY_TOKEN }}",
412+
" run: publish",
413+
" bracketed:",
414+
" steps:",
415+
" - env:",
416+
" KEY: ${{ Secrets['deploy_token'] }}",
417+
" run: publish",
418+
" default:",
419+
" steps:",
420+
" - uses: actions/checkout@v6",
421+
" - env:",
422+
" A: ${{ Secrets['github_token'] }}",
423+
" B: ${{ SECRETS.Github_Token }}",
424+
" run: gh release view",
425+
);
426+
assert.deepEqual([...credentialedJobs(yaml)].sort(), [
427+
"bracketed",
428+
"publish",
429+
]);
430+
assert.deepEqual(unpinnedRefs(yaml), [
431+
{ job: "publish", uses: "actions/checkout@v6" },
432+
]);
433+
});
434+
435+
test("the needs context and job ids are matched without regard to case (#4934)", () => {
436+
const job = (expr) =>
437+
wf(
438+
"jobs:",
439+
" detect:",
440+
" steps:",
441+
" - uses: actions/checkout@v6",
442+
" lint:",
443+
" runs-on: x",
444+
" publish:",
445+
" needs: [detect, lint]",
446+
" permissions:",
447+
" id-token: write",
448+
" steps:",
449+
" - env:",
450+
` EXPECTED: \${{ ${expr} }}`,
451+
" run: publish",
452+
);
453+
for (const expr of [
454+
"NEEDS.detect.outputs.expected",
455+
"needs.DETECT.outputs.expected",
456+
"Needs['Detect'].Outputs.expected",
457+
]) {
458+
const yaml = job(expr);
459+
assert.deepEqual(
460+
[...credentialedJobs(yaml)].sort(),
461+
["detect", "publish"],
462+
expr,
463+
);
464+
assert.deepEqual(
465+
unpinnedRefs(yaml),
466+
[{ job: "detect", uses: "actions/checkout@v6" }],
467+
expr,
468+
);
469+
}
470+
// `.RESULT` is still only the result, and `NEEDS.*` still every needed job.
471+
assert.deepEqual(
472+
[...credentialedJobs(job("NEEDS.DETECT.RESULT == 'success'"))],
473+
["publish"],
474+
);
475+
assert.deepEqual(
476+
[...credentialedJobs(job("join(NEEDS.*.outputs.v)"))].sort(),
477+
["detect", "lint", "publish"],
478+
);
479+
});
480+
481+
test("a needs entry in another case still reaches the artifact producer (#4934)", () => {
482+
const yaml = wf(
483+
"jobs:",
484+
" build:",
485+
" steps:",
486+
" - uses: actions/upload-artifact@v7",
487+
" publish:",
488+
" needs: [BUILD]",
489+
" permissions:",
490+
" id-token: write",
491+
" steps:",
492+
" - uses: actions/download-artifact@v8",
493+
);
494+
assert.deepEqual([...credentialedJobs(yaml)].sort(), ["build", "publish"]);
495+
});
496+
404497
test("an upload is not credentialed when the consumer downloads nothing", () => {
405498
const yaml = wf(
406499
"jobs:",

0 commit comments

Comments
 (0)