UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

79 lines • 3.3 kB
/** * Where a process starts, judged where the path really leads (the relay host's `real-path.ts`, ported; asynchronous here * because the engine's loop must never block on a wedged mount). The path is walked segment by segment: an existing * symlink is resolved before the next segment, "." is skipped and ".." goes to the parent of the real directory reached * so far; past a missing segment the rest is appended as text. A dangling or looping link, or a call past the * resolution deadline, has no real target: undefined. */ import { lstat, realpath } from "node:fs/promises"; import { dirname, isAbsolute, join, parse, relative, resolve, sep } from "node:path"; import { MachineOpError, RESOLUTION_TIMED_OUT, withResolutionDeadline } from "../../../../vendor/senpi-machine-ops/index.js"; async function bounded(operation) { try { const value = await withResolutionDeadline(operation); return value === RESOLUTION_TIMED_OUT ? { kind: "timeout" } : { kind: "ok", value }; } catch { return { kind: "missing" }; } } const linkKind = (path) => bounded(lstat(path).then((info) => (info.isSymbolicLink() ? "link" : "plain"))); async function realPathUnder(base, raw) { const root = isAbsolute(raw) ? parse(raw).root : base; const start = await bounded(realpath(root)); if (start.kind !== "ok") return undefined; let cur = start.value; let missing = false; for (const seg of raw.slice(isAbsolute(raw) ? root.length : 0).split(sep === "\\" ? /[\\/]/ : "/")) { if (seg === "" || seg === ".") continue; // A drive-relative segment (C:foo) names another drive's cwd on Windows; it is never a jailed name. if (/^[A-Za-z]:/.test(seg)) return undefined; if (seg === "..") { cur = dirname(cur); continue; } const next = join(cur, seg); if (missing) { cur = next; continue; } const kind = await linkKind(next); if (kind.kind === "timeout") return undefined; if (kind.kind === "missing") { missing = true; cur = next; continue; } if (kind.value !== "link") { cur = next; continue; } const target = await bounded(realpath(next)); if (target.kind !== "ok") return undefined; cur = target.value; } return cur; } const within = (root, target) => { const rel = relative(root, target); return rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel)); }; /** * The real place a process starts, checked against a confined root (throws policy_denied); an unconfined root only * anchors a relative cwd, and the OS decides whether the process may start there. */ export async function spawnPathIn(root, raw, confined) { const real = await realPathUnder(root, raw); if (!confined) return real ?? resolve(root, raw); const realRoot = await realPathUnder(root, "."); if (realRoot === undefined || real === undefined || !within(realRoot, real)) throw new MachineOpError("policy_denied", `${raw} does not resolve inside ${root}`); return real; } //# sourceMappingURL=real-path.js.map