From f1625050561c802b5e00d9b84d4c6bdb29dd30f5 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Thu, 12 Nov 2026 15:31:41 -0600 Subject: [PATCH] fix(plugins): review round for the starter-pack reader (#1873 SP1) readSavedTemplate refuses a hard-linked file and an account directory open to group or other; an open error other than ELOOP reads "can't be read", not "symlink"; a file that grows during the read has its own message. installedStarterPacks refuses a promise from starterPack() and swallows its later rejection. Tests pin each refusal's message, so a check that a neighbour shadows is still covered. --- packages/plugins/src/starter-pack.mjs | 25 +++++++--- packages/plugins/tests/starter-pack.test.mjs | 48 ++++++++++++++++---- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/packages/plugins/src/starter-pack.mjs b/packages/plugins/src/starter-pack.mjs index 7759ed0ee..02fa7584e 100644 --- a/packages/plugins/src/starter-pack.mjs +++ b/packages/plugins/src/starter-pack.mjs @@ -376,6 +376,12 @@ export async function installedStarterPacks({ dataRoot = null, rolesDir = ROLES_ } catch { fail("starter-pack-invalid", `starter pack ${name} failed to describe itself`, name); } + // A pack is returned, never awaited: a promise refuses below, and a later rejection of it + // must not reach the process as an unhandled one. + if (doc !== null && (typeof doc === "object" || typeof doc === "function") && typeof doc.then === "function") { + try { Promise.resolve(doc).catch(() => {}); } catch { /* the refusal below stands */ } + fail("starter-pack-invalid", `starter pack ${name} must return its document, not a promise`, name); + } packs.push({ id: name, origin, pack: checkPack(doc, { origin, rolesDir, name }) }); } catch (error) { if (!(error instanceof PluginError)) throw error; @@ -398,20 +404,23 @@ function lstatOrNull(path) { } // A directory on the way to a saved template: a real directory owned by the instance's user that -// no group or other user can write. -function checkDirectory(path, uid) { +// no group or other user can write. `private` also refuses group and other read and list, for the +// account's own directory, which the writer creates 0700. +function checkDirectory(path, uid, { private: closed = false } = {}) { const st = lstatOrNull(path); if (st === null) fail("starter-pack-not-found", "no saved template has that name"); if (st.isSymbolicLink() || !st.isDirectory()) fail("starter-pack-invalid", "the saved templates directory must be a real directory"); if (st.uid !== uid) fail("starter-pack-invalid", "the saved templates directory must belong to the instance's user"); if ((st.mode & 0o022) !== 0) fail("starter-pack-invalid", "the saved templates directory must not be writable by group or others"); + if (closed && (st.mode & 0o077) !== 0) fail("starter-pack-invalid", "the account's saved templates directory must be mode 0700 or narrower"); } // Reads /templates//.json as data and checks it with origin `saved`. The // account is the calling owner's id (s.human), checked against ID_PATTERN; the name is checked // against NAME. Both are checked before any path is built, so neither ".." nor "/" reaches the file -// system. The file must be a regular file, not a symlink, owned by the instance's user, mode 0600 -// or narrower, at most 256 KiB, checked before the read. Returns { name, origin: "saved", pack }. +// system. The account directory must be mode 0700 or narrower. The file must be a regular file, not +// a symlink or a hard link, owned by the instance's user, mode 0600 or narrower, at most 256 KiB, +// checked before the read. Returns { name, origin: "saved", pack }. export function readSavedTemplate({ configDir, account, name, rolesDir = ROLES_DIR, uid = process.getuid() } = {}) { if (typeof account !== "string" || !ID_PATTERN.test(account)) fail("starter-pack-invalid", `the saved template account must match ${ID_PATTERN}`); if (typeof name !== "string" || !NAME.test(name)) fail("starter-pack-invalid", `the saved template name must match ${NAME}`); @@ -419,7 +428,7 @@ export function readSavedTemplate({ configDir, account, name, rolesDir = ROLES_D const templates = join(configDir, "templates"); const accountDir = join(templates, account); checkDirectory(templates, uid); - checkDirectory(accountDir, uid); + checkDirectory(accountDir, uid, { private: true }); const file = join(accountDir, `${name}.json`); const before = lstatOrNull(file); if (before === null) fail("starter-pack-not-found", "no saved template has that name", name); @@ -429,13 +438,15 @@ export function readSavedTemplate({ configDir, account, name, rolesDir = ROLES_D fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); } catch (error) { if (error.code === "ENOENT") fail("starter-pack-not-found", "no saved template has that name", name); - fail("starter-pack-invalid", "the saved template must be a regular file, not a symlink", name); + if (error.code === "ELOOP") fail("starter-pack-invalid", "the saved template must be a regular file, not a symlink", name); + fail("starter-pack-invalid", "the saved template can't be read", name); } let bytes; try { const st = fstatSync(fd); if (!st.isFile() || st.dev !== before.dev || st.ino !== before.ino) fail("starter-pack-invalid", "the saved template changed while it was read", name); if (st.uid !== uid) fail("starter-pack-invalid", "the saved template must belong to the instance's user", name); + if (st.nlink !== 1) fail("starter-pack-invalid", "the saved template must not be a hard link", name); if ((st.mode & 0o077) !== 0) fail("starter-pack-invalid", "the saved template must be mode 0600 or narrower", name); if (st.size > SAVED_MAX_BYTES) fail("starter-pack-invalid", `the saved template must be at most ${SAVED_MAX_BYTES} bytes`, name); // Read at most one byte past the limit, so a file that grew after the fstat still refuses. @@ -445,7 +456,7 @@ export function readSavedTemplate({ configDir, account, name, rolesDir = ROLES_D const n = readSync(fd, buffer, length, buffer.length - length, null); if (n === 0) break; length += n; - if (length > SAVED_MAX_BYTES) fail("starter-pack-invalid", `the saved template must be at most ${SAVED_MAX_BYTES} bytes`, name); + if (length > SAVED_MAX_BYTES) fail("starter-pack-invalid", `the saved template grew past ${SAVED_MAX_BYTES} bytes while it was read`, name); } bytes = buffer.subarray(0, length); } finally { diff --git a/packages/plugins/tests/starter-pack.test.mjs b/packages/plugins/tests/starter-pack.test.mjs index 0a4a9958b..87f92e2a1 100644 --- a/packages/plugins/tests/starter-pack.test.mjs +++ b/packages/plugins/tests/starter-pack.test.mjs @@ -3,7 +3,7 @@ // without it (the red-before run). import test from "node:test"; import assert from "node:assert/strict"; -import { chmodSync, cpSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { chmodSync, cpSync, linkSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { tmpdir } from "node:os"; import { fileURLToPath, pathToFileURL } from "node:url"; @@ -21,9 +21,10 @@ const MARK = "MARK-sp1-not-echoed-7c1f"; let loaded; const sp = async () => (loaded ??= await import("../src/starter-pack.mjs")); -const code = (expected) => (e) => { +const code = (expected, message) => (e) => { assert.equal(e?.name, "PluginError", e?.stack); assert.equal(e.code, expected, e.message); + if (message) assert.match(e.message, message); assert.ok(!e.message.includes(MARK), e.message); assert.ok(!e.message.includes(REPO), e.message); return true; @@ -416,19 +417,23 @@ test("SP1 readSavedTemplate refuses .., a name or account holding /, a symlink, const real = join(f.root, "real.json"); writeFileSync(real, JSON.stringify(pack()), { mode: 0o600 }); symlinkSync(real, join(f.dir, "link.json")); - assert.throws(read("link"), code("starter-pack-invalid")); + assert.throws(read("link"), code("starter-pack-invalid", /regular file, not a symlink/)); // A 0644 file and a 0640 file refuse. f.put("wide", JSON.stringify(pack()), 0o644); - assert.throws(read("wide"), code("starter-pack-invalid")); + assert.throws(read("wide"), code("starter-pack-invalid", /mode 0600 or narrower/)); f.put("group", JSON.stringify(pack()), 0o640); - assert.throws(read("group"), code("starter-pack-invalid")); + assert.throws(read("group"), code("starter-pack-invalid", /mode 0600 or narrower/)); + // A hard link to a file outside the account's directory refuses. + const solo = f.put("solo", JSON.stringify(pack())); + linkSync(solo, join(f.root, "alias.json")); + assert.throws(read("solo"), code("starter-pack-invalid", /hard link/)); f.put("narrow", JSON.stringify(pack()), 0o400); assert.ok(read("narrow")()); // 300 KiB refuses before the read; JSON padding keeps the file otherwise valid. const big = JSON.stringify(pack()); f.put("big", big + " ".repeat(300 * 1024 - big.length)); assert.equal(lstatSync(join(f.dir, "big.json")).size, 300 * 1024); - assert.throws(read("big"), code("starter-pack-invalid")); + assert.throws(read("big"), code("starter-pack-invalid", /must be at most 262144 bytes/)); // A missing file, and a missing account directory, are not found. assert.throws(read("missing"), code("starter-pack-not-found")); assert.throws(read("good", "carol"), code("starter-pack-not-found")); @@ -443,7 +448,7 @@ test("SP1 readSavedTemplate refuses .., a name or account holding /, a symlink, f.put("forbidden", JSON.stringify(pack((p) => { p.definitions.coder.copyBindings = MARK; }))); assert.throws(read("forbidden"), code("starter-pack-invalid")); // Another user's file, and a relative configDir, refuse. - assert.throws(read("good", "alice.b", { uid: process.getuid() + 1 }), code("starter-pack-invalid")); + assert.throws(read("good", "alice.b", { uid: process.getuid() + 1 }), code("starter-pack-invalid", /directory must belong to the instance's user/)); assert.throws(() => readSavedTemplate({ configDir: "cfg", account: "alice.b", name: "good", rolesDir: ROLES }), code("starter-pack-invalid")); }); @@ -453,20 +458,31 @@ test("SP1 readSavedTemplate refuses a symlinked or group-writable templates dire f.put("good", JSON.stringify(pack())); const read = () => readSavedTemplate({ configDir: f.configDir, account: "alice.b", name: "good", rolesDir: ROLES }); chmodSync(f.dir, 0o770); - assert.throws(read, code("starter-pack-invalid")); + assert.throws(read, code("starter-pack-invalid", /not be writable by group or others/)); + // The account's directory is 0700 or narrower: group or other read or list refuses too. + for (const mode of [0o750, 0o705, 0o755]) { + chmodSync(f.dir, mode); + assert.throws(read, code("starter-pack-invalid", /mode 0700 or narrower/)); + } chmodSync(f.dir, 0o700); assert.ok(read()); + // The templates directory itself may be listed by others; it only must not be writable. + chmodSync(join(f.configDir, "templates"), 0o755); + assert.ok(read()); + chmodSync(join(f.configDir, "templates"), 0o775); + assert.throws(read, code("starter-pack-invalid", /not be writable by group or others/)); + chmodSync(join(f.configDir, "templates"), 0o700); // The account directory as a symlink to a real one refuses. const g = savedFixture(t, "other"); g.put("good", JSON.stringify(pack())); symlinkSync(g.dir, join(f.configDir, "templates", "linked")); - assert.throws(() => readSavedTemplate({ configDir: f.configDir, account: "linked", name: "good", rolesDir: ROLES }), code("starter-pack-invalid")); + assert.throws(() => readSavedTemplate({ configDir: f.configDir, account: "linked", name: "good", rolesDir: ROLES }), code("starter-pack-invalid", /must be a real directory/)); // The templates directory as a symlink refuses. const h = mkdtempSync(join(tmpdir(), "sp1-")); t.after(() => rmSync(h, { recursive: true, force: true })); mkdirSync(join(h, "cfg"), { mode: 0o700 }); symlinkSync(join(f.configDir, "templates"), join(h, "cfg", "templates")); - assert.throws(() => readSavedTemplate({ configDir: join(h, "cfg"), account: "alice.b", name: "good", rolesDir: ROLES }), code("starter-pack-invalid")); + assert.throws(() => readSavedTemplate({ configDir: join(h, "cfg"), account: "alice.b", name: "good", rolesDir: ROLES }), code("starter-pack-invalid", /must be a real directory/)); }); // ---- installedStarterPacks ---- @@ -558,4 +574,16 @@ test("SP1 an installed pack lists with origin installed; a refused one is omitte const res = await installedStarterPacks({ dataRoot: broken.dataRoot, rolesDir: ROLES, log: (e) => log.push(e) }); assert.deepEqual(res.packs, []); assert.deepEqual(log.map((e) => [e.name, e.code]), [["brokenpack", "missing-export"]]); + // An async starterPack is refused, and its later rejection never reaches the process. + const unhandled = []; + const onUnhandled = (reason) => unhandled.push(reason); + process.on("unhandledRejection", onUnhandled); + t.after(() => process.off("unhandledRejection", onUnhandled)); + const later = installed(t, "asyncpack", "export const interfaceVersion = 1;\nexport async function starterPack() { await new Promise((r) => setTimeout(r, 20)); throw new Error('late'); }\n"); + const res2 = await installedStarterPacks({ dataRoot: later.dataRoot, rolesDir: ROLES, log() {} }); + assert.deepEqual(res2.packs, []); + assert.deepEqual(res2.refused.map((e) => [e.name, e.code]), [["asyncpack", "starter-pack-invalid"]]); + assert.match(res2.refused[0].message, /not a promise/); + await new Promise((r) => setTimeout(r, 100)); + assert.deepEqual(unhandled, []); });