|
1 | 1 | import assert from "node:assert/strict"; |
2 | | -import { chmodSync, mkdtempSync, writeFileSync } from "node:fs"; |
| 2 | +import { chmodSync, existsSync, mkdtempSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import path from "node:path"; |
5 | 5 | import test from "node:test"; |
6 | 6 |
|
7 | | -import { planUpgrade } from "../src/upgrade.mjs"; |
| 7 | +import { planUpgrade, selfSpec } from "../src/upgrade.mjs"; |
8 | 8 |
|
9 | 9 | function withFakeTools(fn) { |
10 | 10 | const dir = mkdtempSync(path.join(tmpdir(), "moshcode-upgrade-")); |
@@ -64,3 +64,52 @@ test("unknown upgrade targets remain visible to the caller", () => { |
64 | 64 | assert.deepEqual(plan.unknown, ["not-a-tool"]); |
65 | 65 | assert.equal(plan.items.length, 0); |
66 | 66 | }); |
| 67 | + |
| 68 | +// The self-upgrade spec embeds MOSHCODE_HOME + the installer URL in a `sh -c` |
| 69 | +// command line. Values must be POSIX single-quote escaped: an apostrophe in the |
| 70 | +// install path used to break the command with a shell syntax error (and a |
| 71 | +// hostile path could inject extra shell). Run the generated command for real, |
| 72 | +// with a stub `curl` on PATH that records the env it sees and emits a no-op |
| 73 | +// "installer". |
| 74 | +function runSelfSpec(home) { |
| 75 | + const root = mkdtempSync(path.join(tmpdir(), "moshcode-selfspec-")); |
| 76 | + const bin = path.join(root, "bin"); |
| 77 | + mkdirSync(bin); |
| 78 | + const capture = path.join(root, "home.txt"); |
| 79 | + writeFileSync(path.join(bin, "curl"), `#!/bin/sh\nprintf '%s' "$MOSHCODE_HOME" > ${JSON.stringify(capture)}\necho ':'\n`); |
| 80 | + chmodSync(path.join(bin, "curl"), 0o755); |
| 81 | + return { root, bin, capture }; |
| 82 | +} |
| 83 | + |
| 84 | +test("selfSpec survives an apostrophe in MOSHCODE_HOME", async () => { |
| 85 | + const home = "/Users/o'brien/moshcode home"; |
| 86 | + const { bin, capture } = runSelfSpec(home); |
| 87 | + const spec = selfSpec(home, "https://example.com/install.sh"); |
| 88 | + const before = process.env.PATH; |
| 89 | + process.env.PATH = `${bin}${path.delimiter}${before || ""}`; |
| 90 | + try { |
| 91 | + const { runCmd, ranOk } = await import("../src/engines.mjs"); |
| 92 | + const result = await runCmd(spec.cmd, spec.args); |
| 93 | + assert.ok(ranOk(result), `self-upgrade command should exit 0, got ${JSON.stringify(result)}`); |
| 94 | + } finally { |
| 95 | + process.env.PATH = before; |
| 96 | + } |
| 97 | + assert.equal(readFileSync(capture, "utf8"), home); |
| 98 | +}); |
| 99 | + |
| 100 | +test("selfSpec cannot be broken out of by a hostile install path", async () => { |
| 101 | + const { bin, capture, root } = runSelfSpec("unused"); |
| 102 | + const marker = path.join(root, "INJECTED"); |
| 103 | + const home = `/tmp/x'; touch '${marker}'; '`; |
| 104 | + const spec = selfSpec(home, "https://example.com/install.sh"); |
| 105 | + const before = process.env.PATH; |
| 106 | + process.env.PATH = `${bin}${path.delimiter}${before || ""}`; |
| 107 | + try { |
| 108 | + const { runCmd } = await import("../src/engines.mjs"); |
| 109 | + await runCmd(spec.cmd, spec.args); |
| 110 | + } finally { |
| 111 | + process.env.PATH = before; |
| 112 | + } |
| 113 | + assert.equal(existsSync(marker), false, "path must not inject shell into the update command"); |
| 114 | + assert.equal(readFileSync(capture, "utf8"), home); |
| 115 | +}); |
0 commit comments