From 419a5c2854d281656af0a55d651d85f1583266a6 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 07:11:16 -0600 Subject: [PATCH] fix(pkarr-worker): fail closed on unrecognized deploy arguments (#3264) deploy.mjs read only the exact spellings --dry-run and --deploy-version and ignored everything else, so a typo'd --dry-rnu or the --deploy-version= form fell through to a real deploy of HEAD. Parsing now lives in deploy-args.mjs and rejects any unknown argument, a missing or empty version id, and --dry-run with a value, before any bws/cf call. The equals form is accepted and matches the space form. --dry-run with --deploy-version stays accepted so #3252 can make it preview a rollback. --- pkgs/pkarr-worker/deploy-args.d.mts | 6 ++ pkgs/pkarr-worker/deploy-args.mjs | 62 ++++++++++++++++++++ pkgs/pkarr-worker/deploy.mjs | 17 ++++-- pkgs/pkarr-worker/test/deploy-args.test.ts | 67 ++++++++++++++++++++++ 4 files changed, 146 insertions(+), 6 deletions(-) create mode 100644 pkgs/pkarr-worker/deploy-args.d.mts create mode 100644 pkgs/pkarr-worker/deploy-args.mjs create mode 100644 pkgs/pkarr-worker/test/deploy-args.test.ts diff --git a/pkgs/pkarr-worker/deploy-args.d.mts b/pkgs/pkarr-worker/deploy-args.d.mts new file mode 100644 index 0000000000..05a1c5aed0 --- /dev/null +++ b/pkgs/pkarr-worker/deploy-args.d.mts @@ -0,0 +1,6 @@ +export declare const USAGE: string; + +export declare function parseArgs(argv: string[]): { + dryRun: boolean; + deployVersion: string | null; +}; diff --git a/pkgs/pkarr-worker/deploy-args.mjs b/pkgs/pkarr-worker/deploy-args.mjs new file mode 100644 index 0000000000..3b4d9457a6 --- /dev/null +++ b/pkgs/pkarr-worker/deploy-args.mjs @@ -0,0 +1,62 @@ +// Argument parsing for deploy.mjs, kept in its own module so the rules can +// be tested without importing the deploy script (which runs cf/bws at import +// time). The rule is fail-closed: a deploy tool must never ignore a word it +// does not understand, because the operator may have asked for a preview or a +// rollback and would instead ship HEAD (#3264). + +export const USAGE = [ + "usage: deploy.mjs [--dry-run]", + " deploy.mjs --deploy-version ", +].join("\n"); + +/** + * Parse deploy arguments. + * + * Accepted: + * --dry-run + * --deploy-version + * --deploy-version= + * + * Anything else throws, so the caller can abort before any bws/cf call. + * + * @param {string[]} argv arguments after the script name + * @returns {{ dryRun: boolean, deployVersion: string | null }} + */ +export function parseArgs(argv) { + let dryRun = false; + let deployVersion = null; + + for (let i = 0; i < argv.length; i += 1) { + const arg = argv[i]; + + if (arg === "--dry-run") { + dryRun = true; + continue; + } + + if (arg === "--deploy-version") { + const value = argv[i + 1]; + if (value === undefined || value === "" || value.startsWith("-")) { + throw new Error(`--deploy-version needs a version id\n${USAGE}`); + } + deployVersion = value; + i += 1; + continue; + } + + if (arg.startsWith("--deploy-version=")) { + const value = arg.slice("--deploy-version=".length); + if (value === "") { + throw new Error(`--deploy-version needs a version id\n${USAGE}`); + } + deployVersion = value; + continue; + } + + throw new Error(`unrecognized argument ${JSON.stringify(arg)}\n${USAGE}`); + } + + // --dry-run with --deploy-version is deliberately accepted: #3252 makes it + // preview a rollback instead of creating the deployment. + return { dryRun, deployVersion }; +} diff --git a/pkgs/pkarr-worker/deploy.mjs b/pkgs/pkarr-worker/deploy.mjs index b0cf0f065a..ff84a785a9 100644 --- a/pkgs/pkarr-worker/deploy.mjs +++ b/pkgs/pkarr-worker/deploy.mjs @@ -30,16 +30,21 @@ import { existsSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; +import { parseArgs } from "./deploy-args.mjs"; + const here = dirname(fileURLToPath(import.meta.url)); const WORKER = "pkarr"; const WORKER_URL = "https://pkarr.developing-today.workers.dev"; -const args = process.argv.slice(2); -const dryRun = args.includes("--dry-run"); -const deployVersionAt = args.indexOf("--deploy-version"); -const deployVersion = deployVersionAt >= 0 ? args[deployVersionAt + 1] : null; -if (deployVersionAt >= 0 && !deployVersion) { - throw new Error("--deploy-version needs a version id"); +// Fail closed on anything we do not understand, before any bws/cf call: a +// typo'd --dry-run or --dry-version must not turn into a live deploy (#3264). +let dryRun; +let deployVersion; +try { + ({ dryRun, deployVersion } = parseArgs(process.argv.slice(2))); +} catch (error) { + console.error(`deploy.mjs: ${error.message}`); + process.exit(2); } const accountId = requireEnv("CLOUDFLARE_ACCOUNT_ID"); diff --git a/pkgs/pkarr-worker/test/deploy-args.test.ts b/pkgs/pkarr-worker/test/deploy-args.test.ts new file mode 100644 index 0000000000..5d077a04e2 --- /dev/null +++ b/pkgs/pkarr-worker/test/deploy-args.test.ts @@ -0,0 +1,67 @@ +import { describe, expect, it } from "vitest"; + +import { parseArgs, USAGE } from "../deploy-args.mjs"; + +describe("deploy argument parsing", () => { + it("accepts no arguments", () => { + expect(parseArgs([])).toEqual({ dryRun: false, deployVersion: null }); + }); + + it("accepts --dry-run", () => { + expect(parseArgs(["--dry-run"])).toEqual({ dryRun: true, deployVersion: null }); + }); + + it("accepts --deploy-version with a separate value", () => { + expect(parseArgs(["--deploy-version", "v-rollback-123"])).toEqual({ + dryRun: false, + deployVersion: "v-rollback-123", + }); + }); + + it("accepts the --deploy-version= equals form", () => { + expect(parseArgs(["--deploy-version=v-rollback-123"])).toEqual({ + dryRun: false, + deployVersion: "v-rollback-123", + }); + }); + + it("keeps both flags when combined, in either order", () => { + const expected = { dryRun: true, deployVersion: "v-rollback-123" }; + expect(parseArgs(["--dry-run", "--deploy-version", "v-rollback-123"])).toEqual(expected); + expect(parseArgs(["--deploy-version=v-rollback-123", "--dry-run"])).toEqual(expected); + }); + + it("refuses a misspelled safety flag instead of deploying", () => { + expect(() => parseArgs(["--dry-rnu"])).toThrow(/unrecognized argument "--dry-rnu"/); + }); + + it("refuses an unknown flag", () => { + expect(() => parseArgs(["--prod"])).toThrow(/unrecognized argument "--prod"/); + }); + + it("refuses --dry-run with a value", () => { + expect(() => parseArgs(["--dry-run=true"])).toThrow(/unrecognized argument "--dry-run=true"/); + }); + + it("refuses a bare positional argument", () => { + expect(() => parseArgs(["deploy"])).toThrow(/unrecognized argument "deploy"/); + }); + + it("refuses --deploy-version without a value", () => { + expect(() => parseArgs(["--deploy-version"])).toThrow(/needs a version id/); + }); + + it("refuses --deploy-version followed by another flag", () => { + expect(() => parseArgs(["--deploy-version", "--dry-run"])).toThrow(/needs a version id/); + }); + + it("refuses an empty equals-form version id", () => { + expect(() => parseArgs(["--deploy-version="])).toThrow(/needs a version id/); + }); + + it("prints usage with every refusal", () => { + for (const argv of [["--dry-rnu"], ["--deploy-version"], ["--deploy-version="]]) { + expect(() => parseArgs(argv)).toThrow(USAGE); + } + }); +});