@sema-agent/core
Version:
Stateless, task-oriented AI agent core
1,911 lines • 96.5 kB
JavaScript
import { homedir } from "node:os";
import { createHash } from "node:crypto";
import { shellWordsOf } from "../shell/shell-words.js";
import { PARSE_FAILURE_SENTENCE, SHELL_LEX_DIALECT, lexShell } from "../shell/lex.js";
import { startedShellOfCall } from "./call-shell.js";
import { readProgramPosition } from "../tools/fs/bash-program-position.js";
import { namesASetOfTools, ruleNameCovers } from "./namespaced-rule-name.js";
import { toolNameFaultOf } from "./tool-name-aliases.js";
import { A2A_FINGERPRINT_MAX_CHARS, A2A_NAMESPACE, isNameableToolName, parsePermissionRule, ruleSpellingOf, isRuleBehavior, peerReadingsOf, readsNameGlob, RULE_BEHAVIOR_PRECEDENCE, RULE_BEHAVIORS, RULE_BEHAVIORS_BY_PRECEDENCE, splitNamespacedName, } from "@sema-agent/primitives";
import { compileReadDeny } from "../tools/fs/read-deny.js";
import { carriesShellRedirection, parseLeadingCommandName, splitShellCompoundSegments } from "../tools/fs/bash-readonly-classifier.js";
import { INLINE_TRUNCATION_MARK, inlineUntrusted, UNRENDERABLE_CHAR_CLASS } from "./untrusted-text.js";
import { foreignWords } from "./protocol-http-auth.js";
import { boundedForeignReading, cutAt } from "./untrusted-egress.js";
import { catalogRuleFaceOf, catalogToolNames, hostTargetOf, isShellFaceToolName, pathTargetOf, protectivePathTargetOf } from "./tool-registry.js";
import { HOST_UNREADABLE_SENTENCE, hostPatternAdmits, readHostPattern, spelledHostPattern } from "./host-target.js";
import { heldBy, heldValues, wireOf } from "./tool-name-readings.js";
import { carriesDotSegment, isAbsoluteForFamily, isIdentityDir, isPathIdentity, isShellRootedSpellingUnmapped, isUncIdentity, pathFamilyOf, pathIdentityOf, shellDriveSpellingOf } from "../tools/fs/safety.js";
import { bindPathRules, judgeRuleRows, rowsWhereTheyLead } from "./path-rule-binding.js";
import { lexedWordOf, readShellCommandIn } from "./shell-run-candidates.js";
import { lineDescriptorsOfText, readOnlyLineObjection, readOnlyShellVerdictIn } from "./read-only-shell.js";
import { ARTIFACT_ACTIONS } from "./artifact-host.js";
import { ARTIFACT_TOOL_NAME, TOOL_CATALOG_ENTRIES } from "./tool-catalog-entries.js";
import { paramRuleReaches } from "./param-rule-reach.js";
export { isRuleBehavior, RULE_BEHAVIOR_PRECEDENCE, RULE_BEHAVIORS, RULE_BEHAVIORS_BY_PRECEDENCE };
export function readRowBehavior(v) {
if (v === undefined)
return "allow";
return isRuleBehavior(v) ? v : undefined;
}
export const COMMAND_RULE_TOOL = "Bash";
if (!isShellFaceToolName(COMMAND_RULE_TOOL))
throw new Error(`permission rules: the command rule word ${JSON.stringify(COMMAND_RULE_TOOL)} names no definition on the catalog's shell face`);
export const READ_RULE_TOOL = "Read";
export const PATH_RULE_FACES = Object.freeze({
[READ_RULE_TOOL]: Object.freeze({ deny: Object.freeze(["read", "write"]), ask: Object.freeze(["read"]), allow: Object.freeze(["read"]) }),
Edit: Object.freeze({ deny: Object.freeze(["write"]), ask: Object.freeze(["write"]), allow: Object.freeze(["write", "read"]) }),
});
export function callPathFaceOf(call) {
const target = protectivePathTargetOf(call);
return target === undefined ? undefined : target.access === "read" ? "read" : "write";
}
export function faceRowsSpeakFor(face) {
return face !== undefined && Object.values(PATH_RULE_FACES).some((columns) => columns.deny?.includes(face) === true || columns.ask?.includes(face) === true);
}
function catalogVocabularyOf(tool) {
const params = catalogRuleFaceOf(tool)?.params;
if (params === undefined)
throw new Error(`permission rules: the catalog declares no rule vocabulary for ${JSON.stringify(tool)}`);
return params;
}
const DELEGATION_RULE_TOOL = (() => {
const row = TOOL_CATALOG_ENTRIES.find((e) => e.id === "agent");
if (row === undefined)
throw new Error("permission rules: the catalog carries no delegation tool row (`agent`)");
return row.name;
})();
const WORKFLOW_RULE_TOOL = (() => {
const row = TOOL_CATALOG_ENTRIES.find((e) => e.id === "workflow");
if (row === undefined)
throw new Error("permission rules: the catalog carries no workflow tool row (`workflow`)");
return row.name;
})();
export const NAME_RULE_FACES = Object.freeze({
Skill: Object.freeze({ param: "skill", params: Object.freeze(catalogVocabularyOf("Skill")) }),
[ARTIFACT_TOOL_NAME]: Object.freeze({ param: "action", params: Object.freeze(["action"]), words: ARTIFACT_ACTIONS }),
[DELEGATION_RULE_TOOL]: Object.freeze({ param: "subagent_type", params: Object.freeze(["subagent_type"]), values: "mounted" }),
[WORKFLOW_RULE_TOOL]: Object.freeze({
param: "name",
exact: true,
yieldsTo: Object.freeze({ subject: Object.freeze(["scriptPath"]), grant: Object.freeze(["script"]) }),
}),
});
function nameRuleFaceOf(tool) {
return Object.hasOwn(NAME_RULE_FACES, tool) ? NAME_RULE_FACES[tool] : undefined;
}
function keyedValueParamOf(tool) {
const face = nameRuleFaceOf(tool);
return face?.words !== undefined ? face.param : undefined;
}
export function normalizeSubagentType(value) {
return value.normalize("NFKC").toLowerCase().replace(/[\p{White_Space}\p{Pd}_]+/gu, "");
}
export function canonicalNameValue(value, values) {
if (values.includes(value))
return { kind: "entry", entry: value };
const key = normalizeSubagentType(value);
const hits = key === "" ? [] : [...new Set(values.filter((v) => normalizeSubagentType(v) === key))];
if (hits.length === 1)
return { kind: "entry", entry: hits[0] };
return hits.length > 1 ? { kind: "ambiguous", entries: hits } : { kind: "unknown" };
}
export function nameValueAbsentReason(tool) {
const face = nameRuleFaceOf(tool);
if (face === undefined || (face.words === undefined && face.values === undefined))
return undefined;
const key = face.param;
return `the ${tool} call names no ${key} as the tool reads it, so the ${key} it runs is not one its arguments show — a rule by ${key} cannot be read against it`;
}
export function nameGrammarReads(tool, content) {
if (ruleToolGrammarOf(tool) !== "name")
return false;
const face = nameRuleFaceOf(tool);
const key = contentKeyOf(content);
if (face.words !== undefined)
return key === face.param;
if (face.exact === true)
return key === undefined;
return key === undefined || face.params?.includes(key) === true || !(catalogRuleFaceOf(tool)?.params ?? []).includes(key);
}
export function mountedValueArmOf(text, tool, behavior) {
if (behavior === "allow" || nameRuleFaceOf(tool)?.values !== "mounted")
return undefined;
const parsed = parseRuleText(text, behavior);
return "rule" in parsed && parsed.rule.match === "name" ? parsed.rule : undefined;
}
export function mountedOnlyArgumentOf(tool, content, mountedParams) {
if (nameRuleFaceOf(tool)?.values !== "mounted" || mountedParams === undefined)
return undefined;
const key = contentKeyOf(content);
return key !== undefined && mountedParams.includes(key) && !(catalogRuleFaceOf(tool)?.params ?? []).includes(key) ? key : undefined;
}
export function mountedArgumentArmOf(rule) {
const face = nameRuleFaceOf(rule.tool);
if (rule.behavior === "allow" || rule.match !== "name" || face?.values !== "mounted")
return undefined;
const key = contentKeyOf(rule.command);
if (key === undefined || face.params?.includes(key) === true || !(catalogRuleFaceOf(rule.tool)?.params ?? []).includes(key))
return undefined;
const pattern = rule.command.slice(rule.command.indexOf(":") + 1).trim();
return pattern === "" ? undefined : { param: key, pattern };
}
function contentKeyOf(content) {
const colon = content.indexOf(":");
return colon > 0 ? content.slice(0, colon).trim() : undefined;
}
export const DOMAIN_RULE_MARKER = "domain:";
export function ruleToolGrammarOf(toolName) {
if (toolName === COMMAND_RULE_TOOL)
return "command";
if (Object.hasOwn(NAME_RULE_FACES, toolName))
return "name";
if (hostTargetOf(toolName) !== undefined)
return "domain";
return pathTargetOf(toolName) !== undefined ? "path" : undefined;
}
export function cardRuleFormOf(toolName, call = { toolName }) {
if (toolName === COMMAND_RULE_TOOL)
return "command";
const grammar = ruleToolGrammarOf(toolName);
if (grammar === "name" || grammar === "domain")
return grammar;
return grammar === undefined && ruleWordsOf(call, "deny").length === 0 && ruleWordsOf({ toolName }, "deny").length === 0 ? "tool" : undefined;
}
export function shellRuleParamOf(tool, content) {
const colon = content.indexOf(":");
if (colon <= 0)
return undefined;
const param = content.slice(0, colon).trim();
return /^[A-Za-z_][A-Za-z0-9_]*$/.test(param) && (catalogRuleFaceOf(tool)?.params ?? []).includes(param) ? param : undefined;
}
export function ruleLaneOf(tool, content) {
if (isShellFaceToolName(tool))
return shellRuleParamOf(tool, content) !== undefined && !(tool === COMMAND_RULE_TOOL && content.endsWith(":*")) ? "param" : "command";
switch (ruleToolGrammarOf(tool)) {
case "name":
return nameGrammarReads(tool, content) ? "name" : "param";
case "path":
return content.indexOf(":") <= 0 ? "path" : "param";
case "domain":
return content.startsWith(DOMAIN_RULE_MARKER) ? "domain" : "param";
default:
return "param";
}
}
export function nameRuleSubjectOf(toolName, args, behavior = "deny") {
const face = Object.hasOwn(NAME_RULE_FACES, toolName) ? NAME_RULE_FACES[toolName] : undefined;
if (face === undefined || typeof args !== "object" || args === null || Array.isArray(args) || !Object.hasOwn(args, face.param))
return undefined;
const a = args;
const carries = (keys) => keys !== undefined && keys.some((k) => Object.hasOwn(a, k) && typeof a[k] === "string" && a[k] !== "");
if (carries(face.yieldsTo?.subject) || (behavior === "allow" && carries(face.yieldsTo?.grant)))
return undefined;
const value = a[face.param];
return typeof value === "string" && value !== "" ? value : undefined;
}
export function ruleWordsOf(req, behavior) {
if (behavior === "allow") {
const word = ruleWordOf(wireOf("policy.persistedGrammar.grant", req));
return word === undefined ? [] : [word];
}
const words = heldValues("policy.persistedGrammar", { get: ruleWordOf }, req);
return words.length <= 1 ? words : [...new Set(words)];
}
function ruleWordOf(name) {
if (isShellFaceToolName(name))
return COMMAND_RULE_TOOL;
return ruleToolGrammarOf(name) !== undefined ? name : undefined;
}
export const PERSISTED_RULE_MATCHES = Object.freeze(["exact", "prefix", "wildcard", "pattern", "subpath", "path", "name", "name_prefix", "host", "subdomain", "tool", "call"]);
function isCommandMatch(match) {
return match === "exact" || match === "prefix" || match === "wildcard" || match === "pattern";
}
export const RULE_ADD_ORIGINS = Object.freeze(["user", "imported-cc", "starter"]);
export function isAddStamp(v) {
return typeof v === "string" && v.length > 0 && v.length <= A2A_FINGERPRINT_MAX_CHARS;
}
export function addStampsOf(adds) {
const stamps = [...new Set(adds.flatMap((a) => (Object.hasOwn(a, "peerFingerprint") && typeof a.peerFingerprint === "string" && a.peerFingerprint !== "" ? [a.peerFingerprint] : [])))];
return stamps.length > 0 ? { peerFingerprints: Object.freeze(stamps) } : {};
}
export const MAX_RULE_TEXT_CHARS = 512;
export const NOTICE_RULE_TEXT_MAX = 2 * MAX_RULE_TEXT_CHARS;
export function noticeRuleText(text) {
if (text.length <= NOTICE_RULE_TEXT_MAX)
return text;
let kept = 0;
let end = 0;
for (const ch of text) {
if (kept === NOTICE_RULE_TEXT_MAX)
return `${text.slice(0, end)}${INLINE_TRUNCATION_MARK}`;
end += ch.length;
kept += 1;
}
return text;
}
export const BARE_INTERPRETER_NAMES = new Set([
"node", "deno", "bun", "python", "python2", "python3", "perl", "ruby", "php", "osascript",
"java", "dotnet", "mono", "lua", "luajit", "julia", "Rscript", "tclsh", "groovy", "scala",
"awk", "gawk", "mawk", "nawk", "sed",
"bash", "sh", "zsh", "ksh", "csh", "tcsh", "dash", "fish", "cmd", "powershell", "pwsh", "busybox",
"source", ".", "trap", "eval", "exec", "command", "builtin",
"enable", "compgen", "complete", "bind", "mapfile", "readarray",
"env", "noglob", "nocorrect", "xargs",
"sudo", "doas", "pkexec", "su", "runuser", "ssh", "chroot", "setpriv",
"nohup", "nice", "ionice", "chrt", "taskset", "stdbuf", "timeout", "time", "watch",
"setsid", "flock", "unshare", "nsenter", "script", "numactl", "prlimit", "systemd-run", "parallel",
"strace", "ltrace", "valgrind", "gdb", "lldb", "perf", "firejail", "bwrap",
]);
export const SHELL_RESERVED_WORDS = shellWordsOf("rule_reserved");
export const SUGGESTION_LEXICON = [
"git stash list", "git stash show", "git stash push", "git stash pop", "git stash drop",
"git remote show", "git worktree list", "git submodule update", "git submodule status",
"yarn workspaces list",
"go mod tidy", "go mod download", "go mod verify", "go mod graph",
"docker compose up", "docker compose down", "docker compose ps", "docker compose logs",
"docker compose build", "docker compose pull",
"kubectl rollout status", "kubectl rollout restart", "kubectl rollout history",
"gh pr view", "gh pr list", "gh pr diff", "gh pr checks", "gh pr status", "gh pr create",
"gh pr merge", "gh issue view", "gh issue list", "gh issue create", "gh repo view",
"gh repo clone", "gh release list", "gh release view", "gh run list", "gh run view",
"gh run watch", "gh workflow list", "gh workflow view", "gh auth status",
"gh search repos", "gh search issues", "gh search prs", "gh search code",
];
const LEXICON_BODIES = SUGGESTION_LEXICON.map((b) => b.split(" "));
const GENERIC_SUBCOMMAND_WORD = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
const SCREENED_HEAD_NAMES = new Set([...BARE_INTERPRETER_NAMES].map((n) => n.toLowerCase()));
const TRAILING_DOTS = /\.+$/;
const EXECUTABLE_SUFFIX = /\.(?:exe|com|bat|cmd)$/;
const PROPOSABLE_HEAD_NAME = /^[A-Za-z0-9._+-]+$/;
function screenedHeadName(head) {
let name = commandBasename(head).toLowerCase();
for (;;) {
const peeled = name.replace(TRAILING_DOTS, "").replace(EXECUTABLE_SUFFIX, "");
if (peeled === name || peeled === "")
return name;
name = peeled;
}
}
function genericPrefixBody(tokens) {
const head = tokens[0];
const second = tokens[1];
if (head === undefined || second === undefined)
return undefined;
if (!PROPOSABLE_HEAD_NAME.test(commandBasename(head)))
return undefined;
if (SCREENED_HEAD_NAMES.has(screenedHeadName(head)))
return undefined;
return GENERIC_SUBCOMMAND_WORD.test(second) ? `${head} ${second}` : undefined;
}
function longestReviewedBody(tokens) {
let best;
for (const words of LEXICON_BODIES) {
if (words.length > tokens.length)
continue;
if (best !== undefined && words.length <= best.length)
continue;
let hit = true;
for (let i = 0; i < words.length; i++) {
if (words[i] !== tokens[i]) {
hit = false;
break;
}
}
if (hit)
best = words;
}
return best?.join(" ");
}
export function foldSpacing(s) {
let out = "";
let quote;
let pendingGap = false;
for (const ch of s) {
if (quote !== undefined) {
if (ch === quote)
quote = undefined;
out += ch;
continue;
}
if (ch === "'" || ch === '"') {
if (pendingGap) {
if (out !== "")
out += " ";
pendingGap = false;
}
quote = ch;
out += ch;
continue;
}
if (ch === " " || ch === "\t") {
pendingGap = true;
continue;
}
if (pendingGap) {
if (out !== "")
out += " ";
pendingGap = false;
}
out += ch;
}
return quote === undefined ? out : undefined;
}
function reject(code, message) {
return { reject: { code, message } };
}
const RULE_LANE_FLOOR = {
pathPrefixedNameIsText: true,
quotedOperatorsAreText: true,
};
const MATCH_READING = { terminator: "keep", redirection: "reject" };
const OFFER_READING = { terminator: "keep", redirection: "tolerate" };
function commandBasename(name) {
return name.slice(name.lastIndexOf("/") + 1);
}
function ruleLaneShapeOf(shell, command, reading) {
const split = splitShellCompoundSegments(shell, command, { trailingTerminator: reading.terminator, redirection: reading.redirection });
if ("reject" in split)
return { reject: split.reject };
const names = [];
for (const segment of split.segments) {
if (reading.redirection === "tolerate" && carriesShellRedirection(segment)) {
names.push(undefined);
continue;
}
const floor = parseLeadingCommandName(segment, RULE_LANE_FLOOR);
if ("reject" in floor) {
return {
reject: split.segments.length > 1
? `the segment "${escapeForDisclosure(segment.trim())}" is not a single simple command (${floor.reject})`
: floor.reject,
};
}
if (SHELL_RESERVED_WORDS.has(floor.name)) {
return {
reject: `"${escapeForDisclosure(floor.name)}" is a shell keyword, not a command name — a control structure's real program is not named by the text this lane compares`,
};
}
names.push(floor.name);
}
return { segments: split.segments, names };
}
const CONTROL_CHARS_RE = new RegExp(`[${UNRENDERABLE_CHAR_CLASS}]`, "u");
const CONTROL_CHARS_GLOBAL_RE = new RegExp(CONTROL_CHARS_RE.source, "gu");
const RULE_SPELLING_REJECT_BYTES = "<>";
const RULE_SPELLING_REJECT_RE = new RegExp(`[${RULE_SPELLING_REJECT_BYTES}]`);
function valueRuleFormsSentence() {
return Object.keys(NAME_RULE_FACES)
.map((tool) => `${nameRuleFormOf(tool)} rules`)
.join(", ");
}
function nameRuleFormOf(tool) {
return formatRuleText(`<${nameRuleFaceOf(tool).param}>`, "name", tool);
}
export function ruleShapeOf(call, behavior) {
if (isShellFaceToolName(call.toolName))
return formatRuleText("<command>", "exact", COMMAND_RULE_TOOL);
switch (ruleToolGrammarOf(call.toolName)) {
case "name":
return nameRuleFormOf(call.toolName);
case "domain":
return formatRuleText("<host>", "host", call.toolName);
case "path": {
if (behavior !== "allow")
return formatRuleText("<path>", "path", call.toolName);
const face = callPathFaceOf(call) ?? (pathTargetOf(call.toolName)?.access === "read" ? "read" : "write");
const word = Object.keys(PATH_RULE_FACES).find((tool) => PATH_RULE_FACES[tool].allow?.includes(face) === true) ?? call.toolName;
return formatRuleText("<path>", "path", word);
}
default:
return undefined;
}
}
const DISCLOSED_RULE_TEXT_MAX_CHARS = 120;
export function escapeForDisclosure(value) {
let text;
try {
text = typeof value === "string" ? value : String(value);
}
catch {
return "<unprintable>";
}
const { words: escaped, dropped } = boundedForeignReading(text, DISCLOSED_RULE_TEXT_MAX_CHARS, (part) => foreignWords(part, ESCAPED));
return escaped.length <= DISCLOSED_RULE_TEXT_MAX_CHARS && !dropped ? escaped : `${cutAt(escaped, DISCLOSED_RULE_TEXT_MAX_CHARS)}…`;
}
const escapeControl = (ch) => {
const cp = ch.codePointAt(0) ?? 0;
const hex = cp.toString(16).toUpperCase();
return cp > 0xffff ? `\\u{${hex}}` : `\\u${hex.padStart(4, "0")}`;
};
const escapeControls = (text) => text.replace(CONTROL_CHARS_GLOBAL_RE, escapeControl);
const ESCAPED = {
read: (text) => {
const start = new Int32Array(text.length);
const end = new Int32Array(text.length);
let spelled = "";
for (let i = 0; i < text.length;) {
const ch = String.fromCodePoint(text.codePointAt(i));
const piece = escapeControls(ch);
for (let k = 0; k < ch.length; k++)
[start[i + k], end[i + k]] = [spelled.length, spelled.length + piece.length];
spelled += piece;
i += ch.length;
}
return { spelled, place: (from, to) => [start[from], end[to - 1]] };
},
part: escapeControls,
};
const DISPLAY_STRIP_FORMAT_RE = /\p{Cf}/gu;
export function stripFormatCharacters(text) {
return text.replace(DISPLAY_STRIP_FORMAT_RE, "");
}
export function renderUntrustedCommandText(text, maxLen = DISCLOSED_RULE_TEXT_MAX_CHARS) {
let raw;
try {
raw = typeof text === "string" ? text : String(text);
}
catch {
return "<unprintable>";
}
return inlineUntrusted(stripFormatCharacters(raw), maxLen);
}
export function hasUnrenderableCharacters(text) {
return CONTROL_CHARS_RE.test(text);
}
export function ruleContentOf(parsed) {
const content = parsed.ruleContent;
return content === undefined || (parsed.toolName === COMMAND_RULE_TOOL && foldSpacing(content) === "*") ? undefined : content;
}
export function respelledRuleTextOf(text, behavior) {
const outer = text.trim();
const open = outer.indexOf("(");
const candidate = open < 0 ? outer : outer.slice(0, open).replace(/\s+$/u, "") + outer.slice(open);
if (candidate === text || ruleSpellingOf(candidate) === undefined)
return undefined;
const read = parseRuleText(candidate, behavior);
return "rule" in read ? read.rule.rule : undefined;
}
function parseWholeToolRule(text, tool, behavior) {
const peer = a2aWholePeerName(tool);
if (peer !== undefined) {
const peerFault = toolNameFaultOf(peer, catalogToolNames(), behavior);
if (peerFault === undefined)
return { rule: { behavior, rule: peer, tool: peer, match: "tool", command: "" } };
const said = peerFault.reason === "near_miss" ? `; write "${peerFault.currentName}"` : peerFault.guidance !== undefined ? ` — ${peerFault.guidance}` : "";
return reject("unsupported.tool", `"${text}" names no peer under that name${said}`);
}
if (!isNameableToolName(tool) || namesASetOfTools(tool, behavior)) {
return reject("invalid.grammar", `"${text}" names a set of tools — a stored rule names one tool, or one tool's content; a set of tools is a list entry's form`);
}
const fault = toolNameFaultOf(tool, catalogToolNames(), behavior);
if (fault === undefined)
return { rule: { behavior, rule: tool, tool, match: "tool", command: "" } };
const why = fault.reason === "near_miss"
? `is another spelling of "${fault.currentName}", which the engine's tool catalog names — a rule names its tool exactly as spelled, so this one would reach no tool`
: `names no tool under that name${fault.guidance !== undefined ? ` — ${fault.guidance}` : ""}`;
const write = fault.currentName !== undefined && !why.includes(`"${fault.currentName}"`) ? `; write "${fault.currentName}"` : "";
return reject("unsupported.tool", `"${text}" ${why}${write}`);
}
export function a2aWholePeerName(name) {
const split = splitNamespacedName(name);
if (split === undefined || split.ns.id !== A2A_NAMESPACE.id || split.peer === "" || split.peer.includes("*") || (split.tool !== undefined && split.tool !== "*"))
return undefined;
return `${split.ns.prefix}${split.peer}`;
}
export function a2aPeerRuleOf(name) {
const split = splitNamespacedName(name);
if (split === undefined || split.ns.id !== A2A_NAMESPACE.id || split.peer === "" || split.peer.includes("*"))
return undefined;
return split.tool === undefined || split.tool === "*" || (split.tool !== "" && !split.tool.includes("*")) ? `${split.ns.prefix}${split.peer}` : undefined;
}
export function parseRuleText(text, behavior) {
if (!isRuleBehavior(behavior)) {
return reject("invalid.grammar", `rule "${text}" was parsed under behavior ${escapeForDisclosure(behavior)}, which is not one of ${RULE_BEHAVIORS.join("/")}`);
}
const tighten = behavior !== "allow";
if (text.length > MAX_RULE_TEXT_CHARS) {
return reject("invalid.too_long", `rule text exceeds ${MAX_RULE_TEXT_CHARS} characters`);
}
if (CONTROL_CHARS_RE.test(text)) {
return reject("invalid.control_chars", "rule text contains control characters (C0/C1, tab excepted), which cannot be part of a readable command");
}
const parsed = ruleSpellingOf(text);
if (parsed === undefined) {
const respelling = respelledRuleTextOf(text, behavior);
return {
reject: {
code: "invalid.grammar",
message: `"${text}" is not a rule spelling — a rule is the tool's name exactly as spelled, then its content in parentheses (no blank, tab or invisible character around the rule or before the "("), and the ")" ends it${respelling !== undefined ? `; spelled as a rule it is "${respelling}"` : ""}`,
...(respelling !== undefined ? { respelling } : {}),
},
};
}
const ruleContent = ruleContentOf(parsed);
if (ruleContent === undefined)
return parseWholeToolRule(text, parsed.toolName, behavior);
if (parsed.toolName !== COMMAND_RULE_TOOL) {
const grammar = ruleToolGrammarOf(parsed.toolName);
if (grammar === undefined) {
return reject("unsupported.tool", `only Bash command rules, rules on path-targeting tools (Read, Edit, Write, …), ${valueRuleFormsSentence()} and WebFetch domain rules have a rule form in this version (got "${parsed.toolName}")`);
}
if (!tighten && RULE_SPELLING_REJECT_RE.test(ruleContent)) {
return reject("invalid.grammar", `"${text}" carries a shell redirection byte (${[...RULE_SPELLING_REJECT_BYTES].map((b) => `\`${b}\``).join(" or ")}) — no path, name or host allow spells one, and the grant would not read as what it looks like`);
}
if (grammar === "name")
return parseNameRuleContent(text, parsed.toolName, ruleContent, behavior);
if (grammar === "domain")
return parseDomainRuleContent(text, parsed.toolName, ruleContent, behavior);
return parsePathRuleContent(text, parsed.toolName, ruleContent, behavior);
}
const content = foldSpacing(ruleContent) ?? ruleContent;
const prefixBody = /^(.+):\*$/.exec(content)?.[1];
let match;
let body;
if (prefixBody !== undefined) {
if (prefixBody.includes("*")) {
return reject("unsupported.wildcard", `a \`*\` inside a prefix body matches anything at that position, which has no rule form in this version ("${text}")`);
}
match = "prefix";
body = prefixBody;
}
else {
const wildcardBody = /^(.+) \*$/.exec(content)?.[1];
if (wildcardBody !== undefined && !hasUnescapedStar(wildcardBody)) {
match = "wildcard";
body = wildcardBody;
}
else if (hasUnescapedStar(content)) {
match = "pattern";
body = content;
}
else {
match = "exact";
body = content;
}
}
if (match === "pattern" && !tighten && hasUnescapedStar(body.trim().split(/[ \t]+/)[0] ?? "")) {
return reject("unsupported.wildcard", `a \`*\` in the first word of "${text}" leaves open which program runs — an allow names the program it grants (a deny or an ask may leave it open)`);
}
const shape = ruleLaneShapeOf(SHELL_LEX_DIALECT, match === "pattern" ? globFloorText(body) : body, MATCH_READING);
if ("reject" in shape) {
if (body.trim() === "" || shape.reject === "empty command")
return reject("invalid.empty_command", `rule "${text}" names no command`);
if (foldSpacing(body) === undefined)
return reject("invalid.unbalanced_quotes", `rule "${text}" has unbalanced quoting, so the command it names is not determinable`);
return reject("invalid.not_simple_command", `rule "${text}" is not a command this lane can name (${shape.reject})`);
}
if ((match === "wildcard" || match === "pattern") && shape.segments.length > 1) {
return reject("unsupported.wildcard", match === "pattern"
? `a \`*\` pattern names one command, and "${text}" names more than one — a compound is covered segment by segment, and the compound-prefix form spells a chain with the \`:*\` marker`
: `a \`<chain> *\` pattern names more than one command, and the trailing-star form has no chain reading — the compound-prefix form spells one with the \`:*\` marker ("${text}")`);
}
const head = commandBasename(shape.names[shape.names.length - 1] ?? "");
if (match !== "exact" && BARE_INTERPRETER_NAMES.has(head) && !tighten) {
return reject("invalid.bare_interpreter_prefix", shape.segments.length > 1
? `prefix rule "${text}" leaves its final segment open-ended under the interpreter "${head}" — such a rule authorizes running arbitrary programs, which one approval click cannot be read as having granted (an exact rule naming the whole command line is accepted)`
: `${match === "pattern" ? "pattern" : "prefix"} rule "${text}" is headed by the interpreter "${head}" — such a rule authorizes running arbitrary programs, which one approval click cannot be read as having granted (an exact rule naming the whole command line is accepted)`);
}
const command = foldSpacing(body);
if (command === undefined) {
return reject("invalid.unbalanced_quotes", `rule "${text}" has unbalanced quoting, so the command it names is not determinable`);
}
return { rule: { behavior, rule: formatRuleText(command, match, COMMAND_RULE_TOOL), tool: COMMAND_RULE_TOOL, match, command } };
}
export function formatRuleText(command, match, tool) {
if (match === "tool")
return tool;
if (match === "call")
return `${tool}(<call sha256:${createHash("sha256").update(command).digest("hex")}>)`;
if (match === "subpath")
return `${tool}(//${command.startsWith("/") ? command.slice(1) : command}/**)`;
if (match === "path")
return `${tool}(${command})`;
if (match === "name")
return `${tool}(${keyedValueParamOf(tool) !== undefined ? `${keyedValueParamOf(tool)}:` : ""}${command})`;
if (match === "host" || match === "subdomain")
return `${tool}(domain:${spelledHostPattern({ match, host: command })})`;
return `${tool}(${command}${match === "prefix" || match === "name_prefix" ? ":*" : match === "wildcard" ? " *" : ""})`;
}
function parseNameRuleContent(text, tool, content, behavior, verifyCanonical = true) {
const face = NAME_RULE_FACES[tool];
if (face?.words !== undefined)
return parseKeyedValueRuleContent(text, tool, content, behavior, { param: face.param, words: face.words });
if (face?.values === "mounted")
return parseMountedValueRuleContent(text, tool, content, behavior, face);
const bare = content.startsWith("/") ? content.slice(1) : content;
const colon = bare.indexOf(":");
if (face !== undefined && colon > 0 && face.params?.includes(bare.slice(0, colon).trim()) === true) {
return reject("invalid.grammar", `"${text}" is the parameter spelling, which a ${tool} rule no longer has — a ${tool} rule names the ${face.param} itself: ${tool}(<name>) for one, ${tool}(<name>:*) for every ${face.param} whose name begins with <name>`);
}
const open = bare.endsWith(":*") || bare.endsWith(" *");
if (open && face?.exact === true) {
return reject("unsupported.wildcard", `"${text}" names ${tool} ${face.param}s by a prefix, which has no rule form — a ${tool} rule names one ${face.param} exactly: ${tool}(<${face.param}>); the whole tool is the bare ${tool} entry`);
}
if (colon >= 0 && face?.exact === true) {
return reject("invalid.grammar", `"${text}" is not a ${tool} ${face.param} (a ${face.param} carries no \`:\`) — a rule on one of the call's arguments, ${tool}(<argument>:<pattern>), is read by a tool policy's parameter rules and has no stored or listed form; a ${tool} rule names one ${face.param} exactly: ${tool}(<${face.param}>)`);
}
const body = open ? bare.slice(0, -2) : bare;
const match = open ? "name_prefix" : "name";
if (body === "" || body.trim() !== body) {
return reject("invalid.grammar", `the name in "${text}" is empty or carries surrounding whitespace — a ${tool} rule names the ${face?.param ?? "name"} exactly as it is spelled, so the spelling must be the one meant`);
}
if (body.includes("*")) {
return reject("unsupported.wildcard", `a \`*\` inside the name of "${text}" matches anything at that position, which has no rule form — only the trailing \`<name>:*\` prefix form does`);
}
const canonical = formatRuleText(body, match, tool);
const back = parsePermissionRule(canonical);
if (back.toolName !== tool || back.ruleContent !== (open ? `${body}:*` : body)) {
return reject("invalid.grammar", `the name in "${text}" has no spelling the rule syntax reads back (a backslash or an unbalanced parenthesis) — no ${tool} rule can name it`);
}
const rule = { behavior, rule: canonical, tool, match, command: body };
if (verifyCanonical) {
const again = parseNameRuleContent(canonical, tool, back.ruleContent, behavior, false);
if (!("rule" in again) || again.rule.rule !== canonical || again.rule.match !== match || again.rule.command !== body) {
return reject("invalid.grammar", `"${text}" has no canonical spelling that reads back as itself ("${canonical}" does not) — no ${tool} rule can name it exactly`);
}
}
return { rule };
}
function parseKeyedValueRuleContent(text, tool, content, behavior, face) {
const vocabulary = face.words.join(", ");
const colon = content.indexOf(":");
if (colon <= 0 || content.slice(0, colon).trim() !== face.param) {
return reject("invalid.grammar", `${tool} rules name the ${face.param}: ${tool}(${face.param}:<${face.param}>) with one of ${vocabulary} ("${text}") — a bare value, or a rule on another argument, has no rule form here`);
}
const word = content.slice(colon + 1).trim();
if (word === "") {
return reject("invalid.grammar", `the ${face.param} in "${text}" is empty — ${tool} rules name the ${face.param} exactly as the call carries it`);
}
if (word.includes("*")) {
return reject("unsupported.wildcard", `a \`*\` in the ${face.param} of "${text}" has no rule form — ${tool} rules name one ${face.param} (${vocabulary}); the whole tool is the bare ${tool} entry`);
}
if (!face.words.includes(word)) {
return reject("invalid.grammar", `"${word}" is not one of the ${face.param} values ${tool} declares (${vocabulary}) — the rule would never reach a call`);
}
return { rule: { behavior, rule: formatRuleText(word, "name", tool), tool, match: "name", command: word } };
}
function parseMountedValueRuleContent(text, tool, content, behavior, face) {
const form = nameRuleFormOf(tool);
const key = contentKeyOf(content);
if (key !== undefined && face.params?.includes(key) === true) {
return reject("invalid.grammar", `"${text}" is the parameter spelling, which a ${tool} rule does not have — a ${tool} rule names the ${face.param} itself: ${form}`);
}
if (behavior === "allow" && key !== undefined && (catalogRuleFaceOf(tool)?.params ?? []).includes(key)) {
return reject("invalid.grammar", `"${text}" is a rule on the ${key} argument, which an allow entry has no form for — an allow reads one way, so it does not grant the ${face.param} "${content}" either; a ${tool} allow names the ${face.param}: ${form}`);
}
if (content === "" || content.trim() !== content) {
return reject("invalid.grammar", `the ${face.param} in "${text}" is empty or carries surrounding whitespace — a ${tool} rule names the ${face.param} exactly as it is spelled, so the spelling must be the one meant`);
}
if (content.includes("*")) {
return reject("unsupported.wildcard", `a \`*\` in the ${face.param} of "${text}" has no rule form — a ${tool} rule names one ${face.param} (${form}); the whole tool is the bare ${tool} entry`);
}
const canonical = formatRuleText(content, "name", tool);
const back = parsePermissionRule(canonical);
if (back.toolName !== tool || back.ruleContent !== content) {
return reject("invalid.grammar", `the ${face.param} in "${text}" has no spelling the rule syntax reads back (a backslash or an unbalanced parenthesis) — no ${tool} rule can name it`);
}
return { rule: { behavior, rule: canonical, tool, match: "name", command: content } };
}
export function nameRuleAdmits(rule, name) {
if (rule.match === "name")
return name === rule.command;
if (rule.match === "name_prefix")
return name.startsWith(rule.command);
return false;
}
export function nameRuleReach(rule, name, values) {
const face = nameRuleFaceOf(rule.tool);
if (face?.values !== "mounted")
return nameRuleAdmits(rule, name) ? "reached" : "not_reached";
if (rule.match !== "name")
return "not_reached";
if (values === undefined)
return "unread";
const operand = canonicalNameValue(rule.command, values);
const called = canonicalNameValue(name, values);
if (operand.kind === "ambiguous")
return rule.behavior !== "allow" && called.kind === "entry" && operand.entries.includes(called.entry) ? "reached" : "not_reached";
if (operand.kind === "entry" && called.kind === "entry")
return operand.entry === called.entry ? "reached" : "not_reached";
return name === rule.command ? "reached" : "not_reached";
}
export function nameValuesUnreadReason(tool) {
const face = nameRuleFaceOf(tool);
return `the ${tool} tool this call reaches declares no ${face?.param ?? "value"} values it offers, so which ${face?.param ?? "value"} the call runs cannot be read against a ${tool} rule`;
}
function parseDomainRuleContent(text, tool, content, behavior) {
if (!content.startsWith(DOMAIN_RULE_MARKER)) {
return reject("invalid.grammar", `a ${tool} rule names a host: ${tool}(domain:<host>) for one, ${tool}(domain:*.<domain>) for every host below a domain ("${text}")`);
}
const read = readHostPattern(content.slice(DOMAIN_RULE_MARKER.length));
if ("problem" in read) {
switch (read.problem) {
case "every_host":
return reject("unsupported.wildcard", `"${text}" names every host — the whole tool is the bare ${tool} entry`);
case "inner_wildcard":
return reject("unsupported.wildcard", `a \`*\` in "${text}" other than a leading \`*.\` has no rule form — a ${tool} rule names one host, or every host below one domain with \`*.<domain>\``);
case "empty_label":
return reject("invalid.grammar", `"${text}" names a host with an empty label (a leading or doubled dot) — write the host itself, and every host below a domain as \`*.<domain>\``);
case "not_a_host":
return reject("invalid.grammar", `"${text}" does not name a host alone — write the host as a URL carries it, with no scheme, path, port or credentials`);
case "below_address":
return reject("invalid.grammar", `"${text}" names hosts below an address, which has none — write the address itself`);
}
}
const { match, host } = read.pattern;
return { rule: { behavior, rule: formatRuleText(host, match, tool), tool, match, command: host } };
}
export function domainRuleAdmits(rule, host) {
return (rule.match === "host" || rule.match === "subdomain") && hostPatternAdmits({ match: rule.match, host: rule.command }, host);
}
function parsePathRuleContent(text, tool, content, behavior) {
if (behavior === "allow" && PATH_RULE_FACES[tool]?.allow === undefined) {
return reject("unsupported.tool", `an allow by path is read on Read and Edit rules only — an Edit rule speaks for every tool that edits a file, a Read rule for every tool that reads one ("${text}"); write Edit(<path>) or Read(<path>)`);
}
const back = parsePermissionRule(formatRuleText(content, "path", tool));
if (back.toolName !== tool || back.ruleContent !== content) {
return reject("invalid.grammar", `the path in "${text}" has no spelling the rule syntax reads back (two backslashes in a row, a backslash before a parenthesis, or one at the end) — no ${tool} rule can name it`);
}
const dirForm = /^\/\/(.+)\/\*\*$/.exec(content);
if (dirForm !== null) {
const bodyR = dirForm[1];
if (bodyR.includes("*")) {
return reject("unsupported.wildcard", `a \`*\` inside the directory body matches anything at that position, which has no rule form ("${text}")`);
}
const segments = bodyR.split("/");
if (segments.some((sg) => sg === "" || sg.trim() === "")) {
return reject("invalid.grammar", `the directory body of "${text}" is not in the canonical spelling (an empty or whitespace-only path segment) — the lexical normal form is the only accepted spelling`);
}
if (segments.some((sg) => sg === "." || sg === "..")) {
return reject("invalid.path_traversal", `the directory body of "${text}" carries a \`.\` or \`..\` segment — the lexical normal form is the only accepted spelling, and a traversal segment is not normalized away`);
}
const dir = "/" + bodyR;
return { rule: { behavior, rule: formatRuleText(dir, "subpath", tool), tool, match: "subpath", command: dir } };
}
if (content.trim() !== content || content === "") {
return reject("invalid.grammar", `the path pattern of "${text}" is empty or carries surrounding whitespace — the pattern is stored as spelled, so the spelling must be the one meant`);
}
if (content.startsWith("!"))
return reject("invalid.grammar", `a negated path pattern ("${text}") is not a rule — a rule says what it reaches, never what it excepts`);
if (/[?[\]{}]/.test(content)) {
return reject("unsupported.wildcard", `the path pattern of "${text}" carries a glob class (\`?\`, \`[]\`, \`{}\`) this lane does not read — only \`*\` inside a segment and \`**\` as a whole segment have a rule form`);
}
const body = content.startsWith("//") ? content.slice(2) : content.startsWith("~/") ? content.slice(2) : content.startsWith("/") ? content.slice(1) : content.startsWith("./") ? content.slice(2) : content;
const segments = body.split("/");
if (segments.some((sg) => sg === "" || sg.trim() === "")) {
return reject("invalid.grammar", `the path pattern of "${text}" carries an empty path segment — the lexical normal form is the only accepted spelling`);
}
if (segments.some((sg) => sg === "." || sg === "..")) {
return reject("invalid.path_traversal", `the path pattern of "${text}" carries a \`.\` or \`..\` segment — the lexical normal form is the only accepted spelling, and a traversal segment is not normalized away`);
}
if (segments.some((sg) => sg.includes("**") && sg !== "**")) {
return reject("unsupported.wildcard", `\`**\` in the path pattern of "${text}" must stand as a whole segment (\`a/**/b\`), never inside one`);
}
return { rule: { behavior, rule: formatRuleText(content, "path", tool), tool, match: "path", command: content } };
}
export const PATH_RULE_BASES = Object.freeze(["cwd", "root", "home"]);
export const PATH_RULE_BASE_LABEL = {
cwd: "the working directory",
root: "the project root",
home: "the home directory of the execution environment",
};
export function ruleBasesNeeded(rule) {
if (rule.match !== "path")
return [];
const pattern = rule.command;
if (pattern.startsWith("//"))
return [];
if (pattern.startsWith("~/"))
return ["home"];
if (pattern.startsWith("/"))
return ["root"];
return ["cwd"];
}
function baseValueOf(base, bases) {
switch (base) {
case "home":
return bases.home;
case "root":
return bases.root;
case "cwd":
return bases.cwd ?? bases.root;
default: {
const _exhaustive = base;
return _exhaustive;
}
}
}
function resolvePathPattern(pattern, bases, behavior, treeFamily = pathFamilyOf(bases)) {
const family = treeFamily;
const tree = bases.root ?? bases.cwd;
if (pattern.startsWith("//")) {
const abs = pathIdentityOf("/" + pattern.slice(2), family, tree);
return abs.ok ? { resolved: abs.identity, literal: 0 } : { missingBase: "root" };
}
const needed = ruleBasesNeeded({ match: "path", command: pattern })[0];
const baseValue = baseValueOf(needed, bases);
const base = baseValue !== undefined && isAbsoluteForFamily(family ?? "own", baseValue) ? pathIdentityOf(baseValue, family, tree) : undefined;
if (base === undefined || !base.ok)
return { missingBase: needed };
const normal = base.identity;
const rest = pattern.startsWith("~/") ? pattern.slice(2) : pattern.startsWith("/") ? pattern.slice(1) : pattern.startsWith("./") ? pattern.slice(2) : pattern;
const peeled = rest.endsWith("/**") ? rest.slice(0, -3) : rest;
const bare = needed === "cwd" && peeled !== "" && !peeled.includes("/");
const anyDepth = bare && (peeled === rest || behavior !== "allow");
const body = anyDepth ? "**/" + peeled : rest;
return { resolved: (normal === "/" ? "" : normal) + "/" + body, literal: normal.split("/").filter((sg) => sg !== "").length };
}
function firstGlobSegment(segments, literal) {
return segments.findIndex((sg, i) => i >= literal && sg.includes("*"));
}
export function isUsablePathBase(base, family) {
return base !== undefined && isAbsoluteForFamily(family ?? "own", base) && pathIdentityOf(base, family, base).ok;
}
export function unusableDeclaredBase(rule, declared) {
for (const base of ruleBasesNeeded(rule)) {
const key = base === "cwd" && declared.cwd === undefined ? "root" : base;
const value = declared[key];
if (value !== undefined && !isUsablePathBase(value, pathFamilyOf(declared)))
return { base, key };
}
return undefined;
}
function segmentGlobMatches(glob, segment) {
if (!glob.includes("*"))
return glob === segment;
const parts = glob.split("*");
let at = 0;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (i === 0) {
if (!segment.startsWith(part))
return false;
at = part.length;
continue;
}
if (i === parts.length - 1)
return part === "" || (segment.endsWith(part) && segment.length - part.length >= at);
const k = segment.indexOf(part, at);
if (k === -1)
return false;
at = k + part.length;
}
return true;
}
function globSegmentsReach(globs, target, literal) {
const seen = new Set();
const width = target.length + 1;
const walk = (g, t) => {
if (g === globs.length)
return true;
const key = g * width + t;
if (seen.has(key))
return false;
seen.add(key);
if (g < literal)
return t < target.length && globs[g] === target[t] && walk(g + 1, t + 1);
if (globs[g] === "**")
return walk(g + 1, t) || (t < target.length && walk(g, t + 1));
if (t >= target.length)
return false;
return segmentGlobMatches(globs[g], target[t]) && walk(g + 1, t + 1);
};
return walk(0, 0);
}
export function pathRuleReachOf(rule, target, bases, treeFamily = pathFamilyOf(bases)) {
if (rule.match === "subpath")
return directoryRuleAdmits(rule, target, bases, treeFamily) ? REACHED : NOT_REACHED;
if (rule.match !== "path")
return NOT_REACHED;
const family = treeFamily;
if (!isPathIdentity(target, family))
return NOT_REACHED;
const r = resolvePathPattern(rule.command, bases, rule.behavior, family);
if ("missingBase" in r) {
return { reach: "unreadable", reason: `the rule is relative to ${PATH_RULE_BASE_LABEL[r.missingBase]} and this call supplies no absolute \`${r.missingBase}\` base of this tree's family to resolve it against` };
}
if (isUncIdentity(r.resolved) !== isUncIdentity(target))
return NOT_REACHED;
const globs = r.resolved.split("/").filter((sg) => sg !== "");
const segments = target.split("/").filter((sg) => sg !== "");
return globSegmentsReach(globs, segments, r.literal) ? REACHED : NOT_REACHED;
}
export function pathRuleReachShapeOf(rule, bases) {
const family = pathFamilyOf(bases);
if (rule.match === "subpath") {
if (carriesDotSegment(rule.command, family))
return undefined;
const dir = pathIdentityOf(rule.command, family, bases.root ?? bases.cwd);
return dir.ok && isIdentityDir(dir.identity, family) && dir.identity.startsWith("/") && !isUncIdentity(dir.identity) ? { at: dir.identity, globs: [] } : undefined;
}
if (rule.match !== "path")
return undefined;
const r = resolvePathPattern(rule.command, bases, rule.behavior, family);
if ("missingBase" in r || !r.resolved.startsWith("/") || isUncIdentity(r.resolved))
return undefined;
const segments = r.resolved.split("/").filter((sg) => sg !== "");
const wild = firstGlobSegment(segments, r.literal);
const lead = wild === -1 ? segments : segments.slice(0, wild);
return { at: `/${lead.join("/")}`, globs: wild === -1 ? [] : segments.slice(wild) };
}
export function pathRuleHeadOf(rule, bases) {
const family = pathFamilyOf(bases);
const tree = bases.root ?? bases.cwd;
let head;
if (rule.match === "subpath") {
if (carriesDotSegment(rule.command, family))
return undefined;
const dir = pathIdentityOf(rule.command, family, tree);
return dir.ok && isIdentityDir(dir.identity, family) ? dir.identity : undefined;
}
else if (rule.match === "path") {
const r = resolvePathPattern(rule.command, bases, rule.behavior);
if ("missingBase" in r)
return undefined;
const segments = r.resolved.split("/").filter((sg) => sg !== "");
const wild = firstGlobSegment(segments, r.literal);
const lead = wild === -1 ? segments : segments.slice(0, wild);
head = lead.length === 0 ? "" : `${isUncIdentity(r.resolved) ? "//" : "/"}${lead.join("/")}`;
}
else
return undefined;
const id = pathIdentityOf(head === "" ? "/" : head, family, tree);
return id.ok ? id.identity : undefined;
}
function hasUnescapedStar(s) {
for (let i = 0; i < s.length; i++) {
if (s[i] !== "*")
continue;
let backslashes = 0;
for (let j = i - 1; j >= 0 && s[j] === "\\"; j--)
backslashes++;
if (backslashes % 2 === 0)
return true;
}
return false;
}
export function ruleAdmitsCommand(rule, command) {
return admitsUnder(SHELL_LEX_DIALECT, rule, command, MATCH_READING);
}
export function ruleAdmitsCommandIn(shell, rule, command) {
return admitsUnder(shell, rule, command, MATCH_READING);
}
export const PROGRAM_RUN_REACHES = Object.freeze(["reached", "not_reached", "unreadable"]);
const REACHED = { reach: "reached" };
const NOT_REACHED = { reach: "not_reached" };
export function programRunReachOf(rule, command) {
return programRunReachIn(SHELL_LEX_DIALECT, rule, command);
}
export function programRunReachIn(shell, rule, command) {
if (!isCommandMatch(rule.match))
return NOT_REACHED;
const lineReach = lineReachOf(rule);
return lineReach === undefined ? NOT_REACHED : withSubstitutedLines(readCommandMemo(shell, command), lineReach);
}
function lineReachOf(rule) {
if (rule.match === "pattern") {
const pattern = commandGlobOf(rule.command, "words");
return pattern === undefined ? undefined : (shape) => anySegmentReach(shape, (argv) => globArgvReachOf(pattern, argv));
}
const body = readRuleBody(rule.command);
if (body === undefined)
return undefined;
const exact = rule.match === "exact";
if (body.segments.length > 1)
return (shape) => chainReachOf(body, shape, exact);
const bodyArgv = body.segments[0].argv;
return (shape) => anySegmentReach(shape, (argv) => argvReachOf(bodyArgv, argv, exact));
}
function withSubstitutedLines(shape, lineReach) {
let outcome = lineReach(shape);
if (outcome.reach === "reached")
return REACHED;
for (const segment of shape.segments) {
for (const line of segment.substituted ?? []) {
const r = withSubstitutedLines(line, lineReach);
if (r.reach === "reached")
return REACHED;
if (r.reach === "unreadable" && outcome.reach === "not_reached")
outcome = r;
}
}
return outcome;
}
function chainReachOf(body, shape, exact) {
const broken = shape.segments.find((s) => s.unreadable !== undefined);
if (broken !== undefined)
return { reach: "unreadable", reason: broken.unreadable };
const hidden = shape.segments.find((s) => s.peelUnreadable !== undefined);
const shaped = !shape.grouped && shape.segments.length === body.segments.length && shape.connectors.every((c, i) => c === body.connectors[i]);
if (!shaped)
return hidden === undefined ? NOT_REACHED : { reach: "unreadable", reason: hidden.peelUnreadable };
let outcome = REACHED;
for (let i = 0; i < body.segments.length; i++) {
const bodyArgv = body.segments[i].argv;
const r = segmentReachOf(shape.segments[i], (argv) => argvReachOf(bodyArgv, argv, exact || i < body.segments.length - 1));
if (r.reach === "not_reached")
return hidden === undefined ? NOT_REACHED : { reach: "unreadable", reason: hidden.peelUnreadable };
if (r.reach === "unreadable")
outcome = r;
}
return outcome;
}
function anySegmentReach(shape, reachOf) {
let unreadable;
for (const segment of shape.segments) {
const r = segmentReachOf(segment, reachOf);
if (r.reach === "reached")
return REACHED;
if (r.reach === "unreadable")
unreadable ??= r;
}
return unreadable ?? NOT_REACHED;
}
export function ruleReachesProgramRun(rule, command) {
return programRunReachOf(rule, command).reach === "reached";
}
function segmentReachOf(segment, reachOf) {
if (segment.unreadable !== undefined)
return { reach: "unreadable", reason: segment.unreadable };
let unreadable;
for (const argv of [segment.argv, ...segment.peeled]) {
const r = reachOf(argv);
if (r.reach === "reached")
return REACHED;
if (r.reach === "unreadable")
unreadable ??= r;
}
if (segment.peelUnreadable !== undefined)
unreadable ??= { reach: "unreadable", reason: segment.peelUnreadable };
return unreadable ?? NOT_REACHED;
}
function argvReachOf(bodyArgv, argv, exact) {
for (let i = 0; i < bodyArgv.length; i++) {
const word = argv[i];
const wanted = bodyArgv[i];
if (word === undefined)
return NOT_REACHED;
if (word.expands !== false) {
if (word.raw === wanted.raw)
continue;
return { reach: "unreadable", reason: i === 0 ? "the program name is an expansion" : `an expansion (${word.raw}) stands where the rule reads a word` };
}
if (word.text !== wanted.text)
return NOT_REACHED;
}
if (!exact || argv.length === bodyArgv.length)
return REACHED;
const loose = argv.slice(bodyArgv.length).find((w) => w.expands === "many");
return loose === undefined ? NOT_REACHED : { reach: "unreadable", reason: `an unquoted expansion (${loose.raw}) may expand to nothing` };
}
const COARSE_ALLOW = Object.freeze({ action: "allow" });
export function coarseCallVerdictOf(lists, req) {
return coarseCommandVerdictOf(startedShellOfCall(req), lists, req.args?.command);
}
export function coarseCommandVerdictOf(shell, lists, command) {
if (typeof command !== "string")
return byCoarseDefault(lists.unreadable, "the call carries no command string");
const read = programNamesReachOf(lists, readCommandMemo(shell, command));
if (read.deny !== undefined)
return { action: "deny", listed: read.deny };
const answers = [];
if (read.ask !== undefined)
answers.push({ action: "ask", listed: read.ask });
if (read.unreadable !== undefined)
answers.push(byCoarseDefault(lists.unreadable, `the command cannot be read against the command-name lists (${read.unreadable})`));
if (lists.allow !== undefined && read.ask === undefined) {
const parsed = shell === SHELL_LEX_DIALECT ? parseLeadingCommandName(command) : { reject: PARSE_FAILURE_SENTENCE.started_shell_not_bash };
if ("reject" in parsed)
answers.push(byCoarseDefault(lists.unlisted, `command is not a single simple command (${parsed.reject})`));
else if (!lists.allow.includes(parsed.name))
answers.push(byCoarseDefault(lists.unlisted, `command "${parsed.name}" is not in the allowlist`));
}
return answers.reduce((held, next) => (RULE_BEHAVIOR_PRECEDENCE[next.action] < RULE_BEHAVIOR_PRECEDENCE[held.action] ? next : held), COARSE_ALLOW);
}
const byCoarseDefault = (word, why) => (word === "allow" ? COARSE_ALLOW : { action: word, cannotJudge: why });
const programBaseName = (word) => word.text.slice(word.text.lastIndexOf("/") + 1);
function programNamesReachOf(lists, shape) {
if (lists.deny.length === 0 && lists.ask.length === 0)
return {};
let deny;
let ask;
const operands = new Set();
const outcome = withSubstitutedLines(shape, (line) => anySegmentReach(line, (argv) => {
const word = argv[0];
if (word === undefined || operands.has(word))
return NOT_REACHED;
const lexed = lexedWordOf(word);
if (lexed?.assignment?.bare === true) {
if (lexed.atCommandPosition)
return NOT_REACHED;
for (const w of argv.slice(1))
operands.add(w);
}
if (word.expands !== false)
return { reach: "unreadable", reason: "the program name is an expansion" };
const name = programBaseName(word);
if (lists.deny.includes(name))
deny ??= name;
if (lists.ask.includes(name))
ask ??= name;
return NOT_REACHED;
}));
return { ...(deny !== undefined ? { deny } : {}), ...(ask !== undefined ? { ask } : {}), ...(outcome.reach === "unreadable" ? { unreadable: outcome.reason } : {}) };
}
const INVISIBLE_CHARACTER = /[\p{Cc}\p{Cf}]/u;
export function coarseCommandNameProblem(entry) {
if (typeof entry !== "string")
return "is not a string";
if (entry.length === 0)
return "is empty";
const unseen = INVISIBLE_CHARACTER.exec(entry);
if (unseen !== null)
return `carries a character that cannot be seen (U+${unseen[0].codePointAt(0).toString(16).toUpperCase().padStart(4, "0")} at index ${unseen.index}), so it never matches the name it shows`;
if (entry.includes("/"))
return "carries a path — the lists compare a program's base name, so write the name alone";
const named = parseLeadingCommandName(entry);
if ("reject" in named || named.name !== entry)
return "is not a bare program name (one word, with no quoting, expansion, operator, assignment or wildcard character)";
const shape = readShellCommandIn(SHELL_LEX_DIALECT, entry);
const only = shape.segments.length === 1 && !shape.grouped && !shape.backgrounded ? shape.segments[0] : undefined;
const word = only !== undefined && only.unreadable === undefined && only.redirections.length === 0 && only.substituted === undefined && only.argv.length === 1 ? only.argv[0] : undefined;
if (word === undefined || word.expands !== false || word.raw !== entry || word.text !== entry || lexedWordOf(word)?.assignment !== undefined) {
return "is not a bare program name (one word, with no quoting, expansion, operator, keyword or assignment)";
}
return undefined;
}
const GLOB_STAR = -1;
const GLOB_HOLE = -2;
const GLOB_GROUP = -3;
function globFloorText(body) {
let out = "";
for (let i = 0; i < body.length; i++) {
if (body[i] === "\\" && body[i + 1] === "*") {
out += "*";
i++;
}
else
out += body[i] === "*" ? "x" : body[i];
}
return out;
}
function commandGlobOf(body, reading) {
const shape = ruleLaneShapeOf(SHELL_LEX_DIALECT, globFloorText(body), MATCH_READING);
return "reject" in shape || shape.segments.length !== 1 ? undefined : compileGlob(reading === "words" ? unquotedPattern(body) : body);
}
function unquotedPattern(body) {
let out = "";
let quote;
for (const ch of body) {
if (quote === undefined ? ch === "'" || ch === '"' : ch === quote)
quote = quote === undefined ? ch : undefined;
else
out += ch;
}
return out;
}
function compileGlob(pattern) {
const nodes = [];
const groupEnds = new Map();
for (let i = 0; i < pattern.length; i++) {
const ch = pattern[i];
if (ch === "\\" && pattern[i + 1] === "*") {
nodes.push(0x2a);
i++;
continue;
}
if (ch === "/" && pattern.startsWith("**/", i + 1)) {
let j = i + 1;
while (pattern.startsWith("**/", j))
j += 3;
nodes.push(0x2f, GLOB_GROUP, GLOB_STAR, 0x2f);
groupEnds.set(nodes.length - 3, nodes.length);
i = j - 1;
continue;
}
nodes.push(ch === "*" ? GLOB_STAR : ch.charCodeAt(0));
}
return { nodes, groupEnds };
}
function globMatches(glob, subject, holes) {
const { nodes, groupEnds } = glob;
const m = nodes.length;
let live = new Uint8Array(m + 1);
let next = new Uint8Array(m + 1);
const close = (set) => {
for (let p = 0; p < m; p++) {
if (set[p] !== 1)
continue;
if (nodes[p] === GLOB_STAR || nodes[p] === GLOB_GROUP)
set[p + 1] = 1;
if (nodes[p] === GLOB_GROUP)
set[groupEnds.get(p)] = 1;
}
};
live[0] = 1;
close(live);
for (const unit of subject) {
next.fill(0);
if (unit === GLOB_HOLE && holes === "any")
next.fill(1, live.indexOf(1));
else {
for (let p = 0; p < m; p++) {
if (live[p] !== 1)
continue;
if (nodes[p] === GLOB_STAR)
next[p] = 1;
else if (nodes[p] === unit)
next[p + 1] = 1;
}
close(next);
}
if (next.indexOf(1) < 0)
return false;
[live, next] = [next, live];
}
return live[m] === 1;
}
function unitsOf(text) {
const out = [];
for (let i = 0; i < text.length; i++)
out.push(text.charCodeAt(i));
return out;
}
function globAdmitsSpelling(body, folded) {
const glob = commandGlobOf(body, "spelling");
return glob !== undefined && globMatches(glob, unitsOf(folded), "inside_star");
}
function globArgvReachOf(glob, argv) {
if (argv.length === 0)
return NOT_REACHED;
const subject = [];
let hole;
let anchored = false;
argv.forEach((w, k) => {
if (w.expands !== "many") {
if (anchored)
subject.push(0x20);
anchored = true;
}
if (w.expands === false)
subject.push(...unitsOf(w.text));
else {
subject.push(GLOB_HOLE);
hole ??= { at: k, raw: w.raw };
}
});
if (globMatches(glob, subject, "inside_star"))
return REACHED;
if (hole === undefined || !globMatches(glob, subject, "any"))
return NOT_REACHED;
return { reach: "unreadable", reason: hole.at === 0 ? "the program name is an expansion" : `an expansion (${hole.raw}) stands where the rule's pattern reads` };
}
export function commandPatternMayRun(body, programs) {
const lexed = lexShell(SHELL_LEX_DIALECT, body);
const words = "parseFailure" in lexed ? undefined : lexed.segments[0]?.tokens.filter((t) => t.kind === "word");
if (words === undefined || words.length === 0)
return true;
const position = readProgramPosition(words);
const program = position.program;
if (program === undefined || position.refusal !== undefined || hasUnescapedStar(words[program.index]?.raw ?? "*"))
return true;
const rest = words.slice(program.index + 1).map((w) => ` ${w.raw}`).join("");
const glob = compileGlob(unquotedPattern(`${program.basename}${rest}`.toLowerCase()));
return programs.some((p) => {
const head = unitsOf(p.toLowerCase());
return globMatches(glob, head, "inside_star") || globMatches(glob, [...head, 0x20, GLOB_HOLE], "any");
});
}
function readRuleBody(body) {
const shape = readShellCommandIn(SHELL_LEX_DIALECT, body);
if (shape.grouped || shape.stopped !== undefined || shape.segments.length === 0)
return undefined;
return shape.segments.every((s) => s.unreadable === undefined && s.argv.length > 0) ? shape : undefined;
}
let lastRead;
function readCommandMemo(shell, command) {
if (lastRead?.command !== command || lastRead.shell !== shell)
lastRead = { shell, command, shape: readShellCommandIn(shell, command) };
return lastRead.shape;
}
function admitsUnder(shell, rule, command, reading) {
if (!isCommandMatch(rule.match))
return false;
const shape = ruleLaneShapeOf(shell, command, reading);
if ("reject" in shape)
return false;
const folded = foldSpacing(command);
if (folded === undefined)
return false;
if (rule.match === "exact")
return folded === rule.command;
if (rule.match === "pattern")
return shape.segments.length === 1 && globAdmitsSpelling(rule.command, folded);
const bodyShape = ruleLaneShapeOf(SHELL_LEX_DIALECT, rule.command, reading);
if ("reject" in bodyShape || foldSpacing(rule.command) === undefined)
return false;
if (shape.segments.length !== bodyShape.segments.length)
return false;
return folded === rule.command || folded.startsWith(rule.command + " ");
}
export const EDITED_RULE_BREADTH_WARNING_CODES = Object.freeze(["broad_prefix", "compound_prefix"]);
export function ruleBreadthWarningsOf(rule) {
if (rule.match !== "prefix" && rule.match !== "wildcard" && rule.match !== "pattern")
return [];
const bodyShape = ruleLaneShapeOf(SHELL_LEX_DIALECT, rule.match === "pattern" ? globFloorText(rule.command) : rule.command, MATCH_READING);
if ("reject" in bodyShape)
return [];
if (bodyShape.segments.length > 1) {
return [
{
code: "compound_prefix",
message: `this rule's body is a command chain — it admits every run of the whole chain "${rule.command}", with any text extending its final segment`,
},
];
}
if (rule.command.split(/\s+/).filter((t) => t !== "").length === 1) {
return [
{
code: "broad_prefix",
message: `this rule admits every argument form of "${rule.command}" — any \`${rule.command} …\` command, not just the one on this card`,
},
];
}
return [];
}
export function pathWithinRoot(path, root) {
if (path === root)
return true;
const base = root.endsWith("/") ? root : root + "/";
return path.startsWith(base);
}
export function lexicalNormalAbsolutePathOf(path, family, anchor) {
const r = pathIdentityOf(path, family, anchor);
return r.ok ? r.identity : undefined;
}
function isLexicalNormalAbsoluteDir(path) {
if (typeof path !== "string" || !path.startsWith("/") || path.startsWith("//"))
return false;
const segments = path.split("/").slice(1);
return segments.length > 0 && segments.every((s) => s !== "" && s !== "." && s !== "..");
}
export function directoryRuleAdmits(rule, path, bases, treeFamily = pathFamilyOf(bases)) {
if (rule.match !== "subpath")
return false;
const family = treeFamily;
if (carriesDotSegment(rule.command, family))
return false;
const dir = pathIdentityOf(rule.command, family, bases.root ?? bases.cwd);
if (!dir.ok || !isIdentityDir(dir.identity, family))
return false;
if (!isPathIdentity(path, family) || !isIdentityDir(path, family))
return false;
return pathWithinRoot(path, dir.identity);
}
export function directoryIdentityOf(spelling, bases) {
const r = pathIdentityOf(spelling, pathFamilyOf(bases), bases.root ?? bases.cwd);
return r.ok ? r.identity : undefined;
}
const CD_TARGET_REJECT_CHARS = new RegExp(String.raw `[*?[\]{}$` + "`" + String.raw `\\'"\n\r` + RULE_SPELLING_REJECT_BYTES + "]");
function cdSegmentDirectoryOf(segment, opts) {
if (/[^\S \t]/.test(segment))
return undefined;
const tokens = segment.split(/[ \t]+/).filter((t) => t !== "");
if (tokens.length !== 2 || tokens[0] !== "cd")
return undefined;
const target = tokens[1];
if (CD_TARGET_REJECT_CHARS.test(target))
return undefined;
if (target.startsWith("-") || target.startsWith("~"))
return undefined;
let resolved;
if (target.startsWith("//"))
return undefined;
if (target.startsWith("/")) {
resolved = target;
}
else if (target.startsWith("./")) {
if (opts.priorCd || opts.cwd === undefined)
return undefined;
if (!opts.cwd.startsWith("/") || opts.cwd.startsWith("//"))
return undefined;
resolved = opts.cwd.replace(/\/+$/, "") + target.slice(1);
}
else {
return undefined;
}
if (!isLexicalNormalAbsoluteDir(resolved))
return undefined;
if (HOME_TOP_SHAPE.test(resolved))
return undefined;
if (HOME_ANCESTOR_SHAPE.test(resolved))
return undefined;
const processHome = userHomeTopLevel();
if (processHome !== undefined && (resolved === processHome || isStrictAncestorDir(resolved, processHome))) {
return undefined;
}
return resolved;
}
function isStrictAncestorDir(ancestor, path) {
return ancestor !== "/" && path.startsWith(`${ancestor}/`);
}
const HOME_TOP_SHAPE = /^(?:\/home\/[^/]+|\/Users\/[^/]+|\/root|\/var\/root)$/;
const HOME_ANCESTOR_SHAPE = /^(?:\/home|\/Users|\/var)$/;
let HOME_TOP_LEVEL = null;
function userHomeTopLevel() {
if (HOME_TOP_LEVEL === null) {
try {
const h = homedir();
HOME_TOP_LEVEL = typeof h === "string" && h.startsWith("/") && !h.startsWith("//") ? h.replace(/\/+$/, "") : undefined;
}
catch {
HOME_TOP_LEVEL = undefined;
}
}
return HOME_TOP_LEVEL === null ? undefined : HOME_TOP_LEVEL;
}
const CURSOR_MOVING_HEADS = shellWordsOf("base_mover");
const OPAQUE_CURSOR_MOVERS = new Set(["eval", "source", ".", "trap", "enable", "builtin", "command", "time"]);
function pushdPopdLeavesCursor(head, args) {
for (const a of args) {
if (a === "--")
return false;
if (/^-[a-zA-Z]*n[a-zA-Z]*$/.test(a))
return true;
if (head === "popd" && /^\+0*[1-9]\d*$/.test(a))
return true;
}
return false;
}
function movesShellCursor(segment) {
if (carriesShellRedirection(segment))
return true;
const words = segment
.split(/[ \t]+/)
.map((w) => w.replace(/['"]/g, ""))
.filter((t) => t !== "");
const head = words[0];
if (head === undefined)
return false;
if (OPAQUE_CURSOR_MOVERS.has(head))
return true;
if (!CURSOR_MOVING_HEADS.has(head))
return false;
return !(shellWordsOf("stack_mover").has(head) && pushdPopdLeavesCursor(head, words.slice(1)));
}
function resolveCdSegmentDirectories(segments, cwd) {
let priorCd = false;
return segments.map((segment) => {
const dir = cdSegmentDirectoryOf(segment, { cwd, priorCd });
if (!priorCd && movesShellCursor(segment))
priorCd = true;
const bases = basesOfCwd(cwd);
const family = pathFamilyOf(bases);
return dir === undefined || isShellRootedSpellingUnmapped(family, dir) || shellDriveSpellingOf(family, dir) !== undefined ? undefined : directoryIdentityOf(dir, bases);
});
}
export function scopeCoversCwd(scope, cwd, sessionId, identity) {
if (scope.kind === "global")
return true;
if (scope.kind === "session")
return sessionId !== undefined && sessionId === scope.sessionId;
if (cwd === undefined)
return false;
if (pathWithinRoot(cwd, scope.root))
return true;
return identity !== undefined && pathWithinRoot(identity.canonicalRoot, identity.canonicalOf(scope.root) ?? scope.root);
}
export function isRuleLive(rule) {
return rule.adds.length > 0;
}
export function eligibleContext(rule, call) {
return rule.tool === call.tool && scopeCoversCwd(rule.scope, call.cwd, call.sessionId, call.scopeIdentity);
}
export function eligiblePersisted(rule, call, behavior) {
return rule.behavior === behavior && isRuleLive(rule) && eligibleContext(rule, behavior === "allow" ? spelledScope(call) : call);
}
export function spelledScope(call) {
const { scopeIdentity: _identity, ...spelled } = call;
return spelled;
}
function firstEligibleAdmittingRule(rules, commandText, call) {
for (const rule of rules) {
if (!eligiblePersisted(rule, call, "allow"))
continue;
if (ruleAdmitsCommandIn(call.shell, rule, commandText))
return rule;
}
return undefined;
}
export function findAdmittingRule(rules, call) {
return findAdmittingRuleIn(rules, { ...call, shell: SHELL_LEX_DIALECT });
}
export function findAdmittingRuleIn(rules, call) {
const coverage = commandCoverageOf(rules, call);
return "covering" in coverage ? coverage.covering : undefined;
}
export function commandCoverageOf(rules, call) {
const shape = ruleLaneShapeOf(call.shell, call.command, MATCH_READING);
if ("reject" in shape)
return { uncovered: undefined };
const whole = firstEligibleAdmittingRule(rules, call.command, call);
if (whole !== undefined)
return { covering: [whole] };
const cdDirs = resolveCdSegmentDirectories(shape.segments, call.execCwd ?? call.cwd);
if (shape.segments.length < 2) {
const dir = cdDirs[0];
const lone = dir !== undefined ? firstEligibleDirectoryRule(rules, dir, call) : undefined;
return lone !== undefined ? { covering: [lone] } : { uncovered: shape.segments[0] ?? call.command };
}
const covering = [];
const lineObjection = readOnlyLineObjection(call.shell, call.command, call.execCwd ?? call.cwd);
for (const [i, segment] of shape.segments.entries()) {
const dir = cdDirs[i];
const first = firstEligibleAdmittingRule(rules, segment, call) ?? (dir !== undefined ? firstEligibleDirectoryRule(rules, dir, call) : undefined);
if (first === undefined) {
if (call.posture !== "narrowing" && readsOnlyAsSegment(call.shell, segment, lineObjection, call.command))
continue;
return { uncovered: segment };
}
if (!covering.includes(first))
covering.push(first);
}
return covering.length > 0 ? { covering } : { uncovered: undefined };
}
function readsOnlyAsSegment(shell, segment, lineObjection, whole) {
return lineObjection === undefined && !movesShellCursor(segment) && readOnlyShellVerdictIn(shell, segment, { bound: () => lineDescriptorsOfText(shell, whole) }).readOnly;
}
function firstEligibleDirectoryRule(rules, directory, call) {
for (const rule of rules) {
if (!eligiblePersisted(rule, { tool: "Read", cwd: call.cwd, sessionId: call.sessionId }, "allow"))
continue;
if (directoryRuleAdmits(rule, directory, basesOfCwd(call.execCwd ?? call.cwd)))
return rule;
}
return undefined;
}
function basesOfCwd(cwd) {
return cwd === undefined ? {} : { root: cwd };
}
export function adjudicatePersistedRules(rules, call) {
return adjudicatePersistedRulesIn(rules, { ...call, shell: SHELL_LEX_DIALECT });
}
export function adjudicatePersistedRulesIn(rules, call) {
for (const behavior of RULE_BEHAVIORS_BY_PRECEDENCE) {
if (behavior === "allow") {
const covering = findAdmittingRuleIn(rules, call);
if (covering !== undefined)
return { behavior, rules: covering };
continue;
}
let unreadable;
for (const rule of rules) {
if (!eligiblePersisted(rule, call, behavior))
continue;
const outcome = programRunReachIn(call.shell, rule, call.command);
if (outcome.reach === "reached")
return { behavior, rules: [rule] };
if (outcome.reach === "unreadable")
unreadable ??= { behavior: "ask", rules: [rule], unreadable: outcome.reason };
}
if (unreadable !== undefined)
return unreadable;
}
return undefined;
}
export function adjudicatePersistedPathRules(rules, call, views, anchorsOf, leads) {
const speaks = (rule, behavior) => eligiblePersisted(rule, call, behavior) ||
(call.face !== undefined && PATH_RULE_FACES[rule.tool]?.[behavior]?.includes(call.face) === true && eligiblePersisted(rule, { ...call, tool: rule.tool }, behavior));
const bind = (rows) => bindPathRules(rows.map((rule) => ({ parsed: rule, ruleText: rule.rule, rule })), (s) => anchorsOf(s.rule));
for (const behavior of RULE_BEHAVIORS_BY_PRECEDENCE) {
if (behavior === "allow") {
const face = call.face;
if (views.allow === undefined || face === undefined)
continue;
const allowing = rules.filter((rule) => (rule.match === "path" || rule.match === "subpath") && PATH_RULE_FACES[rule.tool]?.allow?.includes(face) === true && eligiblePersisted(rule, { ...call, tool: rule.tool }, behavior));
const hit = judgeRuleRows(bind(allowing), views.allow, "allow");
if (hit !== undefined)
return { behavior, rules: [hit.row.spec.rule] };
continue;
}
if (views.tighten === undefined)
continue;
const rows = rules.filter((rule) => (rule.match === "path" || rule.match === "subpath") && speaks(rule, behavior));
if (rows.length === 0)
continue;
const hit = judgeRuleRows(rowsWhereTheyLead(bind(rows), leads), views.tighten, "tighten");
if (hit === undefined)
continue;
const rule = hit.row.spec.rule;
if (hit.unreadable !== undefined)
return { behavior: "ask", rules: [rule], unreadable: hit.unreadable };
return { behavior, rules: [rule], ...(behavior === "deny" && hit.view !== undefined && call.face !== undefined ? { denial: { rule: rule.rule, view: hit.view, face: call.face } } : {}) };
}
return undefined;
}
export function adjudicatePersistedNameRules(rules, call) {
const name = nameRuleSubjectOf(call.tool, call.request.args, "deny");
const granted = nameRuleSubjectOf(call.tool, call.request.args, "allow");
const values = call.request.face?.nameValues;
if (name === undefined) {
if (nameValueAbsentReason(call.tool) === undefined)
return undefined;
for (const behavior of ["deny", "ask"]) {
const row = rules.find((rule) => rule.match === "name" && eligiblePersisted(rule, call, behavior));
if (row !== undefined)
return { behavior: "deny", rules: [row] };
}
return undefined;
}
for (const behavior of RULE_BEHAVIORS_BY_PRECEDENCE) {
for (const rule of rules) {
if (!eligiblePersisted(rule, call, behavior))
continue;
const arm = mountedArgumentArmOf(rule);
if (arm !== undefined && paramRuleReaches(call.request, arm.param, arm.pattern, undefined, behavior === "allow" ? "allow" : "tighten"))
return { behavior, rules: [rule] };
const subject = behavior === "allow" ? granted : name;
if (subject === undefined)
continue;
const reach = nameRuleReach(rule, subject, values);
if (reach === "reached")
return { behavior, rules: [rule] };
if (reach === "unread" && behavior !== "allow")
return behavior === "deny" ? { behavior, rules: [rule] } : { behavior, rules: [rule], unreadable: nameValuesUnreadReason(call.tool) };
}
}
return undefined;
}
export function adjudicatePersistedDomainRules(rules, call) {
for (const behavior of RULE_BEHAVIORS_BY_PRECEDENCE) {
const reading = behavior === "allow" ? call.hosts.grant : call.hosts.tighten;
if (reading === undefined)
continue;
const speaks = (rule) => (rule.match === "host" || rule.match === "subdomain") && eligiblePersisted(rule, call, behavior);
if ("unreadable" in reading) {
if (behavior === "allow")
continue;
const row = rules.find(speaks);
if (row === undefined)
continue;
return behavior === "deny" ? { behavior: "deny", rules: [row] } : { behavior: "ask", rules: [row], unreadable: `the host the call reaches cannot be read (${HOST_UNREADABLE_SENTENCE[reading.unreadable]})` };
}
const row = rules.find((rule) => speaks(rule) && domainRuleAdmits(rule, reading.host));
if (row !== undefined)
return { behavior, rules: [row] };
}
return undefined;
}
export function wholeToolRuleReaches(tool, call, behavior) {
const covers = (readsNameGlob(behavior) && peerReadingsOf(tool).length > 0) || a2aWholePeerName(tool) === tool;
return heldBy("policy.persistedToolRule", { has: (name) => name === tool || (covers && ruleNameCovers(tool, name)) }, call);
}
export function adjudicatePersistedToolRules(rules, call) {
const at = { cwd: call.cwd, ...(call.sessionId !== undefined ? { sessionId: call.sessionId } : {}), ...(call.scopeIdentity !== undefined ? { scopeIdentity: call.scopeIdentity } : {}) };
for (const behavior of RULE_BEHAVIORS_BY_PRECEDENCE) {
const row = rules.find((rule) => rule.match === "tool" && wholeToolRuleReaches(rule.tool, call.request, behavior) && eligiblePersisted(rule, { ...at, tool: rule.tool }, behavior));
if (row !== undefined)
return { behavior, rules: [row] };
}
return undefined;
}
export function suggestRuleForTool(tool) {
if (cardRuleFormOf(tool) !== "tool")
return undefined;
const parsed = parseRuleText(formatRuleText("", "tool", tool), "allow");
if (!("rule" in parsed) || parsed.rule.match !== "tool" || parsed.rule.tool !== tool)
return undefined;
return { kind: "single", rule: parsed.rule.rule, match: "tool", command: "", ...cautionsSeatOf("tool", "") };
}
export function suggestRuleForPeer(toolName, peerFingerprint) {
const peer = a2aPeerRuleOf(toolName);
const offer = peer !== undefined && peer !== toolName ? suggestRuleForTool(peer) : undefined;
return offer !== undefined ? { ...offer, peerFingerprint } : undefined;
}
export function suggestRuleForHost(tool, host) {
if (ruleToolGrammarOf(tool) !== "domain")
return undefined;
const parsed = parseRuleText(formatRuleText(host, "host", tool), "allow");
if (!("rule" in parsed) || parsed.rule.match !== "host" || parsed.rule.command !== host)
return undefined;
return { kind: "single", rule: parsed.rule.rule, match: "host", command: host, ...cautionsSeatOf("host", host) };
}
export function suggestRuleForName(tool, name) {
if (ruleToolGrammarOf(tool) !== "name")
return undefined;
const parsed = parseRuleText(formatRuleText(name, "name", tool), "allow");
if (!("rule" in parsed) || parsed.rule.match !== "name" || parsed.rule.command !== name)
return undefined;
return { kind: "single", rule: parsed.rule.rule, match: "name", command: name };
}
export function segmentCoverageOf(command, rules, call) {
const folded = foldSpacing(command);
if (folded === undefined)
return undefined;
const shape = ruleLaneShapeOf(SHELL_LEX_DIALECT, folded, OFFER_READING);
if ("reject" in shape)
return undefined;
const proposed = rules.proposed ?? [];
const cdDirs = resolveCdSegmentDirectories(shape.segments, call.execCwd ?? call.cwd);
const lineObjection = readOnlyLineObjection(SHELL_LEX_DIALECT, folded, call.execCwd ?? call.cwd);
return shape.segments.map((segment, i) => {
const dir = cdDirs[i];
return {
segment: segment.trim(),
covered: readsOnlyAsSegment(SHELL_LEX_DIALECT, segment, lineObjection, folded) ||
firstEligibleAdmittingRule(rules.persisted, segment, { ...call, shell: SHELL_LEX_DIALECT }) !== undefined ||
(dir !== undefined && firstEligibleDirectoryRule(rules.persisted, dir, call) !== undefined) ||
proposed.some((p) => p.rule.tool === "Read"
? dir !== undefined &&
eligibleContext({ tool: p.rule.tool, scope: p.scope }, { ...spelledScope(call), tool: "Read" }) &&
directoryRuleAdmits(p.rule, dir, basesOfCwd(call.execCwd ?? call.cwd))
: eligibleContext({ tool: p.rule.tool, scope: p.scope }, spelledScope(call)) && ruleAdmitsCommand(p.rule, segment)),
};
});
}
export const RULE_OFFER_CAUTIONS = Object.freeze(["recursive_delete", "writes_files", "runs_project_code", "whole_domain", "whole_tool"]);
const FILE_WRITING_COMMANDS = new Set(["mkdir", "touch", "rmdir", "rm", "mv", "cp"]);
const PROJECT_CODE_COMMAND = /^(?:npm test|make(?: [A-Za-z][A-Za-z0-9_-]*)?)$/;
const OPEN_COMMAND_MATCHES = new Set(["prefix", "wildcard", "pattern"]);
export function ruleOfferCautions(match, command) {
if (match === "tool")
return ["whole_tool"];
if (match === "host" || match === "subdomain")
return ["whole_domain"];
if (match !== "exact" && !OPEN_COMMAND_MATCHES.has(match))
return [];
const shape = ruleLaneShapeOf(SHELL_LEX_DIALECT, command, OFFER_READING);
const found = new Set();
for (const segment of "reject" in shape ? [command] : shape.segments) {
const text = segment.trim();
const argv = text.split(/\s+/).filter((t) => t !== "");
const head = argv[0] ?? "";
if (PROJECT_CODE_COMMAND.test(text))
found.add("runs_project_code");
else if (FILE_WRITING_COMMANDS.has(head))
found.add(head === "rm" && (match !== "exact" || argv.some((a) => /^-[A-Za-z]*[rR]/.test(a) || a === "--recursive")) ? "recursive_delete" : "writes_files");
}
return RULE_OFFER_CAUTIONS.filter((w) => found.has(w));
}
export function cautionsSeatOf(match, command) {
const words = ruleOfferCautions(match, command);
return words.length > 0 ? { cautions: words } : {};
}
export const RULE_OFFER_BATCH_MEMBER_KINDS = Object.freeze(["command", "directoryRead"]);
export const RULE_OFFER_KINDS = Object.freeze(["single", "batch"]);
export const UNCOVERED_SEGMENT_REASONS = Object.freeze(["redirection", "no_rule_form", "cap_overflow"]);
export const UNCOVERED_SEGMENT_REASON_BASELINE = {
redirection: "This part carries a redirection, so it is approved per use — no rule can cover this spelling. If you trust its redirection-free form, a rule can be written for that form (the card's edit row, or settings).",
no_rule_form: "No rule form exists for this part; it can be approved per use.",
cap_overflow: "This batch does not cover this part; you will be asked again when it comes up.",
};
export const RULE_OFFERS_ABSENCE_BASELINE = {
mandated: "This approval is required by the deployment's policy each time; a rule cannot waive it.",
allow_outranked: "An allow rule already matches this call, and this question outranks it — another allow rule would be outranked the same way; it is approved per use.",
ask_rule_matched: "An ask-each-time rule matches this call and keeps asking — a new allow rule changes nothing while it stands; to stop being asked, review the existing ask rules.",
lane_cannot_speak: "This card has no rule to offer for this exact command; it can be approved per use. A coverable form of what you trust can still get a rule — through a card drawn for that form, or settings.",
};
export function suggestRulesForCommand(command, ctx) {
const shape = ruleLaneShapeOf(SHELL_LEX_DIALECT, command, OFFER_READING);
if ("reject" in shape)
return [];
const folded = foldSpacing(command);
if (folded === undefined)
return [];
const offers = [];
const exact = parseRuleText(formatRuleText(folded, "exact", COMMAND_RULE_TOOL), "allow");
if ("rule" in exact && exact.rule.match === "exact" && ruleAdmitsCommand(exact.rule, command)) {
offers.push({ kind: "single", rule: exact.rule.rule, match: "exact", command: exact.rule.command, ...cautionsSeatOf("exact", exact.rule.command) });
}
const tokensOf = (s) => s.split(/\s+/).filter((t) => t !== "");
const bodyOf = (tokens) => longestReviewedBody(tokens) ?? genericPrefixBody(tokens);
const directoryGateOpen = (ctx?.scope?.kind === "project" || ctx?.scope?.kind === "session") && scopeCoversCwd(ctx.scope, ctx?.cwd, ctx?.sessionId);
const deniesDirectory = ctx?.deniesDirectoryRead ?? defaultDeniesDirectoryRead;
const mintDirectoryMember = (directory, segment) => {
if (!directoryGateOpen || directory === undefined)
return undefined;
let denied;
try {
denied = deniesDirectory(directory) === true;
}
catch {
denied = true;
}
if (denied)
return undefined;
const parsed = parseRuleText(formatRuleText(directory, "subpath", READ_RULE_TOOL), "allow");
if (!("rule" in parsed) || parsed.rule.match !== "subpath")
return undefined;
if (!directoryRuleAdmits(parsed.rule, directory, basesOfCwd(ctx?.execCwd ?? ctx?.cwd)))
return undefined;
return { kind: "directoryRead", rule: parsed.rule.rule, directory: parsed.rule.command, segment };
};
if (shape.segments.length === 1) {
if (offers.length === 1) {
const body = bodyOf(tokensOf(folded));
if (body !== undefined) {
const parsed = parseRuleText(formatRuleText(body, "wildcard", COMMAND_RULE_TOOL), "allow");
if ("rule" in parsed && parsed.rule.match === "wildcard" && ruleAdmitsCommand(parsed.rule, command)) {
offers.push({ kind: "single", rule: parsed.rule.rule, match: "wildcard", command: parsed.rule.command, ...cautionsSeatOf("wildcard", parsed.rule.command) });
}
}
}
const lone = folded.trim();
const loneCovered = ctx?.coverage !== undefined && ctx.coverage.length === 1 && ctx.coverage[0]?.segment === lone && ctx.coverage[0]?.covered === true;
if (!loneCovered) {
const member = mintDirectoryMember(resolveCdSegmentDirectories([lone], ctx?.execCwd ?? ctx?.cwd)[0], lone);
if (member !== undefined)
offers.push({ kind: "batch", rules: [member], uncoveredSegments: 0, uncoveredDetail: [] });
}
return offers;
}
const foldedShape = ruleLaneShapeOf(SHELL_LEX_DIALECT, folded, OFFER_READING);
if ("reject" in foldedShape || foldedShape.segments.length !== shape.segments.length)
return offers;
const segments = foldedShape.segments.map((s) => s.trim());
const table = ctx?.coverage;
const alignedTable = table !== undefined && table.length === segments.length && table.every((row, i) => row.segment === segments[i]) ? table : undefined;
const coveredAt = (i) => alignedTable?.[i]?.covered === true;
const mintSegmentRule = (segment) => {
if (carriesShellRedirection(segment))
return undefined;
const body = bodyOf(tokensOf(segment));
if (body !== undefined) {
const parsed = parseRuleText(formatRuleText(body, "wildcard", COMMAND_RULE_TOOL), "allow");
if ("rule" in parsed && parsed.rule.match === "wildcard" && ruleAdmitsCommand(parsed.rule, segment)) {
return { rule: parsed.rule.rule, match: "wildcard", command: parsed.rule.command };
}
}
const segExact = parseRuleText(formatRuleText(segment, "exact", COMMAND_RULE_TOOL), "allow");
if ("rule" in segExact && segExact.rule.match === "exact" && ruleAdmitsCommand(segExact.rule, segment)) {
return { rule: segExact.rule.rule, match: "exact", command: segExact.rule.command };
}
return undefined;
};
const cdDirs = resolveCdSegmentDirectories(segments, ctx?.execCwd ?? ctx?.cwd);
const minted = [];
for (let i = 0; i < segments.length; i++) {
if (coveredAt(i))
continue;
const segment = segments[i];
const dirMember = mintDirectoryMember(cdDirs[i], segment);
const member = dirMember ?? (() => {
const rule = mintSegmentRule(segment);
return rule !== undefined ? { kind: "command", ...rule, segment, ...cautionsSeatOf(rule.match, rule.command) } : undefined;
})();
if (member !== undefined && !minted.some((m) => m.rule === member.rule))
minted.push(member);
}
const batchRules = minted.slice(0, 5);
if (batchRules.length > 0) {
const parsedBatch = batchRules.flatMap((r) => {
const p = parseRuleText(r.rule, "allow");
return "rule" in p ? [p.rule] : [];
});
const uncoveredDetail = [];
for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
if (coveredAt(i))
continue;
if (parsedBatch.some((p) => p.tool !== "Read" && ruleAdmitsCommand(p, segment)))
continue;
const dir = cdDirs[i];
if (dir !== undefined && parsedBatch.some((p) => directoryRuleAdmits(p, dir, basesOfCwd(ctx?.execCwd ?? ctx?.cwd))))
continue;
const reason = carriesShellRedirection(segment) ? "redirection" : mintSegmentRule(segment) === undefined ? "no_rule_form" : "cap_overflow";
uncoveredDetail.push({ segment, reason });
}
offers.push({ kind: "batch", rules: batchRules, uncoveredSegments: uncoveredDetail.length, uncoveredDetail });
}
return offers;
}
let DEFAULT_DIRECTORY_DENY;
function defaultDeniesDirectoryRead(directory) {
if (DEFAULT_DIRECTORY_DENY === undefined) {
const matcher = compileReadDeny([], "directoryRead mint floor");
DEFAULT_DIRECTORY_DENY = (d) => matcher.matchPath(d) !== null;
}
return DEFAULT_DIRECTORY_DENY(directory);
}