diff --git a/pm-stack/aggregator/scripts/collect-fleet-feed.ts b/pm-stack/aggregator/scripts/collect-fleet-feed.ts index 21b3fac88b..d29310adc0 100644 --- a/pm-stack/aggregator/scripts/collect-fleet-feed.ts +++ b/pm-stack/aggregator/scripts/collect-fleet-feed.ts @@ -121,8 +121,34 @@ const PKG = join(dirname(fileURLToPath(import.meta.url)), ".."); const HOME = homedir(); const HIDDEN = join(HOME, "workspace/goals/keep-the-four-repos-healthy/hidden_files"); -const outArg = process.argv.indexOf("--out"); -const outPath = outArg >= 0 ? process.argv[outArg + 1] : (process.env.FLEET_FEED_OUT ?? join(PKG, "fleet-feed.json")); +// Strict operator-argument contract (#3352, #3509): the only accepted +// forms are no arguments or one documented separated `--out `. +// Every other token — a trailing/empty/flag-like --out value, an +// --out= join, a duplicate --out, or any unknown or positional +// token — is a usage error (exit 2) raised here, before any collection, +// network, subprocess, lock, or file-write side effect. +function usageError(message: string): never { + console.error(`collect-fleet-feed: ${message}; usage: bun run scripts/collect-fleet-feed.ts [--out ]`); + process.exit(2); +} + +function resolveOutPath(): string { + const fallback = process.env.FLEET_FEED_OUT ?? join(PKG, "fleet-feed.json"); + const args = process.argv.slice(2); + if (args.length === 0) return fallback; + if (args.filter((a) => a === "--out").length > 1) + usageError(`duplicate --out (got ${JSON.stringify(args)}); the documented form is a single --out `); + if (args[0] === "--out") { + const value: string | undefined = args[1]; + if (value === undefined || value === "" || value.startsWith("--")) + usageError(`--out requires a path value (got ${value === undefined ? "no value" : JSON.stringify(value)})`); + if (args.length > 2) usageError(`unexpected argument ${JSON.stringify(args[2])}`); + return value; + } + usageError(`unknown argument ${JSON.stringify(args[0])}`); +} + +const outPath = resolveOutPath(); const quotaViewPath = process.env.QUOTA_VIEW_PATH ?? join( diff --git a/pm-stack/aggregator/test/collect-fleet-feed-args-3509.test.ts b/pm-stack/aggregator/test/collect-fleet-feed-args-3509.test.ts new file mode 100644 index 0000000000..7c5189419c --- /dev/null +++ b/pm-stack/aggregator/test/collect-fleet-feed-args-3509.test.ts @@ -0,0 +1,111 @@ +// Pinning test for #3509: collect-fleet-feed must strictly validate its +// operator arguments BEFORE any collection begins. A trailing --out used +// to crash at dirname(undefined) with a raw TypeError (exit 1), +// --out --weird wrote a file named --weird (exit 0), and a typo'd --otu +// silently wrote the default feed path (exit 0). Per the #3352 +// unknown-means-stop contract, the only accepted forms are no arguments +// or one documented separated --out ; everything else is a usage +// error (exit 2) naming the offending token, with no collection or +// file-write side effect. A valid explicit --out and the no-argument +// default keep working. +import { describe, expect, test } from "bun:test"; +import { mkdtemp, readdir, rm, stat } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +const script = join(import.meta.dir, "../scripts/collect-fleet-feed.ts"); + +async function runScript( + home: string, + cwd: string, + args: string[], +): Promise<{ code: number; stderr: string; stdout: string }> { + const proc = Bun.spawn([process.execPath, script, ...args], { + cwd, + env: { + PATH: process.env.PATH ?? "/run/current-system/sw/bin:/usr/bin:/bin", + HOME: home, + TMPDIR: home, + FLEET_FEED_OUT: join(home, "feed.json"), + }, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()]); + return { code: await proc.exited, stdout, stderr }; +} + +async function exists(path: string): Promise { + try { + await stat(path); + return true; + } catch { + return false; + } +} + +describe("collect-fleet-feed argument validation (#3509)", () => { + test("invalid arguments exit 2 naming the token, with no collection or file writes", async () => { + const badArgSets: { args: string[]; names: string }[] = [ + { args: ["--out"], names: "--out" }, + { args: ["--out", ""], names: "--out" }, + { args: ["--out", "--weird"], names: "--out" }, + { args: ["--otu", "requested.json"], names: "--otu" }, + { args: ["requested.json"], names: "requested.json" }, + { args: ["--out=requested.json"], names: "--out=requested.json" }, + { args: ["--out", "a.json", "--out", "b.json"], names: "--out" }, + { args: ["--out", "a.json", "extra"], names: "extra" }, + ]; + for (const { args, names } of badArgSets) { + const home = await mkdtemp(join(tmpdir(), "fleet-feed-args-3509-home-")); + const cwd = await mkdtemp(join(tmpdir(), "fleet-feed-args-3509-cwd-")); + try { + const { code, stderr } = await runScript(home, cwd, args); + expect(code, `args ${JSON.stringify(args)}`).toBe(2); + expect(stderr, `args ${JSON.stringify(args)}`).toContain("collect-fleet-feed:"); + expect(stderr, `args ${JSON.stringify(args)}`).toContain(names); + expect(stderr, `args ${JSON.stringify(args)}`).not.toContain("TypeError"); + // No collection output: the default feed path is untouched and no + // stray file (e.g. a relative --weird) lands in the working dir. + expect(await exists(join(home, "feed.json")), `args ${JSON.stringify(args)}`).toBe(false); + expect(await exists(join(home, "aa-last-good.json")), `args ${JSON.stringify(args)}`).toBe(false); + expect(await readdir(cwd), `args ${JSON.stringify(args)}`).toEqual([]); + } finally { + await rm(home, { recursive: true, force: true }); + await rm(cwd, { recursive: true, force: true }); + } + } + }, 60000); + + test("valid explicit --out writes the requested path", async () => { + const home = await mkdtemp(join(tmpdir(), "fleet-feed-args-3509-ok-")); + const cwd = await mkdtemp(join(tmpdir(), "fleet-feed-args-3509-okcwd-")); + try { + const target = join(home, "requested.json"); + const { code, stdout } = await runScript(home, cwd, ["--out", target]); + expect(code).toBe(0); + expect(stdout).toContain(target); + expect(await exists(target)).toBe(true); + // The untouched default proves the explicit path was honored. + expect(await exists(join(home, "feed.json"))).toBe(false); + } finally { + await rm(home, { recursive: true, force: true }); + await rm(cwd, { recursive: true, force: true }); + } + }, 60000); + + test("no arguments keep the FLEET_FEED_OUT default", async () => { + const home = await mkdtemp(join(tmpdir(), "fleet-feed-args-3509-default-")); + const cwd = await mkdtemp(join(tmpdir(), "fleet-feed-args-3509-defaultcwd-")); + try { + const { code, stdout } = await runScript(home, cwd, []); + expect(code).toBe(0); + const fallback = join(home, "feed.json"); + expect(stdout).toContain(fallback); + expect(await exists(fallback)).toBe(true); + } finally { + await rm(home, { recursive: true, force: true }); + await rm(cwd, { recursive: true, force: true }); + } + }, 60000); +});