@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
74 lines • 3.33 kB
JavaScript
import { isAbsolute, relative, resolve, sep } from "node:path";
import { MachineOpError } from "../errors.js";
import { canonicalizeFilesystemPath } from "./filesystem.js";
/** Resolve a request path against the scope root (or the process cwd when the scope has none). */
export function resolveOpPath(path, ctx) {
return resolve(ctx.scope.root ?? process.cwd(), path);
}
function contains(root, target) {
const rel = relative(root, target);
return rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
}
/**
* Containment and filesystem policy for one path, before any effect. A path (or a symlink in it) resolving outside
* `ctx.scope.root` is a denial. With neither a root nor a filesystem policy the path is not canonicalized at all.
*/
export async function checkPath(absolutePath, operation, op, ctx) {
const root = ctx.scope.root;
const filesystem = ctx.policy.filesystem;
if (root === undefined && filesystem === undefined)
return;
const canonicalPath = await canonicalizeFilesystemPath(absolutePath);
if (root !== undefined && !contains(await canonicalizeFilesystemPath(root), canonicalPath)) {
ctx.audit?.({ op, principal: ctx.principal, target: canonicalPath, verdict: "denied" });
throw new MachineOpError("policy_denied", `${absolutePath} is outside the scope root`);
}
if (filesystem !== undefined) {
const decision = await filesystem({ operation, canonicalPath, toolName: op });
if (!decision.allow) {
ctx.audit?.({ op, principal: ctx.principal, target: canonicalPath, verdict: "denied" });
throw new MachineOpError("policy_denied", decision.reason);
}
}
}
/** Run `beforeOp`, the effect, then `afterOp`. A denial throws before the effect runs. */
export async function gated(op, args, ctx, effect) {
if (ctx.signal.aborted)
throw new MachineOpError("aborted", "Operation aborted", { cause: ctx.signal.reason });
if (ctx.policy.beforeOp !== undefined) {
const decision = await ctx.policy.beforeOp(op, args, ctx);
if (!decision.allow) {
ctx.audit?.({ op, principal: ctx.principal, target: describeTarget(args), verdict: "denied" });
throw new MachineOpError("policy_denied", decision.reason);
}
}
ctx.audit?.({ op, principal: ctx.principal, target: describeTarget(args), verdict: "allowed" });
let value;
try {
value = await effect();
}
catch (error) {
await notifyAfter(op, args, { ok: false, error }, ctx);
throw error;
}
await notifyAfter(op, args, { ok: true, value }, ctx);
return value;
}
async function notifyAfter(op, args, outcome, ctx) {
if (ctx.policy.afterOp !== undefined)
await ctx.policy.afterOp(op, args, outcome, ctx);
}
/** The audit target: a path, or the first 64 characters of a command; never a payload. */
function describeTarget(args) {
if (typeof args !== "object" || args === null)
return "";
const record = args;
if (typeof record.path === "string")
return record.path;
if (typeof record.command === "string")
return record.command.slice(0, 64);
if (typeof record.topic === "string")
return record.topic;
return "";
}
//# sourceMappingURL=gate.js.map