Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions scripts/release/publication.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand Down
26 changes: 22 additions & 4 deletions scripts/release/publication.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -40,6 +40,7 @@ export interface PublicationIO {
github(url: string, init?: RequestInit): Promise<Response>;
registry(url: string): Promise<Response>;
publish(args: readonly string[]): Promise<void>;
npmConfigDir: string;
}

const API = `https://api.github.com/repos/${RELEASE_REPOSITORY}`;
Expand Down Expand Up @@ -110,17 +111,33 @@ 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,
node: string,
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 [
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand All @@ -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(),
);
Expand All @@ -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.");
Expand Down