diff --git a/scripts/release/publication.test.ts b/scripts/release/publication.test.ts index a4ed78e..0d5cd9c 100644 --- a/scripts/release/publication.test.ts +++ b/scripts/release/publication.test.ts @@ -1,11 +1,16 @@ import { expect, test } from "bun:test"; +import { execFileSync } from "node:child_process"; import { createHash } from "node:crypto"; +import { lstatSync, mkdtempSync, readFileSync, realpathSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import { type PackageArtifact, parseReleaseConfig, readReleaseConfig } from "./config"; import { type PublicationIO, assertTrustedPublishingVersions, compareReleaseVersions, npmEnvironment, + prepareNpmConfig, publishRelease, trustedRuntimePaths, versionAtLeast, @@ -139,7 +144,59 @@ test("version probes have no ambient authority; only trusted publication gets OI } }); -function fixture(version = "0.1.0", line: "v1" | "v2" = "v2") { +test("real npm version probe accepts the same isolated config flags as publication", async () => { + const cwd = mkdtempSync(join(tmpdir(), "agentbus-npm-config-test-")); + try { + prepareNpmConfig(cwd); + const { input, io, state } = fixture("0.1.0", "v2", cwd); + await publishRelease(input, io); + const flags = state.publishCalls[0]?.filter((arg) => + /^--(?:userconfig|globalconfig)=/u.test(arg), + ); + expect(flags).toHaveLength(2); + const nodePath = Bun.which("node"); + const npmPath = Bun.which("npm"); + if (!nodePath || !npmPath || !flags) throw new Error("Installed Node/npm required."); + const node = realpathSync(nodePath); + const npm = realpathSync(npmPath); + const env = npmEnvironment( + { NPM_CONFIG_USERCONFIG: "/untrusted/user", NPM_CONFIG_GLOBALCONFIG: "/untrusted/global" }, + node, + cwd, + false, + ); + const user = flags?.find((flag) => flag.startsWith("--userconfig="))?.slice(13); + const global = flags?.find((flag) => flag.startsWith("--globalconfig="))?.slice(15); + if (!user || !global) throw new Error("Both npm config flags required."); + for (const [args, configEnv] of [ + [[npm, "--version"], env], + [ + [npm, "--version", ...flags], + { ...env, NPM_CONFIG_USERCONFIG: undefined, NPM_CONFIG_GLOBALCONFIG: undefined }, + ], + [[npm, "--version", ...flags], env], + ] as const) + expect( + execFileSync(node, args, { + cwd, + env: configEnv, + encoding: "utf8", + stdio: ["ignore", "pipe", "pipe"], + }).trim(), + ).toMatch(/^[0-9]+\.[0-9]+\.[0-9]+$/u); + expect(env.NPM_CONFIG_USERCONFIG).toBe(user); + expect(env.NPM_CONFIG_GLOBALCONFIG).toBe(global); + expect(user).not.toBe(global); + expect(global.startsWith(`${cwd}/`)).toBe(true); + expect(lstatSync(global).isFile()).toBe(true); + expect(readFileSync(global, "utf8")).toBe(""); + expect(lstatSync(global).mode & 0o777).toBe(0o600); + } finally { + rmSync(cwd, { recursive: true, force: true }); + } +}); + +function fixture(version = "0.1.0", line: "v1" | "v2" = "v2", npmConfigDir = "/isolated") { const config = parseReleaseConfig({ ...readReleaseConfig(), releaseLine: line, @@ -202,6 +259,7 @@ function fixture(version = "0.1.0", line: "v1" | "v2" = "v2") { }; const response = (value: unknown, status = 200) => Response.json(value, { status }); const io: PublicationIO = { + npmConfigDir, github: async (url, init) => { if (init?.method === "POST") { state.writes.push(url); @@ -298,7 +356,7 @@ test("publication uses exact tgz, OIDC-compatible CLI flags and existing release "--provenance", "--registry=https://registry.npmjs.org", "--userconfig=/dev/null", - "--globalconfig=/dev/null", + "--globalconfig=/isolated/.npmrc-global", ]); expect([...state.assets.keys()]).toEqual([ "agentbus.tgz", diff --git a/scripts/release/publication.ts b/scripts/release/publication.ts index 8c56a9b..eca8623 100644 --- a/scripts/release/publication.ts +++ b/scripts/release/publication.ts @@ -1,6 +1,6 @@ import { execFileSync } from "node:child_process"; import { createHash } from "node:crypto"; -import { lstatSync, mkdtempSync, readFileSync, realpathSync } from "node:fs"; +import { lstatSync, mkdtempSync, readFileSync, realpathSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, isAbsolute, join, relative, resolve } from "node:path"; import { @@ -40,6 +40,7 @@ export interface PublicationIO { github(url: string, init?: RequestInit): Promise; registry(url: string): Promise; publish(args: readonly string[]): Promise; + npmConfigDir: string; } const API = `https://api.github.com/repos/${RELEASE_REPOSITORY}`; @@ -110,6 +111,19 @@ export function trustedRuntimePaths(toolCache: string, node: string) { return { node, npmCli: join(dirname(node), "../lib/node_modules/npm/bin/npm-cli.js") }; } +function npmConfigPaths(cwd: string) { + return { user: "/dev/null", global: join(cwd, ".npmrc-global") }; +} + +export function npmConfigArgs(cwd: string): [string, string] { + const config = npmConfigPaths(cwd); + return [`--userconfig=${config.user}`, `--globalconfig=${config.global}`]; +} + +export function prepareNpmConfig(cwd: string) { + writeFileSync(npmConfigPaths(cwd).global, "", { flag: "wx", mode: 0o600 }); +} + /** Version probes get no credentials; only the real publish receives short-lived OIDC authority. */ export function npmEnvironment( source: NodeJS.ProcessEnv, @@ -117,10 +131,13 @@ export function npmEnvironment( cwd: string, publish: boolean, ): NodeJS.ProcessEnv { + const config = npmConfigPaths(cwd); const env: NodeJS.ProcessEnv = { PATH: `${dirname(node)}:/usr/bin:/bin`, HOME: cwd, npm_config_cache: join(cwd, ".npm-cache"), + NPM_CONFIG_USERCONFIG: config.user, + NPM_CONFIG_GLOBALCONFIG: config.global, CI: "true", }; for (const key of [ @@ -343,8 +360,7 @@ export async function publishRelease(input: PublicationInput, io: PublicationIO) release.npmTag, "--provenance", "--registry=https://registry.npmjs.org", - "--userconfig=/dev/null", - "--globalconfig=/dev/null", + ...npmConfigArgs(io.npmConfigDir), ]); const published = await json( await io.registry( @@ -415,6 +431,7 @@ if (import.meta.main) { throw new Error("Trusted Node/npm files must be regular, canonical tool-cache files."); } const cwd = mkdtempSync(join(process.env.RUNNER_TEMP ?? tmpdir(), "agentbus-publish-")); + prepareNpmConfig(cwd); const probeOptions = { cwd, env: npmEnvironment(process.env, tools.node, cwd, false), @@ -425,7 +442,7 @@ if (import.meta.main) { execFileSync(tools.node, ["--version"], probeOptions).trim(), execFileSync( tools.node, - [tools.npmCli, "--version", "--userconfig=/dev/null", "--globalconfig=/dev/null"], + [tools.npmCli, "--version", ...npmConfigArgs(cwd)], probeOptions, ).trim(), ); @@ -442,6 +459,7 @@ if (import.meta.main) { auditExit, }, { + npmConfigDir: cwd, github: (url, init) => { if (!url.startsWith(`${API}/`) && url !== API && !url.startsWith(`${UPLOADS}/`)) throw new Error("Unexpected GitHub endpoint.");