feat(paths): enforce fifth-domain physical redirects

This commit is contained in:
冰朔 2026-09-12 18:23:38 +08:00
commit c180a12d8d
15 changed files with 350 additions and 18 deletions

View file

@ -2,6 +2,7 @@
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import { classifyPath } from "../fifth-domain-path-gate/fifth-domain-path-gate.mjs";
const POLICY_PATH = "/Volumes/JZAO/HoloLake/persona-runtime/repo-012-main/routing/persona-host-write-boundary.json";
const policy = JSON.parse(fs.readFileSync(POLICY_PATH, "utf8"));
@ -36,11 +37,32 @@ function protectedTarget(resolved) {
return policy.canonical_protected_roots.some(item => patternRegex(item).test(resolved));
}
function sensitiveHostEntryTarget(resolved) {
return (policy.sensitive_host_entry_paths || []).some(item => patternRegex(item).test(resolved));
}
function isAllowed(host, target) {
const rule = policy.hosts[host];
if (!rule) return { allowed: false, code: "HOST_UNKNOWN" };
if (rule.write_mode.startsWith("READ_ONLY")) return { allowed: false, code: "HOST_READ_ONLY" };
const resolved = normal(target);
const hostRoot = rule.allowed_write_roots.find((item) => patternRegex(item).test(resolved));
const preliminaryGate = classifyPath(resolved, hostRoot ? "BRANCH_WORK" : "CANONICAL_WRITE");
if ([78, 79].includes(preliminaryGate.exit_code)) {
const console = activeConsole(host);
const matched = console?.write_roots?.find((item) => patternRegex(item).test(resolved));
const maintenance = /PATH_CONVERGENCE|PHYSICAL_RETIREMENT/.test(console?.execution_semantics || "");
if (matched && maintenance) return { allowed: true, code: "ACTIVE_ZERO_CORE_CONSOLE_RETIRED_PATH_MAINTENANCE_SCOPE", resolved, matched, console_id: console.console_id };
return { allowed: false, code: `FIFTH_DOMAIN_${preliminaryGate.code}`, resolved, redirect_to: preliminaryGate.redirect_to, history: preliminaryGate.history };
}
if (sensitiveHostEntryTarget(resolved)) {
const console = activeConsole(host);
const matched = console?.write_roots?.find((item) => patternRegex(item).test(resolved));
return matched
? { allowed: true, code: "ACTIVE_ZERO_CORE_CONSOLE_SENSITIVE_HOST_ENTRY_SCOPE", resolved, matched, console_id: console.console_id }
: { allowed: false, code: "SENSITIVE_HOST_ENTRY_REQUIRES_ACTIVE_ZERO_CORE_CONSOLE", resolved };
}
if (hostRoot) return { allowed: true, code: "WITHIN_HOST_WRITE_ROOT", resolved, matched: hostRoot };
if (protectedTarget(resolved)) {
const console = activeConsole(host);
const matched = console?.write_roots?.find((item) => patternRegex(item).test(resolved));
@ -48,10 +70,7 @@ function isAllowed(host, target) {
? { allowed: true, code: "ACTIVE_ZERO_CORE_CONSOLE_TASK_SCOPE", resolved, matched, console_id: console.console_id }
: { allowed: false, code: "PROTECTED_ROOT_REQUIRES_ACTIVE_ZERO_CORE_CONSOLE", resolved };
}
const matched = rule.allowed_write_roots.find((item) => patternRegex(item).test(resolved));
return matched
? { allowed: true, code: "WITHIN_HOST_WRITE_ROOT", resolved, matched }
: { allowed: false, code: "WRITE_OUTSIDE_HOST_ROOT", resolved };
return { allowed: false, code: "WRITE_OUTSIDE_HOST_ROOT", resolved };
}
function emit(result, hook = false) {
@ -93,10 +112,22 @@ const MUTATING_SHELL = /(?:^|[;&|\s])(?:rm|mv|cp|install|mkdir|rmdir|touch|chmod
function evaluateHook(host, input) {
const tool = String(input.tool_name || input.toolName || "");
const toolInput = input.tool_input || input.toolInput || {};
if (READ_TOOLS.has(tool)) return { allowed: true, code: "READ_ONLY_TOOL", host, tool };
if (READ_TOOLS.has(tool)) {
for (const candidate of collectPathValues(toolInput)) {
const gated = classifyPath(candidate, "CURRENT_SELECTION");
if ([78, 79].includes(gated.exit_code)) return { allowed: false, code: `FIFTH_DOMAIN_${gated.code}`, host, tool, resolved: gated.resolved, redirect_to: gated.redirect_to, history: gated.history };
}
return { allowed: true, code: "READ_ONLY_TOOL", host, tool };
}
if (tool === "Bash") {
const command = String(toolInput.command || "");
if (!MUTATING_SHELL.test(command)) return { allowed: true, code: "READ_ONLY_SHELL", host, tool };
if (!MUTATING_SHELL.test(command)) {
for (const candidate of shellPaths(command)) {
const gated = classifyPath(candidate, "CURRENT_SELECTION");
if ([78, 79].includes(gated.exit_code)) return { allowed: false, code: `FIFTH_DOMAIN_${gated.code}`, host, tool, resolved: gated.resolved, redirect_to: gated.redirect_to, history: gated.history };
}
return { allowed: true, code: "READ_ONLY_SHELL", host, tool };
}
const rule = policy.hosts[host];
if (!rule || rule.write_mode.startsWith("READ_ONLY")) return { allowed: false, code: "HOST_READ_ONLY", host, tool };
const candidates = shellPaths(command);