UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

855 lines (854 loc) • 41.6 kB
import { randomUUID } from "node:crypto"; import { describeRejectedValue } from "../../core/tool-search-setting.js"; import { sliceHeadSafe, sliceTailSafe } from "../../core/surrogate-safe-slice.js"; import { isAbsoluteForFamily, normalizeAbsPathLexically, pathFamilyOf, pathIdentityOf, spellingOfIdentity } from "./safety.js"; import { localDriveSpellingOf } from "../../core/unc-spelling.js"; import { PROBE_REASON_MAX } from "../../core/untrusted-text.js"; import { wildcardMatch } from "../../core/namespaced-rule-name.js"; import { validatePath } from "../../core/effective-path-target.js"; import { judgePathRows } from "../../core/path-guard-rows.js"; export const READ_DENY_HOME_WORD = "~"; export function readDenyPatternRun(pattern) { const segments = pattern.split("/").filter(Boolean); return segments[0] === READ_DENY_HOME_WORD ? { homeAnchored: true, run: segments.slice(1).join("/") } : { homeAnchored: false, run: pattern }; } export function readDenyRunHead(run) { const segments = run.split("/").filter(Boolean); const wild = segments.findIndex((seg) => seg.includes("*")); return wild === -1 ? segments.length : wild; } export const DENY_FACES = Object.freeze(["read", "write"]); const REACHED = { reach: "reached" }; const NOT_REACHED = { reach: "not_reached" }; const reachOf = (row, spelled) => row.reach?.(spelled) ?? (row.reaches(spelled) ? REACHED : NOT_REACHED); export function judgeRuleRows(rows, views, direction) { const hit = judgePathRows(rows.map(ruleGuard), views, direction); return hit === undefined ? undefined : { ...hit, row: hit.row.rule }; } function ruleGuard(rule) { return { rule, reach: (view) => reachOf(rule, view.path) }; } export function ruleHeadParts(rows) { const out = new Set(); for (const row of rows) if (row.head !== undefined) for (const part of partsOf(row.head)) out.add(part); return [...out]; } function partsOf(head) { const family = pathFamilyOf({ root: head }); const id = pathIdentityOf(head, family, head); if (!id.ok) return [head]; const unc = id.identity.startsWith("//"); const segments = id.identity.split("/").filter((seg) => seg !== ""); const parts = []; for (let k = unc ? 2 : 1; k <= segments.length; k++) parts.push(spellingOfIdentity(`${unc ? "//" : "/"}${segments.slice(0, k).join("/")}`, family)); return parts.length > 0 ? parts : [head]; } export function rowsWhereTheyLead(rows, leads) { const out = []; for (const row of rows) { out.push(row); if (row.head === undefined || leads.size === 0) continue; let parentLed; let parentPart; for (const part of partsOf(row.head)) { const led = leads.get(part); const extendsParent = led !== undefined && parentLed !== undefined && parentPart !== undefined && sameLocation(led, `${parentLed.replace(/[\\/]+$/, "")}/${part.slice(parentPart.replace(/[\\/]+$/, "").length).replace(/^[\\/]+/, "")}`); const twin = extendsParent ? undefined : rowLedTo(row, part, led); if (twin !== undefined) out.push(twin); parentLed = led; parentPart = part; } } return out; } export function locationLeadsTo(path, leads) { const parts = partsOf(path); return leads.get(parts[parts.length - 1]); } export async function withRuleRowsWhereTheyLead(rows, leadsTo) { const parts = ruleHeadParts(rows); const led = await Promise.all(parts.map((part) => leadsTo(part).catch(() => undefined))); return rowsWhereTheyLead(rows, new Map(parts.map((part, i) => [part, led[i]]))); } function sameLocation(a, b) { const family = pathFamilyOf({ root: a }); const x = pathIdentityOf(a, family, a); const y = pathIdentityOf(b, family, b); return x.ok && y.ok && x.identity === y.identity; } function rowLedTo(row, head, at) { if (at === undefined) return undefined; const family = pathFamilyOf({ root: head }); const from = pathIdentityOf(at, family, head); const to = pathIdentityOf(head, family, head); if (!from.ok || !to.ok || from.identity === to.identity) return undefined; const base = from.identity; const under = base.endsWith("/") ? base : `${base}/`; const onto = to.identity.endsWith("/") ? to.identity.slice(0, -1) : to.identity; const mapped = (absolutePath) => { const id = pathIdentityOf(absolutePath, family, head); if (!id.ok) return undefined; if (id.identity === base) return to.identity; return id.identity.startsWith(under) ? `${onto}/${id.identity.slice(under.length)}` : undefined; }; const { head: _head, reachShape: shape, ...rest } = row; const reach = row.reach; const twinShape = shape === undefined ? undefined : shape.at === to.identity ? { at: base, globs: shape.globs } : shape.at.startsWith(`${onto}/`) ? { at: `${under}${shape.at.slice(onto.length + 1)}`, globs: shape.globs } : undefined; return { ...rest, ...(twinShape !== undefined ? { reachShape: twinShape } : {}), reaches: (absolutePath) => { const m = mapped(absolutePath); return m !== undefined && row.reaches(m); }, ...(reach !== undefined ? { reach: (absolutePath) => { const m = mapped(absolutePath); return m === undefined ? NOT_REACHED : reach(m); }, } : {}), }; } export function ruleGuardRow(rule) { return { rule, name: rule.rule, grammar: { kind: "rule", rule: rule.rule }, polarity: "deny", faces: rule.faces, placement: "anywhere", fold: "exact", exemptible: false, origin: "rule", reach: (view) => reachOf(rule, view.path) }; } export function segmentGuardRow(pat, columns) { const placement = pat.anchors === undefined ? "anywhere" : pat.rootedOnly !== true ? "home" : pat.anchors.length === 1 && pat.anchors[0].length === 0 ? "rooted" : { below: `/${(pat.anchors[0] ?? []).join("/")}` }; return { name: pat.raw, grammar: { kind: "segment-glob", run: pat.raw }, placement, ...columns, reach: (view) => (segmentRowMeets(pat, segmentReadingsOf(view.path, view.aliasResolved === true)) ? REACHED : NOT_REACHED), }; } function denyTableHit(rows, views) { const hit = judgePathRows(rows, views, "tighten"); if (hit === undefined) return null; const view = hit.view ?? views.views[0]?.path; return { pattern: hit.row.name, ...(view !== undefined ? { matchedView: view } : {}) }; } function denyTargetViews(canonicalKey, lexicalView, opts) { const views = [{ path: canonicalKey, kind: "canonical", ...(opts?.aliasResolved === true ? { aliasResolved: true } : {}) }]; if (lexicalView !== undefined) views.push({ path: lexicalView, kind: "lexical" }); for (const hop of opts?.hops ?? []) views.push({ path: hop, kind: "hop" }); return { views }; } export function compileWriteDeny(table) { if (table === null || typeof table !== "object" || !Array.isArray(table.tableRows)) { throw new TypeError("compileWriteDeny: expected a table compiled by compileReadDeny (missing tableRows) — pass the compiled table, not a separate rule list."); } let cachedSource; let cached; const projected = () => { const source = table.tableRows; if (!Array.isArray(source)) { throw new TypeError("compileWriteDeny: the table carries no compiled tableRows to project."); } if (cached === undefined || cachedSource !== source) { const rows = source.filter((r) => r.faces.includes("write")); const ruleRows = []; const writeSegments = []; for (const r of rows) { if ("rule" in r) ruleRows.push(r.rule); else if ("compiledSegment" in r) writeSegments.push(r.compiledSegment); } const readRows = new Set(rows.filter((r) => r.faces.includes("read")).map((r) => r.name)); cachedSource = source; cached = { rows, ruleRows, writeSegments, readRows }; } return cached; }; projected(); return { face: "write", get hasRows() { return projected().rows.length > 0; }, get ruleRows() { return projected().ruleRows; }, matchPath: (path) => { const hit = denyTableHit(projected().rows, { views: [{ path, kind: "lexical" }] }); return hit === null ? null : { pattern: hit.pattern }; }, matchTarget: (canonicalKey, lexicalView, opts) => denyTableHit(projected().rows, denyTargetViews(canonicalKey, lexicalView, opts)), guardsReads: (pattern) => projected().readRows.has(pattern), rowUnderRoot: (scope) => { const { writeSegments } = projected(); if (writeSegments.length === 0 || !isAbsoluteForFamily("own", scope)) return null; return rowNamedByRootOf(writeSegments, normalizeAbsPathLexically(scope)); }, rowAtOrBelow: (path) => { const { writeSegments, ruleRows } = projected(); const hit = rowAtOrBelowOf(writeSegments, path); if (hit !== null) return { pattern: hit }; return ruleRows.length === 0 ? null : { pattern: ruleRows[0].rule }; }, get unanchoredRows() { return projected().writeSegments.filter(unanchoredRow).map((pat) => pat.raw); }, rowLedByName: (name) => rowLedByNameOf(projected().writeSegments, name), }; } function unanchoredRow(pat) { return pat.anchors === undefined && pat.rootedOnly !== true; } function rowLedByNameOf(rows, name) { const readings = foldedViewsOf([[name]]); for (const pat of rows) { if (!unanchoredRow(pat)) continue; const lead = pat.segments.find((s) => !s.anything); if (lead === undefined || readings(pat.fold).some((view) => lead.test(view[0]))) return pat.raw; } return null; } export function seatForCall(seat, toolCallId) { const forCall = seat?.forCall; return typeof forCall === "function" && toolCallId !== undefined ? forCall.call(seat, toolCallId) : seat; } export function seatReady(seat, signal) { const ready = seat?.ready; return typeof ready === "function" ? Promise.resolve(ready.call(seat, signal)) : Promise.resolve(); } export function readDenySeat(matcher) { const entries = [...(matcher?.entries ?? []).map((e) => e.pattern), ...(matcher?.ruleRows ?? []).map((r) => r.rule)]; if (matcher === undefined || entries.length === 0) return { denyEntries: entries }; return { denyMatch: (p) => matcher.matchPath(p)?.pattern ?? null, denyUnder: (p) => { const hit = matcher.rowUnderRoot?.(p) ?? null; return hit === null ? null : hit.unplaced === true ? { unplaced: hit.pattern } : hit.pattern; }, denyAtOrBelow: (p) => (matcher.rowAtOrBelow !== undefined ? (matcher.rowAtOrBelow(p)?.pattern ?? null) : entries[0]), denyEntries: entries, }; } export function sensitivePathDenyList(pattern, face = "read") { const rows = typeof pattern === "string" ? [pattern] : pattern; return `the ${face === "write" ? "write" : "sensitive-path read"} deny list (pattern${rows.length === 1 ? "" : "s"} ${rows.map((p) => `"${p}"`).join(", ")})`; } export const SENSITIVE_PATH_DENY_SCOPE = "This list guards credential-class paths and applies regardless of the containment roots."; export const WRITE_DENY_SCOPE = "A write deny row holds regardless of the containment roots and of the permission mode."; export const READ_ROW_WRITE_SCOPE = "A read deny row holds on writes too, regardless of the containment roots and of the permission mode."; export function readDenySentence(finding) { return readDenyRefusal(finding).sentence; } export function readDenyRefusal(finding) { const write = finding.face === "write"; const canonicalNote = finding.canonical !== undefined && finding.canonical !== finding.target ? `(canonical target: "${fitMiddle(finding.canonical, READ_DENY_SENTENCE_PART_MAX)}") ` : ""; const lexicalHit = finding.matchedView !== undefined && finding.matchedView !== (finding.canonical ?? finding.target); const under = finding.under === true; const frame = `${finding.action ?? (under && !write ? "searching below" : write ? "writing" : "reading")} "${fitMiddle(finding.target, READ_DENY_SENTENCE_PART_MAX)}" ${canonicalNote}is refused:`; const pattern = fitMiddle(finding.pattern, READ_DENY_SENTENCE_PART_MAX); const echoed = Object.freeze([ Object.freeze({ kind: "path", value: finding.target, ...(finding.canonical !== undefined && finding.canonical !== finding.target ? { canonical: finding.canonical } : {}) }), ...(lexicalHit ? [Object.freeze({ kind: "path", value: finding.matchedView })] : []), Object.freeze({ kind: "pattern", value: finding.pattern }), ]); if (write && finding.lookup !== undefined) { const row = finding.readRow === true ? `the read deny row "${pattern}"` : sensitivePathDenyList(pattern, "write"); const found = finding.lookup.found; const held = `it holds "${fitMiddle(found, READ_DENY_SENTENCE_PART_MAX)}", a name ${row} starts with, and a recursive removal removes everything below it`; const quoted = [echoed[0], Object.freeze({ kind: "path", value: found }), echoed.at(-1)]; return { sentence: `${frame} ${held}. ${finding.readRow === true ? READ_ROW_WRITE_SCOPE : WRITE_DENY_SCOPE}`, echoed: Object.freeze(quoted) }; } if (write && finding.readRow === true) { const reaches = under ? lexicalHit ? `the requested path's spelling ("${fitMiddle(finding.matchedView, READ_DENY_SENTENCE_PART_MAX)}") holds a location the read deny row "${pattern}" denies reading` : `that path holds a location the read deny row "${pattern}" denies reading` : lexicalHit ? `the path passes through "${fitMiddle(finding.matchedView, READ_DENY_SENTENCE_PART_MAX)}", which the read deny row "${pattern}" denies reading` : `the target is a path the read deny row "${pattern}" denies reading`; return { sentence: `${frame} ${reaches} — a path whose reads are denied is not written either. ${READ_ROW_WRITE_SCOPE}`, echoed }; } if (under) { const holder = lexicalHit ? `the requested path's spelling ("${fitMiddle(finding.matchedView, READ_DENY_SENTENCE_PART_MAX)}")` : "that path"; return { sentence: `${frame} ${holder} holds a location on ${sensitivePathDenyList(pattern, write ? "write" : "read")} — locations are checked, not which files exist, so no such file need be there. ${write ? WRITE_DENY_SCOPE : SENSITIVE_PATH_DENY_SCOPE}`, echoed }; } const judged = lexicalHit ? `the requested path's spelling ("${fitMiddle(finding.matchedView, READ_DENY_SENTENCE_PART_MAX)}") matches` : "the target matches"; return { sentence: `${frame} ${judged} ${sensitivePathDenyList(pattern, write ? "write" : "read")}. ${write ? WRITE_DENY_SCOPE : SENSITIVE_PATH_DENY_SCOPE}`, echoed }; } export const READ_DENY_SENTENCE_PART_MAX = PROBE_REASON_MAX; export function fitMiddle(s, budget) { return s.length <= budget ? s : elideMiddle(s, budget); } export function elideMiddle(s, budget) { if (budget < 12) return sliceHeadSafe(s, Math.max(0, budget)); const keep = budget - 1; const head = Math.ceil(keep / 2); return `${sliceHeadSafe(s, head)}\u2026${sliceTailSafe(s, keep - head)}`; } export const READ_DENY_BUILTIN_TIERS = Object.freeze(["credentials", "shell-history", "browser", "wallet", "agent-config"]); export const READ_FACE_BUILTIN_DENY_TABLE = [ { pattern: ".ssh", tier: "credentials" }, { pattern: "id_rsa*", tier: "credentials" }, { pattern: "id_ed25519*", tier: "credentials" }, { pattern: "id_ecdsa*", tier: "credentials" }, { pattern: ".gnupg", tier: "credentials" }, { pattern: ".aws", tier: "credentials" }, { pattern: ".config/gcloud", tier: "credentials" }, { pattern: ".azure", tier: "credentials" }, { pattern: ".kube", tier: "credentials" }, { pattern: ".netrc", tier: "credentials" }, { pattern: "_netrc", tier: "credentials" }, { pattern: ".git-credentials", tier: "credentials" }, { pattern: ".docker/config.json", tier: "credentials" }, { pattern: ".config/gh", tier: "credentials" }, { pattern: "~/.npmrc", tier: "credentials" }, { pattern: "~/.pypirc", tier: "credentials" }, { pattern: ".local/share/keyrings", tier: "credentials" }, { pattern: "Library/Keychains", tier: "credentials" }, { pattern: ".credentials.json", tier: "credentials" }, { pattern: "credentials.env", tier: "credentials" }, { pattern: ".codex/auth.json", tier: "credentials" }, { pattern: ".config/github-copilot", tier: "credentials" }, { pattern: ".gemini/oauth_creds.json", tier: "credentials" }, { pattern: ".bash_history", tier: "shell-history" }, { pattern: ".zsh_history", tier: "shell-history" }, { pattern: "Library/Application Support/Google/Chrome", tier: "browser" }, { pattern: "Library/Application Support/Firefox", tier: "browser" }, { pattern: "Library/Safari", tier: "browser" }, { pattern: ".config/google-chrome", tier: "browser" }, { pattern: ".config/chromium", tier: "browser" }, { pattern: ".mozilla/firefox", tier: "browser" }, { pattern: "AppData/Local/Google/Chrome/User Data", tier: "browser" }, { pattern: "AppData/Local/Microsoft/Edge/User Data", tier: "browser" }, { pattern: "AppData/Roaming/Mozilla/Firefox", tier: "browser" }, { pattern: ".bitcoin", tier: "wallet" }, { pattern: ".ethereum", tier: "wallet" }, { pattern: ".electrum", tier: "wallet" }, { pattern: "Library/Application Support/Exodus", tier: "wallet" }, { pattern: "Library/Application Support/Ledger Live", tier: "wallet" }, { pattern: "wallet.dat", tier: "wallet" }, { pattern: "~/.sema/settings.json", tier: "agent-config" }, { pattern: "~/.sema/settings.local.json", tier: "agent-config" }, { pattern: "~/.sema.*", tier: "agent-config" }, { pattern: "~/.claude/settings.json", tier: "agent-config" }, { pattern: "~/.claude/settings.local.json", tier: "agent-config" }, { pattern: "~/.claude.*", tier: "agent-config" }, { pattern: "~/.mcp.json", tier: "agent-config" }, { pattern: "~/.ai-agent/.env", tier: "agent-config" }, { pattern: "~/.sema/engine-data/.env", tier: "agent-config" }, { pattern: "~/.sema/config-lkg-*.json", tier: "agent-config" }, { pattern: "~/.sema/settings.json.*", tier: "agent-config" }, { pattern: "~/.sema/settings.local.json.*", tier: "agent-config" }, { pattern: "~/.sema/.sema.*", tier: "agent-config" }, { pattern: "~/.sema/.config.json*", tier: "agent-config" }, { pattern: "~/.codex/config.toml", tier: "agent-config" }, { pattern: "~/.cursor/mcp.json", tier: "agent-config" }, { pattern: "~/.continue/config.json", tier: "agent-config" }, { pattern: "~/.continue/config.yaml", tier: "agent-config" }, { pattern: "~/.aider.conf.yml", tier: "agent-config" }, { pattern: "~/.gemini/settings.json", tier: "agent-config" }, ]; export const READ_DENY_DEFAULT_TIERS = ["credentials", "agent-config"]; export function resolveReadDenyBuiltins(config) { let activeTiers; if (config?.tiers === undefined) { activeTiers = new Set(READ_DENY_DEFAULT_TIERS); } else { if (!Array.isArray(config.tiers)) { throw new Error(`readDenyBuiltinTiers: expected an array of tier names, got ${describeRejectedValue(config.tiers)}.`); } for (const t of config.tiers) { if (typeof t !== "string" || !READ_DENY_BUILTIN_TIERS.includes(t)) { throw new Error(`readDenyBuiltinTiers: unknown tier ${JSON.stringify(t)} — known tiers: ${READ_DENY_BUILTIN_TIERS.join(", ")}.`); } } activeTiers = new Set(config.tiers); } let excluded; if (config?.exclude === undefined) { excluded = new Set(); } else { if (!Array.isArray(config.exclude)) { throw new Error(`readDenyBuiltinExclude: expected an array of built-in row names (canonical pattern texts), got ${describeRejectedValue(config.exclude)}.`); } for (const name of config.exclude) { if (typeof name !== "string" || !READ_FACE_BUILTIN_DENY_TABLE.some((r) => r.pattern === name)) { const anchored = typeof name === "string" ? READ_FACE_BUILTIN_DENY_TABLE.find((r) => r.pattern === `${READ_DENY_HOME_WORD}/${name}`) : undefined; throw new Error(`readDenyBuiltinExclude: ${JSON.stringify(name)} names no built-in row — ${anchored !== undefined ? `the row is anchored at the home directory and is named ${JSON.stringify(anchored.pattern)}; ` : ""}row names are the canonical pattern texts of READ_FACE_BUILTIN_DENY_TABLE (e.g. ".ssh", ".config/gcloud", "~/.sema/settings.json").`); } } excluded = new Set(config.exclude); } return READ_FACE_BUILTIN_DENY_TABLE.filter((r) => activeTiers.has(r.tier) && !excluded.has(r.pattern)); } export function readDenyBuiltinConfigOf(seats) { resolveReadDenyBuiltins(seats); return { ...(seats.tiers !== undefined ? { tiers: [...seats.tiers] } : {}), ...(seats.exclude !== undefined ? { exclude: [...seats.exclude] } : {}), }; } export function activeReadDenyBuiltinTiers(config) { const rows = resolveReadDenyBuiltins(config); return READ_DENY_BUILTIN_TIERS.filter((t) => rows.some((r) => r.tier === t)); } export const READ_DENY_REQUEST_RELEASABLE_ROWS = Object.freeze(["~/.sema/settings.json"]); function invalidArgument(message) { return Object.assign(new Error(message), { code: "config.invalid_argument" }); } export function screenRequestReleasableSeat(value) { if (value === undefined) return undefined; if (!Array.isArray(value)) { throw invalidArgument(`RunnerDeps.readDenyBuiltinRequestExcludable: expected an array of built-in row names, got ${describeRejectedValue(value)}.`); } const rows = new Set(); for (const name of value) { if (typeof name !== "string" || !READ_DENY_REQUEST_RELEASABLE_ROWS.includes(name)) { throw invalidArgument(`RunnerDeps.readDenyBuiltinRequestExcludable: ${JSON.stringify(name)} is not a row a request may release — the one releasable row is ${READ_DENY_REQUEST_RELEASABLE_ROWS.map((r) => JSON.stringify(r)).join(", ")}.`); } rows.add(name); } return Object.freeze([...rows]); } export function requestReleasedRows(grant, named) { if (named === undefined) return []; if (!Array.isArray(named)) { throw invalidArgument(`TaskSpec.readDenyBuiltinExclude: expected an array of built-in row names, got ${describeRejectedValue(named)}.`); } const rows = new Set(); for (const name of named) { if (typeof name !== "string" || !(grant ?? []).includes(name)) { throw invalidArgument(`TaskSpec.readDenyBuiltinExclude: ${JSON.stringify(name)} is not a row this deployment lets a request release (RunnerDeps.readDenyBuiltinRequestExcludable: ${grant === undefined || grant.length === 0 ? "none" : grant.map((r) => JSON.stringify(r)).join(", ")}).`); } rows.add(name); } return Object.freeze([...rows]); } export function dropReadsReadRefuses(records, seen, table) { for (const key of [...records.keys()]) { if (table.matchTarget(key) === null) continue; records.delete(key); seen.add(key); } } export function structuredReadBuiltinOf(config, released) { if (released.length === 0) return undefined; return { ...config, exclude: [...new Set([...(config.exclude ?? []), ...released])] }; } export const READ_FACE_DEFAULT_DENY_ENTRIES = resolveReadDenyBuiltins().map((r) => r.pattern); function asciiLower(s) { let out = ""; for (let i = 0; i < s.length; i++) { const c = s.charCodeAt(i); out += c >= 0x41 && c <= 0x5a ? String.fromCharCode(c + 32) : s[i]; } return out; } let UNIT_FOLD; function foldedUnit(c) { if (c < 0x80) return c >= 0x61 && c <= 0x7a ? c - 0x20 : c; const table = (UNIT_FOLD ??= new Int32Array(0x10000).fill(-1)); const known = table[c]; if (known >= 0) return known; const upper = String.fromCharCode(c).toUpperCase(); const folded = upper.length === 1 && upper.charCodeAt(0) >= 0x80 ? upper.charCodeAt(0) : c; table[c] = folded; return folded; } export function ignoreCaseFold(s) { let out = ""; for (let from = 0; from < s.length; from += 4096) { const codes = []; for (let i = from, to = Math.min(from + 4096, s.length); i < to; i++) codes.push(foldedUnit(s.charCodeAt(i))); out += String.fromCharCode(...codes); } return out; } function composed(s) { return /[^\x00-\x7f]/.test(s) ? s.normalize("NFC") : s; } export const SEGMENT_FOLDS = { none: composed, ascii: (s) => asciiLower(composed(s)), unicode: (s) => ignoreCaseFold(composed(s)), }; function foldSegments(fold, segs) { return segs.map((s) => SEGMENT_FOLDS[fold](s)); } const SPELLED_FOLDS = { none: (s) => s, ascii: asciiLower, unicode: ignoreCaseFold }; function readingsOfViews(fold, views) { const out = []; for (const segs of views) { const twin = foldSegments(fold, segs); out.push(twin); const spelled = segs.map((s) => SPELLED_FOLDS[fold](s)); if (spelled.some((s, i) => s !== twin[i])) out.push(spelled); } return out; } function foldedViewsOf(views) { const memo = new Map(); return (fold) => { let folded = memo.get(fold); if (folded === undefined) memo.set(fold, (folded = readingsOfViews(fold, views))); return folded; }; } function segmentMatcher(pattern) { return { anything: /^\*+$/.test(pattern), source: pattern, test: (segment) => !segment.includes("/") && wildcardMatch(pattern, segment), }; } export function compileSegmentPattern(raw, fold) { const source = SEGMENT_FOLDS[fold](raw); const parts = source.split("/").filter(Boolean); if (parts.length === 0) return null; return { raw, segments: parts.map(segmentMatcher), fold }; } export function matchSegmentPatterns(path, compiled, opts) { const readings = segmentReadingsOf(path, opts?.aliasResolved === true); for (const pat of compiled) if (segmentRowMeets(pat, readings)) return pat.raw; return null; } const SEGMENT_READINGS = new Map(); function segmentReadingsOf(path, aliasResolved) { const key = `${aliasResolved ? "1" : "0"}${path}`; const known = SEGMENT_READINGS.get(key); if (known !== undefined) return known; const spelledViews = segmentViewsOf(path, aliasResolved ? { aliasResolved } : undefined); const views = readingsOfViews("none", spelledViews); let folded; const readings = { views, lowered: readingsOfViews("ascii", spelledViews), folded: () => (folded ??= readingsOfViews("unicode", spelledViews)), rooted: isAbsoluteForFamily("own", path), }; if (SEGMENT_READINGS.size >= 512) SEGMENT_READINGS.clear(); SEGMENT_READINGS.set(key, readings); return readings; } function segmentRowMeets(pat, readings) { const n = pat.segments.length; const views = pat.fold === "ascii" ? readings.lowered : pat.fold === "unicode" ? readings.folded() : readings.views; for (const segs of views) { const runAt = (i) => i + n <= segs.length && pat.segments.every((re, j) => re.test(segs[i + j] ?? "")); if (pat.anchors !== undefined && readings.rooted) { for (const anchor of pat.anchors) if (anchor.every((a, j) => segs[j] === a) && runAt(anchor.length)) return true; continue; } if (pat.rootedOnly === true) continue; for (let i = 0; i + n <= segs.length; i++) if (runAt(i)) return true; } return false; } function segmentViewsOf(path, opts) { const literalSegs = path.split(/[\\/]/).filter(Boolean); const win32Segs = []; for (const raw of literalSegs) { const dot = /^\.{1,2}$/.test(raw) ? raw : raw.replace(/ +$/, ""); if (dot === ".") continue; if (dot === "..") { const last = win32Segs[win32Segs.length - 1]; if (win32Segs.length > 0 && last !== "..") win32Segs.pop(); else win32Segs.push(".."); continue; } const stripped = raw.replace(/[. ]+$/, ""); win32Segs.push(stripped.length > 0 ? stripped : raw); } const sameView = opts?.aliasResolved === true || (win32Segs.length === literalSegs.length && win32Segs.every((s, i) => s === literalSegs[i])); return sameView ? [literalSegs] : [literalSegs, win32Segs]; } function rowMeetsPath(pat, segs, rooted) { const n = pat.segments.length; if (pat.anchors !== undefined && rooted) { return pat.anchors.some((a) => { const k = Math.min(segs.length, a.length + n); for (let i = 0; i < k; i++) if (i < a.length ? segs[i] !== a[i] : !pat.segments[i - a.length].test(segs[i])) return false; return true; }); } if (pat.rootedOnly === true) return false; for (let i = 0; i < segs.length; i++) { const m = Math.min(n, segs.length - i); if (pat.segments.slice(0, m).every((re, j) => re.test(segs[i + j]))) return true; } return false; } function rowNamedByRoot(pat, segs, rooted) { if (pat.anchors !== undefined) return rooted && rowMeetsPath(pat, segs, rooted) ? "named" : "none"; if (pat.rootedOnly === true) return "none"; let k = 0; while (k < pat.segments.length && pat.segments[k].anything) k++; const run = pat.segments.slice(k); for (let i = 0; run.length > 0 && i < segs.length; i++) { const m = Math.min(run.length, segs.length - i); if (run.slice(0, m).every((re, j) => re.test(segs[i + j]))) return "named"; } return pat.unplacedHome === true && rooted ? "unplaced" : "none"; } function rowNamedByRootOf(rows, path) { const rooted = isAbsoluteForFamily("own", path); const foldedViews = foldedViewsOf(shareDriveReadingsOf(path).flatMap((at) => segmentViewsOf(at))); let unplaced; for (const pat of rows) { for (const view of foldedViews(pat.fold)) { const relation = rowNamedByRoot(pat, view, rooted); if (relation === "named") return { pattern: pat.raw }; if (relation === "unplaced") unplaced ??= pat.raw; } } return unplaced === undefined ? null : { pattern: unplaced, unplaced: true }; } function rowAtOrBelowOf(rows, path) { const rooted = isAbsoluteForFamily("own", path); const foldedViews = foldedViewsOf(shareDriveReadingsOf(path).flatMap((at) => segmentViewsOf(at))); for (const pat of rows) { for (const view of foldedViews(pat.fold)) if (rowMeetsPath(pat, view, rooted)) return pat.raw; } return null; } function shareDriveReadingsOf(path) { const drive = localDriveSpellingOf(path); const at = drive === undefined ? undefined : normalizeAbsPathLexically(drive); return at === undefined || at === normalizeAbsPathLexically(path) ? [path] : [path, at]; } function homeAnchorSegments(home) { return shareDriveReadingsOf(home).map((at) => normalizeAbsPathLexically(at).split(/[\\/]/).filter(Boolean)); } function rgGlobEscape(pattern) { return pattern.replace(/[?[\]{}]/g, (c) => `[${c}]`); } export function readDenyEntryProblem(entry, layer) { try { normalizeEntry(entry, layer); return undefined; } catch (e) { return e.message; } } function normalizeEntry(entry, layer) { const shape = typeof entry === "string" ? { pattern: entry, caseSensitive: false } : entry; if (typeof shape !== "object" || shape === null || typeof shape.pattern !== "string") { throw new Error(`readDenyPatterns (${layer}): entry ${JSON.stringify(entry)} is not a pattern string or { pattern, caseSensitive? } object.`); } if (shape.caseSensitive !== undefined && typeof shape.caseSensitive !== "boolean") { throw new Error(`readDenyPatterns (${layer}): entry ${JSON.stringify(entry)} has a non-boolean caseSensitive.`); } if (shape.pattern.includes("\\")) { throw new Error(`readDenyPatterns (${layer}): pattern ${JSON.stringify(shape.pattern)} contains a backslash — patterns are "/"-separated segment runs (both path families are matched); spell the segments with "/".`); } const segments = shape.pattern.split("/").filter(Boolean); const { homeAnchored, run } = readDenyPatternRun(segments.join("/")); if (run.length === 0) { throw new Error(`readDenyPatterns (${layer}): pattern ${JSON.stringify(shape.pattern)} contains no path segments${homeAnchored ? " below the home word" : ""} and would ${homeAnchored ? "name the whole home directory" : "deny nothing"}. ` + `Patterns are "/"-separated runs of path SEGMENTS (e.g. ".ssh", ".config/gcloud", "id_rsa*", "~/.sema/settings.json"); remove the entry or spell the segments.`); } return { pattern: canonicalReadDenyPattern(shape.pattern), caseSensitive: shape.caseSensitive === true }; } function canonicalReadDenyPattern(pattern) { const joined = pattern.split("/").filter(Boolean).join("/"); return readDenyRowAnchor(pattern) === "root" ? `/${joined}` : joined; } export function readDenyRowAnchor(pattern) { if (readDenyPatternRun(pattern).homeAnchored) return "home"; return pattern.startsWith("/") ? "root" : "none"; } export function persistedReadDenyEntryProblem(e) { if (typeof e !== "object" || e === null) return "entry is not an object"; const pattern = e.pattern; const caseSensitive = e.caseSensitive; if (typeof pattern !== "string") return "pattern is not a string"; if (typeof caseSensitive !== "boolean") return "caseSensitive is not a boolean"; if (pattern.includes("\\")) return "pattern contains a backslash"; const segments = pattern.split("/").filter(Boolean); if (readDenyPatternRun(segments.join("/")).run.length === 0) return "pattern has no path segments"; if (canonicalReadDenyPattern(pattern) !== pattern) return "pattern is not in canonical a/b form"; return null; } export function checkedReadDenyHomes(homes, seat = "readDenyHomes") { if (!Array.isArray(homes) || !homes.every((h) => validatePath(h, { intent: "validate-coordinate" }) !== undefined)) { throw new Error(`${seat}: expected an array of absolute ${seat === "readDenyHomes" ? "home paths" : "locations"}, got ${describeRejectedValue(homes)} — a home-anchored row ("~/…") is placed ${seat === "readDenyHomes" ? "under each" : "at each"}, so a relative or empty spelling would guard a place that is not the home's.`); } return homes; } export function compileReadDeny(additions = [], layer = "additions", builtin, rules = [], homes = [], linked = new Map()) { const ruleRows = rules.filter((r) => r.faces.includes("read")); const normalized = []; const seen = new Set(); const entryKey = (e) => `${e.caseSensitive ? "s" : "i"}:${e.pattern}`; const push = (e) => { const key = entryKey(e); if (seen.has(key)) return; seen.add(key); normalized.push(e); }; const columns = new Map(); for (const row of resolveReadDenyBuiltins(builtin)) { const e = normalizeEntry(row.pattern, "built-in"); push(e); const k = entryKey(e); if (!columns.has(k)) columns.set(k, { origin: "builtin", tier: row.tier }); } for (const entry of additions) { const e = normalizeEntry(entry, layer); push(e); const k = entryKey(e); const prev = columns.get(k); columns.set(k, { origin: "addition", ...(prev?.tier !== undefined ? { tier: prev.tier } : {}) }); } const homeSegments = [...new Set(checkedReadDenyHomes(homes))].flatMap(homeAnchorSegments); const compiled = normalized.map((e) => { const { run } = readDenyPatternRun(e.pattern); const rowAnchor = readDenyRowAnchor(e.pattern); const fold = e.caseSensitive ? "none" : "ascii"; const c = compileSegmentPattern(run, fold); if (c === null) throw new Error(`readDenyPatterns: pattern ${JSON.stringify(e.pattern)} compiled to zero segments.`); if (rowAnchor === "root") return { ...c, raw: e.pattern, anchors: [[]], rootedOnly: true }; if (rowAnchor === "none") return { ...c, raw: e.pattern }; if (homeSegments.length === 0) return { ...c, raw: e.pattern, unplacedHome: true }; return { ...c, raw: e.pattern, anchors: homeSegments.map((segs) => foldSegments(fold, segs)) }; }); if (!(linked instanceof Map)) throw new Error(`readDenyHomeLinks: expected a Map from a row's pattern to its locations, got ${typeof linked}.`); const linkedPlacements = []; normalized.forEach((e, i) => { const locations = linked.get(e.pattern); const { run } = readDenyPatternRun(e.pattern); if (locations === undefined || readDenyRowAnchor(e.pattern) === "none") return; const c = compiled[i]; const rest = c.segments.slice(readDenyRunHead(run)); const key = entryKey(e); for (const location of new Set(checkedReadDenyHomes(locations, "readDenyHomeLinks"))) { linkedPlacements.push({ pat: { raw: e.pattern, segments: rest, fold: c.fold, anchors: homeAnchorSegments(location).map((segs) => foldSegments(c.fold, segs)), rootedOnly: true }, key }); } }); const judgedEntries = [ ...compiled.map((pat, i) => ({ pat, key: entryKey(normalized[i]) })), ...linkedPlacements, ]; const judged = judgedEntries.map((x) => x.pat); const segmentRows = judgedEntries.map(({ pat, key }) => { const col = columns.get(key) ?? { origin: "addition" }; const faces = col.origin === "addition" ? ["read", "write"] : ["read"]; return Object.assign(segmentGuardRow(pat, { faces, polarity: "deny", exemptible: false, fold: pat.fold === "ascii" ? "ascii" : "exact", origin: col.origin, ...(col.tier !== undefined ? { tier: col.tier } : {}) }), { compiledSegment: pat }); }); const allTableRows = Object.freeze([...segmentRows, ...rules.map(ruleGuardRow)]); const readTableRows = allTableRows.filter((r) => r.faces.includes("read")); const rgExclusionGlobs = []; const rgProbeGlobs = []; for (const row of segmentRows) { if (row.placement !== "anywhere" || !row.faces.includes("read") || row.grammar.kind !== "segment-glob") continue; const flag = row.fold === "exact" ? "--glob" : "--iglob"; const g = rgGlobEscape(readDenyPatternRun(row.grammar.run).run); rgExclusionGlobs.push({ flag, glob: `!**/${g}` }, { flag, glob: `!**/${g}/**` }); rgProbeGlobs.push({ flag, glob: `**/${g}` }, { flag, glob: `**/${g}/**` }); } const placed = judged.filter((c) => c.anchors !== undefined); const unexcludableUnder = (scope) => placed.length > 0 && (!isAbsoluteForFamily("own", scope) || rowAtOrBelowOf(placed, normalizeAbsPathLexically(scope)) !== null); const rowAtOrBelow = (path) => { const hit = rowAtOrBelowOf(judged, path); if (hit !== null) return { pattern: hit }; return ruleRows.length === 0 ? null : { pattern: ruleRows[0].rule }; }; const rowUnderRoot = (scope) => { if (judged.length === 0 || !isAbsoluteForFamily("own", scope)) return null; return rowNamedByRootOf(judged, normalizeAbsPathLexically(scope)); }; const matchPath = (path, opts) => { const hit = denyTableHit(readTableRows, { views: [{ path, kind: "lexical", ...(opts?.aliasResolved === true ? { aliasResolved: true } : {}) }] }); return hit === null ? null : { pattern: hit.pattern }; }; const table = { entries: normalized, ...(ruleRows.length > 0 ? { ruleRows } : {}), matchPath, matchTarget: (canonicalKey, lexicalView, opts) => denyTableHit(readTableRows, denyTargetViews(canonicalKey, lexicalView, opts)), rgExclusionGlobs, rgProbeGlobs, unexcludableUnder, rowUnderRoot, rowAtOrBelow, compiledSegments: Object.freeze([...judged]), tableRows: allTableRows, }; Object.defineProperty(table, "id", { value: `rdt-${randomUUID()}`, enumerable: false }); return table; }