Public: anyone can read this; the crew can change it.
Install scripts off for unreviewed repositories (bun install --ignore-scripts) #16886
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/3970-install-scripts-off-for-unreviewed-repos → main 0 commits fork 2 threads · 2 unresolved
6 files
changed +258
−5
src/apps/process.ts +7 −2
| @@ -31,7 +31,7 @@ import { toolchainUse, type ToolchainUse } from "./toolchain.ts"; | ||
| 31 | 31 | import { notFoundHint, shellCommand } from "./run_command.ts"; |
| 32 | 32 | import { floorRefusal } from "../spawn/disk_floor.ts"; |
| 33 | 33 | import { usedRefusal } from "./disk_guard.ts"; |
| 34 | import { cellInstall, meteredInstall, prefillInstallCache, withLock } from "../checks/install.ts"; | |
| 34 | import { cellInstall, meteredInstall, prefillInstallCache, withLock, installCommand } from "../checks/install.ts"; | |
| 35 | 35 | import { |
| 36 | 36 | commandPid, |
| 37 | 37 | diskEntriesMax, |
| @@ -111,6 +111,8 @@ export interface StartInput { | ||
| 111 | 111 | * fill will not trust (no `bun.lock`) installs as before, into its home. |
| 112 | 112 | */ |
| 113 | 113 | readonly installCache?: string | undefined; |
| 114 | /** The repository's review has not passed: its install runs with `--ignore-scripts` (#3970). */ | |
| 115 | readonly scriptsOff?: boolean | undefined; | |
| 114 | 116 | /** |
| 115 | 117 | * This environment's build-cache root (`apps/build_cache.ts`): cargo's |
| 116 | 118 | * target dir and Exact's checkout live in a directory under it, keyed by |
| @@ -596,6 +598,9 @@ async function install( | ||
| 596 | 598 | use?: ToolchainUse, |
| 597 | 599 | built?: string, |
| 598 | 600 | ): Promise<Result<void, string>> { |
| 601 | const scripted = installCommand(command, input.scriptsOff === true); | |
| 602 | if (scripted === undefined || scripted === null) | |
| 603 | return { ok: false, error: "the install command cannot run without its scripts, and this repository's review has not passed" }; | |
| 599 | 604 | let filled = false; |
| 600 | 605 | if (input.installCache !== undefined) { |
| 601 | 606 | const fill = await prefillInstallCache({ |
| @@ -609,7 +614,7 @@ async function install( | ||
| 609 | 614 | if (fill.ok) filled = fill.value; |
| 610 | 615 | else input.log(`cache not filled: ${fill.error}`); |
| 611 | 616 | } |
| 612 | const plan = installPlan(command, input, env, filled, use, built); | |
| 617 | const plan = installPlan(scripted, input, env, filled, use, built); | |
| 613 | 618 | const step = (): Promise<Result<void, string>> => |
| 614 | 619 | runStep("install", plan.command, { cwd: input.dir, env: plan.env, log: input.log, signal: input.signal, cell: plan.cell, build: stepLimits(input) }); |
| 615 | 620 | if (plan.lock !== undefined) { |
src/apps/runtime_bringup.ts +3
| @@ -9,6 +9,7 @@ import { existsSync, rmSync, writeFileSync } from "node:fs"; | ||
| 9 | 9 | import { join } from "node:path"; |
| 10 | 10 | import { mintAppToken, revokeAppToken } from "./token.ts"; |
| 11 | 11 | import { checkInstallCache } from "../checks/install.ts"; |
| 12 | import { isQuarantined } from "../core/quarantine.ts"; | |
| 12 | 13 | import { getPull } from "../core/pulls.ts"; |
| 13 | 14 | import { guestWaits } from "../origins/guest.ts"; |
| 14 | 15 | import type { Repo } from "../core/repos.ts"; |
| @@ -140,6 +141,8 @@ export async function bringUp( | ||
| 140 | 141 | // Every environment, production too, installs from the cache the |
| 141 | 142 | // repository's checks fill, so a new preview fetches nothing it has. |
| 142 | 143 | installCache: checkInstallCache(rt.app.config.dataDir, repo.id), |
| 144 | // Install scripts run only once the repository's review has passed (#3970). | |
| 145 | scriptsOff: isQuarantined(rt.app.db, repo.id), | |
| 143 | 146 | // Its own, per environment: a preview never touches production's. |
| 144 | 147 | buildCache: buildCacheRoot(rt.app.config.dataDir, state.app.id, state.env), |
| 145 | 148 | }; |
src/checks/install.ts +47
| @@ -122,6 +122,53 @@ export function copying(command: string): string { | ||
| 122 | 122 | return command.replace(/\bbun install\b(?![^&;|]*--backend)/g, "bun install --backend=copyfile"); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | const INSTALLING: Readonly<Record<string, readonly string[]>> = { | |
| 126 | bun: ["install", "i", "add", "a", "ci"], | |
| 127 | npm: ["install", "i", "ci", "add", "install-clean", "ic"], | |
| 128 | pnpm: ["install", "i", "add"], | |
| 129 | yarn: ["install", "add"], | |
| 130 | }; | |
| 131 | const HARMLESS = new Set(["cd", "mkdir", "echo", "true", "export", ":"]); | |
| 132 | ||
| 133 | /** | |
| 134 | * The command with every package-manager install told not to run lifecycle scripts (#3970): what | |
| 135 | * a repository whose review has not passed installs with. `bun`, `npm`, `pnpm` and `yarn` get | |
| 136 | * `--ignore-scripts`; any form already written (`=false`, `--no-ignore-scripts`) is struck first, so | |
| 137 | * the part cannot switch it back on. `manifest.install` is the repository's | |
| 138 | * own text, so a form not recognised answers undefined and is not run: `bunx`, `npx`, `bun run`, | |
| 139 | * a script, a subshell. Only `cd`, `mkdir`, `echo`, `true` and `export` may stand beside an | |
| 140 | * install. | |
| 141 | */ | |
| 142 | function withoutScripts(command: string): string | undefined { | |
| 143 | if (/[`()$<>{}]/.test(command)) return undefined; | |
| 144 | const parts = command.split(/(&&|\|\||[;&|\n])/); | |
| 145 | for (let i = 0; i < parts.length; i += 2) { | |
| 146 | const part = (parts[i] ?? "").replace(/\s+--(?:no-)?ignore-scripts(?:=\S*)?(?=\s|$)/g, ""); | |
| 147 | if (part.trim() === "") continue; | |
| 148 | const m = /^(\s*(?:[A-Za-z_]\w*=\S*\s+)*)(\S+)(?:(\s+)(\S+))?/.exec(part); | |
| 149 | if (m === null) return undefined; | |
| 150 | const [whole, lead = "", tool = "", gap = "", sub = ""] = m; | |
| 151 | const rest = part.slice(whole.length); | |
| 152 | const subs = INSTALLING[tool]; | |
| 153 | if (subs === undefined) { | |
| 154 | if (!HARMLESS.has(tool)) return undefined; | |
| 155 | continue; | |
| 156 | } | |
| 157 | const bare = tool === "yarn" && (sub === "" || sub.startsWith("-")); | |
| 158 | if (!bare && !subs.includes(sub)) return undefined; | |
| 159 | parts[i] = bare ? `${lead}yarn install --ignore-scripts${gap}${sub}${rest}` : `${lead}${tool} ${sub} --ignore-scripts${rest}`; | |
| 160 | } | |
| 161 | return parts.join(""); | |
| 162 | } | |
| 163 | ||
| 164 | /** | |
| 165 | * The install command a run uses: as written, or `withoutScripts` while the repository's review has | |
| 166 | * not passed (`scriptsOff`). Null stays null; undefined is a command that is refused. | |
| 167 | */ | |
| 168 | export function installCommand(command: string | null, scriptsOff: boolean): string | null | undefined { | |
| 169 | return command === null || !scriptsOff ? command : withoutScripts(command); | |
| 170 | } | |
| 171 | ||
| 125 | 172 | /** Where, under the cell's home, a contained install keeps its writable copy of the warm cache. */ |
| 126 | 173 | export const WARM_COPY = ".install-warm"; |
| 127 | 174 |
src/checks/tier_install.ts +12 −2
| @@ -4,7 +4,8 @@ import type { Repo } from "../core/repos.ts"; | ||
| 4 | 4 | import { childEnv, runStep } from "../apps/process.ts"; |
| 5 | 5 | import type { CellSpec } from "../apps/contain.ts"; |
| 6 | 6 | import type { Result } from "../kernel/result.ts"; |
| 7 | import { cellInstall, checkInstallCache, meteredInstall, installWithRetry, prefillInstallCache, withLock } from "./install.ts"; | |
| 7 | import { isQuarantined } from "../core/quarantine.ts"; | |
| 8 | import { cellInstall, checkInstallCache, installCommand, installWithRetry, meteredInstall, prefillInstallCache, withLock } from "./install.ts"; | |
| 8 | 9 | import { evictSoon, lend } from "./install_evict.ts"; |
| 9 | 10 | import type { CheckOutcome } from "./runner.ts"; |
| 10 | 11 | import type { LaneState } from "./lane_state.ts"; |
| @@ -31,7 +32,16 @@ export async function runInstall(lane: LaneState, c: InstallStep): Promise<Check | ||
| 31 | 32 | const { dir, env, cell, repo, manifest, write, signal } = c; |
| 32 | 33 | const run = { id: c.runId }; |
| 33 | 34 | if (manifest.install !== null) { |
| 34 | const install = manifest.install; | |
| 35 | // Install scripts run only once the repository's review has passed (#3970). | |
| 36 | const install = installCommand(manifest.install, isQuarantined(app.db, repo.id)); | |
| 37 | if (install === null) return undefined; | |
| 38 | if (install === undefined) | |
| 39 | return { | |
| 40 | ok: false, | |
| 41 | error: "the install command is not one that can run without its scripts, and this repository's review has not passed", | |
| 42 | infrastructure: false, | |
| 43 | stage: "install", | |
| 44 | }; | |
| 35 | 45 | // A cell's home is its own cache. Uncontained, the repository's |
| 36 | 46 | // cache is shared, so installs into it take turns (`checks/install.ts`). |
| 37 | 47 | const shared = |
src/checks/tier_runner.ts +11 −1
| @@ -16,6 +16,8 @@ import type { LaneState } from "./lane_state.ts"; | ||
| 16 | 16 | import type { Execution } from "./local_runner.ts"; |
| 17 | 17 | import type { CheckManifest } from "../apps/manifest.ts"; |
| 18 | 18 | import { readFileAt } from "../apps/checkout.ts"; |
| 19 | import { isQuarantined } from "../core/quarantine.ts"; | |
| 20 | import { installCommand } from "./install.ts"; | |
| 19 | 21 | import { runsAsBaseGate, withBaseGateFrom } from "./base_gate.ts"; |
| 20 | 22 | |
| 21 | 23 | /** What a check handed to a runner is made of. */ |
| @@ -67,6 +69,14 @@ export async function tryRunner( | ||
| 67 | 69 | return undefined; |
| 68 | 70 | } |
| 69 | 71 | if (step !== command) write(`gate list: read from ${base!.slice(0, 7)}'s scripts/gate.ts, on the runner`); |
| 72 | // Install scripts run only once the repository's review has passed (#3970), on a runner too: | |
| 73 | // the job carries the command already rewritten. One not recognised stays here, which refuses | |
| 74 | // it. | |
| 75 | const install = installCommand(manifest.install, isQuarantined(app.db, repo.id)); | |
| 76 | if (install === undefined) { | |
| 77 | write("placement: runner; the install command cannot be run without its scripts; running here"); | |
| 78 | return undefined; | |
| 79 | } | |
| 70 | 80 | const lines: string[] = []; |
| 71 | 81 | // The shard count an admin set reaches a runner's check as it does one run here (#14844); the |
| 72 | 82 | // default is sized as one run here is (`checkShardCpus`), never from the runner machine's CPUs. |
| @@ -83,7 +93,7 @@ export async function tryRunner( | ||
| 83 | 93 | ...(base === undefined ? {} : { base }), |
| 84 | 94 | depth: base === undefined ? 1 : await headDepth(bare, base, run.commitOid), |
| 85 | 95 | steps: [ |
| 86 | ...(manifest.install === null ? [] : [{ name: "install" as const, command: manifest.install }]), | |
| 96 | ...(install === null ? [] : [{ name: "install" as const, command: install }]), | |
| 87 | 97 | { name: "check" as const, command: step }, |
| 88 | 98 | ], |
| 89 | 99 | // Only the check's own variables cross, never the forge's; the runner adds its cell's. |
test/install-scripts-off.test.ts added +178
| @@ -0,0 +1,178 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { existsSync, mkdtempSync, writeFileSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import type { Database } from "bun:sqlite"; | |
| 6 | import type { CheckManifest } from "../src/apps/manifest.ts"; | |
| 7 | import type { LaneState } from "../src/checks/lane_state.ts"; | |
| 8 | import type { Execution } from "../src/checks/local_runner.ts"; | |
| 9 | import type { Repo } from "../src/core/repos.ts"; | |
| 10 | import { installCommand } from "../src/checks/install.ts"; | |
| 11 | import { runInstall, type InstallStep } from "../src/checks/tier_install.ts"; | |
| 12 | import { tryRunner, type RunnerCheck } from "../src/checks/tier_runner.ts"; | |
| 13 | import { startProcess, type StartInput } from "../src/apps/process.ts"; | |
| 14 | ||
| 15 | test("withoutScripts adds the flag to every bun install, once", () => { | |
| 16 | expect(installCommand("bun install", true)).toBe("bun install --ignore-scripts"); | |
| 17 | expect(installCommand("cd a && bun install && bun install --frozen-lockfile", true)).toBe( | |
| 18 | "cd a && bun install --ignore-scripts && bun install --ignore-scripts --frozen-lockfile", | |
| 19 | ); | |
| 20 | expect(installCommand("bun install --ignore-scripts", true)).toBe("bun install --ignore-scripts"); | |
| 21 | }); | |
| 22 | ||
| 23 | test("a flag already written cannot switch scripts back on", () => { | |
| 24 | expect(installCommand("bun install --ignore-scripts=false", true)).toBe("bun install --ignore-scripts"); | |
| 25 | expect(installCommand("npm ci --ignore-scripts=false", true)).toBe("npm ci --ignore-scripts"); | |
| 26 | expect(installCommand("npm ci --no-ignore-scripts", true)).toBe("npm ci --ignore-scripts"); | |
| 27 | expect(installCommand("bun install --frozen-lockfile --ignore-scripts=0", true)).toBe("bun install --ignore-scripts --frozen-lockfile"); | |
| 28 | }); | |
| 29 | ||
| 30 | test("npm, pnpm, yarn and bun add are told too", () => { | |
| 31 | expect(installCommand("npm install", true)).toBe("npm install --ignore-scripts"); | |
| 32 | expect(installCommand("npm ci", true)).toBe("npm ci --ignore-scripts"); | |
| 33 | expect(installCommand("npm i left-pad", true)).toBe("npm i --ignore-scripts left-pad"); | |
| 34 | expect(installCommand("pnpm install --frozen-lockfile", true)).toBe("pnpm install --ignore-scripts --frozen-lockfile"); | |
| 35 | expect(installCommand("yarn install", true)).toBe("yarn install --ignore-scripts"); | |
| 36 | expect(installCommand("yarn", true)).toBe("yarn install --ignore-scripts"); | |
| 37 | expect(installCommand("yarn --frozen-lockfile", true)).toBe("yarn install --ignore-scripts --frozen-lockfile"); | |
| 38 | expect(installCommand("bun add x", true)).toBe("bun add --ignore-scripts x"); | |
| 39 | expect(installCommand("CI=1 npm ci; mkdir out", true)).toBe("CI=1 npm ci --ignore-scripts; mkdir out"); | |
| 40 | }); | |
| 41 | ||
| 42 | test("a command that is not recognised is refused", () => { | |
| 43 | for (const c of ["bunx foo", "npx foo", "bun run build", "make deps", "./setup.sh", "bun install && make", "bun install $(curl x)", "bun install `x`", "(bun install)", "npm run prepare", "yarn run x", "bun install | sh", "bun install & make", "bun install & ./evil.sh", "bun install &./evil.sh"]) { | |
| 44 | expect(installCommand(c, true)).toBeUndefined(); | |
| 45 | } | |
| 46 | }); | |
| 47 | ||
| 48 | test("installCommand rewrites only while the review has not passed", () => { | |
| 49 | expect(installCommand("bun install", true)).toBe("bun install --ignore-scripts"); | |
| 50 | expect(installCommand("bun install", false)).toBe("bun install"); | |
| 51 | expect(installCommand(null, true)).toBeNull(); | |
| 52 | expect(installCommand("make", true)).toBeUndefined(); | |
| 53 | expect(installCommand("make", false)).toBe("make"); | |
| 54 | }); | |
| 55 | ||
| 56 | function fixture(): string { | |
| 57 | const dir = mkdtempSync(join(tmpdir(), "scripts-")); | |
| 58 | writeFileSync(join(dir, "package.json"), JSON.stringify({ name: "fx", version: "1.0.0", scripts: { postinstall: "echo ran > marker" } })); | |
| 59 | return dir; | |
| 60 | } | |
| 61 | const install = async (dir: string, command: string): Promise<void> => { | |
| 62 | const proc = Bun.spawn(["sh", "-c", command], { cwd: dir, env: { ...process.env, BUN_INSTALL_CACHE_DIR: join(dir, ".c") }, stdout: "ignore", stderr: "ignore" }); | |
| 63 | await proc.exited; | |
| 64 | }; | |
| 65 | ||
| 66 | test("a postinstall marker is not written before the review passes, and is after", async () => { | |
| 67 | const held = fixture(); | |
| 68 | await install(held, installCommand("bun install", true)!); | |
| 69 | expect(existsSync(join(held, "marker"))).toBe(false); | |
| 70 | const passed = fixture(); | |
| 71 | await install(passed, "bun install"); | |
| 72 | expect(existsSync(join(passed, "marker"))).toBe(true); | |
| 73 | }); | |
| 74 | ||
| 75 | // A database that answers the review state of every repository with `state`. | |
| 76 | const reviewed = (state: string): Database => ({ query: () => ({ get: () => ({ state }) }) }) as unknown as Database; | |
| 77 | const lane = (state: string, dir: string, extra: object = {}): LaneState => | |
| 78 | ({ app: { db: reviewed(state), config: { dataDir: dir } }, options: {}, ...extra }) as unknown as LaneState; | |
| 79 | const repo = { id: "r1", ownerName: "o", name: "n" } as Repo; | |
| 80 | const manifestOf = (install: string): CheckManifest => ({ run: "true", install, fast: null, full: "true", verify: null, timeout: 30, egress: [], limits: {} as CheckManifest["limits"] }); | |
| 81 | const step = (dir: string, install: string, path: string, write: (l: string) => void = () => {}): InstallStep => ({ | |
| 82 | dir, | |
| 83 | env: { PATH: path } as InstallStep["env"], | |
| 84 | cell: undefined, | |
| 85 | repo, | |
| 86 | runId: "run1", | |
| 87 | manifest: manifestOf(install), | |
| 88 | write, | |
| 89 | signal: new AbortController().signal, | |
| 90 | }); | |
| 91 | ||
| 92 | async function checkInstall(state: string, install: string): Promise<{ lines: string[]; failed: boolean }> { | |
| 93 | const dir = mkdtempSync(join(tmpdir(), "scripts-")); | |
| 94 | const lines: string[] = []; | |
| 95 | const out = await runInstall(lane(state, dir), step(dir, install, process.env["PATH"] ?? "", (l) => lines.push(l))); | |
| 96 | return { lines, failed: out !== undefined }; | |
| 97 | } | |
| 98 | ||
| 99 | test("the check refuses an install it cannot run without scripts, only while the review has not passed", async () => { | |
| 100 | const held = await checkInstall("pending", "make deps"); | |
| 101 | expect(held.failed).toBe(true); | |
| 102 | expect(held.lines.join("\n")).not.toContain("make"); | |
| 103 | expect((await checkInstall("passed", "echo ok")).failed).toBe(false); | |
| 104 | }); | |
| 105 | ||
| 106 | test("the check's install command carries --ignore-scripts only while quarantined", async () => { | |
| 107 | for (const [state, flagged] of [["pending", true], ["passed", false]] as const) { | |
| 108 | const dir = mkdtempSync(join(tmpdir(), "scripts-")); | |
| 109 | const bin = join(dir, "bin"); | |
| 110 | writeFileSync(join(dir, "package.json"), "{}"); | |
| 111 | await Bun.write(join(bin, "npm"), `#!/bin/sh\necho "args:$*" > "${dir}/args"\n`); | |
| 112 | await Bun.spawn(["chmod", "+x", join(bin, "npm")]).exited; | |
| 113 | await runInstall(lane(state, dir), step(dir, "npm ci", `${bin}:${process.env["PATH"] ?? ""}`)); | |
| 114 | expect(await Bun.file(join(dir, "args")).text()).toBe(flagged ? "args:ci --ignore-scripts\n" : "args:ci\n"); | |
| 115 | } | |
| 116 | }); | |
| 117 | ||
| 118 | test("a deploy's install carries --ignore-scripts only when scriptsOff", async () => { | |
| 119 | for (const scriptsOff of [true, false]) { | |
| 120 | const dir = mkdtempSync(join(tmpdir(), "scripts-")); | |
| 121 | const bin = join(dir, "bin"); | |
| 122 | await Bun.write(join(bin, "npm"), `#!/bin/sh\necho "args:$*" > "${dir}/args"\n`); | |
| 123 | await Bun.spawn(["chmod", "+x", join(bin, "npm")]).exited; | |
| 124 | const prior = process.env["PATH"]; | |
| 125 | process.env["PATH"] = `${bin}:${prior ?? ""}`; | |
| 126 | try { | |
| 127 | await startProcess({ | |
| 128 | dir, | |
| 129 | dataDir: dir, | |
| 130 | manifest: { run: "true", install: "npm ci", test: null, health: "/", ready: 1, tty: null, service: null, looks: ["/"] } as StartInput["manifest"], | |
| 131 | env: "production", | |
| 132 | url: "http://app.test", | |
| 133 | commitOid: "0".repeat(40), | |
| 134 | forgeUrl: "http://forge.test", | |
| 135 | identityKey: "key", | |
| 136 | scriptsOff, | |
| 137 | log: () => {}, | |
| 138 | signal: new AbortController().signal, | |
| 139 | } satisfies StartInput); | |
| 140 | } finally { | |
| 141 | process.env["PATH"] = prior; | |
| 142 | } | |
| 143 | expect(await Bun.file(join(dir, "args")).text()).toBe(scriptsOff ? "args:ci --ignore-scripts\n" : "args:ci\n"); | |
| 144 | } | |
| 145 | }); | |
| 146 | ||
| 147 | test("a runner job carries the install command already without scripts", async () => { | |
| 148 | const sent: string[] = []; | |
| 149 | const x = (state: string, install: string) => | |
| 150 | tryRunner( | |
| 151 | lane(state, tmpdir(), { placement: { mode: "runner", waitMs: 0 } }), | |
| 152 | { bare: tmpdir(), repo, id: "x", runId: "run1", tier: "run", commitOid: "a".repeat(40) } as Execution, | |
| 153 | { | |
| 154 | run: { id: "run1", commitOid: "a".repeat(40) }, | |
| 155 | repo, | |
| 156 | bare: tmpdir(), | |
| 157 | manifest: manifestOf(install), | |
| 158 | learned: "", | |
| 159 | reverts: {}, | |
| 160 | command: "bun test", | |
| 161 | base: undefined, | |
| 162 | builtOnBase: undefined, | |
| 163 | tree: { ok: false } as RunnerCheck["tree"], | |
| 164 | key: undefined, | |
| 165 | outside: false, | |
| 166 | write: () => {}, | |
| 167 | signal: new AbortController().signal, | |
| 168 | }, | |
| 169 | async (_app, input) => { | |
| 170 | sent.push(input.job.steps[0]!.command); | |
| 171 | return { taken: false, why: "none joined" }; | |
| 172 | }, | |
| 173 | ); | |
| 174 | await x("pending", "bun install"); | |
| 175 | await x("passed", "bun install"); | |
| 176 | await x("pending", "make deps"); | |
| 177 | expect(sent).toEqual(["bun install --ignore-scripts", "bun install"]); | |
| 178 | }); |
new above
offline
A lone
&is not split on, sobun install & ./evil.shis one part and./evil.shruns unchecked. Split on&as well, or refuse it, and add it to the refused test list.