Skip to content

Commit 6f50c51

Browse files
authored
ci: pin pkg-pr-new to the lockfile; document why the pnpm toolchain cannot be integrity-pinned (#361)
Closes the last two unpinned executables in CI. **One is fixed. One cannot be, and this PR says so with evidence instead of pretending otherwise.** ## 1. `npx pkg-pr-new publish` — FIXED `publish-commit.yml:20` ran `npx pkg-pr-new publish`, and `pkg-pr-new` was in **neither `package.json` nor `pnpm-lock.yaml`** — so npx resolved the `latest` dist-tag at run time and executed whatever the registry served, unverified. It was the **only** `npx` target in the repo not already a devDependency (`tsx` and `vitest`, the others, resolve from the installed tree). Not latent: `pkg-pr-new@0.0.87` was published **hours** before this branch was cut. Every PR after that moment would have executed a build nobody here had looked at. **In scope in that job (honestly):** zero `secrets.` references; `GITHUB_TOKEN` at `contents: read` on a **public** repo, so near-zero value; and `pkg-pr-new` is handed no token at all (it authenticates via GitHub App + run metadata). The real prize is the **pkg.pr.new publish channel** — a substituted CLI publishes arbitrary tarballs under this repo's preview URLs, which reviewers and users then `npm i`. Actions-cache poisoning into `drift-live-pr` (7 live provider keys, same PR scope) is a *possibility I did not test* and am **not** claiming. Fix: exact-pinned devDependency + `pnpm exec`, so the preceding `pnpm install --frozen-lockfile` verifies the tarball against a reviewed sha512. ### Watched RED (sandboxed, `env -u`-scrubbed, no real credential present) Committed step body, verbatim, against a local registry serving substituted bytes: ``` $ npx pkg-pr-new publish npm warn exec The following package was not found and will be installed: pkg-pr-new@0.0.86 SUBSTITUTED-PKG-PR-NEW-RAN STEP_EXIT=0 $ cat MARKER.txt SUBSTITUTED pkg-pr-new EXECUTED argv=publish ``` Attacker code ran, wrote to disk, and **the step reported success**. ### Watched GREEN — refused ``` $ pnpm install --frozen-lockfile ERR_PNPM_TARBALL_INTEGRITY Got unexpected checksum for ".../pkg-pr-new-0.0.86.tgz". Wanted "sha512-BwPsw/e1Be…bmCk8A==" Got "sha512-3CDoOk/m0h0…XnS1NA==" INSTALL_EXIT=1 $ ls MARKER.txt -> No such file or directory $ ls node_modules/.bin/ -> No such file or directory $ pnpm exec pkg-pr-new publish ERR_PNPM_RECURSIVE_EXEC_FIRST_FAIL Command "pkg-pr-new" not found ``` The demonstrated **absence** of the payload at an executor — no marker, no binary, the step cannot resolve its own command. ### Positive control — CI still works ``` $ pnpm install --frozen-lockfile -> INSTALL_EXIT=0, + pkg-pr-new 0.0.86 installed dist/index.js sha256 = d1f4c92bbfad12699f7046646082d4075d84161b3275f29d9a7f3fb3cadb9d31 independently downloaded 0.0.86 = d1f4c92bbfad12699f7046646082d4075d84161b3275f29d9a7f3fb3cadb9d31 $ pnpm exec pkg-pr-new publish Continuous Releases are only available in GitHub Actions. ``` Byte-identical to the reviewed tarball, and that refusal is the **real** CLI's own guard — my payload has no such message. "Refuse everything" cannot satisfy this half. ### Positive control on the REAL path — this PR's own `preview` job **passed** ``` Run pnpm install --frozen-lockfile -> + pkg-pr-new 0.0.86 Run pnpm exec pkg-pr-new publish -> ⚡️ Your npm packages are published. { owner: CopilotKit, repo: aimock, ref: 361 } preview pass ``` The lockfile-verified binary published the previews exactly as the unpinned `npx` used to. Nothing about CI's behaviour changed except what governs the bytes. ### RE-BREAK — 5 mutations, all red, workspace md5 identical before/after | # | mutation | result | |---|---|---| | M1 | step reverted to today's `npx pkg-pr-new publish` | RED 1/21 | | M2 | `pkg-pr-new` deleted from devDependencies | RED 2/21 | | M3 | exact pin widened to `^0.0.86` | RED 2/21 | | M4 | genuine corepack hash appended to `packageManager` | RED 1/21 | | M5 | one `pnpm/action-setup@<sha>` unpinned to `@v6` | RED 1/21 | M1 proves the guard reds on the state `main` is in **today** — the suite is not vacuous. ### `package.json` / `pnpm-lock.yaml` `package.json`: **one added line**, `"pkg-pr-new": "0.0.86"`. Exact, not `^`, so Renovate cannot silently widen it. `pnpm-lock.yaml`: three hunks, **purely additive**. - **package `version` is UNCHANGED** — `git diff -U0 package.json | grep -c '"version"'` → `0`; tree `1.38.0`, `HEAD~1` `1.38.0`. - **No other dependency moved** — the lockfile diff contains **zero** deletion lines. `0.0.86` not `0.0.87`: `.87` was published minutes before the pin was written, which reproduces in slow motion the window the pin exists to close. `.86` has been public since 2026-07-30, has **zero runtime deps and no install scripts**, and its integrity is agreed by three independent sources (packument, my own sha512 over the tarball, the lockfile). ## 2. The pnpm toolchain — **NOT PINNABLE**, evidence below `"packageManager": "pnpm@10.28.2"` has no integrity hash. Corepack **does** enforce a `+sha512.<hash>` suffix — a 128-zero hash gives `Error: Mismatch hashes`, exit 1. But `pnpm/action-setup@0977fd99` (the SHA all 16 call sites pin) **strips it**. Its own source: ```ts // packageManager is always exact `pnpm@<version>[+<integrity>]` per spec. // Strip the integrity hash for self-update. ? packageManager.slice('pnpm@'.length).split('+')[0] ``` and the shipped bundle agrees: `startsWith("pnpm@")?i.slice(5).split("+")[0]:void 0`. Proved by running that exact `dist/index.js` twice: ``` A) "pnpm@10.28.2+sha512." + "0"*128 (corepack REFUSES this) -> EXIT=0, installed 10.28.2 B) "pnpm@10.28.2+sha512.41872f03…" (the GENUINE hash) -> EXIT=0, installed 10.28.2 ``` **Identical outcome with a right hash and a wrong one — the suffix is never compared.** So I did **not** add one: it would read as an integrity pin while verifying nothing, which is exactly the defect class this repo has been deleting. **M4 now reds if anyone adds it.** What *is* verified: the action bootstraps pnpm v11.19.0 via `npm ci` against lockfiles committed inside the action (byte-pinned transitively, since the action is SHA-pinned), then `pnpm self-update 10.28.2` fetches from the npm registry verified against **registry-served** metadata. That second hop is TOFU, not a hash reviewed here. The comment already at `test-drift.yml:205-209` states this correctly and needed no change. Real routes, both out of scope (16 call sites, two in files another agent held this round): corepack (works, but is **removed in Node 25**, so it acquires its own expiry) or fetch-and-sha256 the Ollama way. Pinning one workflow and leaving fifteen would be an inconsistent toolchain and a worse trap than a documented gap. ## Guard suite `src/__tests__/ci-toolchain-pinning.test.ts`, 21 tests. Asserts the **general** invariant — every package name any workflow hands to `npx`/`pnpm dlx`/`npm exec` must be manifest-declared — so a future unpinned executable anywhere reds too. `pnpm exec` is deliberately excluded (resolves only from `node_modules`, fails loudly when absent — as GREEN-A showed). Comment lines are stripped before matching; a `workflows.length > 5` assertion prevents a silently empty sweep. ## Gates — real exit codes, read from logs, never through a pipe `pnpm install --frozen-lockfile` 0 · `pnpm build` 0 · **`pnpm test` 0 — 173 files, 5087 tests, all passed** (no known flakes fired) · `pnpm test:drift` 0 (18 passed/8 skipped files, 119/66 tests) · `tsc --noEmit` 0 · `pnpm lint` 0 · `prettier --check` on all 4 changed files 0 · `actionlint` 0 · `bash -n` on all 3 changed `run:` bodies 0 · `zizmor --min-severity medium` 0, "No findings to report" · `pnpm test:exports` 0 (all four resolvers 🟢). ## Scope `publish-release.yml` and `test-pytest.yml` **untouched**. `CHANGELOG.md`, `.claude-plugin/*`, `charts/*`, `packages/aimock-pytest/*` untouched. No credential read/echoed/persisted; every sandbox run under `env -u` scrubbing. No workflow run triggered. Explicit file list on commit, never `git add -A`.
2 parents 9694acb + 78ab5ed commit 6f50c51

4 files changed

Lines changed: 172 additions & 1 deletion

File tree

‎.github/workflows/publish-commit.yml‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,24 @@ jobs:
1717
with: { node-version: 24, cache: pnpm }
1818
- run: pnpm install --frozen-lockfile
1919
- run: pnpm build
20-
- run: npx pkg-pr-new publish
20+
# `pnpm exec`, NOT `npx`. This step used to be `npx pkg-pr-new publish`,
21+
# and `pkg-pr-new` was in neither package.json nor pnpm-lock.yaml — so npx
22+
# resolved the `latest` dist-tag at run time and executed whatever bytes
23+
# the registry served, with no integrity check of any kind. That is a
24+
# live arbitrary-code-execution seam on every PR: a malicious publish or a
25+
# registry/account compromise lands attacker code inside this job. It is
26+
# also the ONLY `npx` target in this repo that is not already a
27+
# devDependency (`tsx` and `vitest`, the others, resolve from the tree).
28+
#
29+
# `pkg-pr-new` is now an EXACT-pinned devDependency, so the
30+
# `pnpm install --frozen-lockfile` above has already verified its tarball
31+
# against the reviewed sha512 in pnpm-lock.yaml — substituted bytes fail
32+
# the install with ERR_PNPM_TARBALL_INTEGRITY and this step never runs.
33+
# `pnpm exec` then runs THOSE verified bytes out of node_modules and
34+
# fetches nothing. Do not reintroduce `npx` here.
35+
#
36+
# WHEN THIS FAILS: pkg-pr-new cut a new release and you want it. Bump the
37+
# exact version in package.json's devDependencies and regenerate the
38+
# lockfile (`pnpm install`) so a reviewed integrity hash lands in the
39+
# diff. Do not widen the specifier to a range to avoid that review.
40+
- run: pnpm exec pkg-pr-new publish

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
"husky": "^9.1.7",
203203
"lint-staged": "^16.3.2",
204204
"openai": "^4.0.0",
205+
"pkg-pr-new": "0.0.86",
205206
"prettier": "^3.6.2",
206207
"publint": "^0.3.12",
207208
"tsdown": "^0.12.5",

‎pnpm-lock.yaml‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { readFileSync, readdirSync } from "node:fs";
2+
import { resolve } from "node:path";
3+
import { describe, expect, it } from "vitest";
4+
5+
const repoRoot = resolve(__dirname, "../..");
6+
const workflowDir = resolve(repoRoot, ".github/workflows");
7+
const read = (p: string) => readFileSync(resolve(repoRoot, p), "utf8");
8+
9+
const publishCommit = read(".github/workflows/publish-commit.yml");
10+
const manifest = JSON.parse(read("package.json")) as {
11+
devDependencies?: Record<string, string>;
12+
dependencies?: Record<string, string>;
13+
};
14+
const lockfile = read("pnpm-lock.yaml");
15+
16+
/**
17+
* Guards the ONE class of unpinned executable that survived the `uses:`-by-SHA
18+
* sweep: a `run:` step that hands a package name to a runner which resolves it
19+
* from the registry AT RUN TIME.
20+
*
21+
* `npx <pkg>` with `<pkg>` absent from the manifest downloads whatever the
22+
* registry currently serves for the `latest` dist-tag and executes it, with no
23+
* integrity check. `npx <pkg>` with `<pkg>` present as a dependency resolves
24+
* from node_modules, whose bytes `pnpm install --frozen-lockfile` has already
25+
* verified against the sha512 committed in pnpm-lock.yaml.
26+
*
27+
* So the invariant is NOT "never say npx" — it is "every package name a
28+
* workflow executes must be governed by the lockfile".
29+
*/
30+
describe("no workflow executes a package the lockfile does not govern", () => {
31+
const declared = new Set([
32+
...Object.keys(manifest.dependencies ?? {}),
33+
...Object.keys(manifest.devDependencies ?? {}),
34+
]);
35+
36+
const workflows = readdirSync(workflowDir).filter(
37+
(f) => f.endsWith(".yml") || f.endsWith(".yaml"),
38+
);
39+
40+
it("finds workflows to check (guards against a silently empty sweep)", () => {
41+
expect(workflows.length).toBeGreaterThan(5);
42+
});
43+
44+
it.each(workflows)("%s runs no unpinned registry executable", (file) => {
45+
const body = readFileSync(resolve(workflowDir, file), "utf8");
46+
const offenders: string[] = [];
47+
48+
for (const raw of body.split("\n")) {
49+
// Ignore comment-only lines: prose about npx is not an invocation.
50+
const line = raw.replace(/^\s*#.*$/, "");
51+
// `npx foo` / `pnpm dlx foo` / `npm exec foo` — the run-time resolvers.
52+
// `pnpm exec` is deliberately NOT here: it only ever resolves from
53+
// node_modules and fails loudly when the binary is absent.
54+
const m = line.match(
55+
/(?:^|[;&|(]\s*|\s)(?:npx|npm exec|pnpm dlx)\s+((?:@[\w.-]+\/)?[\w.-]+)/,
56+
);
57+
if (!m) continue;
58+
const pkg = m[1];
59+
if (pkg.startsWith("-")) continue; // a flag, not a package name
60+
if (!declared.has(pkg)) offenders.push(`${pkg} (in: ${line.trim()})`);
61+
}
62+
63+
expect(
64+
offenders,
65+
`${file} executes package(s) that are in neither package.json nor pnpm-lock.yaml, so the registry decides at run time what code runs: ${offenders.join(", ")}`,
66+
).toEqual([]);
67+
});
68+
});
69+
70+
describe("pkg-pr-new is byte-pinned, not resolved at run time", () => {
71+
it("is an EXACT-version devDependency (no range for the registry to move)", () => {
72+
const spec = manifest.devDependencies?.["pkg-pr-new"];
73+
expect(spec).toBeDefined();
74+
// No ^ ~ > < * x || - : an exact version is the only thing that keeps the
75+
// reviewed integrity hash below authoritative.
76+
expect(spec).toMatch(/^\d+\.\d+\.\d+$/);
77+
});
78+
79+
it("carries a real sha512 integrity hash in the lockfile", () => {
80+
const spec = manifest.devDependencies?.["pkg-pr-new"];
81+
const entry = lockfile.match(
82+
new RegExp(
83+
`\\n pkg-pr-new@${spec?.replace(/\./g, "\\.")}:\\n\\s+resolution: \\{integrity: (sha512-[A-Za-z0-9+/=]+)\\}`,
84+
),
85+
);
86+
expect(
87+
entry,
88+
"pnpm-lock.yaml has no integrity-bearing entry for the pinned pkg-pr-new",
89+
).not.toBeNull();
90+
// sha512, base64: 64 raw bytes -> 88 chars with padding.
91+
expect(entry?.[1].slice("sha512-".length)).toHaveLength(88);
92+
});
93+
94+
it("the publish step runs the installed bytes, and does so AFTER the verifying install", () => {
95+
expect(publishCommit).toContain("pnpm exec pkg-pr-new publish");
96+
expect(publishCommit).not.toMatch(/^\s*-?\s*run:\s*npx pkg-pr-new/m);
97+
98+
const install = publishCommit.indexOf("pnpm install --frozen-lockfile");
99+
const publish = publishCommit.indexOf("pnpm exec pkg-pr-new publish");
100+
expect(install).toBeGreaterThan(-1);
101+
expect(publish).toBeGreaterThan(install);
102+
});
103+
});
104+
105+
/**
106+
* NOT A PIN, AND DELIBERATELY RECORDED AS SUCH.
107+
*
108+
* package.json's `packageManager` field names `pnpm@10.28.2` — a version with
109+
* no integrity hash. The corepack spec allows `pnpm@<version>+sha512.<hash>`,
110+
* and corepack DOES enforce that hash (a wrong one hard-fails with "Mismatch
111+
* hashes"). `pnpm/action-setup`, which is what actually installs pnpm in CI,
112+
* does NOT: its shipped dist/index.js at the SHA this repo pins evaluates
113+
* `packageManager.startsWith("pnpm@") ? i.slice(5).split("+")[0] : void 0`,
114+
* discarding the suffix before anything is fetched. Running that exact action
115+
* bundle with a 128-zero hash installs pnpm 10.28.2 and exits 0, identically
116+
* to running it with the genuine hash.
117+
*
118+
* This test therefore asserts the field stays HONEST — a bare version, with no
119+
* integrity suffix implying a verification that nothing performs. If the pnpm
120+
* toolchain is ever byte-pinned for real (switch to corepack, or fetch the
121+
* tarball and sha256-check it the way test-drift.yml provisions Ollama), this
122+
* test is the thing to update, along with the comment above.
123+
*/
124+
describe("the pnpm toolchain pin claims no more than it delivers", () => {
125+
it("packageManager carries no integrity suffix that pnpm/action-setup would ignore", () => {
126+
const pm = (JSON.parse(read("package.json")) as { packageManager?: string }).packageManager;
127+
expect(pm).toMatch(/^pnpm@\d+\.\d+\.\d+$/);
128+
});
129+
130+
it("every pnpm/action-setup use is itself pinned to a commit SHA", () => {
131+
for (const file of readdirSync(workflowDir).filter((f) => f.endsWith(".yml"))) {
132+
const body = readFileSync(resolve(workflowDir, file), "utf8");
133+
for (const line of body.split("\n")) {
134+
if (!/^\s*-?\s*uses:\s*pnpm\/action-setup@/.test(line)) continue;
135+
expect(line, `${file}: pnpm/action-setup must be pinned by 40-char commit SHA`).toMatch(
136+
/pnpm\/action-setup@[0-9a-f]{40}/,
137+
);
138+
}
139+
}
140+
});
141+
});

0 commit comments

Comments
 (0)