Files
skills/orchestrator/templates/page-firewall.js
T

490 lines
28 KiB
JavaScript
Raw Normal View History

const FIREWALL_LISTS = {"permissions.allow": "Allow", "permissions.ask": "Ask", "permissions.deny": "Deny", "sandbox.filesystem.allowWrite": "Sandbox: write", "sandbox.filesystem.denyRead": "Sandbox: no read", "sandbox.network.allowedDomains": "Sandbox: domain"};
const LIST_TONES = {allow: "ok", ask: "warn", deny: "bad"};
const ROW_FILTERS = [["allow", "Allow"], ["ask", "Ask"], ["deny", "Deny"], ["sandbox", "Sandbox"], ["signed", "Signed"]];
const ACTION_ORDER = ["deny", "ask", "allow", "sandbox", "signed"];
const ENTRY_HINTS = {
allow: "Claude Code runs a matching call without asking, so it never reaches the ask ledger.",
ask: "Claude Code asks before a matching call; the ask reaches the Agent Orchestrator.",
deny: "Claude Code refuses a matching call, so it never reaches the ask ledger.",
sandbox: "Bounds what a sandboxed command may write, read or reach.",
};
const HUB_SOURCE = "hub";
const SETTINGS_RESULTS = {done: ["written", "ok"], refused: ["refused", "bad"], failed: ["failed", "bad"]};
const EVERY_AGENT = "*";
const TARGET_GONE = "gone";
const TARGET_PICK = "";
const SESSION_KEY = /^(?!-)[A-Za-z0-9_-]{1,128}$/;
const firewall = {data: null, busy: false, staged: {}, dom: null, targets: new Set(), picks: {}, shown: new Set(ROW_FILTERS.map(([id]) => id)), open: new Set()};
const knownAgents = () => Object.keys(people()).filter((key) => key !== HUMAN && SESSION_KEY.test(key)).sort();
const firewallGroups = () => (firewall.data ? firewall.data.groups : []);
const settingsFile = (path) => (firewall.data ? firewall.data.files.find((file) => file.path === path) : null);
const everyAgent = (agents) => !agents || (agents.length === 1 && agents[0] === EVERY_AGENT);
const groupOf = (agents) => firewallGroups().find((entry) => entry.members.join(",") === [...agents].sort().join(","));
const firewallPickOf = (key) => (Object.hasOwn(firewall.picks, key) ? firewall.picks[key] : TARGET_PICK);
function targetLabel(agents) {
if (everyAgent(agents)) {
return "all agents";
}
const group = groupOf(agents);
return revealed(group ? `group ${group.name}` : agents.map(labelOf).join(", "));
}
const signedMembers = (agents) => agents.map((key) => `${labelOf(key)} (${key})`).join(", ");
/**
* @param {string[]} agents the session ids a payload is signed for.
* @returns {string} the scope the passkey prompt names with every session id the signature covers: for all agents, for group X: A (s1), B (s2), for agent A (s1) or for agents A (s1), B (s2).
*/
function scopeLabel(agents) {
if (everyAgent(agents)) {
return "for all agents";
}
const group = groupOf(agents);
return group ? `for group ${group.name}: ${signedMembers(agents)}` : `for ${agents.length === 1 ? "agent" : "agents"} ${signedMembers(agents)}`;
}
/**
* @param {Object} rule a rule as signed: kinds, tool, pattern, scope, expires in epoch seconds and agents.
* @returns {string[]} the lines the passkey prompt shows for it: what it grants, in which directory, until when and for which sessions.
*/
function ruleLines(rule) {
const end = new Date(rule.expires * 1000).toISOString().slice(0, 16).replace("T", " ");
const days = (Math.max(rule.expires - nowSeconds(), 0) / DAY).toFixed(1);
return [`${rule.kinds.join(", ")}${rule.tool ? ` ${rule.tool}` : ""}: ${rule.pattern}`, `in ${rule.scope}`, `until ${end} UTC, in ${days} days`, scopeLabel(Object.hasOwn(rule, "agents") ? rule.agents : [EVERY_AGENT])];
}
/**
* @param {string} label what the operator signs.
* @param {Object} prepared the hub's offer: payload, the signed rule itself or an approval, and enables, the signed rule an approval turns on.
* @returns {string} the passkey prompt's text, with every line of ruleLines() for the rule that takes effect, each line through revealed().
*/
function promptText(label, prepared) {
const rule = prepared.payload && prepared.payload.kind === "rule" ? prepared.payload : prepared.enables;
return [label, ...(rule ? ruleLines(rule) : [])].map(revealed).join("\n");
}
const targetPill = (agents) => h("span", {class: "pill", "data-tone": "neutral", "data-agents": JSON.stringify(agents === undefined ? [EVERY_AGENT] : agents), title: "Applies to"}, targetLabel(agents));
function targetOptions(keyed) {
return [
...(keyed ? [h("option", {value: TARGET_PICK, disabled: true}, "Pick a target")] : []),
h("option", {value: "all"}, "All agents"),
...knownAgents().map((key) => h("option", {value: `agent:${key}`}, revealed(`Agent ${labelOf(key)}`))),
...firewallGroups().map((group) => h("option", {value: `group:${group.id}`}, revealed(`Group ${group.name}`))),
];
}
function fillTargets(select) {
const keyed = "targetKey" in select.dataset;
const value = keyed ? firewallPickOf(select.dataset.targetKey) : select.value;
select.replaceChildren(...targetOptions(keyed));
const gone = value !== TARGET_PICK && ![...select.options].some((option) => option.value === value);
if (gone) {
select.append(h("option", {value: TARGET_GONE, disabled: true}, "Target gone: pick again"));
select.value = TARGET_GONE;
} else if (value !== TARGET_PICK || keyed) {
select.value = value;
}
const waiting = keyed && value === TARGET_PICK;
select.setCustomValidity(gone ? "The chosen group or agent is gone: pick the target again" : waiting ? "Pick a target first" : "");
const scope = select.form || select.parentElement;
for (const button of scope ? scope.querySelectorAll("[data-sign]") : []) {
button.disabled = (gone || waiting) && !select.disabled;
}
}
/**
* @param {string} label the select's accessible name.
* @param {string|null} key the rule set whose pick the page keeps across redraws, with no default until the operator picks; null for a select that starts on all agents.
* @returns {HTMLSelectElement} the target select.
*/
function targetSelect(label, key) {
const select = h("select", {class: "field", "aria-label": label, "data-target-key": key});
fillTargets(select);
select.addEventListener("change", () => {
if (key !== null) {
firewall.picks[key] = select.value;
}
fillTargets(select);
});
firewall.targets.add(select);
return select;
}
/**
* @param {string} value the target select's value: all, agent:<session>, group:<id> or gone.
* @returns {string[]|null} the sorted session ids the rule is signed for, ["*"] only for all, null for a gone or unknown target.
*/
function agentsOf(value) {
if (value.startsWith("agent:")) {
return [value.slice("agent:".length)];
}
if (value.startsWith("group:")) {
const group = firewallGroups().find((entry) => entry.id === value.slice("group:".length));
return group ? group.members : null;
}
return value === "all" ? [EVERY_AGENT] : null;
}
/**
* @param {string} kind the rule kind; a review rule always applies to every agent.
* @param {string} value the target select's value.
* @returns {string[]|null} the agents to sign; null for a group that is gone, which the hub refuses.
*/
const ruleAgents = (kind, value) => (kind === "review" ? [EVERY_AGENT] : agentsOf(value));
async function fetchFirewall() {
if (firewall.busy) {
return;
}
firewall.busy = true;
try {
const response = await fetch("/api/firewall", {cache: "no-store"});
if (response.ok) {
firewall.data = await response.json();
}
} catch {
ui.offline = true;
}
firewall.busy = false;
drawFirewall();
}
function drawFirewall() {
const dom = firewall.dom;
if (!dom || !dom.settings.isConnected) {
return;
}
for (const select of [...firewall.targets]) {
if (select.isConnected) {
fillTargets(select);
} else {
firewall.targets.delete(select);
}
}
for (const node of document.querySelectorAll("#main [data-agents]")) {
node.textContent = targetLabel(JSON.parse(node.dataset.agents));
}
dom.groups.replaceChildren(groupsCard());
dom.sets.replaceChildren(setsCard());
dom.patterns.replaceChildren(patternsCard());
dom.settings.replaceChildren(settingsCard());
}
function firewallSections() {
firewall.dom = {groups: h("div", {}), sets: h("div", {}), patterns: h("div", {}), settings: h("div", {})};
window.setTimeout(() => {
drawFirewall();
fetchFirewall();
}, 0);
return [firewall.dom.settings, firewall.dom.groups, firewall.dom.sets, firewall.dom.patterns];
}
async function firewallPost(path, payload, label) {
const result = await sendJson(path, payload);
toast(result.ok ? label : `${label}: not possible`, result.ok ? [] : fail(result), result.ok);
await fetchFirewall();
return result.ok;
}
function groupsCard() {
const name = h("input", {class: "field", type: "text", placeholder: "builders", "aria-label": "Group name"});
const members = h("select", {class: "field", multiple: true, size: Math.min(Math.max(knownAgents().length, 2), 6), "aria-label": "Members"}, knownAgents().map((key) => h("option", {value: key}, labelOf(key))));
const form = h("form", {class: "fw-form", "aria-label": "New agent group"}, h("label", {}, "Name", name), h("label", {}, "Members", members),
h("button", {type: "submit", class: "btn soft", ...named("Add group", "Add group: name the chosen agents as one rule target")}, icon("bi-people"), "Add group"));
form.addEventListener("submit", async (event) => {
event.preventDefault();
const chosen = [...members.selectedOptions].map((option) => option.value).sort();
await firewallPost("/api/firewall/groups", {name: name.value.trim(), members: chosen}, "Group added");
});
const rows = firewallGroups().map((group) => h("div", {class: "rule-row", "data-group": group.name}, h("strong", {}, revealed(group.name)), h("span", {class: "muted"}, revealed(group.members.map(labelOf).join(", "))), h("span", {class: "spacer"}),
iconBtn("bi-trash3", "Delete group", "btn small danger", () => firewallPost(`/api/firewall/groups/${encodeURIComponent(group.id)}/delete`, {}, "Group deleted"), `Delete group ${group.name}: rules signed for it keep their agents`)));
return h("section", {class: "card stack-tight", "aria-label": "Agent groups"}, h("h3", {}, "Agent groups"),
h("p", {class: "muted"}, "A rule applies to one agent, a group or all agents. A group's members are signed into each rule when you sign it."),
rows.length ? h("div", {class: "mini"}, rows) : h("p", {class: "muted"}, "No group yet."), form);
}
function setsCard() {
const name = h("input", {class: "field", type: "text", placeholder: "ci", "aria-label": "Set name"});
const days = h("input", {class: "field", type: "number", min: 1, max: 366, value: 7, "aria-label": "Days"});
const chosen = h("select", {class: "field", multiple: true, size: Math.min(Math.max(state.rules.length, 2), 6), "aria-label": "Rules"}, state.rules.map((rule) => h("option", {value: rule.id}, `${rule.kind} ${rule.tool} ${rule.pattern}`.replace(" ", " "))));
const form = h("form", {class: "fw-form", "aria-label": "New rule set"}, h("label", {}, "Name", name), h("label", {}, "Days", days), h("label", {}, "Rules", chosen),
h("button", {type: "submit", class: "btn soft", ...named("Save set", "Save set: keep the chosen rules as one named rule set")}, icon("bi-collection"), "Save set"));
form.addEventListener("submit", async (event) => {
event.preventDefault();
await firewallPost("/api/firewall/sets", {name: name.value.trim(), days: Number(days.value), rules: [...chosen.selectedOptions].map((option) => option.value)}, "Rule set saved");
});
const sets = firewall.data ? firewall.data.sets : [];
const rows = sets.map((set) => setRow(set));
return h("section", {class: "card stack-tight", "aria-label": "Rule sets"}, h("h3", {}, "Rule sets"),
h("p", {class: "muted"}, "Take a set over as a whole for one agent, a group or all agents: each of its rules is signed with your passkey for that target."),
rows.length ? h("div", {class: "mini"}, rows) : h("p", {class: "muted"}, "No rule set yet."), form);
}
function setRow(set) {
const target = targetSelect(`Take over ${set.name} for`, set.id);
const take = h("button", {type: "button", class: "btn small soft", "data-sign": "", ...named("Take over", `Take over: sign the ${set.rules.length} rules of ${set.name} for the chosen agents`)}, icon("bi-box-arrow-in-down"), "Take over");
take.addEventListener("click", async () => {
take.disabled = true;
await takeOver(set, agentsOf(target.value));
fillTargets(target);
});
const row = h("div", {class: "rule-row", "data-set": set.name}, h("strong", {}, revealed(set.name)), h("span", {class: "muted"}, `${set.rules.length} rules, ${set.days} days`), h("span", {class: "spacer"}), target, take,
iconBtn("bi-trash3", "Delete set", "btn small danger", () => firewallPost(`/api/firewall/sets/${encodeURIComponent(set.id)}/delete`, {}, "Rule set deleted"), `Delete set ${set.name}: its signed rules stay`));
fillTargets(target);
return row;
}
/**
* @param {Object} set the rule set to take over: name, days and rules.
* @param {string[]|null} agents the target from agentsOf().
*/
async function takeOver(set, agents) {
if (!agents) {
toast(`${set.name}: not taken over`, ["No target picked, or the picked group or agent is gone"], false);
return;
}
const expires = Math.floor(nowSeconds()) + set.days * DAY;
let created = 0;
for (const rule of set.rules) {
const prepared = await sendJson("/api/rules/sign", {...rule, expires, agents: rule.kind === "review" ? [EVERY_AGENT] : agents});
const signed = prepared.ok && prepared.data ? await assertion(prepared.data, `Rule set ${set.name}`) : null;
const result = signed ? await sendJson("/api/rules", {rule: prepared.data.payload, assertion: signed, enabled: true}) : prepared;
created += signed && result.ok ? 1 : 0;
}
toast(`${set.name}: ${created} of ${set.rules.length} rules signed for ${targetLabel(agents)}`, [], created === set.rules.length);
await load();
}
function patternsCard() {
const patterns = firewall.data ? firewall.data.patterns : [];
return h("section", {class: "card stack-tight", "aria-label": "Never granted"}, h("h3", {}, "Never granted by a rule"),
h("p", {class: "muted"}, "An ask rule never grants a command that matches one of these patterns; Claude Code asks instead."),
h("ul", {class: "mini fw-patterns"}, patterns.map((pattern) => h("li", {}, h("div", {class: "row"}, pill(pattern.label, "bad"), h("code", {}, pattern.pattern))))));
}
function stageChange(path, change) {
const changes = (firewall.staged[path] || []).filter((entry) => !(entry.list === change.list && entry.entry === change.entry));
firewall.staged[path] = [...changes, change];
drawFirewall();
}
function unstageChange(path, change) {
firewall.staged[path] = (firewall.staged[path] || []).filter((entry) => entry !== change);
if (!firewall.staged[path].length) {
delete firewall.staged[path];
}
drawFirewall();
}
/**
* @param {Object} file a settings file as /api/firewall lists it.
* @returns {Object[]} one row per permissions entry and sandbox value: filter, action, tone, tool, pattern, list, entry (null for a sandbox value no list holds), hits and never.
*/
function fileRows(file) {
const permissions = file.groups.flatMap((group) => group.entries.map((entry) => ({filter: entry.list, action: FIREWALL_LISTS[`permissions.${entry.list}`], tone: LIST_TONES[entry.list], tool: group.tool, pattern: entry.spec, list: `permissions.${entry.list}`, entry: entry.entry, hits: entry.hits, never: entry.never})));
const sandbox = file.sandbox.flatMap((entry) => {
const base = {filter: "sandbox", action: FIREWALL_LISTS[entry.key] || "Sandbox", tone: "info", tool: entry.key.replace(/^sandbox\./, ""), list: entry.key, hits: null, never: ""};
return entry.values ? entry.values.map((value) => ({...base, pattern: value, entry: value})) : [{...base, pattern: JSON.stringify(entry.value), entry: null}];
});
return [...permissions, ...sandbox];
}
/**
* @returns {Object[]} one row per stored hub rule, with the hits /api/firewall counted for it; pending until the next fetch counts a new rule.
*/
function signedRows() {
const counted = firewall.data.rules;
return state.rules.map((rule) => ({filter: "signed", action: `Grant ${rule.kind}`, tone: "ok", tool: rule.tool, pattern: rule.pattern, rule, ...(Object.hasOwn(counted, rule.id) ? counted[rule.id] : {hits: null, never: "", pending: true})}));
}
const fileStatus = (file) => (file.error ? pill(file.error, "bad") : file.exists ? pill(`sha256 ${file.sha256.slice(0, 12)}`, "neutral") : pill("missing: a change creates it", "warn"));
const byAction = (one, two) => ACTION_ORDER.indexOf(one.filter) - ACTION_ORDER.indexOf(two.filter);
function sourceGroups() {
return [{id: HUB_SOURCE, label: "Hub rules", file: false, status: null, rows: signedRows()}, ...firewall.data.files.map((file) => ({id: file.path, label: file.path, file: true, status: fileStatus(file), rows: fileRows(file)}))];
}
function hitsCell(row) {
if (row.pending) {
return h("span", {class: "muted", title: "Counted with the next refresh"}, "…");
}
if (row.never) {
return h("span", {class: "muted", title: `The matcher never grants a command that ${row.never}`}, `never granted: ${row.never}`);
}
if (row.hits === null) {
return h("span", {class: "muted", title: "Only asks reach the ledger the hits come from"}, "not seen");
}
return row.hits ? h("strong", {}, String(row.hits)) : ["0 ", h("span", {class: "pill", "data-tone": "warn", title: "No ask of the counted days matched this entry"}, "unused")];
}
function actionCell(row) {
const rule = row.rule;
const action = h("span", {class: "pill", "data-tone": row.tone, title: rule ? RULE_HINTS[rule.kind] : ENTRY_HINTS[row.filter]}, row.action);
if (!rule) {
return action;
}
const until = `until ${absolute.format(stampOf(rule.expires))}`;
return [action, rule.expires <= nowSeconds() ? pill("expired", "bad") : null, h("span", {class: "pill", "data-tone": rule.enabled ? "ok" : "neutral", title: until}, rule.enabled ? "active" : "off")];
}
function changeCell(group, row) {
const rule = row.rule;
if (rule) {
return [btn(rule.enabled ? "Turn off" : "Turn on", "btn small", () => changeRule(rule, rule.enabled ? "disable" : "enable")), btn("Delete", "btn small danger", () => {
if (window.confirm("Delete rule?")) {
changeRule(rule, "delete");
}
})];
}
const editable = row.entry !== null && Object.hasOwn(FIREWALL_LISTS, row.list);
return editable ? iconBtn("bi-dash-circle", "Remove", "btn small ghost", () => stageChange(group.id, {list: row.list, op: "remove", entry: row.entry}), `Remove: stage the removal of ${revealed(row.entry)} from ${row.list}`) : null;
}
/**
* @param {Object} group the source the row belongs to: id, label and file, false for the hub rules.
* @param {Object} row a row of fileRows() or signedRows().
* @returns {HTMLTableRowElement} the row: action, tool, pattern, scope, source, hits and its change buttons.
*/
function ruleRow(group, row) {
const rule = row.rule;
const pattern = [h("code", {}, revealed(row.pattern)), rule && rule.kind !== "decision" ? h("span", {class: "muted"}, " in ", h("code", {}, rule.scope ? revealed(rule.scope) : "–")) : null];
const source = rule ? h("span", {class: "muted"}, rule.signed ? "Hub, passkey-signed" : "Hub, unsigned: has no effect") : h("code", {class: "fw-path"}, revealed(group.label));
return h("tr", {"data-filter": row.filter, "data-list": row.list, "data-entry": row.entry, "data-rule": rule ? rule.id : null, "data-unused": row.hits === 0},
h("td", {}, actionCell(row)), h("td", {}, row.tool ? h("code", {}, revealed(row.tool)) : "–"), h("td", {}, pattern),
h("td", {}, rule ? targetPill(rule.agents) : h("span", {class: "muted"}, "file")), h("td", {}, source),
h("td", {class: "fw-hits"}, hitsCell(row)), h("td", {class: "fw-change"}, changeCell(group, row)));
}
function sourceBody(group) {
const rows = group.rows.filter((row) => firewall.shown.has(row.filter)).sort(byAction);
if (!rows.length && group.rows.length) {
return null;
}
const open = firewall.open.has(group.id);
const unused = rows.filter((row) => row.hits === 0).length;
const fold = h("button", {type: "button", class: "fw-fold", "aria-expanded": String(open), on: {click: () => {
if (open) {
firewall.open.delete(group.id);
} else {
firewall.open.add(group.id);
}
drawFirewall();
}}}, icon(open ? "bi-chevron-down" : "bi-chevron-right"), group.file ? h("code", {class: "fw-path"}, revealed(group.label)) : h("strong", {}, group.label), counter(rows.length), unused ? pill(`${unused} unused`, "warn") : null);
return h("tbody", {"data-source": group.id}, h("tr", {class: "fw-source"}, h("th", {colspan: 7, scope: "rowgroup"}, h("div", {class: "row"}, fold, group.status))), open ? rows.map((row) => ruleRow(group, row)) : []);
}
function filterChips() {
return h("div", {class: "chips", role: "group", "aria-label": "Show rules"}, ROW_FILTERS.map(([id, label]) => h("button", {type: "button", class: "chip", "data-filter": id, "aria-pressed": String(firewall.shown.has(id)), on: {click: () => {
if (firewall.shown.has(id)) {
firewall.shown.delete(id);
} else {
firewall.shown.add(id);
}
drawFirewall();
}}}, label)));
}
function ruleTable() {
const bodies = sourceGroups().map(sourceBody).filter(Boolean);
if (!bodies.length) {
return h("p", {class: "muted"}, "No rule matches the chosen chips.");
}
const head = h("thead", {}, h("tr", {}, ["Action", "Tool", "Pattern", "Scope", "Source", `Hits (${firewall.data.days} days)`].map((label) => h("th", {scope: "col"}, label)), h("th", {scope: "col", "aria-label": "Change"})));
return h("div", {class: "fw-table-wrap"}, h("table", {class: "fw-table", "aria-label": "Firewall rules"}, head, bodies));
}
function addForm(files) {
const file = h("select", {class: "field", "aria-label": "Settings file"}, files.map((entry) => h("option", {value: entry.path}, entry.path)));
const list = h("select", {class: "field", "aria-label": "List"}, Object.entries(FIREWALL_LISTS).map(([value, label]) => h("option", {value}, `${label} (${value})`)));
const entry = h("input", {class: "field", type: "text", placeholder: "Bash(make test:*)", "aria-label": "Entry"});
const form = h("form", {class: "fw-form", "aria-label": "Stage a settings change"}, h("label", {}, "File", file), h("label", {}, "List", list), h("label", {}, "Entry", entry),
h("button", {type: "submit", class: "btn soft", ...named("Stage", "Stage: add the entry to the list; nothing is written before you sign")}, icon("bi-plus-lg"), "Stage"));
form.addEventListener("submit", (event) => {
event.preventDefault();
if (entry.value.trim()) {
stageChange(file.value, {list: list.value, op: "add", entry: entry.value.trim()});
}
});
return form;
}
const settingsItems = () => Object.entries(firewall.staged).filter(([file]) => settingsFile(file)).map(([file, changes]) => ({kind: "settings", file, base: settingsFile(file).sha256, changes}));
function stagedList() {
const rows = Object.entries(firewall.staged).flatMap(([path, changes]) => changes.map((change) => h("li", {"data-staged": `${change.op} ${change.entry}`}, h("div", {class: "row"},
pill(change.op === "add" ? "add" : "remove", change.op === "add" ? "ok" : "bad"), h("code", {}, revealed(`${change.list}: ${change.entry}`)), h("span", {class: "muted"}, revealed(path)), h("span", {class: "spacer"}),
iconBtn("bi-x-circle", "Unstage", "btn small ghost", () => unstageChange(path, change), "Unstage: drop this change")))));
if (!rows.length) {
return h("p", {class: "muted"}, "No staged change.");
}
const review = h("button", {type: "button", class: "btn primary", ...named("Review and sign", "Review and sign: show the diff, then sign it with your passkey")}, icon("bi-file-diff"), "Review and sign");
review.addEventListener("click", reviewSettings);
return h("div", {class: "stack-tight"}, h("ul", {class: "mini", "aria-label": "Staged changes"}, rows), h("div", {class: "actions"}, review));
}
function settingsCard() {
const data = firewall.data;
if (!data) {
return h("section", {class: "card"}, h("p", {class: "muted"}, "Loading the settings …"));
}
const files = data.files;
return h("section", {class: "card stack-tight fw-rules", "aria-label": "Firewall rules"}, h("h3", {}, "Firewall rules"),
h("p", {class: "muted"}, `Every rule in one table: the hub rules and the permissions and sandbox entries of the listed settings files as the host read them, one folded group per source. Hits count the asks of the last ${data.days} days a rule matches, with the matcher of the rule request dry run; allow, deny and sandbox entries never reach the ask ledger and show not seen. A settings change shows as a diff, is signed with your passkey and is written by the host, never by an agent.`),
data.error ? h("p", {class: "muted"}, data.error) : null,
filterChips(), ruleTable(), files.length ? addForm(files) : null, stagedList());
}
function diffLines(text) {
return h("pre", {class: "fw-diff"}, text.split("\n").map((line) => h("span", {class: line.startsWith("+") && !line.startsWith("+++") ? "add" : line.startsWith("-") && !line.startsWith("---") ? "del" : null}, `${revealed(line)}\n`)));
}
async function reviewSettings() {
const items = settingsItems();
const preview = await sendJson("/api/firewall/preview", {items});
if (!preview.ok || !preview.data) {
toast("Settings: not possible", fail(preview), false);
await fetchFirewall();
return;
}
const outcome = h("div", {class: "mini", "aria-live": "polite"});
const confirm = h("button", {type: "button", class: "btn primary", ...named("Sign and apply", "Sign and apply: hand exactly this diff to the host with your passkey signature")}, icon("bi-pen"), "Sign and apply");
confirm.addEventListener("click", async () => {
confirm.disabled = true;
if (!await applySettings(items, outcome)) {
confirm.disabled = false;
}
});
openSheet("Settings change", [
h("p", {class: "muted"}, "The host writer checks your signature, refuses a file that changed since and writes exactly this change; it keeps every other key and a backup beside the file."),
h("div", {class: "stack-tight", "aria-label": "Diff"}, preview.data.diffs.map((entry) => h("div", {"data-diff": entry.file}, h("code", {}, revealed(entry.file)), diffLines(entry.diff)))),
outcome,
h("div", {class: "actions"}, h("span", {class: "spacer"}), btn("Cancel", "btn ghost", closeSheet), confirm),
], true);
}
async function applySettings(items, outcome) {
const signed = await proofFor(items, "Settings");
if (signed === null) {
outcome.replaceChildren(h("p", {class: "muted"}, "Not signed: nothing was handed over."));
return false;
}
const result = await sendJson("/api/firewall/settings", {items, proof: signed.proof});
if (!result.ok || !result.data) {
toast("Settings: not possible", fail(result), false);
outcome.replaceChildren(h("p", {class: "muted"}, `${result.status} ${result.text}`.trim()));
return false;
}
outcome.replaceChildren(h("p", {class: "muted", "data-firewall": result.data.id}, `Signed and handed to the host (job ${result.data.id}); the firewall unit writes now.`));
followJob(`/api/firewall/settings/${encodeURIComponent(result.data.id)}`, outcome, Date.now(), (data) => {
const {good, lines} = showJob(data, outcome, "file", SETTINGS_RESULTS);
toast(`Settings: ${good ? "written" : "not everything written"}`, lines, good);
if (good) {
firewall.staged = {};
}
fetchFirewall();
});
return true;
}