diff --git a/CHANGELOG.md b/CHANGELOG.md index dfb4eb6..f7df27e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,8 @@ Versions 0.1.0 to 0.4.2 (2026-10-08 to 2026-10-10) are in [docs/changelog-0.x.md ### Fixed +- Vercel: `-y` inside a short group (`vercel project inspect -dy`) asks on a protected environment, since it can create + and link a project, and `vercel teams change` and the `team` alias are exempt like `teams switch` (#148). - Help no longer exempts a command that runs anyway (#153): a kubectl or helm plugin given `--help` (it gets the flag as an argument), a value flag that takes it (`gh secret set X --body --help`), and Wrangler's `--help false` and `--no-help`. `gh ... --web --help` is checked as well, at the cost of one check. diff --git a/ROADMAP.md b/ROADMAP.md index 41bb3fc..718a470 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -49,6 +49,7 @@ issues it ships. Open an [issue](https://github.com/paureis/cloudpin/issues) to `pull ` and `dev ` are checked against the link of the folder they name. - **Exemption leftovers** ([#153](https://github.com/paureis/cloudpin/issues/153)): plugins given `--help`, value flags that take it and Wrangler's `--no-help` are done; kubectl per-command long values and `vercel dev` services remain. +- **Vercel `-y` in a group and `teams change`** ([#148](https://github.com/paureis/cloudpin/issues/148)): done. - **Windows case gaps** ([#136](https://github.com/paureis/cloudpin/issues/136)): done. cloudpin's own variables and the read-only check read names without regard to case (Wrangler's per-platform login folder came with #103). diff --git a/src/providers/vercel-args.ts b/src/providers/vercel-args.ts index d0bfcdb..127d684 100644 --- a/src/providers/vercel-args.ts +++ b/src/providers/vercel-args.ts @@ -1,6 +1,7 @@ // How the Vercel CLI parses its arguments, read by vercel.ts (#147 moved this here). import { lstatSync } from "node:fs"; import { resolve as resolvePath } from "node:path"; +import { hasAnyFlag } from "../args.js"; import { isDynamic } from "../word-expansion.js"; // Global options (vercel/vercel packages/cli/src/util/arg-common.ts globalCommandOptions; 50.35). @@ -129,6 +130,16 @@ export function splitShortGroups(args: string[]): string[] { return out; } +/** + * Whether the command may get --yes (#148), as `project inspect` reads it (vercel 50.35): + * project's main parses permissively (src/commands/project/index.ts), so a group's -y it + * doesn't know stays a word and a first "--" is dropped; inspect parses those words again with + * --yes/-y (src/commands/project/inspect.ts, yesOption), and arg sets a boolean on `--yes=x` + * too. So every word counts, groups after "--" included; a value never starts with "-". + */ +export const passesYes = (args: string[]) => + hasAnyFlag(splitShortGroups(args.filter((word) => word !== "--")), ["--yes", "-y"]); + const isDirectory = (path: string) => { try { return lstatSync(path).isDirectory(); diff --git a/src/providers/vercel.ts b/src/providers/vercel.ts index 4ee1253..72c3c0f 100644 --- a/src/providers/vercel.ts +++ b/src/providers/vercel.ts @@ -15,7 +15,11 @@ import { HELP_COMMANDS, linkDirs, mainParse, splitShortGroups } from "./vercel-a // A command may touch both, so both must be the pinned team. const EXEMPT_COMMANDS = new Set(["login", "logout", "switch", "whoami", "help", "telemetry"]); -const EXEMPT_TEAMS_SUBCOMMANDS = new Set(["ls", "list", "switch"]); +// vercel 50.35 src/commands/teams/index.ts COMMAND_CONFIG: list = ls|list, switch = switch|change +// (add = create|add and invite change things). teamsCommand's aliases are switch and team; the +// subcommand is the word after the one typed (`switch` itself runs teams switch). +const TEAMS_COMMANDS = new Set(["teams", "team"]); +const EXEMPT_TEAMS_SUBCOMMANDS = new Set(["ls", "list", "switch", "change"]); export interface Team { id: string; @@ -190,7 +194,7 @@ export const vercel: ProviderDef<"vercel"> = { if (!command) return parsed.flags.has("--version") || parsed.flags.has("--help"); // A flag here (`--prod --name whoami`) is a deploy, which is in no list below. if (EXEMPT_COMMANDS.has(command)) return true; - if (command === "teams" && sub !== undefined && EXEMPT_TEAMS_SUBCOMMANDS.has(sub)) return true; + if (TEAMS_COMMANDS.has(command) && sub !== undefined && EXEMPT_TEAMS_SUBCOMMANDS.has(sub)) return true; return parsed.flags.has("--help") && HELP_COMMANDS.has(command); }, diff --git a/src/readonly.ts b/src/readonly.ts index 2a57fce..671e016 100644 --- a/src/readonly.ts +++ b/src/readonly.ts @@ -1,11 +1,12 @@ import { existsSync } from "node:fs"; import { dirname, join } from "node:path"; -import { commandWords as argWords, hasAnyFlag, leadingWords } from "./args.js"; +import { commandWords as argWords, leadingWords } from "./args.js"; import type { Provider, ReadOnlyRules } from "./config.js"; import { home } from "./paths.js"; import { commandWords } from "./providers/kubernetes.js"; import { VALUE_FLAGS as SUPABASE_VALUE_FLAGS } from "./providers/supabase.js"; import { VALUE_FLAGS as WRANGLER_VALUE_FLAGS } from "./providers/cloudflare.js"; +import { passesYes } from "./providers/vercel-args.js"; /* * Which commands only read, so they skip the confirmation on a protected @@ -64,9 +65,11 @@ const BUILT_IN: Record< }, // Vercel CLI: bare `vercel ` deploys, `vercel alias` with no subcommand - // creates an alias, and `--yes` can set up a project (vercel.com/docs/cli). + // creates an alias, and `--yes` can set up a project (vercel.com/docs/cli). Of + // this list only `project inspect` with no name uses --yes (ensureLink), read + // also inside a short group (#148); the others never call ensureLink (50.35). vercel: (words, args) => { - if (hasAnyFlag(args, ["--yes", "-y"])) return false; + if (passesYes(args)) return false; const [first, second] = words; if (["ls", "list", "inspect", "logs"].includes(first ?? "")) return true; if (["env", "domains", "dns", "certs", "project", "projects", "teams", "alias"].includes(first ?? "")) { diff --git a/test/vercel-yes-group.test.ts b/test/vercel-yes-group.test.ts new file mode 100644 index 0000000..523e4e1 --- /dev/null +++ b/test/vercel-yes-group.test.ts @@ -0,0 +1,135 @@ +import { mkdirSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { describe, expect, it } from "vitest"; +import type { FoundConfig } from "../src/config.js"; +import { guard } from "../src/guard.js"; +import { vercel } from "../src/providers/vercel.js"; +import { isReadOnly } from "../src/readonly.js"; +import type { Exec } from "../src/types.js"; +import { tempDir } from "./tmp.js"; + +// #148. `vercel project inspect` with no name links the folder through ensureLink, which with +// --yes can create a project, link the folder and connect Git (vercel 50.35: +// src/commands/project/inspect.ts, util/projects/get-project-by-cwd-or-link.ts). The project +// command parses permissively (src/commands/project/index.ts main), so a group's unknown -y is +// kept as a word, and "--" is dropped; inspect then parses those words with --yes/-y +// (inspectSubcommand options: yesOption). So `-dy`, `-yd` and `-- -dy` all pass --yes. + +const split = (cmd: string) => (cmd === "" ? [] : cmd.split(" ")); +const env = { HOME: tempDir("cloudpin-yes-home-"), USERPROFILE: "" }; +const ro = (cmd: string) => isReadOnly("vercel", split(cmd), env, {}); + +describe("isReadOnly: vercel --yes in a short group (#148)", () => { + it.each([ + "project inspect -dy", + "projects inspect -yd", + "project inspect -dyQ x", + "project inspect -- -y", + "project inspect -- -dy", + "project inspect --yes=false", + "ls -dy", + ])("vercel %s needs confirmation", (cmd) => { + expect(ro(cmd)).toBe(false); + }); + + it.each(["project inspect", "project inspect -dQ dir", "projects inspect -d", "ls -d", "env ls -d"])( + "control: vercel %s stays read-only", + (cmd) => { + expect(ro(cmd)).toBe(true); + }, + ); +}); + +// src/commands/teams/index.ts COMMAND_CONFIG: list = ls|list, switch = switch|change, +// add = create|add, invite. teamsCommand's aliases are "switch" and "team" +// (src/commands/teams/command.ts), and the subcommand is the word after the one typed. +describe("vercel.isExempt: teams subcommand aliases (#148)", () => { + it.each(["teams change", "teams change acme", "team ls", "team list", "team switch", "team change acme", "-d team ls"])( + "allows vercel %s", + (cmd) => { + expect(vercel.isExempt(split(cmd))).toBe(true); + }, + ); + + it.each(["teams add", "teams create", "team add", "team create", "team invite a@example.com", "teams -x ls", "team"])( + "checks vercel %s", + (cmd) => { + expect(vercel.isExempt(split(cmd))).toBe(false); + }, + ); +}); + +const TEAMS = JSON.stringify({ + teams: [ + { id: "team_main", slug: "acme", current: true }, + { id: "team_side", slug: "side", current: false }, + ], + pagination: {}, +}); + +const fakeVercel: Exec = async (_bin, args) => { + if (args[0] === "teams") return { code: 0, stdout: TEAMS, stderr: "" }; + return { code: 1, stdout: "", stderr: "unexpected" }; +}; + +const loggedIn = { VERCEL_TOKEN: "example-env-token" }; +const production = { name: "production", protected: true, source: 'branch "main"' }; + +async function check(cmd: string, team = "team_main", cwd = tempDir("cloudpin-vercel-yes-")) { + const config: FoundConfig = { path: "/repo/.cloudpin.yml", pins: { vercel: { team } }, environment: production }; + return guard( + { bin: "vercel", args: split(cmd), env: loggedIn, cwd, mode: "agent" }, + { providers: [vercel], exec: fakeVercel, findConfig: () => config }, + ); +} + +const CONFIRM = { action: "confirm", provider: "vercel", configPath: "/repo/.cloudpin.yml", environment: production }; + +describe("guard: vercel on a protected environment (#148)", () => { + it.each(["project inspect -dy", "projects inspect -yd", "project inspect -dyQ x"])( + "asks before vercel %s, which may link the folder", + async (cmd) => { + expect(await check(cmd)).toEqual(CONFIRM); + }, + ); + + it("still asks before vercel deploy --prod -dy", async () => { + expect(await check("deploy --prod -dy")).toEqual(CONFIRM); + }); + + it.each(["project inspect", "project inspect -dQ dir"])("control: lets vercel %s through", async (cmd) => { + expect(await check(cmd)).toEqual({ action: "allow", reason: "match" }); + }); + + // #146 fixed these false blocks; a wrong team proves the command is exempt, not matched. + it.each(["-dv", "-dh", "--api https://api.example.com whoami", "teams change", "team switch"])( + "allows vercel %s as exempt", + async (cmd) => { + expect(await check(cmd, "team_side")).toEqual({ action: "allow", reason: "exempt" }); + }, + ); +}); + +/** Links `dir` (created if missing) to a project of team `org`. */ +function link(dir: string, org: string): string { + mkdirSync(join(dir, ".vercel"), { recursive: true }); + writeFileSync(join(dir, ".vercel", "project.json"), JSON.stringify({ orgId: org, projectId: `prj_${org}` })); + return dir; +} + +describe("vercel.resolve: --api takes a value (#146, #148)", () => { + // Read as the command, the value `sub` would be a deploy path and add sub's link. + it("reads the folder's link, not the --api value's", async () => { + const root = link(tempDir("cloudpin-vercel-api-"), "team_main"); + link(join(root, "sub"), "team_side"); + const resolution = await vercel.resolve({ args: split("--api sub ls"), env: loggedIn, cwd: root }, fakeVercel); + expect(resolution).toMatchObject({ kind: "identity", identity: { team: "team_main", projectTeam: "team_main" } }); + }); + + it("control: a deploy of sub reads sub's link", async () => { + const root = link(tempDir("cloudpin-vercel-api-"), "team_main"); + link(join(root, "sub"), "team_side"); + const resolution = await vercel.resolve({ args: split("deploy sub"), env: loggedIn, cwd: root }, fakeVercel); + expect(resolution).toMatchObject({ kind: "identity", identity: { projectTeam: "team_side" } }); + }); +});