Skip to content

Commit 7e95cf9

Browse files
cliffhallclaude
andcommitted
fix: the pin guard refuses local uses in a credentialed job and reads secrets past a quoted }}
- A local action or reusable workflow in a credentialed job is a finding: what it runs is not read by the guard, so it could carry a tag-pinned action past it. - A secret reference is found by searching from the first expression opener to the end of the string, not inside each expression body, since a quoted }} inside an expression would cut the body short. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 35daeba commit 7e95cf9

2 files changed

Lines changed: 58 additions & 16 deletions

File tree

‎scripts/verify-action-pins.mjs‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@
5555
// seen without the network. Resolve both from the same lookup when bumping
5656
// (the `release` skill has the command).
5757
//
58-
// ⚠️ Local `./…` actions and reusable workflows are skipped, not traced: the
59-
// remote `uses:` INSIDE one runs under its caller's credentials but is not
60-
// associated with the caller here, and composite-action files are not read at
61-
// all. None exists in this repo; adding the first one to a credentialed job
62-
// means extending this guard to follow it, or it is a silent bypass.
58+
// ⚠️ Local `./…` actions and reusable workflows are REFUSED in a credentialed
59+
// job, not skipped. The remote `uses:` INSIDE one runs under its caller's
60+
// credentials, and this guard reads neither composite-action files nor called
61+
// workflows, so a local definition could carry a tag-pinned action past it.
62+
// None exists in this repo. Adding the first one to a credentialed job means
63+
// teaching this guard to follow it; until then it is a finding (the Inspector's
64+
// guard skips them with a warning, which leaves the bypass open).
6365

6466
import { readdirSync, readFileSync } from "node:fs";
6567
import path from "node:path";
@@ -77,7 +79,14 @@ const repoRoot = path.resolve(
7779
// index included, still reads as a secret.
7880
const DEFAULT_TOKEN =
7981
/\bsecrets\s*(?:\.\s*GITHUB_TOKEN\b|\[\s*(['"])GITHUB_TOKEN\1\s*\])/g;
80-
const EXPRESSION = /\$\{\{([\s\S]*?)\}\}/g;
82+
// Where the first expression in a string opens. Everything from there on is
83+
// searched, rather than each `${{ … }}` body: finding where an expression ENDS
84+
// needs a parser, since a quoted string inside one may itself contain `}}`
85+
// (`${{ format('}}{0}', secrets.X) }}`), and a body cut short at that point
86+
// hides the secret after it. Searching the tail cannot be fooled that way. It
87+
// can over-count a string that mentions `secrets` in prose after an
88+
// expression, which errs toward requiring a pin.
89+
const EXPRESSION_OPEN = "${{";
8190

8291
/** Does this `permissions:` value let the job mint a token or push a package? */
8392
function mints(permissions) {
@@ -104,11 +113,13 @@ function handedSecret(job, inherited) {
104113
// `inherit` hands over every secret with no expression to scan. A mapping
105114
// is scanned below with everything else.
106115
if (job.secrets === "inherit") return true;
107-
return stringsIn([job, inherited]).some((text) =>
108-
[...text.matchAll(EXPRESSION)].some(([, body]) =>
109-
/\bsecrets\b/.test(body.replace(DEFAULT_TOKEN, "")),
110-
),
111-
);
116+
return stringsIn([job, inherited]).some((text) => {
117+
const open = text.indexOf(EXPRESSION_OPEN);
118+
return (
119+
open !== -1 &&
120+
/\bsecrets\b/.test(text.slice(open).replace(DEFAULT_TOKEN, ""))
121+
);
122+
});
112123
}
113124

114125
const needsOf = (job) =>
@@ -206,8 +217,8 @@ export function credentialedJobs(yaml, file) {
206217
/**
207218
* Every `uses:` in a credentialed job that is not a 40-hex SHA followed by a
208219
* `# vX.Y.Z` comment: each step's, and the job's own when it calls a reusable
209-
* workflow, whose ref is just as mutable. Local (`./…`) actions and workflows
210-
* are repository code, not a ref.
220+
* workflow, whose ref is just as mutable. A local (`./…`) action or workflow
221+
* is a finding too: what it runs is not read here (see the header).
211222
*
212223
* A YAML alias anywhere in a credentialed job is itself a finding. Resolving it
213224
* here would accept a pin that a line-oriented reader (a reviewer, or a sweep
@@ -246,9 +257,8 @@ export function unpinnedRefs(yaml, file) {
246257
for (const node of nodes) {
247258
if (!isScalar(node) || typeof node.value !== "string") continue;
248259
const uses = node.value;
249-
if (uses.startsWith("./")) continue;
250-
// A value with no `@ref` at all, or a container image, is not pinned
251-
// either: `parseUses` returns null for both and `isPinned` rejects it.
260+
// A local definition, a value with no `@ref` at all, and a container
261+
// image are not pins either: `parseUses` returns null for all three.
252262
if (parseUses(uses) === null || !isPinned(uses, node.comment))
253263
problems.push({ job: name, uses });
254264
}
@@ -274,6 +284,8 @@ export function main(root = repoRoot) {
274284
"\n\nA job holding `id-token`/`packages: write`, a non-default secret, or building an" +
275285
"\nartifact such a job consumes runs only immutable refs. Pin each as" +
276286
"\n uses: owner/repo@<40-hex sha> # vX.Y.Z" +
287+
"\nA local `./…` action or workflow is refused there as well: this guard does not" +
288+
"\nread what it runs. Inline its steps, or extend the guard to follow it." +
277289
"\nresolving the SHA and the exact release from the same tag lookup. The comment" +
278290
"\nnames the release the SHA came from, for a reviewer and for the dependency sweep.",
279291
);

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,11 @@ test("a credentialed reusable-workflow call must pin its own ref", () => {
331331
" id-token: write",
332332
" uses: ./.github/workflows/publish.yml",
333333
);
334+
// The local call is refused as well: the workflow it names is not read, so
335+
// what runs under this job's credential is unknown.
334336
assert.deepEqual(unpinnedRefs(yaml), [
335337
{ job: "release", uses: "org/repo/.github/workflows/publish.yml@main" },
338+
{ job: "local", uses: "./.github/workflows/publish.yml" },
336339
]);
337340
});
338341

@@ -372,12 +375,39 @@ test("a version comment must name an exact release", () => {
372375
);
373376
// A major-only comment is not what the SHA was resolved from, and the sweep
374377
// would compare it at major precision only — the moving-tag behavior again.
378+
// The local composite action is refused too: its steps are not read.
375379
assert.deepEqual(unpinnedRefs(yaml), [
376380
{ job: "publish", uses: `actions/setup-node@${SHA}` },
377381
{ job: "publish", uses: `actions/cache@${SHA}` },
382+
{ job: "publish", uses: "./.github/actions/local" },
378383
]);
379384
});
380385

386+
test("a local action in a job that holds no credential is left alone", () => {
387+
const yaml = wf(
388+
"jobs:",
389+
" lint:",
390+
" steps:",
391+
" - uses: ./.github/actions/local",
392+
);
393+
assert.deepEqual(unpinnedRefs(yaml), []);
394+
});
395+
396+
test("a quoted }} inside an expression does not hide the secret after it", () => {
397+
const yaml = wf(
398+
"jobs:",
399+
" tricky:",
400+
" steps:",
401+
" - env:",
402+
" KEY: ${{ format('}}{0}', secrets.DEPLOY_TOKEN) }}",
403+
" default-only:",
404+
" steps:",
405+
" - env:",
406+
" KEY: ${{ format('}}{0}', secrets.GITHUB_TOKEN) }}",
407+
);
408+
assert.deepEqual([...credentialedJobs(yaml)], ["tricky"]);
409+
});
410+
381411
function withWorkflows(files, fn) {
382412
const root = mkdtempSync(path.join(tmpdir(), "action-pins-"));
383413
try {

0 commit comments

Comments
 (0)