// A PATH A PAGE NAMED, KEPT INSIDE THE PROJECT (lane palette L8a, review): // POST /api/cli runs a registry row's `path` arg (lib/cli/verbs.mjs) only // when it is a file of the project the form names -- relative to the // project's directory, its REALPATH (every symlink followed, the project's // own too) inside the project's realpath. What runs is that absolute path, so // the CLI's own resolution (most of them path.resolve() from their cwd) cannot // put it anywhere else; a row whose CLI takes it relative to the project // (`pass: "relative"`) gets the path as typed, checked the same way. // // The refusal names the arg and never what the file holds. import { realpath, stat } from "node:fs/promises"; import path from "node:path"; /** * @param {string} projectDir * @param {string} value what the form gave * @returns {Promise<{ ok: true, abs: string } | { ok: false, error: string }>} */ export async function confineToProject(projectDir, value) { const v = String(value ?? "").trim(); if (!v) return { ok: false, error: "no file named" }; if (v.includes("\0")) return { ok: false, error: "a NUL byte" }; let root; try { root = await realpath(/* turbopackIgnore: true */ projectDir); } catch { return { ok: false, error: "the project's directory is not there" }; } const wanted = path.resolve(/* turbopackIgnore: true */ projectDir, v); let real; try { real = await realpath(/* turbopackIgnore: true */ wanted); } catch { return { ok: false, error: `${v}: no such file in the project` }; } if (real !== root && !real.startsWith(root + path.sep)) return { ok: false, error: `${v}: not a file of the project (a form may only name files inside it)` }; const st = await stat(/* turbopackIgnore: true */ real).catch(() => null); if (!st?.isFile()) return { ok: false, error: `${v}: not a file` }; return { ok: true, abs: real }; }