Skip to content
Myr

Refuse a value typed where an `env set` flag goes before effect/cli can print it back, and test the claims 414's tests left open

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Review of 414 against its issue (Spec) and AGENTS.md (Standards); the fixes, each test-first through program(argv): - effect/cli parses before any command runs, so a value typed as an argument that starts with a dash leaked: `env set KEY -Xs3cr3t…` printed one "Unrecognized flag" line per letter, `--s3cr3t…` printed it whole, and a dash-led value holding a `v` or `h` printed the version or help and exited 0 storing nothing. env-set.ts now exports `refuseStrangeFlags(argv)`: after `env set` and before any `--`, a word that reads as a flag `env set` does not take is a usage error (exit 2) in one line that repeats none of it. `program` runs it before parsing; that is one import and one line in apps/cli/src/main.ts, outside the territory (414's Comments #12 tell the integrator to keep it first in 404's `program`). - The screen's spellings are one list (env set's flags, effect/cli's built-ins, `--no-` forms); a test holds it to the flags `env set --help` names, so a global flag added later fails that test until it is listed (checked by dropping `--wizard`: red by assertion). - Tests added to env-set.test.ts: four dash-led values refused with exit 2, nothing on stdout, one stderr line and no call (red by assertion before the screen: exit 0 or 1); its own flags and effect/cli's still pass (`--project=`, `--log-level`, `-h`, `--no-json`); only one trailing newline stripped and a CRLF counted as one; an empty value (nothing piped, or an empty line) is a usage error; --dry-run's envelope on stdout. - Tests added to why.test.ts: from `src/`, `week.ts:3` reports `src/week.ts`; without --project it asks about the project the repo's platform remote names; with neither it exits 2 asking for --project and calls nothing. - Left as judgement calls in the Comments (#14): why.ts' git runner repeats repo-context.ts' private one and its Assisted-by text repeats @repo/git/trailers' private agentOf (both owners outside the territory; hoist with #2's helpers); a secret typed in the <key> slot reads as a key name. - bun run fix && bun run verify green: cli 88 tests.

authored by oscabriel · committed by oscabriel · parent 77bb849 · Browse files · 4 files+248 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+248 added−3 removed

  • tests:+185 added−0 removed
  • other:+63 added−3 removed

4 files+248 added−3 removed

modified:apps/cli/src/local/env-set.ts+57 added-1 removed
28 unmodified lines
2930const encodeSet = Schema.encodeEffect(SetEnv.successSchema);3132/** How a value is piped in, for the person who tried another way. */33const pipeHint = (key: EnvKey) =>33const pipeHint = (key: string) =>34 `printf '%s\\n' "$VALUE" | ${Brand.cli} ${command} ${key} --yes`;3536/**37 * The value from a person at a terminal, through a prompt that shows nothing
49 unmodified lines
8788 return yield* projectOf(named, context);89 });9091/**92 * Every spelling of a flag `env set` takes: its own, and effect/cli's93 * built-in ones. A boolean's `--no-` form counts as its own spelling. A test94 * holds this list to the flags `env set --help` names.95 */96const spellings: ReadonlySet<string> = new Set([97 "--acknowledge-short",98 "--completions",99 "--dry-run",100 "--help",101 "--json",102 "--log-level",103 "--project",104 "--version",105 "--wizard",106 "--yes",107 "-h",108 "-v",109]);110111/** A word that reads as a flag, but as none `env set` takes. */112const strangeFlag = (word: string) => {113 const [flag = word] = word.split("=", 1);114115 return (116 word.startsWith("-") &&117 word !== "-" &&118 !spellings.has(flag.replace(/^--no-/u, "--"))119 );120};121122/**123 * Refuses `<cli> env set` with a word that reads as a flag it does not take,124 * before effect/cli parses the words. The parser would print such a word125 * back (`-abc` a letter per error), or act on a `-h` or `-v` inside it, and126 * a value typed there by mistake must never show, so the line repeats none127 * of it. `program` runs this first. Words after `--` reach the command,128 * which refuses them as `not-the-value`.129 */130export const refuseStrangeFlags = (131 argv: readonly string[]132): Effect.Effect<void, never, Output> => {133 const end = argv.indexOf("--");134 const words = end === -1 ? argv : argv.slice(0, end);135136 const at = words.findIndex(137 (word, index) => word === "env" && words[index + 1] === "set"138 );139140 return at !== -1 && words.slice(at + 2).some(strangeFlag)141 ? usage(142 `${Brand.cli} ${command} was given a flag it does not take, not repeated here in case it holds the value; the value is read from stdin, never from arguments: ${pipeHint("<KEY>")}`143 )144 : Effect.void;145};146147export const envSet = Command.make(148 "set",149 {150 acknowledgeShort: Flag.Boolean("acknowledge-short").pipe(
91 unmodified lines
modified:apps/cli/src/main.ts+6 added-2 removed
modified:apps/cli/test/local/env-set.test.ts+118 added
29 unmodified lines
30import { RpcTest } from "effect/rpc";31import type { Rpc } from "effect/rpc";32import { TestConsole } from "effect/testing";3334import { refuseStrangeFlags } from "../../src/local/env-set.ts";35import { program } from "../../src/main.ts";36import { Output, OutputPlain } from "../../src/output.ts";37import type { Mode } from "../../src/output.ts";38
244 unmodified lines
283 neverShown(ran);284 }).pipe(Effect.provide(NodeServices.layer))285 );286287 it.live.each([288 {289 typed: `-${secret}`,290 why: "a value that starts with a dash, holding a v that would read as --version",291 },292 {293 typed: "-Zq8_w0rd5ecret",294 why: "a value effect/cli would read as short flags and print back letter by letter",295 },296 { typed: `--${secret}`, why: "a value that reads as a long flag" },297 { typed: `--token=${secret}`, why: "a value given to a flag it lacks" },298 ])(299 "refuses $why as a usage error, in one line that repeats none of it",300 ({ typed }) =>301 Effect.gen(function* () {302 const ran = yield* run([key, typed, ...always, "--yes"]);303304 expect(ran.code).toBe(2);305 expect(ran.calls).toStrictEqual([]);306 expect(ran.out).toBe("");307 expect(ran.err.split("\n")).toStrictEqual([308 expect.stringContaining("stdin"),309 ]);310 expect(ran.err).not.toContain(typed.slice(1, 4));311 neverShown(ran);312 }).pipe(Effect.provide(NodeServices.layer))313 );314315 it.live.each([316 { args: [key, "--project=datekit", "--yes"], shows: "set by alice" },317 {318 args: [key, ...always, "--yes", "--log-level", "debug"],319 shows: "set by alice",320 },321 { args: [key, "-h"], shows: "--acknowledge-short" },322 { args: [key, "--no-json", ...always, "--yes"], shows: "set by alice" },323 ])("still takes its own flags and effect/cli's: $args", ({ args, shows }) =>324 Effect.gen(function* () {325 const ran = yield* run(args);326327 expect(ran.code).toBe(0);328 expect(ran.out).toContain(shows);329 }).pipe(Effect.provide(NodeServices.layer))330 );331332 it.live("screens out no flag that `env set --help` names", () =>333 Effect.gen(function* () {334 const help = yield* run(["--help"]);335336 const named = help.out.split("\n").flatMap((line) => {337 const flags = /^\s+(?<flags>-[^\s,]+(?:, -[^\s,]+)*)/u.exec(line)338 ?.groups?.flags;339340 return flags === undefined ? [] : flags.split(", ");341 });342343 expect(named).toStrictEqual(344 expect.arrayContaining(["--yes", "--project", "--help", "-h"])345 );346347 const screened = yield* Effect.filter(named, (flag) =>348 Effect.map(349 Effect.exit(refuseStrangeFlags(["env", "set", key, flag])),350 Exit.isFailure351 )352 );353354 expect(screened).toStrictEqual([]);355 }).pipe(356 Effect.provide([NodeServices.layer, OutputPlain, TestConsole.layer])357 )358 );359360 it.live.each([361 { kept: `${secret}\n`, sent: `${secret}\n\n`, why: "only one newline" },362 { kept: secret, sent: `${secret}\r\n`, why: "a CRLF as one newline" },363 { kept: secret, sent: secret, why: "nothing from a value with no newline" },364 ])("strips $why from the piped value", ({ kept, sent }) =>365 Effect.gen(function* () {366 const ran = yield* run([key, ...always, "--yes"], {367 stdin: Stream.make(new TextEncoder().encode(sent)),368 });369370 expect(ran.code).toBe(0);371 expect(onlyCall(ran).value).toBe(kept);372 }).pipe(Effect.provide(NodeServices.layer))373 );374375 it.live.each([376 { sent: "", why: "nothing piped" },377 { sent: "\n", why: "an empty line piped" },378 ])(379 "with $why, exits 2 telling how to pipe the value, and calls nothing",380 ({ sent }) =>381 Effect.gen(function* () {382 const ran = yield* run([key, ...always, "--yes"], {383 stdin: Stream.make(new TextEncoder().encode(sent)),384 });385386 expect(ran.code).toBe(2);387 expect(ran.calls).toStrictEqual([]);388 expect(ran.err).toContain(`| ${Brand.cli} env set ${key}`);389 }).pipe(Effect.provide(NodeServices.layer))390 );391392 it.live("without --yes, exits 2 naming it and calls nothing", () =>393 Effect.gen(function* () {394 const ran = yield* run([key, ...always]);395
25 unmodified lines
421 },422 method: "plan",423 },424 ]);425 expect(yield* decodeEnvelope(ran.out)).toStrictEqual({426 command: "env set",427 data: {428 dryRun: {429 key: "SENTRY_AUTH_TOKEN",430 project: "datekit",431 value: "<redacted>",432 },433 },434 ok: true,435 v: 1,436 });437 neverShown(ran);438 }).pipe(Effect.provide(NodeServices.layer))439 );440
70 unmodified lines
modified:apps/cli/test/local/why.test.ts+67 added
374 unmodified lines
375 expect(yield* Ref.get(calls)).toStrictEqual([]);376 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))377 );378379 it.live(380 "from a folder inside the repo, takes the path from there and names it from the repo's root",381 () =>382 Effect.gen(function* () {383 const { dir, second } = yield* aRepo;384 const calls = yield* noCalls;385386 const ran = yield* run(387 `${dir}/src`,388 ["week.ts:3", "--project", "datekit", "--json"],389 platformKnowing(Option.some(second), calls)390 );391392 expect(ran.code).toBe(0);393 expect(yield* decodeEnvelope(ran.out)).toMatchObject({394 data: { line: 3, path: "src/week.ts", sha: second },395 });396 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))397 );398399 it.live(400 "without --project, asks about the project the repo's platform remote names",401 () =>402 Effect.gen(function* () {403 const { dir, second } = yield* aRepo;404 const calls = yield* noCalls;405406 yield* gitOk(407 dir,408 "remote",409 "add",410 "origin",411 `https://${Brand.domain}/datekit.git`412 );413414 const ran = yield* run(415 dir,416 ["src/week.ts:3", "--json"],417 platformKnowing(Option.some(second), calls)418 );419420 expect(ran.code).toBe(0);421 expect(yield* Ref.get(calls)).toStrictEqual([422 { project: "datekit", sha: second },423 ]);424 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))425 );426427 it.live(428 "without --project or a platform remote, exits 2 asking for --project, and asks nothing",429 () =>430 Effect.gen(function* () {431 const { dir } = yield* aRepo;432 const calls = yield* noCalls;433434 const ran = yield* run(435 dir,436 ["src/week.ts:3"],437 platformKnowing(Option.none(), calls)438 );439440 expect(ran.code).toBe(2);441 expect(ran.err).toContain("--project");442 expect(yield* Ref.get(calls)).toStrictEqual([]);443 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))444 );445446 it.live("adds the verdict a fetched note carries on that commit", () =>447 Effect.gen(function* () {448 const { dir, first, second } = yield* aRepo;449 const calls = yield* noCalls;
74 unmodified lines