From 4b5d02736146313f5e82a555d239dac2c26bd227 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Wed, 18 Nov 2026 10:52:53 -0600 Subject: [PATCH] fix(seat): hosted route fences P3s: realOr runs once per fixed path, a test hook adds a stand-in fixed path so a linked one is tested, the refuse list adds LUA_INIT, CURL_HOME and SSLKEYLOGFILE, and the plugins README names every refused suffix and hook (#1873, lane triage-p3s) --- packages/plugins/README.md | 6 ++++-- packages/seat/src/sandbox.mjs | 21 ++++++++++++------ packages/seat/tests/hosted-binds.test.mjs | 26 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/packages/plugins/README.md b/packages/plugins/README.md index 6c760ce38..18661ce9c 100644 --- a/packages/plugins/README.md +++ b/packages/plugins/README.md @@ -265,8 +265,10 @@ export returns: `harnessEnv` name only when it is listed here and is not one the host always refuses: `MOSAIC_*`, `NODE_*`, `LD_*`, `DYLD_*`, `PYTHON*`, `BASH_*`, `GIT_*`, `MALLOC_*`, `NPM_CONFIG_*`, `XDG_*`, any name ending in `_DIR`, - `_CONF` or `_CONFIG`, PATH, HOME, TMPDIR, ENV, the founder's and the - provider's names, and named shell, proxy, libc, OpenSSL and Java hooks. `installRoots` lists the directories + `_CONF`, `_CONFIG`, `_ASKPASS`, `_CA_BUNDLE` or `_BASE_URL`, PATH, HOME, + TMPDIR, ENV, the founder's and the provider's names, and named shell, + proxy, libc, OpenSSL, Java, askpass, CA bundle and base URL hooks, with + Lua's start code, curl's config home and a TLS key log. `installRoots` lists the directories where the adapter and the mounts live. A hosted adapter or mount source must resolve inside one of them, and never in or around the data root, the config directory, HOME, `/srv/mosaic` or a path with a `secret` diff --git a/packages/seat/src/sandbox.mjs b/packages/seat/src/sandbox.mjs index 0a1ca5029..290b70924 100644 --- a/packages/seat/src/sandbox.mjs +++ b/packages/seat/src/sandbox.mjs @@ -232,25 +232,32 @@ const secretPath = (p) => p.split("/").some((seg) => /secret/i.test(seg)); const realOr = (p) => { try { return realpathSync(p); } catch { return p; } }; const plainObject = (o) => o !== null && typeof o === "object" && !Array.isArray(o) && [Object.prototype, null].includes(Object.getPrototypeOf(o)); -export function checkHostedRoute({ adapter, harnessMounts, installRoots, protectedPaths, codeRoot = null, codeHolder = null, runDir, node = process.execPath }) { +// `alsoFixed` is a test hook: it adds stand-in fixed paths and never removes a shipped one. +export function checkHostedRoute({ adapter, harnessMounts, installRoots, protectedPaths, codeRoot = null, codeHolder = null, runDir, node = process.execPath, alsoFixed = [] }) { if (!cleanPath(runDir)) refuseRoute("protected", "the run directory is not a clean absolute path"); if (!Array.isArray(protectedPaths)) refuseRoute("protected", "no protected paths were given"); + if (!Array.isArray(alsoFixed)) refuseRoute("protected", "the added fixed paths are not a list"); // Lists are copied once, so a Proxy cannot answer one length to a check and another to the map. protectedPaths = Array.from(protectedPaths); + // Each guarded entry: its spellings, and whether a source in the code root is clear of it. The + // fixed paths and the run dir never hold the code. Their spellings are looked up once, here, and + // serve both the code-root check and their guards, so the two always see the same real path. + const fixed = [...ALWAYS_PROTECTED, ...Array.from(alsoFixed), runDir].map((p) => { + if (!cleanPath(p)) refuseRoute("protected", "a protected path is not a clean absolute path"); + return { spellings: [p, realOr(p)], holdsCode: false }; + }); let code = null; if (codeRoot !== null) { if (!cleanPath(codeRoot)) refuseRoute("protected", "the code root is not a clean absolute path"); if (!protectedPaths.includes(codeHolder)) refuseRoute("protected", "the code root's holder is not a protected path"); code = [codeRoot, realOr(codeRoot)]; - const fixed = [runDir, ...ALWAYS_PROTECTED].flatMap((g) => [g, realOr(g)]); - if (code.some((c) => fixed.some((g) => overlaps(c, g)))) refuseRoute("protected", "the code root is in or around a path that is never set aside"); + if (code.some((c) => fixed.some((g) => g.spellings.some((s) => overlaps(c, s))))) refuseRoute("protected", "the code root is in or around a path that is never set aside"); } - // Each guarded entry: its spellings, and whether a source in the code root is clear of it. - const guarded = []; - for (const [p, mayHoldCode] of [...ALWAYS_PROTECTED.map((g) => [g, false]), ...protectedPaths.map((g) => [g, g === codeHolder]), [runDir, false]]) { + const guarded = [...fixed]; + for (const p of protectedPaths) { if (!cleanPath(p)) refuseRoute("protected", "a protected path is not a clean absolute path"); const spellings = [p, realOr(p)]; - const holdsCode = mayHoldCode && code !== null && spellings.some((g) => code.some((c) => c !== g && within(c, g))); + const holdsCode = p === codeHolder && code !== null && spellings.some((g) => code.some((c) => c !== g && within(c, g))); guarded.push({ spellings, holdsCode }); } if (!Array.isArray(installRoots)) refuseRoute("roots", "the harness module declares no usable install roots"); diff --git a/packages/seat/tests/hosted-binds.test.mjs b/packages/seat/tests/hosted-binds.test.mjs index b8b75992c..fe5c426f4 100644 --- a/packages/seat/tests/hosted-binds.test.mjs +++ b/packages/seat/tests/hosted-binds.test.mjs @@ -177,6 +177,32 @@ test("a protected path that holds the code root (HOME over a copy or a worktree) assert.throws(() => checkHostedRoute({ adapter: join(realRun, "code", "inst", "a.sh"), harnessMounts: [], installRoots: [join(realRun, "code", "inst")], protectedPaths: [p.dataRoot, home], codeRoot: join(realRun, "code"), codeHolder: home, runDir: join(p.root, "runlink") }), (e) => e.fence === "protected", "code root in the run dir's real path"); }); +test("a fixed path named through a link: a code root in its real path is refused before any guard, and a source there is refused by its guard", async (t) => { + const p = await layout(t); + // None of the shipped fixed paths is a link on a test host, so the test hook adds a stand-in. + const home = join(p.root, "home"); + const real = join(home, "fixedreal"); + const inst = join(real, "code", "inst"); + mkdirSync(inst, { recursive: true }); + writeFileSync(join(inst, "a.sh"), "x"); + const link = join(p.root, "fixedlink"); + symlinkSync(real, link); + const base = { adapter: join(inst, "a.sh"), harnessMounts: [], installRoots: [inst], protectedPaths: [p.dataRoot, home], codeHolder: home, runDir: p.runDir, alsoFixed: [link] }; + const refused = (opts, fence, message, label) => assert.throws(() => checkHostedRoute({ ...base, ...opts }), (e) => e.code === "hosted-binds-refused" && e.fence === fence && message.test(e.message), label); + // The code root lies in the stand-in's real path, not under the name the list gives. + refused({ codeRoot: join(real, "code") }, "protected", /never set aside/, "code root in a fixed path's real path"); + // With the code root elsewhere, the source in the stand-in's real path is refused by its guard, + // which holds the same real spelling, not by HOME: HOME sets the code root aside. + mkdirSync(join(home, "code")); + refused({ codeRoot: join(home, "code") }, "adapter", /protected path/, "source in a fixed path's real path"); + // The same source passes without the stand-in: only the stand-in refused it. + assert.equal(checkHostedRoute({ ...base, alsoFixed: [], codeRoot: join(real, "code") }).adapter.src, join(inst, "a.sh")); + // The hook adds fixed paths and never removes one, and takes only a list of clean absolute paths. + refused({ alsoFixed: [], codeRoot: "/srv/mosaic/x" }, "protected", /never set aside/, "shipped fixed path with an empty hook"); + refused({ alsoFixed: ["relative"], codeRoot: join(real, "code") }, "protected", /clean absolute/, "relative stand-in"); + refused({ alsoFixed: "/x", codeRoot: join(real, "code") }, "protected", /clean absolute|list/, "a hook that is not a list"); +}); + test("a list that answers one length to the check and another later is read once", async (t) => { const p = await layout(t); const items = [];