diff --git a/docs/requirements/001-agent-orchestrator.md b/docs/requirements/001-agent-orchestrator.md index 140965e..3288a39 100644 --- a/docs/requirements/001-agent-orchestrator.md +++ b/docs/requirements/001-agent-orchestrator.md @@ -747,14 +747,26 @@ flowchart TD ### Phase 6: Firewall (S6) -- [ ] The tab is called Firewall and lists hub rules, allow and deny +- [x] The tab is called Firewall and lists hub rules, allow and deny patterns and the settings permissions, grouped by tool and prefix as read from the settings files. -- [ ] A rule or rule set applies to one agent, a named group or all agents. -- [ ] A settings change is written only by the host executor after the +- [x] A rule or rule set applies to one agent, a named group or all agents. +- [x] A settings change is written only by the host executor after the passkey check, only to a listed file, with a backup beside it; a forged, replayed or unlisted-file job changes nothing (unit tests). +Limit: a session id is not an authenticated identity; an agent that can run +`claude --resume ` inherits every rule aimed at that session. A +decision names its agent in a free MCP argument: a rule aimed at sessions +decides it only for the one session holding that name now, never through a +name a session held before, and for none when several hold it. + +Widening needs a fresh passkey signature over exactly what takes effect: +signing a rule shows its pattern, directory, end and every session id of its +target, a group's or rule set's resolution at that moment, and turning a rule +on again needs its own signature; groups, rule sets and turning off stay +free, since they grant nothing by themselves. + ### Phase 6: Resources (S7) Replaced by [003 W4 Resources](003-operator-workbench.md#w4-resources). diff --git a/orchestrator/Dockerfile b/orchestrator/Dockerfile index d1b3ac1..c311ccb 100644 --- a/orchestrator/Dockerfile +++ b/orchestrator/Dockerfile @@ -59,7 +59,7 @@ RUN chmod 0755 /opt/voices /opt/whisper COPY server.py /app/server.py COPY kvb_orchestrator /app/kvb_orchestrator -COPY client/hub_limits.py client/signing.py client/passkey.py client/verifier.py client/queue_live.py /app/client/ +COPY client/hub_limits.py client/signing.py client/emoji-variation.json client/passkey.py client/verifier.py client/queue_live.py /app/client/ COPY templates /app/templates COPY vendor /app/vendor diff --git a/orchestrator/client/cleanup.py b/orchestrator/client/cleanup.py index 8cb1e84..433c23e 100755 --- a/orchestrator/client/cleanup.py +++ b/orchestrator/client/cleanup.py @@ -80,7 +80,7 @@ def tree_facts(tree: Path) -> tuple[dict[str, str], bool]: data = json.loads(tree.read_text(encoding="utf-8")) known = {entry["path"]: entry.get("default") if isinstance(entry.get("default"), str) else "" for entry in data["repos"] if entry.get("known") is True and isinstance(entry.get("path"), str)} return known, data.get("delete_ready") is True - except (OSError, ValueError, KeyError, TypeError, AttributeError): + except Exception: # noqa: BLE001 return {}, False diff --git a/orchestrator/client/emoji-variation.json b/orchestrator/client/emoji-variation.json new file mode 100644 index 0000000..4a59186 --- /dev/null +++ b/orchestrator/client/emoji-variation.json @@ -0,0 +1,24 @@ +{ + "source": "https://www.unicode.org/Public/16.0.0/ucd/emoji/emoji-variation-sequences.txt", + "unicode": "16.0.0", + "bases": [ + "00A9", "00AE", "203C", "2049", "2122", "2139", "2194", "2195", "2196", "2197", "2198", "2199", "21A9", "21AA", "231A", "231B", "2328", "23CF", "23E9", "23EA", + "23EB", "23EC", "23ED", "23EE", "23EF", "23F0", "23F1", "23F2", "23F3", "23F8", "23F9", "23FA", "24C2", "25AA", "25AB", "25B6", "25C0", "25FB", "25FC", "25FD", + "25FE", "2600", "2601", "2602", "2603", "2604", "260E", "2611", "2614", "2615", "2618", "261D", "2620", "2622", "2623", "2626", "262A", "262E", "262F", "2638", + "2639", "263A", "2640", "2642", "2648", "2649", "264A", "264B", "264C", "264D", "264E", "264F", "2650", "2651", "2652", "2653", "265F", "2660", "2663", "2665", + "2666", "2668", "267B", "267E", "267F", "2692", "2693", "2694", "2695", "2696", "2697", "2699", "269B", "269C", "26A0", "26A1", "26A7", "26AA", "26AB", "26B0", + "26B1", "26BD", "26BE", "26C4", "26C5", "26C8", "26CE", "26CF", "26D1", "26D3", "26D4", "26E9", "26EA", "26F0", "26F1", "26F2", "26F3", "26F4", "26F5", "26F7", + "26F8", "26F9", "26FA", "26FD", "2702", "2705", "2708", "2709", "270A", "270B", "270C", "270D", "270F", "2712", "2714", "2716", "271D", "2721", "2728", "2733", + "2734", "2744", "2747", "274C", "274E", "2753", "2754", "2755", "2757", "2763", "2764", "2795", "2796", "2797", "27A1", "27B0", "27BF", "2934", "2935", "2B05", + "2B06", "2B07", "2B1B", "2B1C", "2B50", "2B55", "3030", "303D", "3297", "3299", "1F004", "1F170", "1F171", "1F17E", "1F17F", "1F202", "1F21A", "1F22F", "1F237", "1F30D", + "1F30E", "1F30F", "1F315", "1F31C", "1F321", "1F324", "1F325", "1F326", "1F327", "1F328", "1F329", "1F32A", "1F32B", "1F32C", "1F336", "1F378", "1F37D", "1F393", "1F396", "1F397", + "1F399", "1F39A", "1F39B", "1F39E", "1F39F", "1F3A7", "1F3AC", "1F3AD", "1F3AE", "1F3C2", "1F3C4", "1F3C6", "1F3CA", "1F3CB", "1F3CC", "1F3CD", "1F3CE", "1F3D4", "1F3D5", "1F3D6", + "1F3D7", "1F3D8", "1F3D9", "1F3DA", "1F3DB", "1F3DC", "1F3DD", "1F3DE", "1F3DF", "1F3E0", "1F3ED", "1F3F3", "1F3F5", "1F3F7", "1F408", "1F415", "1F41F", "1F426", "1F43F", "1F441", + "1F442", "1F446", "1F447", "1F448", "1F449", "1F44D", "1F44E", "1F453", "1F46A", "1F47D", "1F4A3", "1F4B0", "1F4B3", "1F4BB", "1F4BF", "1F4CB", "1F4DA", "1F4DF", "1F4E4", "1F4E5", + "1F4E6", "1F4EA", "1F4EB", "1F4EC", "1F4ED", "1F4F7", "1F4F9", "1F4FA", "1F4FB", "1F4FD", "1F508", "1F50D", "1F512", "1F513", "1F549", "1F54A", "1F550", "1F551", "1F552", "1F553", + "1F554", "1F555", "1F556", "1F557", "1F558", "1F559", "1F55A", "1F55B", "1F55C", "1F55D", "1F55E", "1F55F", "1F560", "1F561", "1F562", "1F563", "1F564", "1F565", "1F566", "1F567", + "1F56F", "1F570", "1F573", "1F574", "1F575", "1F576", "1F577", "1F578", "1F579", "1F587", "1F58A", "1F58B", "1F58C", "1F58D", "1F590", "1F5A5", "1F5A8", "1F5B1", "1F5B2", "1F5BC", + "1F5C2", "1F5C3", "1F5C4", "1F5D1", "1F5D2", "1F5D3", "1F5DC", "1F5DD", "1F5DE", "1F5E1", "1F5E3", "1F5E8", "1F5EF", "1F5F3", "1F5FA", "1F610", "1F687", "1F68D", "1F691", "1F694", + "1F698", "1F6AD", "1F6B2", "1F6B9", "1F6BA", "1F6BC", "1F6CB", "1F6CD", "1F6CE", "1F6CF", "1F6E0", "1F6E1", "1F6E2", "1F6E3", "1F6E4", "1F6E5", "1F6E9", "1F6F0", "1F6F3" + ] +} diff --git a/orchestrator/client/permission_hook.py b/orchestrator/client/permission_hook.py index ee97f6b..263543a 100755 --- a/orchestrator/client/permission_hook.py +++ b/orchestrator/client/permission_hook.py @@ -30,9 +30,9 @@ from __future__ import annotations import argparse import json -import re import sys import time +import urllib.error import urllib.request from pathlib import Path @@ -51,7 +51,6 @@ DENIED = "The operator denied this in the Agent Orchestrator." CHANGED = "staged tree changed since the preview" UNBOUND = "the staged tree cannot be read, so the allow is not bound to it" REASON_LIMIT = 2000 -CONTROL = re.compile(r"[\x00-\x1f\x7f]") FAILURES = (OSError, ValueError, staged_diff.DiffError) @@ -99,7 +98,26 @@ def commit_diff(ask: dict, rules: Path | None) -> tuple[str | None, dict]: def clean(text: str) -> str: - return CONTROL.sub("?", text.strip())[:REASON_LIMIT] + """Returns: + text on one line for the terminal and the agent: every character + signing.unseen() names, a C0 or C1 control, a bidi or zero-width + character among them, shown as ?, but a selector signing.presented() + accepts; cut at REASON_LIMIT. + """ + stripped = text.strip() + return "".join("?" if signing.unseen(char) and not signing.presented(stripped, index) else char for index, char in enumerate(stripped))[:REASON_LIMIT] + + +def refusal(error: urllib.error.HTTPError) -> str: + """Returns: + The reason the hub sent with an error status, on one line, or the + status text when its body cannot be read. + """ + try: + body = error.read().decode("utf-8", "replace") + except OSError: + body = "" + return clean(body or str(error.reason)) def bound(chosen: dict | None, ask: dict, pinned: str | None) -> dict | None: @@ -194,6 +212,9 @@ def main() -> int: answer = {"status": "pending"} while answer["status"] == "pending" and (left := deadline - time.monotonic()) > 0: answer = status(base, ask_id, min(left, POLL)) + except urllib.error.HTTPError as error: + print(f"kvb-orchestrator: the hub answered {error.code}, Claude Code asks instead: {refusal(error)}", file=sys.stderr) + return 0 except (OSError, ValueError, KeyError, TypeError, AttributeError): return 0 chosen = bound(decision(answer, signing.ask_item(ask_id, ask["tool"], ask["command"], ask["cwd"], ask["session"]), trust), ask, pinned) diff --git a/orchestrator/client/settings_writer.py b/orchestrator/client/settings_writer.py new file mode 100755 index 0000000..1145dcb --- /dev/null +++ b/orchestrator/client/settings_writer.py @@ -0,0 +1,365 @@ +#!/usr/bin/env python3 +"""Args: + --port: port of the Agent Orchestrator; the signature's origin is + http://localhost:. + --jobs: the directory the hub writes signed settings changes into, e.g. + ~/.local/state/kvb-orchestrator/firewall/jobs. + --results: the directory each job's result goes to as .json. + --view: the file the Firewall reads, e.g. + ~/.local/state/kvb-orchestrator/firewall/view.json: per listed file + its path, sha256 and only its permissions and sandbox sections. + --approvers: the pinned passkeys, e.g. ~/.config/kvb-orchestrator/approvers.json. + --replay: this writer's replay store, outside the hub's reach, e.g. + ~/.local/state/kvb-orchestrator-approvals/firewall.json. + --tree: the tree file `pkgmgr branch tree` writes; the repositories it + knows name the project settings files. + +Returns: + Exit code 2 without touching anything when CLAUDECODE or + SANDBOX_RUNTIME is set: it runs only from its unit, never inside an + agent or its sandbox. Else, as the operator on the host, per item of + every job in --jobs: verifies the operator's passkey signature over + file, base and changes with the replay store; refuses a file outside the + fixed list (~/.claude/settings.json and /.claude/settings.json + of every repository the tree file knows), a link or non-directory on its + path, a file of another uid, a non-regular file and a file whose sha256 + is no longer the signed base; then applies exactly the signed changes to + the parsed file, keeps every other key, copies the old bytes to + .before-firewall- beside it and replaces the file + atomically, both with the file's mode (0600 for a new file). A refused + item changes nothing; a failed one keeps the file's content. Writes each + job's result, removes the job and rewrites --view, all through + verifier.drain() and verifier.private_dir(): relative to open directory + descriptors, never through a link, a linked or foreign jobs, results or + view directory refused with one line on stderr. The exit code is 0. +""" + +from __future__ import annotations + +import argparse +import hashlib +import json +import math +import os +import secrets +import stat +import sys +import time +from pathlib import Path + +import signing +import verifier +from cleanup import tree_facts +from offices import cycle + +AGENT_MARKERS = ("CLAUDECODE", "SANDBOX_RUNTIME") +DIRECTORY = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW | os.O_CLOEXEC +FILE = os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK | os.O_CLOEXEC +CREATE = os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW | os.O_CLOEXEC +READ_LIMIT = 1 << 20 +NEW_MODE = 0o600 +BACKUP = "before-firewall" +OUTSIDE = "an agent or its sandbox runs this: the settings writer runs only from the kvb-orchestrator-firewall unit" +MISSING_DIRECTORY = "no directory: create it first" + + +def listed(home: Path, tree: Path) -> list[str]: + """Returns: + The fixed list of settings files: the operator's own and those of + every repository the tree file knows. + """ + own = str(home / ".claude" / "settings.json") + projects = sorted(f"{repository.rstrip('/')}{signing.SETTINGS_FILE}" for repository in tree_facts(tree)[0]) + return [own, *(path for path in projects if path != own)] + + +def parent_of(path: str) -> int: + """Returns: + The open directory holding path, reached from / without following a + link on the way; the caller closes it. + """ + fd = os.open("/", DIRECTORY) + try: + for part in Path(path).parent.parts[1:]: + child = os.open(part, DIRECTORY, dir_fd=fd) + os.close(fd) + fd = child + except BaseException: + os.close(fd) + raise + return fd + + +def read_settings(parent: int, name: str) -> tuple[bytes | None, os.stat_result | None]: + """Returns: + The file's bytes and status, None and None while it is missing. + + Raises: + signing.Refused for a link, a non-regular file or one over + READ_LIMIT bytes. + """ + try: + fd = os.open(name, FILE, dir_fd=parent) + except FileNotFoundError: + return None, None + except OSError as error: + raise signing.Refused(f"the file opens only through a link or not at all: {error.strerror}") from None + with open(fd, "rb") as handle: + found = os.fstat(fd) + if not stat.S_ISREG(found.st_mode): + raise signing.Refused("the file is no regular file") + data = handle.read(READ_LIMIT + 1) + if len(data) > READ_LIMIT: + raise signing.Refused(f"the file is larger than {READ_LIMIT} bytes") + return data, found + + +def write_beside(parent: int, name: str, data: bytes, mode: int) -> str: + """Args: + parent: the open directory. + name: the file to replace or create in it. + data: its new bytes. + mode: its permission bits. + + Returns: + "" once the directory is synced, else why its sync failed after the + file was already replaced. + """ + temporary = f".{name}.{secrets.token_hex(8)}" + fd = os.open(temporary, CREATE, mode, dir_fd=parent) + try: + with open(fd, "wb") as handle: + os.fchmod(fd, mode) + handle.write(data) + handle.flush() + os.fsync(fd) + os.replace(temporary, name, src_dir_fd=parent, dst_dir_fd=parent) + except BaseException: + os.unlink(temporary, dir_fd=parent) + raise + try: + os.fsync(parent) + except OSError as error: + return error.strerror or type(error).__name__ + return "" + + +class Unsafe(ValueError): + pass + + +def no_constant(name: str) -> float: + raise Unsafe(f"the file holds {name}, which JSON readers reject") + + +def finite(text: str) -> float: + value = float(text) + if not math.isfinite(value): + raise Unsafe("the file holds a number beyond the double range, which JSON readers reject") + return value + + +def unique(pairs: list[tuple[str, object]]) -> dict: + found = dict(pairs) + if len(found) != len(pairs): + raise Unsafe("the file repeats a key in one object") + return found + + +def parsed(data: bytes | None) -> dict: + """Returns: + The settings object, {} while the file is missing. + + Raises: + Unsafe for anything but strict JSON every reader parses alike: no + NaN, Infinity or overflowing number, no repeated key, no lone + surrogate, at most signing.DEPTH_LIMIT levels, an object at the top. + The reason never quotes the file. + """ + if data is None: + return {} + try: + settings = json.loads(data.decode("utf-8"), parse_constant=no_constant, parse_float=finite, object_pairs_hook=unique) + except Unsafe: + raise + except (ValueError, RecursionError) as error: + raise Unsafe(f"the file holds no strict JSON: {type(error).__name__}") from None + problem = signing.plain_problem(settings, 0) if isinstance(settings, dict) else "the file holds no JSON object" + if problem: + raise Unsafe(problem) + return settings + + +def digest(data: bytes | None) -> str: + return "" if data is None else hashlib.sha256(data).hexdigest() + + +def view_of(path: str) -> dict: + """Returns: + {"path", "exists", "sha256", "error", "settings"}: settings only the + permissions and sandbox sections; error why the file was unreadable, + for anything unforeseen only the exception's class, so one bad path + never keeps the others or the view from being written. + """ + entry = {"path": path, "exists": False, "sha256": "", "error": "", "settings": {}} + try: + return read_view(path, entry) + except Exception as error: # noqa: BLE001 + return {**entry, "error": f"unreadable: {type(error).__name__}"} + + +def read_view(path: str, entry: dict) -> dict: + try: + parent = parent_of(path) + except FileNotFoundError: + return {**entry, "error": MISSING_DIRECTORY} + except OSError as error: + return {**entry, "error": f"a link or no directory on the path: {error.strerror}"} + try: + data, _ = read_settings(parent, Path(path).name) + return {**entry, "exists": data is not None, "sha256": digest(data), "settings": signing.settings_view(parsed(data))} + except (OSError, Unsafe, signing.Refused) as error: + return {**entry, "error": str(error)[:300]} + finally: + os.close(parent) + + +class Writer: + def __init__(self, trust: verifier.Trust, home: Path, tree: Path) -> None: + self.trust = trust + self.home = home + self.tree = tree + + def view(self) -> dict: + return {"written": time.time(), "files": [view_of(path) for path in listed(self.home, self.tree)]} + + def apply(self, item: dict, job_id: str) -> dict: + """Args: + item: one signed, well-formed settings change. + job_id: the job's id, part of the backup's name. + + Returns: + {"backup", "sha256", "unsynced"}: the backup beside the file, "" + for a file that was missing, the new content's sha256, and why + the directory sync failed after a replace, "" when it did not. + + Raises: + signing.Refused or OSError when nothing was written. + """ + name = Path(item["file"]).name + try: + parent = parent_of(item["file"]) + except OSError as error: + raise signing.Refused(f"a link or no directory on the path: {error.strerror}") from None + try: + data, found = read_settings(parent, name) + if found is not None and found.st_uid != os.getuid(): + raise signing.Refused(f"the file belongs to uid {found.st_uid}, not {os.getuid()}") + if digest(data) != item["base"]: + raise signing.Refused("the file changed since the signature") + text = (json.dumps(signing.apply_changes(parsed(data), item["changes"]), indent=2, ensure_ascii=False, allow_nan=False) + "\n").encode("utf-8") + mode = NEW_MODE if found is None else stat.S_IMODE(found.st_mode) + backup = unsynced = "" + if data is not None: + backup = f"{name}.{BACKUP}-{job_id}" + unsynced = write_beside(parent, backup, data, mode) + unsynced = write_beside(parent, name, text, mode) or unsynced + finally: + os.close(parent) + return {"backup": str(Path(item["file"]).with_name(backup)) if backup else "", "sha256": digest(text), "unsynced": unsynced} + + def item(self, item: dict, proof: object, job_id: str, now: float) -> dict: + """Args: + item: one well-formed settings change. + proof: the job's {"payload", "assertion"}. + job_id: the job's id. + now: the current time in epoch seconds. + + Returns: + The item with status done, refused or failed and the reason; a + refused or failed item wrote nothing, and a failure's reason names + only its kind, never the file's content. + """ + try: + status, reason, written = self.outcome(item, proof, job_id, now) + except OSError as error: + status, reason, written = "failed", f"{error.strerror or error}; the file kept its content", {} + except Exception as error: # noqa: BLE001 + status, reason, written = "failed", f"{error if isinstance(error, Unsafe) else type(error).__name__}; the file kept its content", {} + return {**item, "status": status, "reason": reason, **written} + + def outcome(self, item: dict, proof: object, job_id: str, now: float) -> tuple[str, str, dict]: + try: + verifier.check(proof, signing.settings_item(item["file"], item["base"], item["changes"]), "", self.trust, now) + except (signing.Refused, OSError) as error: + return "refused", f"no valid operator signature: {error}", {} + if item["file"] not in listed(self.home, self.tree): + return "refused", "unlisted file: only ~/.claude/settings.json and the .claude/settings.json of a repository pkgmgr knows", {} + try: + written = self.apply(item, job_id) + except signing.Refused as error: + return "refused", str(error)[:500], {} + unsynced = written.pop("unsynced") + return "done", f"written, but its directory sync failed: {unsynced}; the change may not survive a crash" if unsynced else "", written + + def job(self, folder: int, path: Path, now: float) -> dict: + """Args: + folder: the open jobs directory, as verifier.drain() passes it. + path: the job's name in it. + now: the current time in epoch seconds. + + Returns: + The job's result; whatever goes wrong in one job fails only that + job, so the next job and the view still run. + """ + try: + return self.checked_job(folder, path, now) + except Exception as error: # noqa: BLE001 + return {"id": path.stem, "finished": now, "items": [], "error": f"failed: {type(error).__name__}; nothing changed"} + + def checked_job(self, folder: int, path: Path, now: float) -> dict: + try: + job = json.loads(verifier.read_regular(path, folder)) + except (OSError, ValueError, RecursionError) as error: + return {"id": path.stem, "finished": now, "items": [], "error": f"unreadable job: {error}"} + items = job.get("items") if isinstance(job, dict) else None + if not isinstance(items, list) or not items or any(signing.settings_problem(item) for item in items) or job.get("id") != path.stem: + return {"id": path.stem, "finished": now, "items": [], "error": "malformed job: refused, nothing changed"} + return {"id": path.stem, "finished": time.time(), "items": [self.item(item, job.get("proof"), path.stem, now) for item in items]} + + +def main() -> int: + if any(marker in os.environ for marker in AGENT_MARKERS): + print(f"settings_writer: refused: {OUTSIDE}", file=sys.stderr) + return 2 + parser = argparse.ArgumentParser(prog="settings_writer") + parser.add_argument("--port", type=int, required=True) + for name in ("--jobs", "--results", "--view", "--approvers", "--replay", "--tree"): + parser.add_argument(name, type=Path, required=True) + args = parser.parse_args() + trust = verifier.Trust(approvers=args.approvers, replay=args.replay, origin=signing.origin_of(args.port)) + writer = Writer(trust, Path.home(), args.tree) + try: + cycle(args.jobs, args.results, writer) + finally: + write_view(args.view, writer.view()) + return 0 + + +def write_view(path: Path, view: dict) -> None: + """Args: + path: the view file, written relative to its directory opened by + verifier.private_dir(); a linked or foreign spool, or a write + that fails, prints one line and raises nothing, so an error of + the cycle before it still surfaces. + view: Writer.view(). + """ + try: + with verifier.private_dir(path.parent, True) as folder: + verifier.write_at(folder, path.name, view) + except (signing.Refused, OSError) as error: + print(f"view not written: {error}", file=sys.stderr) + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/orchestrator/client/signing.py b/orchestrator/client/signing.py index 9cf698d..0232d22 100644 --- a/orchestrator/client/signing.py +++ b/orchestrator/client/signing.py @@ -1,10 +1,12 @@ from __future__ import annotations import base64 +import copy import fnmatch import hashlib import json import re +import unicodedata from pathlib import Path RP_ID = "localhost" @@ -35,6 +37,28 @@ GATE_BRANCH = re.compile(r"(?![-/])(?!.*\.\.)(?!.*@\{)(?!.*//)[^\x00-\x20\x7f~^: TOKEN_FREE_MERGE = "ancestor" APPROVAL_KEYS = {"v", "kind", "items", "nonce", "expires"} RULE_KEYS = {"v", "kind", "id", "kinds", "tool", "pattern", "scope", "expires"} +TARGET = "agents" +ALL_AGENTS = "*" +AGENT_LIMIT = 50 +SETTINGS_FILE = "/.claude/settings.json" +SETTINGS_KEYS = ("file", "base", "changes") +SETTINGS_SECTIONS = ("permissions", "sandbox") +SETTINGS_LISTS = ("permissions.allow", "permissions.ask", "permissions.deny", "sandbox.filesystem.allowWrite", "sandbox.filesystem.denyRead", "sandbox.network.allowedDomains") +SETTINGS_OPS = ("add", "remove") +CHANGE_KEYS = ("list", "op", "entry") +CHANGE_LIMIT = 50 +ENTRY = re.compile(r"(?=[^\x00-\x1f\x7f\ud800-\udfff]*\S)[^\x00-\x1f\x7f\ud800-\udfff]{1,500}") +SETTINGS_PATH = re.compile(r"/[^\x00-\x1f\x7f\ud800-\udfff]{0,4095}") +SURROGATE = re.compile(r"[\ud800-\udfff]") +UNSEEN = ("Cc", "Cf", "Zl", "Zp") +DEFAULT_IGNORABLE = ((0xAD, 0xAD), (0x34F, 0x34F), (0x61C, 0x61C), (0x115F, 0x1160), (0x17B4, 0x17B5), (0x180B, 0x180F), (0x200B, 0x200F), (0x202A, 0x202E), (0x2060, 0x206F), (0x3164, 0x3164), (0xFE00, 0xFE0F), (0xFEFF, 0xFEFF), (0xFFA0, 0xFFA0), (0xFFF0, 0xFFF8), (0x1BCA0, 0x1BCA3), (0x1D173, 0x1D17A), (0xE0000, 0xE0FFF)) +IGNORABLE = frozenset(code for start, end in DEFAULT_IGNORABLE for code in range(start, end + 1)) +EMOJI_TABLE = Path(__file__).with_name("emoji-variation.json") +HEX_CODE = re.compile(r"[0-9A-F]{4,6}") +PRESENTATION = (chr(0xFE0E), chr(0xFE0F)) +FREE_TEXT = ("command", "text", "reason", "question", "description") +BREAKS = "\t\n" +DEPTH_LIMIT = 64 WARNINGS = ( ("deletes recursively", re.compile(r"\brm\s+(?:-\S+\s+)*-\S*[rR]")), ("forces", re.compile(r"--force\b|\bpush\b.*\s-f\b")), @@ -284,6 +308,283 @@ def rule_payload(rule_id: str, kinds: list[str], tool: str, pattern: str, scope: return {"v": VERSION, "kind": "rule", "id": rule_id, "kinds": kinds, "tool": tool, "pattern": pattern, "scope": scope, "expires": expires} +def targeted(rule: dict, agents: list[str]) -> dict: + """Args: + rule: a rule_payload(). + agents: [ALL_AGENTS], or the sorted session ids of one agent or of a + named group, resolved when the operator signs. + + Returns: + The rule payload limited to those agents. + """ + return {**rule, TARGET: agents} + + +def enable_item(rule: dict) -> dict: + """Args: + rule: the signed payload of a stored rule. + + Returns: + The item a fresh approval lists to turn exactly that rule on again. + """ + return {"kind": "enable", "rule": rule} + + +def agents_problem(agents: object) -> str | None: + """Returns: + Why agents is neither [ALL_AGENTS] nor 1 to AGENT_LIMIT sorted, + distinct session ids, or None. + """ + if agents == [ALL_AGENTS]: + return None + if not isinstance(agents, list) or not 1 <= len(agents) <= AGENT_LIMIT or not all(isinstance(agent, str) and SESSION.fullmatch(agent) for agent in agents): + return f'agents must be ["{ALL_AGENTS}"] or 1 to {AGENT_LIMIT} session ids' + return None if agents == sorted(set(agents)) else "agents must be sorted and distinct" + + +def checked_target(rule: dict) -> None: + """Raises: + Refused when the rule carries agents that agents_problem() refuses. + """ + problem = agents_problem(rule[TARGET]) if TARGET in rule else None + if problem: + raise Refused(f"the rule names no target: {problem}") + + +def item_sessions(item: dict) -> set[str]: + """Returns: + The session an ask comes from; empty for every other item, which + names none. + """ + return {item["session"]} if item["kind"] == "ask" else set() + + +def reaches(rule: dict, sessions: set[str]) -> bool: + """Args: + rule: a checked rule payload. + sessions: the sessions the item comes from; empty when the item names + none, which only a rule for every agent reaches. + + Returns: + True when the rule carries no agents or [ALL_AGENTS], or names one of + the sessions. + """ + return TARGET not in rule or rule[TARGET] == [ALL_AGENTS] or bool(sessions & set(rule[TARGET])) + + +def settings_item(file: str, base: str, changes: list[dict]) -> dict: + return {"kind": "settings", "file": file, "base": base, "changes": changes} + + +def unseen(char: str) -> bool: + """Returns: + True for a character no prompt shows as itself: a category of UNSEEN, + a space separator other than the space, or a code point of + DEFAULT_IGNORABLE; page-reveal.js marks the same set. + """ + category = unicodedata.category(char) + return category in UNSEEN or (category == "Zs" and char != " ") or ord(char) in IGNORABLE + + +def emoji_base(code: int) -> bool: + return 0x7F < code <= 0x10FFFF and not 0xD800 <= code <= 0xDFFF and not unseen(chr(code)) + + +def variation_table(path: Path) -> frozenset[int]: + """Args: + path: the emoji variation table, a JSON object whose bases list the + hex code points of emoji-variation-sequences.txt. + + Returns: + The bases as code points. + + Raises: + ValueError when bases is no list of hex strings, or names an ASCII + code point, a surrogate or one unseen() names; like a missing file, + this stops the module from loading, so no selector is ever accepted + after an unchecked base. + """ + bases = json.loads(path.read_text(encoding="utf-8"))["bases"] + if not isinstance(bases, list) or not all(isinstance(code, str) and HEX_CODE.fullmatch(code) for code in bases): + raise ValueError(f"{path}: bases must be a list of hex code points") + codes = frozenset(int(code, 16) for code in bases) + wrong = sorted(code for code in codes if not emoji_base(code)) + if wrong: + raise ValueError(f"{path}: U+{wrong[0]:04X} is no visible non-ASCII emoji base") + return codes + + +EMOJI_BASES = variation_table(EMOJI_TABLE) + + +def presented(text: str, index: int) -> bool: + """Returns: + True when text[index] is a variation selector of PRESENTATION right + after a code point of EMOJI_BASES, a standardized emoji variation + sequence such as the stopwatch of the score block that every prompt + draws as the symbol itself; page-reveal.js skips the same pairs. + """ + return text[index] in PRESENTATION and index > 0 and ord(text[index - 1]) in EMOJI_BASES + + +def hidden_char(text: str, name: str) -> str | None: + """Returns: + The first character of text that unseen() names, a tab or a line + break only outside the FREE_TEXT fields, and no presented() selector; + None when there is none. + """ + allowed = BREAKS if name in FREE_TEXT else "" + return next((char for index, char in enumerate(text) if unseen(char) and char not in allowed and not presented(text, index)), None) + + +def removal(value: dict) -> bool: + return set(value) == set(CHANGE_KEYS) and value["op"] == "remove" and value["list"] in SETTINGS_LISTS + + +def hidden_problem(value: object, name: str) -> str | None: + """Args: + value: a text, or an object or list of them, that reaches a signed + payload or the approval prompt; an object's texts are named by + their keys. Only the entry of a settings change of op remove is + exempt: removing an allow entry narrows access, but removing a + deny, ask or denyRead entry widens it, and that is safe only + because the writer removes exactly the signed text and the page + marks every character unseen() names. + name: the field a text sits in. + + Returns: + Why the first text holding a character unseen() names (a bidi + control, a zero-width character, a carriage return, a no-break space, + a Hangul filler) is refused, since the prompt would show other text + than what is signed; None when no text holds one. + """ + if isinstance(value, dict): + named = {key: child for key, child in value.items() if not (key == "entry" and removal(value))} + return next(filter(None, (hidden_problem(child, str(key)) for key, child in named.items())), None) + if isinstance(value, list): + return next(filter(None, (hidden_problem(child, name) for child in value)), None) + hidden = hidden_char(value, name) if isinstance(value, str) else None + return None if hidden is None else f"{name} must hold no invisible, format, control or look-alike character such as U+{ord(hidden):04X}, which shows other text than what is signed" + + +def settings_file_problem(file: object) -> str | None: + if not isinstance(file, str) or not SETTINGS_PATH.fullmatch(file) or not file.endswith(SETTINGS_FILE): + return f"file must be the absolute path of a {SETTINGS_FILE.lstrip('/')}" + if any(part in f"{file}/" for part in ("/../", "/./", "//")): + return "file must name no . or .. and no empty part" + return hidden_problem(file, "file") + + +def change_problem(change: object) -> str | None: + if not isinstance(change, dict) or set(change) != set(CHANGE_KEYS) or not all(isinstance(change[name], str) for name in CHANGE_KEYS): + return "a change holds exactly list, op and entry as text" + if change["list"] not in SETTINGS_LISTS or change["op"] not in SETTINGS_OPS: + return f"list must be one of {', '.join(SETTINGS_LISTS)} and op one of {', '.join(SETTINGS_OPS)}" + return hidden_problem(change, "change") if ENTRY.fullmatch(change["entry"]) else "entry must be one line of 1 to 500 characters" + + +def settings_problem(item: object) -> str | None: + """Args: + item: {"file", "base", "changes"}: file the absolute path of a + .claude/settings.json, base the sha256 of its bytes or "" while it + is missing, changes 1 to CHANGE_LIMIT {"list", "op", "entry"}, + each list and entry once. + + Returns: + Why the item is malformed, or None; the hub and the writer check it + the same way. + """ + if not isinstance(item, dict) or set(item) != set(SETTINGS_KEYS): + return "a settings change holds exactly file, base and changes" + if not isinstance(item["base"], str) or not (item["base"] == "" or HASH.fullmatch(item["base"])): + return "base must be the sha256 of the file, or empty for a missing file" + return settings_file_problem(item["file"]) or changes_problem(item["changes"]) + + +def changes_problem(changes: object) -> str | None: + if not isinstance(changes, list) or not 1 <= len(changes) <= CHANGE_LIMIT: + return f"changes must list 1 to {CHANGE_LIMIT} changes" + problem = next(filter(None, map(change_problem, changes)), None) + if problem: + return problem + pairs = {(change["list"], change["entry"]) for change in changes} + return None if len(pairs) == len(changes) else "each list and entry is changed once" + + +def settings_view(settings: dict) -> dict: + """Returns: + The permissions and sandbox sections of a settings object, the only + part of the file the Agent Orchestrator receives. + """ + return {name: settings[name] for name in SETTINGS_SECTIONS if isinstance(settings.get(name), dict)} + + +def plain_problem(value: object, depth: int) -> str | None: + """Args: + value: a parsed JSON value. + depth: how many objects and lists hold it. + + Returns: + Why value is unsafe to copy or write, a lone surrogate in any text or + more than DEPTH_LIMIT levels, or None. + """ + if isinstance(value, str): + return "the file holds a lone surrogate" if SURROGATE.search(value) else None + if not isinstance(value, (dict, list)): + return None + if depth >= DEPTH_LIMIT: + return f"the file nests deeper than {DEPTH_LIMIT} levels" + children = [*value, *value.values()] if isinstance(value, dict) else value + return next(filter(None, (plain_problem(child, depth + 1) for child in children)), None) + + +def plain_copy(settings: dict) -> dict: + """Raises: + Refused for settings plain_problem() refuses, before deepcopy could + recurse into them. + """ + problem = plain_problem(settings, 0) + if problem: + raise Refused(problem) + return copy.deepcopy(settings) + + +def apply_changes(settings: dict, changes: list[dict]) -> dict: + """Args: + settings: the parsed settings object. + changes: changes settings_problem() accepts. + + Returns: + A copy of settings with each change applied in order and every other + key kept. + + Raises: + Refused when settings nest deeper than DEPTH_LIMIT or hold a lone + surrogate, an object on a list's path is no object, a list holds + anything but text, an added entry is listed already or a removed one + is not. + """ + result = plain_copy(settings) + for change in changes: + *path, leaf = change["list"].split(".") + node = result + for name in path: + node = node.setdefault(name, {}) + if not isinstance(node, dict): + raise Refused(f"{name} is no object") + entries = node.setdefault(leaf, []) + if not isinstance(entries, list) or not all(isinstance(entry, str) for entry in entries): + raise Refused(f"{change['list']} is no list of texts") + listed = change["entry"] in entries + if listed == (change["op"] == "add"): + raise Refused(f"{change['list']} {'lists' if listed else 'does not list'} {change['entry']}") + if listed: + entries.remove(change["entry"]) + else: + entries.append(change["entry"]) + return result + + def warnings(command: str) -> list[str]: return [label for label, pattern in WARNINGS if pattern.search(command)] @@ -311,8 +612,9 @@ def checked_approval(payload: object, now: float) -> dict: def checked_rule(payload: object, now: float) -> dict: - if not isinstance(payload, dict) or set(payload) != RULE_KEYS or payload["v"] != VERSION or payload["kind"] != "rule": + if not isinstance(payload, dict) or set(payload) - {TARGET} != RULE_KEYS or payload["v"] != VERSION or payload["kind"] != "rule": raise Refused("the payload is no rule") + checked_target(payload) if not isinstance(payload["id"], str) or not ID.fullmatch(payload["id"]): raise Refused("the rule holds no id") kinds = payload["kinds"] @@ -325,6 +627,10 @@ def checked_rule(payload: object, now: float) -> dict: def rule_allows(rule: dict, item: dict, gate: str, now: float) -> bool: + return reaches(rule, item_sessions(item)) and pattern_allows(rule, item, gate, now) + + +def pattern_allows(rule: dict, item: dict, gate: str, now: float) -> bool: if rule["expires"] <= now or item["kind"] not in rule["kinds"]: return False if item["kind"] == "ask": diff --git a/orchestrator/kvb_orchestrator/approving.py b/orchestrator/kvb_orchestrator/approving.py index 188127d..a3728d3 100644 --- a/orchestrator/kvb_orchestrator/approving.py +++ b/orchestrator/kvb_orchestrator/approving.py @@ -8,12 +8,13 @@ from .choosing import answer_signed from .cleanup import cleanup_request from .denying import DENIABLE, DENY_KEYS, denial_note, request_status, request_words from .errors import Conflict, Missing, ToolError -from .fields import decode, expect, field +from .fields import decode, expect, field, visible +from .firewall import settings_request from .gating import gate_request from .offices import office_request from .records import digest, shown from .register import REGISTER, adder, operator_rows, read_register, runnable -from .rules import rule_matches, subject_of +from .rules import enable_request, rule_matches, subject_of from .signed import ( ask_signed, operator_signed, @@ -63,7 +64,12 @@ class Approving: """Returns: The approval payload over the items, its WebAuthn challenge and the pinned credential ids; payload and challenge are None for no item. + + Raises: + ToolError from visible() for an item holding a text no prompt + shows as signed, whatever source missed it. """ + visible(items) credentials = [key["id"] for key in self.keys()] if not items: return {"payload": None, "challenge": None, "credentials": credentials} @@ -131,7 +137,7 @@ class Approving: def automate(self, kind_name: str, record: dict) -> None: now = time.time() - subject = subject_of(kind_name, record) + subject = subject_of(kind_name, record, self.deciders(kind_name, record)) rule = next((rule for rule in self.rules.listed() if rule_matches(rule, kind_name, subject, now)), None) if rule is None: return @@ -189,6 +195,27 @@ class Approving: raise Conflict({"id": raw["id"], "status": "recipient changed", "to": name}) return name + def operator_entry(self, raw: dict, tasks: dict[str, dict]) -> dict: + """Args: + raw: a checked inbox entry of kind operator. + tasks: the register. + + Returns: + The signed item of the operator row raw names, once its task, + command and directory pass visible(). + """ + row = next((row for row in operator_rows(tasks) if row["task"] == raw["id"]), None) + if row is None: + raise Missing(str(raw["id"])) + visible({name: row[name] for name in ("task", "command", "directory")}) + if raw["verb"] == "approve" and not runnable(row): + raise ToolError("an operator row is approved only when it holds a directory and a command") + if digest(row["command"]) != raw["sha256"]: + raise Conflict({"id": row["task"], "status": "changed"}) + if row["task"] in self.open_jobs(): + raise Conflict({"id": row["task"], "status": "queued"}) + return inbox_item(raw, "", row["command"], "", operator_signed(row), self.requester(raw, adder(tasks[row["task"]]))) + def item_of(self, raw: object, tasks: dict[str, dict]) -> dict: if not isinstance(raw, dict): raise ToolError("every item must be an object with kind, id, verb, label and sha256, and a denial also channel, reason and to") @@ -197,16 +224,7 @@ class Approving: if kind_name not in INBOX_KINDS or verb not in VERBS or not isinstance(raw["label"], str) or not isinstance(raw["sha256"], str): raise ToolError(f"kind must be one of {', '.join(INBOX_KINDS)}, verb approve or reject, label and sha256 text") if kind_name == "operator": - row = next((row for row in operator_rows(tasks) if row["task"] == raw["id"]), None) - if row is None: - raise Missing(str(raw["id"])) - if verb == "approve" and not runnable(row): - raise ToolError("an operator row is approved only when it holds a directory and a command") - if digest(row["command"]) != raw["sha256"]: - raise Conflict({"id": row["task"], "status": "changed"}) - if row["task"] in self.open_jobs(): - raise Conflict({"id": row["task"], "status": "queued"}) - return inbox_item(raw, "", row["command"], "", operator_signed(row), self.requester(raw, adder(tasks[row["task"]]))) + return self.operator_entry(raw, tasks) if kind_name == "rule" and verb == "approve": raise ToolError("a rule request is accepted in its row, with the passkey signature over the rule itself") item_id = field(raw, "id") @@ -253,6 +271,11 @@ class Approving: signed = self.task_changes(raw) elif gate_request(raw): signed = self.gate_changes(raw) + elif settings_request(raw): + signed = self.settings_changes(raw) + elif enable_request(raw): + signed = self.enable_items(raw) + return {**self.challenge_for(signed), "enables": signed[0]["rule"]} else: signed = [item["signed"] for item in self.checked(raw) if item["signed"] is not None] return self.challenge_for(signed) diff --git a/orchestrator/kvb_orchestrator/asking.py b/orchestrator/kvb_orchestrator/asking.py index 8086832..6bed78d 100644 --- a/orchestrator/kvb_orchestrator/asking.py +++ b/orchestrator/kvb_orchestrator/asking.py @@ -16,6 +16,7 @@ from .fields import ( instant, message_text, seconds, + visible, ) from .signed import ask_signed @@ -55,7 +56,9 @@ def option_of(option: object) -> dict: raise ToolError("every option must be an object with label and description") expect(option, ("label", "description")) label = field(option, "label").strip() - return {"label": label, "description": bounded(option, "description", DESCRIPTION_LIMIT).strip()} + description = bounded(option, "description", DESCRIPTION_LIMIT).strip() + visible({"description": description}) + return {"label": label, "description": description} class Asking: @@ -64,6 +67,7 @@ class Asking: diff = valid_diff(payload.pop("diff")) if "diff" in payload else None expect(payload, ("tool", *ASK_TEXTS, "timeout")) values = {name: bounded(payload, name, limit) for name, limit in ASK_TEXTS.items()} + visible(values) lifetime = seconds(payload, "timeout", ASK_LIFETIME) created = time.time() fields = {**values, "tool": field(payload, "tool").strip(), "status": "pending", "decided": None, "proof": None, "diff_files": None if diff is None else len(diff)} @@ -104,6 +108,7 @@ class Asking: def decision_request(self, arguments: dict) -> dict: expect(arguments, ("question", "options", "agent", "recommended")) question = message_text(arguments, "question") + visible({"question": question}) options = arguments["options"] if not isinstance(options, list) or not 1 <= len(options) <= OPTION_LIMIT: raise ToolError(f"options must be a list of 1 to {OPTION_LIMIT} objects") diff --git a/orchestrator/kvb_orchestrator/fields.py b/orchestrator/kvb_orchestrator/fields.py index 9c5bda1..d203bc5 100644 --- a/orchestrator/kvb_orchestrator/fields.py +++ b/orchestrator/kvb_orchestrator/fields.py @@ -5,7 +5,7 @@ import math import re from .errors import ToolError -from .signed import limits +from .signed import limits, signing WAIT_CAP = 300 TEXT_LIMIT = 20_000 @@ -43,9 +43,25 @@ def field(arguments: dict, name: str) -> str: value = arguments[name] if not isinstance(value, str) or not pattern.fullmatch(value): raise ToolError(f"{name} must be {hint}") + visible({name: value}) return value +def visible(value: object) -> None: + """Args: + value: an object of named texts, or a list of them, that reaches a + signed payload or the approval prompt. + + Raises: + ToolError with the reason signing.hidden_problem() gives for the + first text holding an invisible, format, control or look-alike + character. + """ + hidden = signing.hidden_problem(value, "value") + if hidden: + raise ToolError(hidden) + + def bounded(arguments: dict, name: str, limit: int) -> str: value = arguments[name] if not isinstance(value, str) or len(value) > limit: diff --git a/orchestrator/kvb_orchestrator/firewall.py b/orchestrator/kvb_orchestrator/firewall.py new file mode 100644 index 0000000..ab5ffeb --- /dev/null +++ b/orchestrator/kvb_orchestrator/firewall.py @@ -0,0 +1,248 @@ +from __future__ import annotations + +import difflib +import json +import re +import secrets +import time + +from .errors import Conflict, Missing, ToolError +from .fields import decode, expect, visible +from .ledger import Ledger +from .records import read_json, stamp +from .rules import EVERY_AGENT, RULE_KINDS +from .signed import signing + +VIEW = "view.json" +JOB_VERSION = 1 +ITEM_LIMIT = 10 +SET_LIMIT = 50 +DAY_LIMIT = 366 +NAME = re.compile(r"(?=[^\x00-\x1f\x7f]*\S)[^\x00-\x1f\x7f]{1,80}") +RULE_TEXT = re.compile(r"(?P[^()]+?)(?:\((?P.*)\))?", re.DOTALL) +WORD = re.compile(r"[\s:]") +HASH = re.compile(r"[0-9a-f]{64}") +PERMISSION_LISTS = ("allow", "ask", "deny") +SET_KEYS = ("kind", "tool", "pattern", "scope") +CHAINED = "chains, pipes, redirects or substitutions" + + +def settings_request(raw: object) -> bool: + return isinstance(raw, list) and bool(raw) and all(isinstance(entry, dict) and entry.get("kind") == "settings" for entry in raw) + + +def tool_and_prefix(entry: str) -> tuple[str, str]: + """Returns: + The tool of a permission rule such as Bash(git push:*) and the first + word of its specifier, for Bash the command; "" without specifier. + """ + found = RULE_TEXT.fullmatch(entry) + if found is None: + return entry.strip(), "" + return found.group("tool").strip(), WORD.split((found.group("spec") or "").strip(), maxsplit=1)[0] + + +def grouped(permissions: object) -> list[dict]: + """Returns: + The allow, ask and deny entries of a permissions section grouped by + tool and prefix, sorted: [{"tool", "prefix", "entries": [{"list", + "entry"}]}]. + """ + groups: dict[tuple[str, str], list[dict]] = {} + lists = permissions if isinstance(permissions, dict) else {} + for name in PERMISSION_LISTS: + values = lists.get(name) + for entry in values if isinstance(values, list) else []: + if isinstance(entry, str): + groups.setdefault(tool_and_prefix(entry), []).append({"list": name, "entry": entry}) + return [{"tool": tool, "prefix": prefix, "entries": entries} for (tool, prefix), entries in sorted(groups.items())] + + +def sandbox_entries(section: object, path: str) -> list[dict]: + """Returns: + Every leaf of a sandbox section under the dotted path, a list as + {"key", "values"} and any other value as {"key", "value"}. + """ + found: list[dict] = [] + for name, value in section.items() if isinstance(section, dict) else (): + key = f"{path}.{name}" + if isinstance(value, dict): + found += sandbox_entries(value, key) + elif isinstance(value, list): + found.append({"key": key, "values": [entry if isinstance(entry, str) else json.dumps(entry) for entry in value]}) + else: + found.append({"key": key, "value": value}) + return found + + +def shaped_file(entry: object) -> bool: + if not isinstance(entry, dict) or not isinstance(entry.get("path"), str) or not isinstance(entry.get("exists"), bool): + return False + return isinstance(entry.get("settings"), dict) and isinstance(entry.get("sha256"), str) and (entry["sha256"] == "" or bool(HASH.fullmatch(entry["sha256"]))) + + +def section_diff(path: str, before: dict, after: dict) -> str: + """Returns: + The unified diff of the permissions and sandbox sections before and + after the change, as the host writer applies it. + """ + lines = difflib.unified_diff(json.dumps(before, indent=2).splitlines(), json.dumps(after, indent=2).splitlines(), fromfile=path, tofile=path, lineterm="", n=3) + return "\n".join(lines) + + +def unique_name(payload: dict, ledger: Ledger) -> str: + name = payload["name"] + if not isinstance(name, str) or not NAME.fullmatch(name): + raise ToolError("name must be one line of 1 to 80 characters") + visible({"name": name}) + if any(record["name"] == name for record in ledger.listed()): + raise Conflict({"name": name, "status": "exists"}) + return name + + +def dropped(ledger: Ledger, record_id: str) -> dict: + if not ledger.remove(record_id): + raise Missing(record_id) + return {"id": record_id, "deleted": True} + + +def deny_patterns() -> list[dict]: + """Returns: + The commands no ask rule ever grants, as {"label", "pattern"}. + """ + return [*({"label": label, "pattern": pattern.pattern} for label, pattern in signing.WARNINGS), {"label": CHAINED, "pattern": signing.CHAINED.pattern}] + + +class Firewall: + def settings_files(self) -> tuple[list[dict], dict]: + """Returns: + The listed settings files the host writer last read, each + {"path", "exists", "sha256", "error", "settings"} with settings + only their permissions and sandbox sections, and the view's + written time and error. + """ + view = read_json(self.firewall_spool.root / VIEW) + files = view.get("files") if isinstance(view, dict) else None + if not isinstance(files, list): + return [], {"written": None, "error": "no settings view yet: the kvb-orchestrator-firewall unit writes it on the host"} + shown = [{**entry, "settings": signing.settings_view(entry["settings"]), "error": str(entry.get("error") or "")} for entry in files if shaped_file(entry)] + return shown, {"written": view.get("written"), "error": ""} + + def firewall(self) -> dict: + files, meta = self.settings_files() + listed = [{key: entry[key] for key in ("path", "exists", "sha256", "error")} | {"groups": grouped(entry["settings"].get("permissions")), "sandbox": sandbox_entries(entry["settings"].get("sandbox"), "sandbox")} for entry in files] + return {**meta, "files": listed, "lists": list(signing.SETTINGS_LISTS), "patterns": deny_patterns(), "groups": self.groups.listed(), "sets": self.rule_sets.listed()} + + def settings_changes(self, raw: object) -> list[dict]: + """Args: + raw: the page's items, each {"kind": "settings", "file", "base", + "changes"} for one listed file at the sha256 the view shows. + + Returns: + The signed items, each one file whose changes apply to the + sections the view shows. + """ + if not isinstance(raw, list) or not 1 <= len(raw) <= ITEM_LIMIT: + raise ToolError(f"items must be a list of 1 to {ITEM_LIMIT} settings changes") + files = {entry["path"]: entry for entry in self.settings_files()[0]} + signed: list[dict] = [] + for entry in raw: + if not isinstance(entry, dict): + raise ToolError("every item must be an object with kind, file, base and changes") + expect(entry, ("kind", *signing.SETTINGS_KEYS)) + order = {name: entry[name] for name in signing.SETTINGS_KEYS} + problem = signing.settings_problem(order) + if problem: + raise ToolError(problem) + self.applicable(files, order) + if any(item["file"] == order["file"] for item in signed): + raise ToolError("one item per file: list every change of a file in its changes") + signed.append(signing.settings_item(**order)) + return signed + + def applicable(self, files: dict[str, dict], order: dict) -> dict: + shown = files.get(order["file"]) + if shown is None: + raise ToolError(f"{order['file'][:300]} is no settings file the Firewall lists") + if shown["sha256"] != order["base"]: + raise Conflict({"file": order["file"], "status": "changed", "sha256": shown["sha256"]}) + try: + return signing.apply_changes(shown["settings"], order["changes"]) + except signing.Refused as error: + raise ToolError(str(error)) from None + + def settings_preview(self, body: bytes) -> dict: + payload = decode(body) + expect(payload, ("items",)) + items = self.settings_changes(payload["items"]) + files = {entry["path"]: entry for entry in self.settings_files()[0]} + return {"diffs": [{"file": item["file"], "diff": section_diff(item["file"], files[item["file"]]["settings"], self.applicable(files, item))} for item in items]} + + def change_settings(self, body: bytes) -> dict: + payload = decode(body) + expect(payload, ("items", "proof")) + items = self.settings_changes(payload["items"]) + proof = self.covered(payload["proof"], items, time.time()) + for item in items: + self.operator_proven(proof, item) + job_id = secrets.token_hex(8) + job = {"v": JOB_VERSION, "id": job_id, "created": stamp(), "items": [{name: item[name] for name in signing.SETTINGS_KEYS} for item in items], "proof": proof} + self.firewall_spool.put(job_id, job) + lines = [f"{item['file']}: {change['op']} {change['list']} {change['entry']}" for item in items for change in item["changes"]] + self.log.add({"kind": "run", "title": f"Settings change signed: {len(lines)}"[:200], "text": "\n".join(lines), "ref": {"firewall": job_id, "status": "queued"}}) + return {"id": job_id, "status": "queued", "items": job["items"]} + + def settings_state(self, job_id: str) -> dict: + result = read_json(self.firewall_spool.results / f"{job_id}.json") + if isinstance(result, dict) and isinstance(result.get("items"), list): + return {**result, "id": job_id, "status": "done"} + if (self.firewall_spool.jobs / f"{job_id}.json").is_file(): + return {"id": job_id, "status": "queued", "items": []} + raise Missing(job_id) + + def add_group(self, body: bytes) -> dict: + """Args: + body: {"name", "members"}: members 1 to signing.AGENT_LIMIT + sorted session ids the Agent Orchestrator knows. + """ + payload = decode(body) + expect(payload, ("name", "members")) + members = payload["members"] + problem = signing.agents_problem(members) if members != EVERY_AGENT else "members must name sessions, not every agent" + if problem: + raise ToolError(problem) + self.known_agents(members) + record = self.groups.add({"name": unique_name(payload, self.groups), "members": members}) + self.log.add({"kind": "rule", "title": f"Agent group {record['name']}: {len(members)} agents", "text": ", ".join(members), "ref": {"group": record["id"]}}) + return record + + def add_set(self, body: bytes) -> dict: + """Args: + body: {"name", "days", "rules"}: the ids of 1 to SET_LIMIT stored + rules whose kind, tool, pattern and scope the set keeps, and + the lifetime in days each rule gets when the set is taken + over and signed for an agent, a group or every agent. + """ + payload = decode(body) + expect(payload, ("name", "days", "rules")) + days = payload["days"] + if isinstance(days, bool) or not isinstance(days, int) or not 1 <= days <= DAY_LIMIT: + raise ToolError(f"days must be a whole number from 1 to {DAY_LIMIT}") + chosen = self.stored_rules(payload["rules"]) + record = self.rule_sets.add({"name": unique_name(payload, self.rule_sets), "days": days, "rules": [{key: rule[key] for key in SET_KEYS} for rule in chosen]}) + self.log.add({"kind": "rule", "title": f"Rule set {record['name']}: {len(chosen)} rules", "text": "\n".join(f"{rule['kind']} {rule['tool']} {rule['pattern']}".replace(" ", " ") for rule in chosen), "ref": {"set": record["id"]}}) + return record + + def stored_rules(self, ids: object) -> list[dict]: + if not isinstance(ids, list) or not 1 <= len(ids) <= SET_LIMIT or len(set(map(str, ids))) != len(ids): + raise ToolError(f"rules must list 1 to {SET_LIMIT} distinct rule ids") + chosen = [self.rules.get(rule_id) if isinstance(rule_id, str) else None for rule_id in ids] + if None in chosen or any(rule["kind"] not in RULE_KINDS for rule in chosen): + raise ToolError("rules must name stored rules") + return chosen + + def drop_group(self, group_id: str) -> dict: + return dropped(self.groups, group_id) + + def drop_set(self, set_id: str) -> dict: + return dropped(self.rule_sets, set_id) diff --git a/orchestrator/kvb_orchestrator/handler.py b/orchestrator/kvb_orchestrator/handler.py index 2e9d9c7..7639f19 100644 --- a/orchestrator/kvb_orchestrator/handler.py +++ b/orchestrator/kvb_orchestrator/handler.py @@ -64,8 +64,13 @@ SETTINGS_SIGN_ROUTE = "/api/settings/sign" CHOICES_ROUTE = "/api/choices" CHOICES_SIGN_ROUTE = "/api/choices/sign" DIFF_ROUTE = re.compile(r"/api/diffs/(review|ask)/([0-9a-f]{16})") +FIREWALL_ROUTE = "/api/firewall" +FIREWALL_POSTS = {"/api/firewall/preview": "settings_preview", "/api/firewall/settings": "change_settings", "/api/firewall/groups": "add_group", "/api/firewall/sets": "add_set"} +FIREWALL_STATE = re.compile(r"/api/firewall/settings/([0-9a-f]{16})") +FIREWALL_DROP = re.compile(r"/api/firewall/(groups|sets)/([0-9a-f]{16})/delete") +FIREWALL_STATUS = {"settings_preview": HTTPStatus.OK, "change_settings": HTTPStatus.ACCEPTED, "add_group": HTTPStatus.CREATED, "add_set": HTTPStatus.CREATED} WAV_POSTS = (STT_ROUTE, VOICE_ROUTE) -ACTION_POSTS = ("/api/rules", "/api/nl", SETTINGS_ROUTE, TTS_ROUTE, STT_ROUTE, SIGN_ROUTE, RULE_SIGN_ROUTE, CLEANUP_ROUTE, OFFICES_ROUTE, TASKS_ROUTE, VOICE_ROUTE, MESSAGE_SIGN_ROUTE, SETTINGS_SIGN_ROUTE, CHOICES_ROUTE, CHOICES_SIGN_ROUTE, GATES_ROUTE, TOKEN_ROUTE) +ACTION_POSTS = ("/api/rules", "/api/nl", SETTINGS_ROUTE, TTS_ROUTE, STT_ROUTE, SIGN_ROUTE, RULE_SIGN_ROUTE, CLEANUP_ROUTE, OFFICES_ROUTE, TASKS_ROUTE, VOICE_ROUTE, MESSAGE_SIGN_ROUTE, SETTINGS_SIGN_ROUTE, CHOICES_ROUTE, CHOICES_SIGN_ROUTE, GATES_ROUTE, TOKEN_ROUTE, *FIREWALL_POSTS) class Handler(Exchange): @@ -93,10 +98,13 @@ class Handler(Exchange): office = OFFICE_STATE.fullmatch(path) voice = VOICE_ITEM.fullmatch(path) diff = DIFF_ROUTE.fullmatch(path) + firewall = self.firewall_reader(path) if self.host() is None: self.send_text(HTTPStatus.FORBIDDEN, "forbidden host") elif path == "/": self.index() + elif firewall: + self.answer(firewall, HTTPStatus.OK) elif diff: self.answer(lambda: hub.diff_of(diff.group(1), diff.group(2)), HTTPStatus.OK) elif vendored and vendored.group(1) in VENDOR: @@ -197,7 +205,7 @@ class Handler(Exchange): if path == "/api/rules": return lambda body: self.answer(lambda: hub.add_rule(body), HTTPStatus.CREATED) if rule: - return lambda _body: self.answer(lambda: hub.rule_action(rule.group(1), rule.group(2)), HTTPStatus.OK) + return lambda body: self.answer(lambda: hub.rule_action(rule.group(1), rule.group(2), body), HTTPStatus.OK) if path == "/api/nl": return lambda body: self.answer(lambda: hub.nl(body), HTTPStatus.OK) if path == SETTINGS_ROUTE: @@ -216,10 +224,28 @@ class Handler(Exchange): return lambda body: self.answer(lambda: hub.sign_choices(body), HTTPStatus.OK) if path == CHOICES_ROUTE: return lambda body: self.answer(lambda: hub.request_choices(body), HTTPStatus.CREATED) + return self.firewall_action(path) + + def firewall_reader(self, path: str) -> Callable[[], dict] | None: + hub = self.server.hub + job = FIREWALL_STATE.fullmatch(path) + if path == FIREWALL_ROUTE: + return hub.firewall + return (lambda: hub.settings_state(job.group(1))) if job else None + + def firewall_action(self, path: str) -> Callable[[bytes], None] | None: + hub = self.server.hub + drop = FIREWALL_DROP.fullmatch(path) + if path in FIREWALL_POSTS: + method = getattr(hub, FIREWALL_POSTS[path]) + return lambda body: self.answer(lambda: method(body), FIREWALL_STATUS[FIREWALL_POSTS[path]]) + if drop: + remove = hub.drop_group if drop.group(1) == "groups" else hub.drop_set + return lambda _body: self.answer(lambda: remove(drop.group(2)), HTTPStatus.OK) return None def posted(self, path: str) -> bool: - return any(pattern.fullmatch(path) for pattern in (INBOX_ROUTE, UNDO_ROUTE, RULE_ROUTE, REQUEST_ROUTE, RUNNER_ROUTE)) + return any(pattern.fullmatch(path) for pattern in (INBOX_ROUTE, UNDO_ROUTE, RULE_ROUTE, REQUEST_ROUTE, RUNNER_ROUTE, FIREWALL_DROP)) def runner(self, path: str, body: bytes) -> None: hub = self.server.hub diff --git a/orchestrator/kvb_orchestrator/hub.py b/orchestrator/kvb_orchestrator/hub.py index b1649ac..2eb787c 100644 --- a/orchestrator/kvb_orchestrator/hub.py +++ b/orchestrator/kvb_orchestrator/hub.py @@ -19,6 +19,7 @@ from .crons import History, queue_live from .denying import Denying from .engines import Engines, Speech from .events import Log, Watch +from .firewall import Firewall from .gating import Gating from .icons import sheet from .inbox import Inbox @@ -43,7 +44,7 @@ from .redirects import Redirects from .requesting import Requesting from .resources import RESOURCES, SAMPLES from .reviewing import Reviewing -from .rules import Rules +from .rules import Offers, Rules from .settings import Settings from .signed import signing from .skill_scripts import Runner @@ -81,7 +82,7 @@ def environment(templates: Path) -> jinja2.Environment: @dataclass(frozen=True) -class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Requesting, Cleaning, Offices, Tasking, Gating, Inbox, Rules, Jobs, Sources, Timeline, View, Language): +class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Requesting, Cleaning, Offices, Tasking, Gating, Firewall, Inbox, Rules, Jobs, Sources, Timeline, View, Language): council: Path pages: Path public_port: int @@ -96,6 +97,7 @@ class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Reque scripts: Runner templates: jinja2.Environment rules: Ledger + offers: Offers jobs: Ledger marks: Marks log: Log @@ -118,6 +120,9 @@ class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Reque icons: Markup spool: Spool office_spool: Spool + firewall_spool: Spool + groups: Ledger + rule_sets: Ledger touched: Touched voice_notes: VoiceNotes replay: Path @@ -160,6 +165,9 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta icons=sheet(places.vendor, templates), spool=Spool(places.cleanup).ready(), office_spool=Spool(places.offices).ready(), + firewall_spool=Spool(state / "firewall").ready(), + groups=Ledger(state / "groups.json"), + rule_sets=Ledger(state / "rule-sets.json"), touched=Touched(reviews.stored), approvers=approvers, speech=speech, @@ -167,6 +175,7 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta replay=state / "replay" / "messages.json", view_store=ViewStore(state / "view.json"), rules=Ledger(state / "rules.json"), + offers=Offers(), jobs=Ledger(state / "jobs.json"), marks=Marks(state / "marks.json"), log=Log(state / "log.jsonl"), diff --git a/orchestrator/kvb_orchestrator/requesting.py b/orchestrator/kvb_orchestrator/requesting.py index 452ba22..496f4ea 100644 --- a/orchestrator/kvb_orchestrator/requesting.py +++ b/orchestrator/kvb_orchestrator/requesting.py @@ -6,7 +6,7 @@ from .denying import request_status, request_words, requester_of from .errors import Conflict, Missing, ToolError from .fields import WAIT_CAP, bounded, decode, expect, field, seconds from .records import iso_epoch -from .rules import rule_fields, rule_matches, signed_fields, subject_of +from .rules import EVERY_AGENT, rule_fields, rule_matches, signed_fields, subject_of from .signed import signing # Trip-wire: MCP carries no caller identity, so an agent may name itself anew for each request; AGENT_CAP only spreads the queue, TOTAL_CAP is the bound. @@ -33,7 +33,7 @@ class Requesting: def rule_request(self, arguments: dict) -> dict: expect(arguments, (*RULE_FIELDS, "agent", "why")) now = time.time() - fields = rule_fields(arguments, now) + fields = {name: value for name, value in rule_fields(arguments, EVERY_AGENT, now).items() if name in RULE_FIELDS} if fields["kind"] == "decision" and fields["scope"] != "*": raise ToolError("a decision rule takes scope *, as the rules form does") name = field(arguments, "agent").strip() @@ -71,7 +71,7 @@ class Requesting: rule = {**{key: record[key] for key in RULE_FIELDS}, "enabled": True, "proof": {"payload": payload, "assertion": None}} ledger = {"ask": self.asks, "review": self.reviews, "decision": self.decisions}[kind_name] since = now - RECENT_DAYS * 86400 - subjects = [subject_of(kind_name, entry) for entry in ledger.listed() if created_of(entry) >= since] + subjects = [subject_of(kind_name, entry, self.deciders(kind_name, entry)) for entry in ledger.listed() if created_of(entry) >= since] return {"matched": sum(rule_matches(rule, kind_name, subject, now) for subject in subjects), "total": len(subjects), "days": RECENT_DAYS} def request_items(self, base: dict, now: float, claims: list[dict]) -> list[dict]: @@ -95,7 +95,7 @@ class Requesting: def sign_request(self, request_id: str) -> dict: record = self.pending_request(request_id) - return {**self.rule_offer(rule_fields(record, time.time())), "sign": True} + return {**self.rule_offer(rule_fields(record, EVERY_AGENT, time.time())), "sign": True} def accept_request(self, request_id: str, body: bytes) -> dict: payload = decode(body) @@ -106,9 +106,8 @@ class Requesting: fields = signed_fields(rule, now) if [fields[key] for key in RULE_FIELDS] != [record[key] for key in RULE_FIELDS]: raise ToolError("the signed rule must be exactly the requested one") - if self.rules.get(rule["id"]) is not None: - raise Conflict({"id": rule["id"], "status": "exists"}) proof = self.proven_rule(rule, payload["assertion"]) + self.offered(rule, now) decided, changed = self.rule_requests.decide(request_id, {"status": "accepted", "rule": rule["id"]}) if not changed: raise Conflict(request_state(decided, now)) diff --git a/orchestrator/kvb_orchestrator/rules.py b/orchestrator/kvb_orchestrator/rules.py index f9bb79a..381777b 100644 --- a/orchestrator/kvb_orchestrator/rules.py +++ b/orchestrator/kvb_orchestrator/rules.py @@ -1,13 +1,19 @@ from __future__ import annotations import fnmatch +import threading import time +from .council import read_claims from .errors import Conflict, Missing, ToolError -from .fields import COMMAND_LIMIT, PATH_LIMIT, bounded, decode, expect +from .fields import COMMAND_LIMIT, PATH_LIMIT, bounded, decode, expect, visible +from .names import Directory from .signed import ask_signed, passkey, review_signed, signing, verifier RULE_KINDS = ("ask", "review", "decision") +RULE_INPUT = ("kind", "tool", "pattern", "scope", "expires") +EVERY_AGENT = [signing.ALL_AGENTS] +OFFER_LIMIT = 1000 def rule_matches(rule: dict, kind_name: str, subject: dict, now: float) -> bool: @@ -15,31 +21,73 @@ def rule_matches(rule: dict, kind_name: str, subject: dict, now: float) -> bool: if not rule["enabled"] or rule["expires"] <= now or rule["kind"] != kind_name or proof is None: return False if kind_name == "decision": - return subject["recommended"] != "" and fnmatch.fnmatchcase(subject["agent"], rule["pattern"]) + named = subject["recommended"] != "" and fnmatch.fnmatchcase(subject["agent"], rule["pattern"]) + return named and signing.reaches(proof["payload"], subject["sessions"]) return signing.rule_allows(proof["payload"], subject["item"], subject["gate"], now) -def subject_of(kind_name: str, record: dict) -> dict: +def subject_of(kind_name: str, record: dict, sessions: set[str]) -> dict: + """Args: + kind_name: ask, review or decision. + record: the ask, review or decision. + sessions: for a decision, the sessions that held the asking agent's + name; empty for an ask or review, whose signed item names its own. + + Returns: + What a rule of that kind is matched against. + """ if kind_name == "ask": return {"item": ask_signed(record), "gate": ""} if kind_name == "review": return {"item": review_signed(record), "gate": record.get("gate", "")} - return {"recommended": record["recommended"], "agent": record["agent"]} + return {"recommended": record["recommended"], "agent": record["agent"], "sessions": sessions} -def rule_fields(raw: dict, now: float) -> dict: +def rule_fields(raw: dict, agents: object, now: float) -> dict: + """Args: + raw: kind, tool, pattern, scope and expires of the rule. + agents: EVERY_AGENT, or the sorted session ids of one agent or group. + now: the current time in epoch seconds. + + Returns: + The checked fields with agents. + """ kind_name, expires = raw["kind"], raw["expires"] if kind_name not in RULE_KINDS: raise ToolError(f"kind must be one of {', '.join(RULE_KINDS)}") + tool_name, pattern, scope = rule_texts(raw, kind_name) + if isinstance(expires, bool) or not isinstance(expires, int | float) or not now + 1 < expires <= now + signing.RULE_LIFETIME: + raise ToolError("expires must be a moment within the next 366 days") + return {"kind": kind_name, "tool": tool_name, "pattern": pattern, "scope": scope, "expires": int(expires), "agents": target_of(kind_name, agents)} + + +def rule_texts(raw: dict, kind_name: str) -> tuple[str, str, str]: + """Returns: + tool, pattern and scope of raw, once each is one line without a + control, format, line or paragraph separator character, as visible() + checks, and only an ask rule names a tool. + """ tool_name = bounded(raw, "tool", 200).strip() if (kind_name == "ask") != bool(tool_name): raise ToolError("an ask rule names a tool; review and decision rules leave tool empty") pattern, scope = bounded(raw, "pattern", COMMAND_LIMIT), bounded(raw, "scope", PATH_LIMIT) if not pattern.strip() or not scope.strip() or "\n" in pattern or "\n" in scope: raise ToolError("pattern and scope must be one non-empty line each") - if isinstance(expires, bool) or not isinstance(expires, int | float) or not now + 1 < expires <= now + signing.RULE_LIFETIME: - raise ToolError("expires must be a moment within the next 366 days") - return {"kind": kind_name, "tool": tool_name, "pattern": pattern, "scope": scope, "expires": int(expires)} + visible({"tool": tool_name, "pattern": pattern, "scope": scope}) + return tool_name, pattern, scope + + +def target_of(kind_name: str, agents: object) -> list[str]: + """Returns: + agents, once signing.agents_problem() accepts it and a review rule + names every agent, since a review names no session. + """ + problem = signing.agents_problem(agents) + if problem: + raise ToolError(problem) + if kind_name == "review" and agents != EVERY_AGENT: + raise ToolError("a review rule applies to every agent: a review names no session") + return agents def signed_fields(rule: object, now: float) -> dict: @@ -49,17 +97,77 @@ def signed_fields(rule: object, now: float) -> dict: raise ToolError(f"rule: {error}") from None if len(rule["kinds"]) != 1: raise ToolError("a rule names exactly one kind") - fields = rule_fields({**{name: rule[name] for name in ("tool", "pattern", "scope", "expires")}, "kind": rule["kinds"][0]}, now) + agents = EVERY_AGENT + if signing.TARGET in rule: + agents = rule[signing.TARGET] + fields = rule_fields({**{name: rule[name] for name in ("tool", "pattern", "scope", "expires")}, "kind": rule["kinds"][0]}, agents, now) if fields["tool"] != rule["tool"]: raise ToolError("tool must not carry surrounding blanks") return fields +class Offers: + def __init__(self) -> None: + self.lock = threading.Lock() + self.ids: dict[str, float] = {} + + def offer(self, rule_id: str, now: float) -> None: + """Args: + rule_id: an id the hub just put into a rule payload for the + operator to sign; held in memory only, so a hub restart + forgets every offer and refuses their proofs. + now: the current time; offers older than signing.LIFETIME and + beyond the newest OFFER_LIMIT are dropped. + """ + with self.lock: + self.ids = {key: at for key, at in self.ids.items() if now - at <= signing.LIFETIME} + self.ids[rule_id] = now + while len(self.ids) > OFFER_LIMIT: + del self.ids[next(iter(self.ids))] + + def take(self, rule_id: str, now: float) -> bool: + with self.lock: + offered = self.ids.pop(rule_id, None) + return offered is not None and now - offered <= signing.LIFETIME + + class Rules: def rule_offer(self, fields: dict) -> dict: - rule = signing.rule_payload(self.rules.fresh(), [fields["kind"]], fields["tool"], fields["pattern"], fields["scope"], fields["expires"]) + rule = signing.targeted(signing.rule_payload(self.rules.fresh(), [fields["kind"]], fields["tool"], fields["pattern"], fields["scope"], fields["expires"]), fields["agents"]) + visible(rule) + self.offers.offer(rule["id"], time.time()) return {"payload": rule, "challenge": signing.challenge(rule), "credentials": [key["id"] for key in self.keys()]} + def offered(self, rule: dict, now: float) -> None: + """Raises: + Conflict unless the hub offered the rule's id and stored no rule + under it since, so a signed rule is stored once and never again + after its deletion. + """ + if not self.offers.take(rule["id"], now): + raise Conflict({"id": rule["id"], "status": "not offered or already stored: sign the rule anew"}) + + def deciders(self, kind_name: str, record: dict) -> set[str]: + """Returns: + For a decision, the one session that holds its agent's name now, + none when no session or several hold it; empty for every other + kind. + """ + if kind_name != "decision": + return set() + directory = Directory(self.names.listed()) + holders = {session for session in directory.aliases(record["agent"]) if directory.now(session) == record["agent"]} + return holders if len(holders) == 1 else set() + + def known_agents(self, agents: list[str]) -> None: + """Raises: + ToolError unless agents is EVERY_AGENT or every session in it is + one the Agent Orchestrator knows. + """ + unknown = sorted(set(agents) - self.office_sessions(read_claims(self.council))) if agents != EVERY_AGENT else [] + if unknown: + raise ToolError(f"agents must be sessions the Agent Orchestrator knows: {', '.join(unknown)[:200]}") + def proven_rule(self, rule: dict, assertion: object) -> dict: """Args: rule: the rule payload, already checked by signed_fields. @@ -80,8 +188,11 @@ class Rules: def sign_rule(self, body: bytes) -> dict: payload = decode(body) - expect(payload, ("kind", "tool", "pattern", "scope", "expires")) - return {**self.rule_offer(rule_fields(payload, time.time())), "sign": True} + targeted = "agents" in payload + expect(payload, (*RULE_INPUT, "agents") if targeted else RULE_INPUT) + fields = rule_fields(payload, payload["agents"] if targeted else EVERY_AGENT, time.time()) + self.known_agents(fields["agents"]) + return {**self.rule_offer(fields), "sign": True} def add_rule(self, body: bytes) -> dict: payload = decode(body) @@ -91,16 +202,49 @@ class Rules: fields = signed_fields(rule, now) if not isinstance(payload["enabled"], bool): raise ToolError("enabled must be true or false") - if self.rules.get(rule["id"]) is not None: - raise Conflict({"id": rule["id"], "status": "exists"}) - return self.store_rule(rule, fields, self.proven_rule(rule, payload["assertion"]), payload["enabled"], now) + proof = self.proven_rule(rule, payload["assertion"]) + self.offered(rule, now) + return self.store_rule(rule, fields, proof, payload["enabled"], now) - def rule_action(self, rule_id: str, verb: str) -> dict: + def enable_items(self, raw: list[dict]) -> list[dict]: + """Args: + raw: [{"kind": "enable", "rule"}] with the id of a stored rule. + + Returns: + The item a fresh approval signs to turn that rule on. + """ + expect(raw[0], ("kind", "rule")) + return [self.enable_subject(raw[0]["rule"])] + + def enable_subject(self, rule_id: object) -> dict: + record = self.rules.get(rule_id) if isinstance(rule_id, str) else None + if record is None: + raise Missing(str(rule_id)[:40]) + if record.get("proof") is None: + raise ToolError("an unsigned rule grants nothing: sign it anew") + return signing.enable_item(record["proof"]["payload"]) + + def rule_action(self, rule_id: str, verb: str, body: bytes) -> dict: + """Args: + rule_id: the stored rule. + verb: delete, disable, or enable, which widens what agents may + do and so needs {"proof"}: a fresh passkey approval listing + signing.enable_item() of the rule. + body: the request body. + """ if verb == "delete": if not self.rules.remove(rule_id): raise Missing(rule_id) return {"id": rule_id, "deleted": True} + if verb == "enable": + payload = decode(body) + expect(payload, ("proof",)) + self.operator_proven(payload["proof"], self.enable_subject(rule_id)) record = self.rules.change(rule_id, {"enabled": verb == "enable"}) if record is None: raise Missing(rule_id) return record + + +def enable_request(raw: object) -> bool: + return isinstance(raw, list) and len(raw) == 1 and isinstance(raw[0], dict) and raw[0].get("kind") == "enable" diff --git a/orchestrator/kvb_orchestrator/view.py b/orchestrator/kvb_orchestrator/view.py index 160bdac..8724ea9 100644 --- a/orchestrator/kvb_orchestrator/view.py +++ b/orchestrator/kvb_orchestrator/view.py @@ -226,5 +226,5 @@ class View: def render(self, nonce: str) -> tuple[str, str]: now = time.time() state = self.state(now - RADIO_WINDOW, now, now, 0) - page = self.templates.get_template("index.html.j2").render(nonce=nonce, state=state, view=self.view_store.get(), icons=self.icons) + page = self.templates.get_template("index.html.j2").render(nonce=nonce, state=state, view=self.view_store.get(), icons=self.icons, emoji_bases=sorted(signing.EMOJI_BASES)) return page, INDEX_CSP.format(nonce, " ".join(["'self'", *state["media"]])) diff --git a/orchestrator/systemd/kvb-orchestrator-firewall.path b/orchestrator/systemd/kvb-orchestrator-firewall.path new file mode 100644 index 0000000..fc5f663 --- /dev/null +++ b/orchestrator/systemd/kvb-orchestrator-firewall.path @@ -0,0 +1,10 @@ +[Unit] +Description=Agent Orchestrator firewall: wake on a signed settings change the Agent Orchestrator wrote + +[Path] +DirectoryNotEmpty=%h/.local/state/kvb-orchestrator/firewall/jobs +MakeDirectory=true +DirectoryMode=0700 + +[Install] +WantedBy=default.target diff --git a/orchestrator/systemd/kvb-orchestrator-firewall.service b/orchestrator/systemd/kvb-orchestrator-firewall.service new file mode 100644 index 0000000..7301fc7 --- /dev/null +++ b/orchestrator/systemd/kvb-orchestrator-firewall.service @@ -0,0 +1,9 @@ +[Unit] +Description=Agent Orchestrator firewall: verify the operator signature, then write exactly the signed change into a listed Claude settings file, and refresh the settings view the Firewall shows + +[Service] +Type=oneshot +Environment=PATH=/usr/bin:/bin +EnvironmentFile=%h/.local/share/kvb-orchestrator/.env +ExecStart=/usr/bin/env python3 %h/.local/share/kvb-orchestrator/client/settings_writer.py --port ${KVB_ORCHESTRATOR_PORT} --jobs %h/.local/state/kvb-orchestrator/firewall/jobs --results %h/.local/state/kvb-orchestrator/firewall/results --view %h/.local/state/kvb-orchestrator/firewall/view.json --approvers %h/.config/kvb-orchestrator/approvers.json --replay %h/.local/state/kvb-orchestrator-approvals/firewall.json --tree %h/.local/state/pkgmgr/tree/branch-tree.json +TimeoutStartSec=120 diff --git a/orchestrator/systemd/kvb-orchestrator-firewall.timer b/orchestrator/systemd/kvb-orchestrator-firewall.timer new file mode 100644 index 0000000..65dbe91 --- /dev/null +++ b/orchestrator/systemd/kvb-orchestrator-firewall.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Agent Orchestrator firewall settings view a minute after its last run + +[Timer] +OnActiveSec=5s +OnUnitInactiveSec=60s +AccuracySec=5s + +[Install] +WantedBy=timers.target diff --git a/orchestrator/templates/index.html.j2 b/orchestrator/templates/index.html.j2 index 05263a7..39abc2e 100644 --- a/orchestrator/templates/index.html.j2 +++ b/orchestrator/templates/index.html.j2 @@ -432,6 +432,8 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi {% include "style-ancestry.css" %} {% include "style-home.css" %} + +{% include "style-firewall.css" %} @@ -467,6 +469,7 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi + @@ -520,6 +523,8 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi {% include "page-state.js" %} +{% include "page-reveal.js" %} + {% include "page-dom.js" %} {% include "page-api.js" %} @@ -610,6 +615,8 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi {% include "page-rules.js" %} +{% include "page-firewall.js" %} + {% include "page-keyboard.js" %} {% include "page-popup.js" %} diff --git a/orchestrator/templates/page-ancestry.js b/orchestrator/templates/page-ancestry.js index 7f6e715..0f1ceb0 100644 --- a/orchestrator/templates/page-ancestry.js +++ b/orchestrator/templates/page-ancestry.js @@ -101,7 +101,7 @@ edges.push({child: item.branch, parent: item.parent, source: "staged"}); } } - const plan = Ancestry.layout(repo.branches.map((entry) => ({id: entry.name, label: entry.name})), edges); + const plan = Ancestry.layout(repo.branches.map((entry) => ({id: entry.name, label: revealed(entry.name)})), edges); const dropping = (link) => staged.some((item) => item.drop && item.branch === link.child && item.parent === link.parent); return ancestryChart(plan, `Ancestry of ${repo.name}`, { item: `ancestry:${repo.path}`, @@ -109,7 +109,7 @@ class: ["ancestry-edge", link.derived ? "derived" : "", link.source === "staged" ? "pending" : "", dropping(link) ? "dropping" : ""].filter(Boolean).join(" "), d, "data-child": link.child, "data-parent": link.parent, "data-source": link.source, on: {click: () => stageParent(repo.path, link.child, link.parent, link.source !== "staged")}, - }, s("title", {}, `${link.parent} → ${link.child} · ${link.source}`)), + }, s("title", {}, revealed(`${link.parent} → ${link.child} · ${link.source}`))), node: (node) => s("g", { class: "node a-branch ancestry-node", transform: `translate(${fixed(node.x)} ${fixed(node.y)})`, tabindex: 0, role: "button", "aria-label": node.label, "data-repo": repo.path, "data-branch": node.id, "data-generation": node.generation, diff --git a/orchestrator/templates/page-cleanup.js b/orchestrator/templates/page-cleanup.js index 859e09d..17b4e76 100644 --- a/orchestrator/templates/page-cleanup.js +++ b/orchestrator/templates/page-cleanup.js @@ -17,8 +17,8 @@ const waits = locked.includes(item); const kept = held.includes(item); return h("li", {class: waits ? "dry-run locked" : kept ? "dry-run held" : "dry-run", "data-token": waits ? "needed" : null, "data-held": kept && entry ? entry.held : null}, - h("div", {class: "row"}, h("strong", {}, repo ? repo.name : item.repository), h("code", {}, item.branch), waits ? chip("bi-key", "token needed", "warn") : kept ? chip("bi-folder-symlink", "held by a worktree: not deleted here", "neutral") : null), - h("pre", {}, (entry ? entry.plan : [`${item.branch} @ ${item.tip.slice(0, 12)}`]).join("\n"))); + h("div", {class: "row"}, h("strong", {}, revealed(repo ? repo.name : item.repository)), h("code", {}, revealed(item.branch)), waits ? chip("bi-key", "token needed", "warn") : kept ? chip("bi-folder-symlink", "held by a worktree: not deleted here", "neutral") : null), + h("pre", {}, (entry ? entry.plan : [`${item.branch} @ ${item.tip.slice(0, 12)}`]).map(revealed).join("\n"))); }); const outcome = h("div", {class: "mini", "aria-live": "polite"}); const confirm = h("button", {type: "button", class: "btn danger", ...named("Delete", "Delete: hand over to the host with your passkey signature")}, icon("bi-trash3"), "Delete"); diff --git a/orchestrator/templates/page-diff.js b/orchestrator/templates/page-diff.js index e7426d0..1441202 100644 --- a/orchestrator/templates/page-diff.js +++ b/orchestrator/templates/page-diff.js @@ -24,15 +24,15 @@ function patchRow(row) { const number = row.new || row.old || null; if (row.kind === "hunk") { - return h("tr", {class: "hunk"}, h("td", {colspan: 3}), h("td", {class: "code"}, row.text)); + return h("tr", {class: "hunk"}, h("td", {colspan: 3}), h("td", {class: "code"}, revealed(row.text))); } return h("tr", {class: row.kind, "data-line": number}, - h("td", {class: "num"}, row.old), h("td", {class: "num"}, row.new), h("td", {class: "sign"}, SIGNS[row.kind] || " "), h("td", {class: "code"}, row.text)); + h("td", {class: "num"}, row.old), h("td", {class: "num"}, row.new), h("td", {class: "sign"}, SIGNS[row.kind] || " "), h("td", {class: "code"}, revealed(row.text))); } function diffBlock(file) { const tone = DIFF_TONES[file.status] || "neutral"; - const head = [h("span", {class: "letter", "data-tone": tone}, file.status), h("code", {class: "path"}, file.path), + const head = [h("span", {class: "letter", "data-tone": tone}, file.status), h("code", {class: "path"}, revealed(file.path)), file.mode && file.mode !== "100644" ? h("code", {class: "mode", title: `mode: ${file.mode}`}, file.mode) : null, h("span", {class: "spacer"}), file.note ? h("span", {class: "note", title: file.note}, icon("bi-eye-slash"), file.note) : null, file.status === "B" ? icon("bi-file-earmark-binary") : h("span", {class: "stat"}, h("span", {class: "add"}, `+${file.additions}`), h("span", {class: "del"}, `-${file.deletions}`))]; diff --git a/orchestrator/templates/page-dom.js b/orchestrator/templates/page-dom.js index 39e846f..7ba8f7d 100644 --- a/orchestrator/templates/page-dom.js +++ b/orchestrator/templates/page-dom.js @@ -48,7 +48,7 @@ const status = (value) => pill((STATUS[value] || [value])[0], (STATUS[value] || [value, "neutral"])[1]); const klass = (value) => pill(CLASSES[value] || value, PILLS[value] || "neutral"); const counter = (value) => h("span", {class: "count"}, String(value)); - const meta = (pairs) => h("dl", {class: "meta"}, pairs.map(([label, value, title]) => h("div", {}, h("dt", {}, label), h("dd", {}, h("code", {title: title || null}, dash(value)))))); + const meta = (pairs) => h("dl", {class: "meta"}, pairs.map(([label, value, title]) => h("div", {}, h("dt", {}, label), h("dd", {}, h("code", {title: title ? revealed(title) : null}, revealed(dash(value))))))); const emptyState = (title, text) => h("div", {class: "empty"}, h("div", {class: "empty-mark", "aria-hidden": "true"}), h("h2", {}, title), h("p", {}, text)); const live = () => ui.preset !== -1 && ui.asOf === null; const alarmRows = () => state.radio.filter((row) => (row.class === "mayday" || row.class === "pan-pan") && row.ts >= state.alarm_since).reverse(); @@ -89,7 +89,7 @@ s("title", {}, PRESENCE[value][0]), s("rect", {class: "presence-dot", x: fixed(-size * 0.65), y: fixed(-size * 0.65), width: fixed(size * 1.3), height: fixed(size * 1.3), rx: fixed(size * 0.65)}), s("g", {class: "glyph"}, s("use", {href: `#${PRESENCE[value][1]}`, x: fixed(-size / 2), y: fixed(-size / 2), width: fixed(size), height: fixed(size)}))) : null; }; - const person = (name) => (name && name !== "-" && name !== "all" && name !== HUMAN ? h("button", {type: "button", class: "linkish", title: `Open ${name}`, on: {click: () => openAgent(name)}}, name) : dash(name)); + const person = (name) => (name && name !== "-" && name !== "all" && name !== HUMAN ? h("button", {type: "button", class: "linkish", title: revealed(`Open ${name}`), on: {click: () => openAgent(name)}}, revealed(name)) : dash(name)); const people = () => state.people || {}; const labelOf = (key) => (people()[key] || {name: String(key).replace(/^name:/, "")}).name; const nameOf = (key) => (people()[key] || {now: String(key).replace(/^name:/, "")}).now; @@ -147,8 +147,9 @@ function openSheet(title, body, wide) { const sheet = document.getElementById("sheet"); - sheet.replaceChildren(h("div", {class: `sheet-box${wide ? " wide" : ""}`, role: "dialog", "aria-modal": "true", "aria-label": title}, - h("div", {class: "sheet-head"}, h("h2", {class: "panel-title"}, title), h("span", {class: "spacer"}), iconBtn("bi-x-lg", "Close", "btn small ghost", closeSheet)), + const shown = revealed(title); + sheet.replaceChildren(h("div", {class: `sheet-box${wide ? " wide" : ""}`, role: "dialog", "aria-modal": "true", "aria-label": shown}, + h("div", {class: "sheet-head"}, h("h2", {class: "panel-title"}, shown), h("span", {class: "spacer"}), iconBtn("bi-x-lg", "Close", "btn small ghost", closeSheet)), h("div", {class: "sheet-body"}, body))); sheet.hidden = false; } diff --git a/orchestrator/templates/page-firewall.js b/orchestrator/templates/page-firewall.js new file mode 100644 index 0000000..4c3b99f --- /dev/null +++ b/orchestrator/templates/page-firewall.js @@ -0,0 +1,384 @@ + 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 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: {}}; + + 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:, group: 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(); + } + + function entryRow(path, list, entry) { + const last = list.split(".").pop(); + const tone = Object.hasOwn(LIST_TONES, last) ? LIST_TONES[last] : "neutral"; + const editable = Object.hasOwn(FIREWALL_LISTS, list); + return h("li", {"data-entry": entry, "data-list": list}, h("div", {class: "row"}, pill(FIREWALL_LISTS[list] || list, tone), h("code", {}, revealed(entry)), h("span", {class: "spacer"}), + editable ? iconBtn("bi-dash-circle", "Remove", "btn small ghost", () => stageChange(path, {list, op: "remove", entry}), `Remove: stage the removal of ${revealed(entry)} from ${list}`) : null)); + } + + function fileCard(file) { + const groups = file.groups.map((group) => h("details", {class: "fw-group", open: true, "data-tool": group.tool, "data-prefix": group.prefix}, + h("summary", {}, h("strong", {}, group.tool), group.prefix ? h("code", {}, group.prefix) : null, counter(group.entries.length)), + h("ul", {class: "mini"}, group.entries.map((entry) => entryRow(file.path, `permissions.${entry.list}`, entry.entry))))); + const sandbox = file.sandbox.map((entry) => (entry.values + ? h("details", {class: "fw-group", "data-key": entry.key}, h("summary", {}, h("strong", {}, entry.key), counter(entry.values.length)), h("ul", {class: "mini"}, entry.values.map((value) => entryRow(file.path, entry.key, value)))) + : h("div", {class: "rule-row", "data-key": entry.key}, h("strong", {}, entry.key), h("code", {}, JSON.stringify(entry.value))))); + const status = file.error ? pill(file.error, "bad") : file.exists ? pill(`sha256 ${file.sha256.slice(0, 12)}`, "neutral") : pill("missing: a change creates it", "warn"); + return h("section", {class: "fw-file", "data-file": file.path, "aria-label": file.path}, h("div", {class: "row"}, h("code", {class: "fw-path"}, file.path), status), + groups.length ? groups : h("p", {class: "muted"}, "No permission entries."), sandbox.length ? h("h4", {}, "Sandbox") : null, sandbox); + } + + 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", "aria-label": "Claude settings"}, h("h3", {}, "Claude settings"), + h("p", {class: "muted"}, "Permissions and sandbox entries of the listed settings files, grouped by tool and prefix as the host read them. A 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, + files.map(fileCard), 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; + } diff --git a/orchestrator/templates/page-inbox.js b/orchestrator/templates/page-inbox.js index 6f633ec..f9ebd51 100644 --- a/orchestrator/templates/page-inbox.js +++ b/orchestrator/templates/page-inbox.js @@ -75,11 +75,11 @@ return; } const rows = usable.map((item) => h("li", {}, - h("div", {class: "row"}, pill(ITEM_LABELS[item.kind], KIND_TONES[item.kind]), item.kind === "decision" && !deny ? h("span", {class: "muted"}, `→ ${item.recommended}`) : null), - h("strong", {}, item.title), - item.kind === "operator" ? h("div", {class: "command"}, h("code", {}, item.command)) : null, - item.kind === "operator" ? h("span", {class: "muted"}, `in ${item.directory}`) : null, - item.kind === "ask" ? h("div", {class: "command"}, h("code", {}, item.command)) : null)); + h("div", {class: "row"}, pill(ITEM_LABELS[item.kind], KIND_TONES[item.kind]), item.kind === "decision" && !deny ? h("span", {class: "muted"}, revealed(`→ ${item.recommended}`)) : null), + h("strong", {}, revealed(item.title)), + item.kind === "operator" ? h("div", {class: "command"}, h("code", {}, revealed(item.command))) : null, + item.kind === "operator" ? h("span", {class: "muted"}, revealed(`in ${item.directory}`)) : null, + item.kind === "ask" ? h("div", {class: "command"}, h("code", {}, revealed(item.command))) : null)); const confirm = h("button", {type: "button", class: deny ? "btn danger" : "btn primary"}, `${deny ? "Deny" : "Grant"} ${usable.length}`); confirm.addEventListener("click", () => { closeSheet(); @@ -135,7 +135,7 @@ } else if (item.kind === "ask") { buttons.push(iconBtn("bi-check-lg", "Grant", "btn small primary", () => approveOne(item))); } else if (item.kind === "decision" && item.recommended) { - buttons.push(h("button", {type: "button", class: "btn small primary", ...named(`Recommendation: ${item.recommended}`), on: {click: () => approveOne(item)}}, icon("bi-check-lg"), item.recommended)); + buttons.push(h("button", {type: "button", class: "btn small primary", ...named(revealed(`Recommendation: ${item.recommended}`)), on: {click: () => approveOne(item)}}, icon("bi-check-lg"), revealed(item.recommended))); } else if (item.kind === "operator" && item.runnable) { buttons.push(iconBtn("bi-play-fill", "Run", "btn small primary", () => approveOne(item))); } else if (item.kind === "rule") { @@ -166,18 +166,18 @@ item.until ? pill(`until ${absolute.format(stampOf(item.until))}`, "neutral") : null), h("div", {class: "entry-body"}, h("div", {class: "route"}, - h("button", {type: "button", class: "linkish headline", title: "Open", on: {click: () => openItem(item)}}, item.kind === "operator" ? item.id : item.kind === "ask" ? item.title.split(": ")[0] : item.title), + h("button", {type: "button", class: "linkish headline", title: "Open", on: {click: () => openItem(item)}}, revealed(item.kind === "operator" ? item.id : item.kind === "ask" ? item.title.split(": ")[0] : item.title)), item.agent ? [face(item.agent, false), person(item.agent)] : null), - item.kind === "review" ? h("div", {class: "muted"}, item.detail, item.score ? ` · ${item.score}` : "") : null, - commanded ? h("div", {class: "command", "data-path": item.directory || "-"}, h("code", {}, item.command), copyButton(item.command)) : null, - commanded ? h("span", {class: "muted"}, `in ${dash(item.directory)}`) : null, - item.kind === "operator" && item.detail && item.detail !== "-" ? h("p", {class: "text reason"}, item.detail) : null, + item.kind === "review" ? h("div", {class: "muted"}, revealed(item.detail), item.score ? revealed(` · ${item.score}`) : "") : null, + commanded ? h("div", {class: "command", "data-path": item.directory || "-"}, h("code", {}, revealed(item.command)), copyButton(item.command)) : null, + commanded ? h("span", {class: "muted"}, revealed(`in ${dash(item.directory)}`)) : null, + item.kind === "operator" && item.detail && item.detail !== "-" ? h("p", {class: "text reason"}, revealed(item.detail)) : null, diffBlocks(item), item.job ? consoleOf(item.job) : null, item.kind === "rule" ? ruleRequestBody(item) : null, item.kind === "decision" ? h("div", {class: "options"}, item.options.map((option) => h("button", { - type: "button", class: `option${option.label === item.recommended ? " recommended" : ""}`, title: `${option.label}: your passkey signs this answer, a post to ${item.agent}`, disabled: item.held, on: {click: () => chooseOne(item, option.label)}, - }, h("strong", {}, option.label), option.description ? h("span", {}, option.description) : null))) : null, + type: "button", class: `option${option.label === item.recommended ? " recommended" : ""}`, title: revealed(`${option.label}: your passkey signs this answer, a post to ${item.agent}`), disabled: item.held, on: {click: () => chooseOne(item, option.label)}, + }, h("strong", {}, revealed(option.label)), option.description ? h("span", {}, revealed(option.description)) : null))) : null, feedbackList(keyOf(item))), h("div", {class: "item-actions line-actions"}, actionsFor(item), @@ -246,8 +246,8 @@ } const body = [ h("div", {class: "who"}, pill(ITEM_LABELS[item.kind], KIND_TONES[item.kind]), item.agent ? face(item.agent, false) : null, item.agent ? person(item.agent) : null, when(item.ts)), - h("p", {class: "text"}, item.title), - item.command ? h("div", {class: "command"}, h("code", {}, item.command), copyButton(item.command)) : null, + h("p", {class: "text"}, revealed(item.title)), + item.command ? h("div", {class: "command"}, h("code", {}, revealed(item.command)), copyButton(item.command)) : null, item.kind === "rule" ? ruleRequestBody(item) : null, meta([["Directory", item.directory], ["Warnings", item.warnings.join(", ")], ["Id", item.id]]), item.kind === "decision" ? h("div", {class: "options"}, item.options.map((option) => h("button", { @@ -255,7 +255,7 @@ closeSheet(); chooseOne(item, option.label); }}, - }, h("strong", {}, option.label), option.description ? h("span", {}, option.description) : null))) : null, + }, h("strong", {}, revealed(option.label)), option.description ? h("span", {}, revealed(option.description)) : null))) : null, h("div", {class: "actions"}, h("span", {class: "spacer"}), actionsFor(item).map((node) => { node.addEventListener("click", closeSheet); return node; diff --git a/orchestrator/templates/page-passkey.js b/orchestrator/templates/page-passkey.js index 5f2fefd..9346a21 100644 --- a/orchestrator/templates/page-passkey.js +++ b/orchestrator/templates/page-passkey.js @@ -16,6 +16,8 @@ toast(`${label}: not signed`, ["No passkey pinned: Settings → Register passkey"], false); return null; } + const prompt = h("div", {class: "toast wait passkey-prompt", id: "passkey-prompt", role: "status", "aria-live": "polite"}, h("strong", {}, "Passkey signs"), h("pre", {}, promptText(label, prepared))); + document.body.append(prompt); try { const credential = await navigator.credentials.get({publicKey: { challenge: unb64url(prepared.challenge), @@ -29,6 +31,8 @@ } catch (error) { toast(`${label}: not signed`, [`${(error && error.name) || error}: KeePassXC locked or canceled; nothing was sent.`], false); return null; + } finally { + prompt.remove(); } } diff --git a/orchestrator/templates/page-reveal.js b/orchestrator/templates/page-reveal.js new file mode 100644 index 0000000..cee10ba --- /dev/null +++ b/orchestrator/templates/page-reveal.js @@ -0,0 +1,25 @@ + const UNSEEN = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}\p{Zs}\p{Default_Ignorable_Code_Point}]/gu; + const EMOJI_BASES = new Set(JSON.parse(document.getElementById("emoji-bases").textContent)); + const PRESENTATION = new Set([0xFE0E, 0xFE0F]); + + /** + * @param {string} text the text a match sits in. + * @param {number} offset the UTF-16 index of the match. + * @returns {number} the code point right before offset, NaN at the start. + */ + const codePointBefore = (text, offset) => { + const pair = offset > 1 ? text.codePointAt(offset - 2) : 0; + return pair > 0xFFFF ? pair : text.charCodeAt(offset - 1); + }; + + /** + * @param {string} text a text a passkey prompt, a signed-item preview or an inbox diff shows. + * @returns {string} the text with every control, format, line or paragraph separator, space separator but the space and default-ignorable character but the tab shown as ⟨U+XXXX⟩, the set signing.unseen() refuses, except a variation selector right after a code point of EMOJI_BASES, the pair signing.presented() accepts, so no text hides, reorders or splits what the passkey signs. + */ + const revealed = (text) => String(text).replace(UNSEEN, (char, offset, whole) => (char === "\t" || char === " " || (PRESENTATION.has(char.codePointAt(0)) && EMOJI_BASES.has(codePointBefore(whole, offset))) ? char : `⟨U+${char.codePointAt(0).toString(16).toUpperCase().padStart(4, "0")}⟩`)); + + /** + * @param {string} text a text of several lines a signed-item preview shows. + * @returns {string} each line through revealed(), joined by real line breaks. + */ + const revealedLines = (text) => String(text).split("\n").map(revealed).join("\n"); diff --git a/orchestrator/templates/page-review.js b/orchestrator/templates/page-review.js index d97723b..f1f3237 100644 --- a/orchestrator/templates/page-review.js +++ b/orchestrator/templates/page-review.js @@ -44,7 +44,7 @@ const head = h("article", {class: "card"}, h("div", {class: "card-head"}, h("div", {}, h("p", {class: "muted"}, "submitted ", when(review.created_ts))), h("div", {class: "who"}, status(review.status || "pending"), review.gate ? pill(review.gate === "pass" ? "Gate green" : "Gate red", review.gate === "pass" ? "ok" : "bad") : null)), meta([["Repo", review.worktree], ["Branch", review.branch], ["Tree", (review.tree || "").slice(0, 12), review.tree], ["Score", review.score]]), - review.notes ? h("pre", {class: "notes"}, review.notes) : null, + review.notes ? h("pre", {class: "notes"}, revealedLines(review.notes)) : null, h("div", {class: "actions"}, h("a", {class: "btn ghost icon-only", href: `/reviews/${encodeURIComponent(review.id)}/page`, target: "_blank", rel: "noopener noreferrer", ...named("Open full page", "Open full page (new tab)")}, icon("bi-box-arrow-up-right")), h("span", {class: "spacer"}), diff --git a/orchestrator/templates/page-rule-request.js b/orchestrator/templates/page-rule-request.js index 9d32dea..ba75223 100644 --- a/orchestrator/templates/page-rule-request.js +++ b/orchestrator/templates/page-rule-request.js @@ -5,7 +5,7 @@ const dry = item.dry_run; const pairs = [["Kind", RULE_KINDS[rule.kind]], rule.kind === "ask" ? ["Tool (glob)", rule.tool] : null, ["Pattern (glob)", rule.pattern], rule.kind === "decision" ? null : ["Directory (glob)", rule.scope], ["Expires on", absolute.format(stampOf(rule.expires))]]; return h("div", {class: "rule-request"}, - item.detail ? h("p", {class: "text"}, item.detail) : null, + item.detail ? h("p", {class: "text"}, revealed(item.detail)) : null, meta(pairs.filter(Boolean)), h("p", {class: "muted dry-run-count"}, "Dry run: the rule would have granted ", h("strong", {}, `${dry.matched} of ${dry.total}`), ` ${COUNTED[rule.kind]} from the last ${dry.days} days.`), h("p", {class: "muted"}, RULE_HINTS[rule.kind])); diff --git a/orchestrator/templates/page-rules.js b/orchestrator/templates/page-rules.js index b00ea45..9a1ccf9 100644 --- a/orchestrator/templates/page-rules.js +++ b/orchestrator/templates/page-rules.js @@ -1,12 +1,23 @@ const RULE_KINDS = {ask: "Ask: tool + command pattern", review: "Review: branch pattern + gate green", decision: "Decision: take the recommendation"}; const RULE_HINTS = { - ask: "Grants matching asks in matching directories, never a command with a warning (rm -r, push, --force, sudo, pipes into a shell …). The passkey signs the rule once; the hook checks signature and pattern itself.", - review: "Grants reviews of matching branches and worktrees, only when their gate is green. The passkey signs the rule once; the commit skill checks signature and pattern itself.", + ask: "Grants matching asks in matching directories, never a command with a warning (rm -r, push, --force, sudo, pipes into a shell …). The passkey signs the rule and the agents it applies to once; the hook checks signature, pattern and agent itself.", + review: "Grants reviews of matching branches and worktrees, only when their gate is green, for every agent: a review names no session. The passkey signs the rule once; the commit skill checks signature and pattern itself.", decision: "Takes the recommended option in decisions of matching agents. The passkey signs the rule once; the decisions themselves stay unsigned.", }; + /** + * @param {Object} rule the stored rule to turn on again, which widens what agents may do. + * @returns {Promise} the hub's answer, null when the operator did not sign. + */ + async function turnOn(rule) { + const signed = await proofFor([{kind: "enable", rule: rule.id}], "Turn on rule"); + return signed ? sendJson(`/api/rules/${encodeURIComponent(rule.id)}/enable`, {proof: signed.proof}) : null; + } + function rulesScreen() { const kind = h("select", {class: "field", "aria-label": "Kind"}, Object.entries(RULE_KINDS).map(([value, label]) => h("option", {value}, label))); + const target = targetSelect("Applies to", null); + const targetField = h("label", {}, "Applies to", target); const tool = h("input", {class: "field", type: "text", placeholder: "Bash", value: "Bash"}); const pattern = h("input", {class: "field", type: "text", placeholder: "make test*"}); const scope = h("input", {class: "field", type: "text", value: "*", placeholder: "/home/…/Repositories/*"}); @@ -19,12 +30,15 @@ const sync = () => { toolField.hidden = kind.value !== "ask"; scopeField.hidden = kind.value === "decision"; + targetField.hidden = kind.value === "review"; + target.disabled = targetField.hidden; + fillTargets(target); pattern.placeholder = {ask: "make test*", review: "feature/*", decision: "* (agent pattern)"}[kind.value]; hint.textContent = RULE_HINTS[kind.value]; }; kind.addEventListener("change", sync); sync(); - const form = h("form", {class: "rule-form"}, h("label", {}, "Kind", kind), toolField, h("label", {}, "Pattern (glob)", pattern), scopeField, h("label", {}, "Expires on", expires), h("label", {class: "check"}, enabled, "active"), h("button", {type: "submit", class: "btn primary"}, "Create rule")); + const form = h("form", {class: "rule-form"}, h("label", {}, "Kind", kind), targetField, toolField, h("label", {}, "Pattern (glob)", pattern), scopeField, h("label", {}, "Expires on", expires), h("label", {class: "check"}, enabled, "active"), h("button", {type: "submit", class: "btn primary", "data-sign": ""}, "Create rule")); form.addEventListener("submit", async (event) => { event.preventDefault(); const end = Date.parse(`${expires.value}T23:59:59`) / 1000; @@ -34,6 +48,7 @@ pattern: pattern.value.trim() || (kind.value === "decision" ? "*" : ""), scope: kind.value === "decision" ? "*" : scope.value.trim(), expires: Number.isFinite(end) ? end : 0, + agents: ruleAgents(kind.value, target.value), }; const prepared = await sendJson("/api/rules/sign", fields); if (!prepared.ok || !prepared.data) { @@ -49,18 +64,19 @@ await load(); }); const change = async (rule, verb) => { - const result = await send(`/api/rules/${encodeURIComponent(rule.id)}/${verb}`, "", "application/json"); - if (!result.ok) { - toast("Rule not changed", [`${result.status} ${result.text}`.trim()], false); + const result = verb === "enable" ? await turnOn(rule) : await send(`/api/rules/${encodeURIComponent(rule.id)}/${verb}`, "", "application/json"); + if (result && !result.ok) { + toast("Rule not changed", fail(result), false); } await load(); }; const now = nowSeconds(); const list = state.rules.length ? h("div", {class: "mini"}, state.rules.map((rule) => h("div", {class: "rule-row"}, pill(rule.kind, KIND_TONES[rule.kind]), - rule.tool ? h("code", {}, rule.tool) : null, - h("code", {}, rule.pattern), - rule.kind === "decision" ? null : h("code", {title: "Directory"}, rule.scope || "–"), + targetPill(rule.agents), + rule.tool ? h("code", {}, revealed(rule.tool)) : null, + h("code", {}, revealed(rule.pattern)), + rule.kind === "decision" ? null : h("code", {title: "Directory"}, rule.scope ? revealed(rule.scope) : "–"), pill(rule.signed ? "signed" : "unsigned: has no effect", rule.signed ? "ok" : "bad"), rule.expires <= now ? pill("expired", "bad") : h("span", {class: "muted"}, `until ${absolute.format(stampOf(rule.expires))}`), rule.enabled ? pill("active", "ok") : pill("off", "neutral"), @@ -71,5 +87,5 @@ change(rule, "delete"); } })))) : h("p", {class: "muted"}, "No rule yet. Every automatic grant shows up in the stream and can be undone for 3 s."); - return h("div", {class: "stack"}, h("h2", {class: "title"}, "Firewall rules for automatic grants"), list, h("div", {class: "card"}, hint, form)); + return h("div", {class: "stack"}, h("h2", {class: "title"}, "Firewall"), h("h3", {}, "Hub rules for automatic grants"), list, h("div", {class: "card"}, hint, form), ...firewallSections()); } diff --git a/orchestrator/templates/page-tasks-prio.js b/orchestrator/templates/page-tasks-prio.js index 8552130..53096a5 100644 --- a/orchestrator/templates/page-tasks-prio.js +++ b/orchestrator/templates/page-tasks-prio.js @@ -51,9 +51,9 @@ const setValue = h("button", {type: "button", class: "btn small primary", ...named("Set business value", "Set the business value as Operator, signed with the passkey; WSJF uses it instead of the median")}, icon("bi-gem"), "Set business value"); const sync = () => { pin.disabled = !prioText(prio.value); - pinCode.textContent = pin.disabled ? `Enter a priority from 1 to ${PRIO_MAX}` : shownArgv(prioArgv()); - estimateCode.textContent = shownArgv(estimateArgv()); - valueCode.textContent = shownArgv(valueArgv()); + pinCode.textContent = pin.disabled ? `Enter a priority from 1 to ${PRIO_MAX}` : revealed(shownArgv(prioArgv())); + estimateCode.textContent = revealed(shownArgv(estimateArgv())); + valueCode.textContent = revealed(shownArgv(valueArgv())); }; for (const field of [prio, value, complexity, alone]) { field.addEventListener("input", sync); @@ -96,7 +96,7 @@ function taskJobs(row) { const jobs = state.jobs.filter((job) => job.kind === "task" && job.task === row.task).slice(-5).reverse(); return jobs.length ? [h("h3", {}, "Signed changes", counter(jobs.length)), h("ul", {class: "mini"}, jobs.map((job) => h("li", {"data-job": job.status}, - h("div", {class: "row"}, h("code", {}, job.command), status(job.status)), + h("div", {class: "row"}, h("code", {}, revealed(job.command)), status(job.status)), job.status === "refused" || job.status === "failed" ? h("pre", {class: "notes"}, job.output) : null)))] : null; } diff --git a/orchestrator/templates/style-firewall.css b/orchestrator/templates/style-firewall.css new file mode 100644 index 0000000..9a8bea7 --- /dev/null +++ b/orchestrator/templates/style-firewall.css @@ -0,0 +1,14 @@ +.stack-tight { display: grid; gap: 10px; min-width: 0; } +.fw-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 200px), 1fr)); gap: 10px; align-items: end; } +.fw-form label { display: grid; gap: 4px; min-width: 0; color: var(--muted); font-size: var(--fs-sm); } +.fw-form .field { width: 100%; min-width: 0; } +.fw-file { display: grid; gap: 8px; min-width: 0; padding: 10px; border: 1px solid var(--line); border-radius: 10px; } +.fw-file > .row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; } +.fw-path { overflow-wrap: anywhere; } +.fw-group > summary { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; cursor: pointer; } +.fw-group code, .fw-patterns code { overflow-wrap: anywhere; } +.fw-diff { max-height: 50vh; margin: 4px 0 0; padding: 8px; overflow: auto; border-radius: 8px; background: var(--sunken); font-size: var(--fs-sm); white-space: pre-wrap; overflow-wrap: anywhere; } +.fw-diff .add { color: var(--ok); } +.fw-diff .del { color: var(--bad); } +.rule-row select.field { width: auto; max-width: 100%; min-width: 0; } +.passkey-prompt { position: fixed; top: 18px; left: 50%; z-index: 60; width: min(440px, calc(100vw - 36px)); transform: translateX(-50%); } diff --git a/scripts/orchestrator.sh b/scripts/orchestrator.sh index 1e3c8b5..ad1afcf 100644 --- a/scripts/orchestrator.sh +++ b/scripts/orchestrator.sh @@ -5,9 +5,9 @@ # server for Claude Code, build pkgmgr from the newest signed tag of # PKGMGR_REPO and enable the tree timer and the cleanup path unit, # enable the sessions timer and the offices path unit, enable the -# resources timer; re-enable the runner and re-register -# the hook when the migration took them over; an enabled runner gets -# the fresh unit and a restart +# resources timer, the firewall path unit and timer; re-enable the +# runner and re-register the hook when the migration took them over; +# an enabled runner gets the fresh unit and a restart # migrate move every trace of the earlier name to the kvb-orchestrator names # once: container, image, code, state, approvals, pinned keys, MCP # server, PermissionRequest hook, runner unit; a second run changes @@ -42,6 +42,7 @@ CLEANUP_UNIT="${NAME}-cleanup" OFFICES_UNIT="${NAME}-offices" SESSIONS_UNIT="${NAME}-sessions" RESOURCES_UNIT="${NAME}-resources" +FIREWALL_UNIT="${NAME}-firewall" INSTALLED_SKILLS="${HOME}/.claude/skills" HOST_SCRIPTS=(kvb-org-organize/scripts/council.py kvb-org-organize/scripts/nofollow.py kvb-org-rasci/scripts/rasci.py kvb-org-operator/scripts/operator.py kvb-org-radio/scripts/radio.py) TREE_DIR="${HOME}/.local/state/pkgmgr/tree" @@ -157,7 +158,7 @@ install_hub() { cp -a "${REPO_ROOT}/orchestrator/." "${HUB}/" # Python loads a cached .pyc whose header matches the source mtime and size, and cp -a keeps the mtime: one planted in the sandbox-writable tree would run on the host find -H "${HUB}" \( -name __pycache__ -o -name '*.py[co]' \) -prune -exec rm -rf {} + - chmod +x "${HUB}/client/permission_hook.py" "${HUB}/client/runner.py" "${HUB}/client/approval.py" "${HUB}/client/cleanup.py" "${HUB}/client/offices.py" "${HUB}/client/sessions.py" "${HUB}/client/resources.py" + chmod +x "${HUB}/client/permission_hook.py" "${HUB}/client/runner.py" "${HUB}/client/approval.py" "${HUB}/client/cleanup.py" "${HUB}/client/offices.py" "${HUB}/client/sessions.py" "${HUB}/client/resources.py" "${HUB}/client/settings_writer.py" log "skills: the PermissionRequest hook client is ${HUB}/client/permission_hook.py" log "skills: approvals need a pinned passkey: settings, 'Passkey registrieren', then 'make orchestrator-pin CREDENTIAL='; until then every executor refuses" printf 'KVB_ORCHESTRATOR_USER=%s:%s\nKVB_ORCHESTRATOR_STATE=%s\nKVB_ORCHESTRATOR_COUNCIL=%s\nKVB_ORCHESTRATOR_SKILLS=%s\nKVB_ORCHESTRATOR_PROJECTS=%s\nKVB_ORCHESTRATOR_TREE=%s\nKVB_ORCHESTRATOR_TIMERS=%s\nKVB_ORCHESTRATOR_APPROVERS=%s\nKVB_ORCHESTRATOR_KEPT=%s\n' \ @@ -257,6 +258,19 @@ resources_timer() { log "skills: ${RESOURCES_UNIT}.timer samples the host and counts the transcripts' tokens into ${HOME}/.local/state/agents-council/resources every minute" } +firewall_unit() { + if ! has_systemd; then + warn "skills: no systemd user session; the Firewall shows no settings and signed settings changes wait." + return 0 + fi + cp -f "${HUB}/systemd/${FIREWALL_UNIT}.service" "${HUB}/systemd/${FIREWALL_UNIT}.path" "${HUB}/systemd/${FIREWALL_UNIT}.timer" "${UNITS}/" + systemctl --user daemon-reload + systemctl --user enable --now "${FIREWALL_UNIT}.path" + systemctl --user enable --now "${FIREWALL_UNIT}.timer" + systemctl --user restart "${FIREWALL_UNIT}.timer" + log "skills: ${FIREWALL_UNIT}.path writes the settings changes the operator signs in the Firewall; ${FIREWALL_UNIT}.timer refreshes the settings view every minute" +} + skills_current() { local script for script in "${HOST_SCRIPTS[@]}"; do @@ -286,6 +300,7 @@ install_all() { tree_timer sessions_timer resources_timer + firewall_unit offices_unit if ((TOOK_RUNNER)) || runner_enabled; then runner || true; fi if ((TOOK_HOOK)); then @@ -374,6 +389,11 @@ check() { else todo "${RESOURCES_UNIT}.timer off: the Resources view waits for make orchestrator-install" fi + if [[ "$(systemctl --user is-active "${FIREWALL_UNIT}.path" 2>/dev/null || true)" == "active" ]]; then + ok "${FIREWALL_UNIT}.path active" + else + todo "${FIREWALL_UNIT}.path off: Firewall settings changes wait for make orchestrator-install" + fi if [[ "$(systemctl --user is-active "${OFFICES_UNIT}.path" 2>/dev/null || true)" == "active" ]]; then ok "${OFFICES_UNIT}.path active" else diff --git a/tests/e2e/test_firewall_e2e.py b/tests/e2e/test_firewall_e2e.py new file mode 100644 index 0000000..2eed4ff --- /dev/null +++ b/tests/e2e/test_firewall_e2e.py @@ -0,0 +1,275 @@ +from __future__ import annotations + +import json +import os +import re +import shutil +import sys +import time +import unittest +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path[:0] = [str(HERE.parent), str(HERE)] + +import test_orchestrator_e2e as base +from hub_case import HubCase +from hub_support import verifier +from test_settings_writer import ORIGINAL +from test_settings_writer import host as writer_host + +OUT = Path(os.environ.get("E2E_OUT", "/tmp/firewall-shots")) +SECRET = ORIGINAL["env"]["EXAMPLE_FLAG"] +SHOW = """(async () => {@HELPERS@ + await screenShown(8000); + const ready = await until(() => document.querySelector("#main [data-file] .fw-group"), 10000); + if (!ready) { + return {error: text(document.getElementById("main"))}; + } + const owned = (svg) => svg.closest("button, a, [title]"); + return { + title: text(document.querySelector("#main h2.title")), + subs: all("#tabs .subtabs button").map(name), + files: all("#main [data-file]").map((node) => node.dataset.file), + groups: all("#main [data-file] details.fw-group[data-tool]").map((node) => [node.dataset.tool, node.dataset.prefix, all("li", node).length]), + lists: all("#main [data-file] li[data-list]").map((node) => [node.dataset.list, node.dataset.entry]), + sandbox: all("#main [data-file] [data-key]").map((node) => node.dataset.key), + patterns: all('#main [aria-label="Never granted"] li').length, + targets: all('#main .rule-form select[aria-label="Applies to"] option').map(text), + unlabelled: all("#main svg.bi").map(owned).filter((owner) => !owner || (!text(owner) && !(owner.getAttribute("title") && owner.getAttribute("aria-label")))).length, + secret: document.documentElement.innerHTML.includes("@SECRET@"), + wide: document.documentElement.scrollWidth > innerWidth + 1, + }; +})()""" +SIGN = """(async () => {@HELPERS@ + const entryNode = (entry) => all("#main li[data-entry]").find((node) => node.dataset.entry === entry); + if (@REMOVE@) { + entryNode("Bash(git status:*)").querySelector('button[aria-label="Remove"]').click(); + await sleep(100); + } + const form = document.querySelector('#main form[aria-label="Stage a settings change"]'); + form.querySelector('select[aria-label="Settings file"]').value = "@FILE@"; + form.querySelector('select[aria-label="List"]').value = "permissions.allow"; + form.querySelector('input[aria-label="Entry"]').value = "@ENTRY@"; + form.requestSubmit(); + await sleep(100); + const staged = all('#main [aria-label="Staged changes"] li').map((node) => node.dataset.staged); + document.querySelector('#main button[aria-label="Review and sign"]').click(); + const diff = await until(() => document.querySelector('.sheet-box [aria-label="Diff"] pre.fw-diff'), 8000); + if (!diff) { + return {error: "no diff", toasts: all("#toasts .toast").map(text)}; + } + const lines = (cls) => all(`span.${cls}`, diff).map((node) => node.textContent.trim()); + const shown = {added: lines("add"), removed: lines("del"), staged, labelled: all(".sheet-box button").every((button) => text(button) || (button.title && button.getAttribute("aria-label")))}; + document.querySelector('.sheet-box button[aria-label="Sign and apply"]').click(); + const done = await until(() => document.querySelector(".sheet-box [data-result]"), 30000); + shown.result = done ? done.dataset.result : null; + shown.text = text(done); + shown.toasts = all("#toasts .toast").map(text); + shown.wide = document.documentElement.scrollWidth > innerWidth + 1; + return shown; +})()""" +GROUP_RULE = """(async () => {@HELPERS@ + const prompts = []; + new MutationObserver((records) => records.forEach((record) => record.addedNodes.forEach((node) => { + if (node.id === "passkey-prompt") { + prompts.push(text(node)); + } + }))).observe(document.body, {childList: true}); + const choose = (select, value) => { + select.value = value; + select.dispatchEvent(new Event("change")); + }; + const groupValue = (select) => [...select.options].map((option) => option.value).find((value) => value.startsWith("group:")); + const setRow = () => document.querySelector('#main [data-set="ci"]'); + const setTarget = () => setRow().querySelector("select"); + const setTake = () => setRow().querySelector('button[aria-label="Take over"]'); + const setState = () => ({label: text(setTarget().selectedOptions[0]), value: setTarget().value.split(":")[0], blocked: setTake().disabled, valid: setTarget().checkValidity()}); + const stage = async (entry) => { + const form = document.querySelector('#main form[aria-label="Stage a settings change"]'); + form.querySelector('input[aria-label="Entry"]').value = entry; + form.requestSubmit(); + return until(() => all('#main [aria-label="Staged changes"] li').find((node) => node.dataset.staged === `add ${entry}`), 8000); + }; + await screenShown(8000); + await until(() => document.querySelector('#main form[aria-label="New agent group"]'), 10000); + const group = document.querySelector('#main form[aria-label="New agent group"]'); + group.querySelector('input[aria-label="Group name"]').value = "builders"; + for (const option of group.querySelector('select[aria-label="Members"]').options) { + option.selected = true; + } + group.requestSubmit(); + const row = await until(() => document.querySelector('#main [data-group="builders"]'), 8000); + const rules = document.querySelector("#main .rule-form"); + const target = rules.querySelector('select[aria-label="Applies to"]'); + const choice = await until(() => [...target.options].find((option) => option.value.startsWith("group:")), 8000); + if (!row || !choice) { + return {error: "no group", toasts: all("#toasts .toast").map(text)}; + } + target.value = choice.value; + rules.querySelector('input[placeholder="make test*"]').value = "make e2e"; + rules.requestSubmit(); + const pill = await until(() => all("#main .rule-row [data-agents]").find((node) => text(node) === "group builders"), 15000); + const found = {group: text(row.querySelector("strong")), members: text(row.querySelector(".muted")), pills: all("#main .rule-row [data-agents]").map(text), found: Boolean(pill), toasts: all("#toasts .toast").map(text)}; + const sets = document.querySelector('#main form[aria-label="New rule set"]'); + sets.querySelector('input[aria-label="Set name"]').value = "ci"; + for (const option of sets.querySelector('select[aria-label="Rules"]').options) { + option.selected = option.textContent.includes("make e2e"); + } + sets.requestSubmit(); + if (!await until(setRow, 8000)) { + return {...found, error: "no rule set", toasts: all("#toasts .toast").map(text)}; + } + found.fresh = setState(); + choose(setTarget(), groupValue(setTarget())); + await stage("Bash(make e2e:*)"); + found.kept = setState(); + setTake().click(); + found.taken = await until(() => all("#toasts .toast").map(text).find((line) => line.startsWith("ci:")), 15000); + found.reloaded = await until(() => setRow() && setTarget().value.startsWith("group:") && document.querySelector("#main .rule-form") && setState(), 8000); + const form = document.querySelector("#main .rule-form"); + const picker = form.querySelector('select[aria-label="Applies to"]'); + const sign = form.querySelector("[data-sign]"); + choose(picker, groupValue(picker)); + found.picked = {value: picker.value.split(":")[0], blocked: sign.disabled}; + const sent = []; + const fetched = window.fetch; + window.fetch = (url, options) => { + sent.push(String(url)); + return fetched(url, options); + }; + document.querySelector('#main [data-group="builders"] button[aria-label="Delete group"]').click(); + await until(() => picker.value === "gone" || !picker.isConnected, 8000); + found.gone = {connected: picker.isConnected, value: picker.value, label: text(picker.selectedOptions[0]), valid: picker.checkValidity(), blocked: sign.disabled, groups: [...picker.options].filter((option) => option.value.startsWith("group:")).length}; + await stage("Bash(make e2e-again:*)"); + found.setGone = setState(); + setTake().click(); + await sleep(500); + found.sent = sent.filter((url) => url.startsWith("/api/rules")); + choose(picker, "all"); + found.again = {value: picker.value, valid: picker.checkValidity(), blocked: sign.disabled, options: [...picker.options].map((option) => option.value).includes("gone")}; + const ruleRow = () => all("#main .rule-row").find((node) => node.querySelector("[data-agents]") && all("code", node).some((code) => text(code) === "make e2e")); + const toggle = (label) => ruleRow() && [...ruleRow().querySelectorAll("button")].find((button) => text(button) === label); + toggle("Turn off").click(); + found.off = Boolean(await until(() => toggle("Turn on"), 8000)); + toggle("Turn on").click(); + found.on = Boolean(await until(() => toggle("Turn off"), 15000)); + found.prompts = prompts; + return found; +})()""" + + +def signed_lines(rule: dict) -> list[str]: + """Returns: + The lines the passkey prompt shows for a stored make e2e rule before + its target, its lifetime in days as N. + """ + return ["ask Bash: make e2e", "in *", f"until {time.strftime('%Y-%m-%d %H:%M', time.gmtime(rule['expires']))} UTC, in N days"] + + +def probe(template: str, values: dict[str, str]) -> str: + text = template.replace("@HELPERS@", base.HELPERS) + for name, value in values.items(): + text = text.replace(f"@{name}@", value) + return text + + +@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium") +class TestFirewallE2E(HubCase): + drive = base.TestOrchestratorE2E.drive + beside = base.TestOrchestratorE2E.beside + authenticator = base.TestOrchestratorE2E.authenticator + + def setUp(self): + super().setUp() + OUT.mkdir(parents=True, exist_ok=True) + self.claim("s1", "Skills") + self.claim("s2", "Stage") + self.snapshot() + home = self.outside / "home" + self.settings = home / ".claude" / "settings.json" + self.settings.parent.mkdir(parents=True) + self.settings.write_text(json.dumps(ORIGINAL, indent=2) + "\n", encoding="utf-8") + self.settings.chmod(0o640) + tree = self.outside / "writer-tree.json" + tree.write_text(json.dumps({"repos": [{"path": str(self.outside / "demo"), "known": True, "default": "main"}]}), encoding="utf-8") + trust = verifier.Trust(approvers=self.approvers, replay=self.replay / "firewall.json", origin=self.origin) + self.writer = writer_host.Writer(trust, home, tree) + self.spool = self.state / "firewall" + verifier.write_private(self.spool / "view.json", self.writer.view()) + self.local = f"http://localhost:{self.port}/" + + def host_unit(self) -> None: + if any((self.spool / "jobs").iterdir()): + writer_host.cycle(self.spool / "jobs", self.spool / "results", self.writer) + verifier.write_private(self.spool / "view.json", self.writer.view()) + + def test_chromium_shows_the_settings_and_signs_a_settings_diff_at_three_widths(self): + authenticator = self.authenticator() + self.beside(self.host_unit) + steps = [] + for index, (width, height) in enumerate(base.WIDTHS): + values = {"SECRET": SECRET, "FILE": str(self.settings), "ENTRY": f"Bash(make lint-{width}:*)", "REMOVE": "true" if index == 0 else "false"} + steps.append({"width": width, "height": height, "url": f"{self.local}?{width}#stream/rules", "wait": 1500, "probe": probe(SHOW, values), "shot": str(OUT / f"chromium-{width}-firewall.png")}) + steps.append({"width": width, "height": height, "url": None, "wait": 0, "probe": probe(SIGN, values), "shot": str(OUT / f"chromium-{width}-firewall-signed.png")}) + data = self.drive(steps, authenticator) + (OUT / "chromium-firewall.json").write_text(json.dumps(data, indent=1), encoding="utf-8") + self.assertEqual(data["errors"], []) + for index, (width, _) in enumerate(base.WIDTHS): + shown, signed = data["results"][2 * index : 2 * index + 2] + with self.subTest(width=width): + self.assertNotIn("error", shown, shown) + self.assertEqual((shown["title"], shown["subs"]), ("Firewall", base.SUBS)) + self.assertEqual(shown["files"], [str(self.settings), str(self.outside / "demo" / ".claude" / "settings.json")]) + self.assertIn(["Bash", "make", 1 + index], shown["groups"]) + self.assertIn(["Read", "~/.ssh/**", 1], shown["groups"]) + self.assertIn(["sandbox.filesystem.allowWrite", "/tmp"], shown["lists"]) + self.assertIn("sandbox.enabled", shown["sandbox"]) + self.assertGreater(shown["patterns"], 5) + self.assertEqual(shown["targets"], ["All agents", "Agent Skills", "Agent Stage"]) + self.assertEqual((shown["unlabelled"], shown["secret"], shown["wide"]), (0, False, False)) + self.assertNotIn("error", signed, signed) + self.assertTrue(any(line.startswith("+") and f'"Bash(make lint-{width}:*)"' in line for line in signed["added"]), signed["added"]) + self.assertEqual(any("git status" in line for line in signed["removed"]), index == 0) + self.assertEqual((signed["result"], signed["labelled"], signed["wide"]), ("done", True, False), signed) + self.assertIn("Settings: written", " ".join(signed["toasts"])) + written = json.loads(self.settings.read_text(encoding="utf-8")) + self.assertEqual(written["permissions"]["allow"], ["Bash(make test:*)", *(f"Bash(make lint-{width}:*)" for width, _ in base.WIDTHS)]) + self.assertEqual({key: written[key] for key in ("model", "env", "hooks", "sandbox")}, {key: ORIGINAL[key] for key in ("model", "env", "hooks", "sandbox")}) + backups = sorted(self.settings.parent.glob("settings.json.before-firewall-*")) + self.assertEqual((len(backups), self.settings.stat().st_mode & 0o777), (3, 0o640)) + + def test_chromium_names_a_group_and_signs_a_rule_for_it(self): + authenticator = self.authenticator() + steps = [{"width": 1366, "height": 768, "url": f"{self.local}#stream/rules", "wait": 1500, "probe": probe(GROUP_RULE, {}), "shot": str(OUT / "chromium-1366-firewall-group-rule.png")}] + data = self.drive(steps, authenticator) + (OUT / "chromium-firewall-group.json").write_text(json.dumps(data, indent=1), encoding="utf-8") + self.assertEqual(data["errors"], []) + found = data["results"][0] + self.assertNotIn("error", found, found) + self.assertEqual((found["group"], found["members"], found["found"]), ("builders", "Skills, Stage", True), found) + stored = [rule for rule in self.httpd.hub.rules.listed() if rule["pattern"] == "make e2e"] + self.assertEqual([(rule["agents"], rule["proof"]["payload"]["agents"]) for rule in stored], [(["s1", "s2"], ["s1", "s2"])] * 2) + for rule in stored: + self.assertLess(abs(rule["expires"] - (time.time() + 7 * 86400)), 2 * 86400) + self.assertEqual(found["fresh"], {"label": "Pick a target", "value": "", "blocked": True, "valid": False}, found) + self.assertEqual(found["kept"], {"label": "Group builders", "value": "group", "blocked": False, "valid": True}, found) + self.assertEqual(found["taken"], "ci: 1 of 1 rules signed for group builders") + self.assertEqual(found["reloaded"], found["kept"]) + self.assertEqual(found["setGone"], {"label": "Target gone: pick again", "value": "gone", "blocked": True, "valid": False}, found) + self.assertEqual(found["sent"], []) + members = "Skills (s1), Stage (s2)" + shown = [re.sub(r"in \d+\.\d days$", "in N days", prompt.removeprefix("Passkey signs"), flags=re.MULTILINE).split("\n") for prompt in found["prompts"]] + made = sorted(stored, key=lambda rule: rule["created_ts"]) + self.assertEqual(shown[:2], [["Rule", *signed_lines(made[0]), f"for group builders: {members}"], ["Rule set ci", *signed_lines(made[1]), f"for group builders: {members}"]], found) + self.assertIn(shown[2:], [[["Turn on rule", *signed_lines(rule), f"for agents {members}"]] for rule in made], found) + self.assertTrue(found["prompts"][1].split("\n")[3].endswith("in 7.0 days"), found["prompts"]) + self.assertEqual((found["off"], found["on"], [rule["enabled"] for rule in self.httpd.hub.rules.listed() if rule["pattern"] == "make e2e"]), (True, True, [True, True])) + self.assertEqual(found["picked"], {"value": "group", "blocked": False}) + self.assertEqual(found["gone"], {"connected": True, "value": "gone", "label": "Target gone: pick again", "valid": False, "blocked": True, "groups": 0}, found) + self.assertEqual(found["again"], {"value": "all", "valid": True, "blocked": False, "options": False}, found) + self.assertEqual(self.httpd.hub.groups.listed(), []) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_hub_asks.py b/tests/test_hub_asks.py index 57d2afe..3078d62 100644 --- a/tests/test_hub_asks.py +++ b/tests/test_hub_asks.py @@ -1,5 +1,6 @@ from __future__ import annotations +import importlib import json import socket import subprocess @@ -10,7 +11,7 @@ import unittest from collections.abc import Callable from hub_case import HubCase -from hub_support import ASK, CLIENT, HOOK, OPTIONS, Key, key +from hub_support import ASK, CLIENT, HOOK, OPTIONS, Key, key, signing class TestHubAsks(HubCase): @@ -155,6 +156,64 @@ class TestHubAsks(HubCase): result = self.tool(tool, {"id": "0123456789abcdef", "timeout_seconds": -1}) self.assertEqual(result["content"][0]["text"], "timeout_seconds must be a finite number of seconds, 0 or more") + def test_an_ask_with_an_invisible_format_character_is_refused_before_it_reaches_a_prompt(self): + for name, value, code in ( + ("command", f"git status{chr(0x202E)} && rm -rf ~", "U+202E"), + ("command", f"make{chr(0x200B)} test", "U+200B"), + ("command", f"rm{chr(0xA0)}-rf ~", "U+00A0"), + ("command", f"make{chr(0x3164)} test", "U+3164"), + ("command", f"make test{chr(0xFE0F)}", "U+FE0F"), + ("cwd", f"/repo{chr(0x2066)}", "U+2066"), + ("session", f"s1{chr(0x200D)}", "U+200D"), + ("tool", f"{chr(0xFEFF)}Bash", "U+FEFF"), + ): + with self.subTest(code=code): + status, _, body = self.post_json("/api/asks", {**ASK, name: value}, self.local()) + self.assertEqual(status, 400) + self.assertIn(code, body.decode("utf-8")) + self.assertEqual(self.httpd.hub.asks.listed(), []) + self.assertEqual([item for item in self.snapshot()["inbox"] if item["kind"] == "ask"], []) + self.create_ask(ASK) + + def test_a_score_block_with_emoji_presentation_reaches_the_prompt_and_a_stray_selector_does_not(self): + vs16 = chr(0xFE0F) + score = f"🎯 Fix: 93\n💥 Break: 4\n{chr(0x23F1)}{vs16} Time: 20\n{chr(0x1F6E1)}{vs16} Security: 100\n🏆 Score: 90" + ask_id = self.create_ask({**ASK, "command": f'git commit -m "fix: x" -m "{score}"'}) + self.assertEqual(self.post_json("/api/sign", {"items": [self.entry("ask", ask_id, "", "")]}, self.guard(self.host))[0], 200) + result = self.tool("decision_request", {"question": f"Commit?\n{score}", "options": [{**OPTIONS[0], "description": score}, OPTIONS[1]], "agent": "Skills", "recommended": ""}) + self.assertFalse(result["isError"], result) + for command in (f"make {chr(0x23F1)}{vs16}{vs16}", f"make{vs16}", f"{vs16}{chr(0x23F1)}"): + with self.subTest(command=command): + status, _, body = self.post_json("/api/asks", {**ASK, "command": command}, self.local()) + self.assertEqual(status, 400) + self.assertIn("U+FE0F", body.decode("utf-8")) + self.assertIn(f'id="emoji-bases" type="application/json">{json.dumps(sorted(signing.EMOJI_BASES))}', self.index()) + + def test_the_hook_names_the_reason_the_hub_refused_an_ask_on_one_line(self): + process = self.hook(self.port, "3", {**HOOK, "tool_input": {"command": f"rm{chr(0xA0)}-rf node_modules"}}) + self.assertEqual(self.finish(process), (0, "")) + self.assertRegex(process.stderr.read(), r"\Akvb-orchestrator: the hub answered 400, Claude Code asks instead: command must hold no [^\n]*U\+00A0[^\n]*\n\Z") + self.assertEqual(self.httpd.hub.asks.listed(), []) + + def test_the_hook_prints_no_control_or_bidi_character_of_a_reason(self): + hook = importlib.import_module("permission_hook") + reason = f" stop{chr(0x9B)}31m{chr(0x202E)}txt\nnext{chr(0x7F)}{chr(0x2066)} {chr(0x23F1)}{chr(0xFE0F)} 20 " + self.assertEqual(hook.clean(reason), f"stop?31m?txt?next?? {chr(0x23F1)}{chr(0xFE0F)} 20") + + def test_a_decision_with_an_invisible_format_character_is_refused_before_it_reaches_a_prompt(self): + plain = {"question": "Merge the hub?", "options": OPTIONS, "agent": "Skills", "recommended": ""} + for change, code in ( + ({"question": f"Merge{chr(0x202E)} the hub?"}, "U+202E"), + ({"options": [{**OPTIONS[0], "description": f"Gate{chr(0x200B)} is green"}, OPTIONS[1]]}, "U+200B"), + ({"options": [{**OPTIONS[0], "label": f"Merge now{chr(0x2067)}"}, OPTIONS[1]]}, "U+2067"), + ): + with self.subTest(code=code): + result = self.tool("decision_request", {**plain, **change}) + self.assertTrue(result["isError"]) + self.assertIn(code, json.dumps(result, ensure_ascii=True)) + self.assertEqual(self.httpd.hub.decisions.listed(), []) + self.assertFalse(self.tool("decision_request", plain)["isError"]) + def test_decisions_are_requested_chosen_and_waited_for(self): for arguments in ( {"question": "Merge?", "options": [], "agent": "Skills"}, diff --git a/tests/test_hub_executors.py b/tests/test_hub_executors.py index 64564ee..979f6d6 100644 --- a/tests/test_hub_executors.py +++ b/tests/test_hub_executors.py @@ -32,6 +32,24 @@ class TestHubExecutors(HubCase): def trust(self, name: str) -> verifier.Trust: return verifier.Trust(approvers=self.approvers, replay=self.replay / f"{name}.json", origin=self.origin) + def test_an_operator_row_with_an_invisible_format_character_is_never_offered_for_a_signature(self): + rows = { + "operator/bidi": (f"echo safe{chr(0x202E)} && rm -rf ~", str(self.outside)), + "operator/zero": (f"make{chr(0x200B)} test", str(self.outside)), + "operator/dir": ("make test", f"{self.outside}{chr(0x2066)}"), + "operator/plain": ("make test", str(self.outside)), + } + self.write_council({"rasci.json": {task: {"title": command, "workdir": workdir, "description": "run it", "check": "manual", "status": "queued", "roles": {}} for task, (command, workdir) in rows.items()}}) + self.pin() + for task, code in (("operator/bidi", "U+202E"), ("operator/zero", "U+200B"), ("operator/dir", "U+2066")): + with self.subTest(task=task): + entry = self.entry("operator", task, "", hashlib.sha256(rows[task][0].encode()).hexdigest()) + status, _, body = self.post_json("/api/sign", {"items": [entry]}, self.guard(self.host)) + self.assertEqual(status, 400) + self.assertIn(code, body.decode("utf-8")) + plain = self.entry("operator", "operator/plain", "", hashlib.sha256(b"make test").hexdigest()) + self.assertEqual(len(self.prepare([plain])["payload"]["items"]), 1) + def test_the_runner_runs_only_signed_commands_once(self): command = "echo hello && echo world >&2" register = { diff --git a/tests/test_hub_firewall.py b/tests/test_hub_firewall.py new file mode 100644 index 0000000..8c4fcb9 --- /dev/null +++ b/tests/test_hub_firewall.py @@ -0,0 +1,366 @@ +from __future__ import annotations + +import functools +import json +import time +from unittest import mock + +from hub_case import HubCase +from hub_support import ASK, PRESENT, key, signing, verifier +from kvb_orchestrator import batches, rules + +HOME_FILE = "/home/op/.claude/settings.json" +PROJECT_FILE = "/home/op/Repositories/demo/.claude/settings.json" +SHA = "ab" * 32 +PERMISSIONS = { + "allow": ["Bash(make test:*)", "Bash(make lint:*)", "Bash(git status:*)", "WebFetch(domain:example.org)", "mcp__memory__search"], + "ask": ["Bash(git push:*)"], + "deny": ["Read(~/.ssh/**)", 7], +} +SANDBOX = {"enabled": True, "filesystem": {"allowWrite": ["/tmp", "~/Repositories"]}, "network": {"allowLocalBinding": False}} +ADD = {"list": "permissions.allow", "op": "add", "entry": "Bash(make build:*)"} + + +class TestHubFirewall(HubCase): + def setUp(self): + super().setUp() + self.claim("s1", "Skills") + self.claim("s2", "Stage") + self.snapshot() + + def view(self, files: list[dict]) -> None: + (self.state / "firewall" / "view.json").write_text(json.dumps({"written": time.time(), "files": files}), encoding="utf-8") + + def home_view(self) -> dict: + return {"path": HOME_FILE, "exists": True, "sha256": SHA, "error": "", "settings": {"permissions": PERMISSIONS, "sandbox": SANDBOX, "env": {"EXAMPLE_FLAG": "fixture-value-not-a-secret"}}} + + def firewall(self) -> dict: + status, _, body = self.get("/api/firewall") + self.assertEqual(status, 200, body) + return json.loads(body) + + def post(self, path: str, payload: object) -> tuple[int, bytes]: + status, _, body = self.post_json(path, payload, self.guard(self.host)) + return status, body + + def test_the_firewall_groups_the_settings_by_tool_and_prefix_and_lists_the_deny_patterns(self): + self.assertEqual(self.firewall()["files"], []) + self.assertIn("kvb-orchestrator-firewall", self.firewall()["error"]) + self.view([self.home_view(), {"path": PROJECT_FILE, "exists": False, "sha256": "", "error": "", "settings": {}}, {"path": "/x", "exists": "yes", "sha256": "", "settings": {}}]) + found = self.firewall() + self.assertEqual([entry["path"] for entry in found["files"]], [HOME_FILE, PROJECT_FILE]) + groups = [(group["tool"], group["prefix"], [(entry["list"], entry["entry"]) for entry in group["entries"]]) for group in found["files"][0]["groups"]] + self.assertEqual(groups, [ + ("Bash", "git", [("allow", "Bash(git status:*)"), ("ask", "Bash(git push:*)")]), + ("Bash", "make", [("allow", "Bash(make test:*)"), ("allow", "Bash(make lint:*)")]), + ("Read", "~/.ssh/**", [("deny", "Read(~/.ssh/**)")]), + ("WebFetch", "domain", [("allow", "WebFetch(domain:example.org)")]), + ("mcp__memory__search", "", [("allow", "mcp__memory__search")]), + ]) + self.assertEqual(found["files"][0]["sandbox"], [{"key": "sandbox.enabled", "value": True}, {"key": "sandbox.filesystem.allowWrite", "values": ["/tmp", "~/Repositories"]}, {"key": "sandbox.network.allowLocalBinding", "value": False}]) + self.assertEqual(found["lists"], list(signing.SETTINGS_LISTS)) + self.assertEqual([pattern["label"] for pattern in found["patterns"]][:len(signing.WARNINGS)], [label for label, _ in signing.WARNINGS]) + self.assertNotIn("fixture-value-not-a-secret", json.dumps(found)) + for path in ("/api/firewall/preview", "/api/firewall/settings", "/api/firewall/groups", "/api/firewall/sets", "/api/firewall/groups/0123456789abcdef/delete"): + with self.subTest(path=path): + self.assertEqual(self.get(path)[0], 405) + self.assertEqual(self.get("/api/firewall/settings/0123456789abcdef")[0], 404) + + def test_a_settings_change_is_previewed_signed_and_queued_for_the_host(self): + self.view([self.home_view()]) + item = {"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [ADD, {"list": "permissions.ask", "op": "remove", "entry": "Bash(git push:*)"}]} + status, body = self.post("/api/firewall/preview", {"items": [item]}) + self.assertEqual(status, 200, body) + diff = json.loads(body)["diffs"][0]["diff"] + changed = [line for line in diff.splitlines() if line[:1] in "+-" and line[:3] not in ("+++", "---")] + self.assertEqual([line[0] + line[1:].strip() for line in changed], ['-"mcp__memory__search"', '+"mcp__memory__search",', '+"Bash(make build:*)"', '-"ask": [', '-"Bash(git push:*)"', '-],', '+"ask": [],']) + self.assertNotIn("EXAMPLE_FLAG", diff) + signed = signing.settings_item(HOME_FILE, SHA, item["changes"]) + self.assertEqual(self.prepare([item])["payload"]["items"], [signing.item_hash(signed)]) + signer = self.pin() + proof = signer.proof(self.prepare([item])["payload"], self.origin) + for name, items, proven, expected in ( + ("no proof", [item], None, 400), + ("forged", [item], key(-7, "intruder").proof(self.prepare([item])["payload"], self.origin), 400), + ("stale base", [{**item, "base": "cd" * 32}], proof, 409), + ("unlisted file", [{**item, "file": PROJECT_FILE}], proof, 400), + ("hooks list", [{**item, "changes": [{**ADD, "list": "hooks.Stop"}]}], proof, 400), + ("added twice", [{**item, "changes": [{**ADD, "entry": "Bash(make test:*)"}]}], proof, 400), + ("two items for one file", [item, item], proof, 400), + ("other item signed", [{**item, "changes": [ADD]}], proof, 400), + ): + with self.subTest(name=name): + self.assertEqual(self.post("/api/firewall/settings", {"items": items, "proof": proven})[0], expected) + self.assertEqual(self.post_json("/api/firewall/settings", {"items": [item], "proof": proof}, {"Host": self.host})[0], 403) + self.assertEqual(list((self.state / "firewall" / "jobs").iterdir()), []) + status, body = self.post("/api/firewall/settings", {"items": [item], "proof": proof}) + self.assertEqual(status, 202, body) + job_id = json.loads(body)["id"] + written = self.state / "firewall" / "jobs" / f"{job_id}.json" + job = json.loads(written.read_text(encoding="utf-8")) + self.assertEqual((sorted(job), job["items"], job["proof"]), (["created", "id", "items", "proof", "v"], [{name: signed[name] for name in signing.SETTINGS_KEYS}], proof)) + self.assertEqual(written.stat().st_mode & 0o777, 0o600) + self.assertEqual(self.post("/api/firewall/settings", {"items": [item], "proof": proof})[0], 400) + self.assertEqual(json.loads(self.get(f"/api/firewall/settings/{job_id}")[2])["status"], "queued") + result = {"id": job_id, "finished": time.time(), "items": [{**job["items"][0], "status": "done", "reason": ""}]} + (self.state / "firewall" / "results" / f"{job_id}.json").write_text(json.dumps(result), encoding="utf-8") + self.assertEqual(json.loads(self.get(f"/api/firewall/settings/{job_id}")[2])["status"], "done") + + def test_a_settings_entry_or_path_with_an_invisible_format_character_is_refused_before_it_reaches_a_prompt(self): + self.view([self.home_view()]) + for name, item, code in ( + ("bidi override in an entry", {"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [{**ADD, "entry": f"Bash(make build{chr(0x202E)}:*)"}]}, "U+202E"), + ("zero-width space in an entry", {"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [{**ADD, "entry": f"Bash(make{chr(0x200B)} build:*)"}]}, "U+200B"), + ("no-break space in a deny entry", {"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [{"list": "permissions.deny", "op": "add", "entry": f"Bash(rm{chr(0xA0)}-r:*)"}]}, "U+00A0"), + ("ideographic space in a deny entry", {"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [{"list": "permissions.deny", "op": "add", "entry": f"Bash(rm{chr(0x3000)}-r:*)"}]}, "U+3000"), + ("Hangul filler in an entry", {"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [{**ADD, "entry": f"Bash(make{chr(0x3164)} build:*)"}]}, "U+3164"), + ("grapheme joiner in an entry", {"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [{**ADD, "entry": f"Bash(make{chr(0x34F)} build:*)"}]}, "U+034F"), + ("variation selector in an entry", {"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [{**ADD, "entry": f"Bash(make build{chr(0xE0100)}:*)"}]}, "U+E0100"), + ("Hangul filler in a path", {"kind": "settings", "file": f"/home/op{chr(0xFFA0)}/.claude/settings.json", "base": SHA, "changes": [ADD]}, "U+FFA0"), + ): + with self.subTest(name=name): + for path in ("/api/firewall/preview", "/api/sign"): + status, body = self.post(path, {"items": [item]}) + self.assertEqual(status, 400) + self.assertIn(code, body.decode("utf-8")) + self.assertIn("U+2067", signing.settings_problem({"file": f"/home/op{chr(0x2067)}/.claude/settings.json", "base": SHA, "changes": [ADD]})) + self.assertIn("U+FEFF", signing.settings_problem({"file": HOME_FILE, "base": SHA, "changes": [{**ADD, "entry": f"{chr(0xFEFF)}Bash(make build:*)"}]})) + self.assertIsNone(signing.settings_problem({"file": HOME_FILE, "base": SHA, "changes": [ADD]})) + self.assertEqual(self.post("/api/firewall/preview", {"items": [{"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [ADD]}]})[0], 200) + + def test_removing_a_settings_entry_with_a_hidden_character_is_offered_and_adding_one_is_not(self): + hidden = f"Bash(make{chr(0x202E)} build:*)" + self.view([{**self.home_view(), "settings": {"permissions": {**PERMISSIONS, "allow": [*PERMISSIONS["allow"], hidden]}, "sandbox": SANDBOX}}]) + removal = {"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [{"list": "permissions.allow", "op": "remove", "entry": hidden}]} + addition = {**removal, "changes": [{"list": "permissions.ask", "op": "add", "entry": hidden}]} + for path in ("/api/firewall/preview", "/api/sign"): + with self.subTest(path=path): + self.assertEqual(self.post(path, {"items": [removal]})[0], 200) + status, body = self.post(path, {"items": [addition]}) + self.assertEqual(status, 400) + self.assertIn("U+202E", body.decode("utf-8")) + self.assertIsNone(signing.hidden_problem(removal["changes"][0], "change")) + for name, value in ( + ("no list", {"op": "remove", "entry": hidden}), + ("extra key", {**removal["changes"][0], "note": "x"}), + ("unknown list", {**removal["changes"][0], "list": "permissions.other"}), + ("an ask with op remove", {"kind": "ask", "op": "remove", "entry": hidden}), + ("nested in an item", {"kind": "review", "branch": {"op": "remove", "entry": hidden}}), + ): + with self.subTest(name=name): + self.assertIn("U+202E", signing.hidden_problem(value, "item")) + + def test_a_group_set_addressee_legacy_rule_or_pattern_with_a_hidden_character_is_never_offered_for_a_signature(self): + rlo = chr(0x202E) + signer = self.pin() + rule = self.signed_rule(signer, {"kind": "ask", "tool": "Bash", "pattern": "make *", "scope": "/repo*", "expires": time.time() + 3600, "agents": ["s1"]}) + for path, payload in (("/api/firewall/groups", {"name": f"ci{rlo}", "members": ["s1"]}), ("/api/firewall/sets", {"name": f"ci{rlo}", "days": 7, "rules": [rule["id"]]})): + with self.subTest(path=path): + status, body = self.post(path, payload) + self.assertEqual(status, 400) + self.assertIn("U+202E", body.decode("utf-8")) + for code in (0x2028, 0x2029, 0x85, 0x0B, 0x0D, 0x34F, 0xFE0F, 0xE0100, 0x3164, 0x1160, 0xA0, 0x3000): + with self.subTest(code=code): + status, body = self.post("/api/rules/sign", {"kind": "ask", "tool": "Bash", "pattern": f"make test{chr(code)}in /safe", "scope": "/repo*", "expires": time.time() + 3600}) + self.assertEqual(status, 400) + self.assertIn(f"U+{code:04X}", body.decode("utf-8")) + stored = self.httpd.hub.rules.get(rule["id"]) + legacy = {key: value for key, value in stored["proof"]["payload"].items() if key != signing.TARGET} + self.httpd.hub.rules.change(rule["id"], {"enabled": False, "proof": {**stored["proof"], "payload": {**legacy, "pattern": f"make test{rlo}*"}}}) + status, body = self.post("/api/sign", {"items": [{"kind": "enable", "rule": rule["id"]}]}) + self.assertEqual(status, 400) + self.assertIn("U+202E", body.decode("utf-8")) + self.claim("s1", f"Skills{rlo}") + ask_id = self.create_ask(ASK) + shown = next(item for item in self.snapshot()["inbox"] if item["id"] == ask_id) + self.assertIn(rlo, shown["to"]) + status, body = self.post("/api/sign", {"items": [{**self.entry("ask", ask_id, "", ""), "verb": "reject", "channel": "post", "reason": "no", "to": shown["to"]}]}) + self.assertEqual(status, 400) + self.assertIn("U+202E", body.decode("utf-8")) + self.assertEqual(self.post("/api/sign", {"items": [self.entry("ask", ask_id, "", "")]})[0], 200) + + def test_the_hub_keeps_only_permissions_and_sandbox_and_refuses_a_view_unsafe_to_copy(self): + self.view([self.home_view()]) + files, _ = self.httpd.hub.settings_files() + self.assertEqual(sorted(files[0]["settings"]), ["permissions", "sandbox"]) + domain = {"kind": "settings", "file": HOME_FILE, "base": SHA, "changes": [{"list": "sandbox.network.allowedDomains", "op": "add", "entry": "example.org"}]} + status, body = self.post("/api/firewall/preview", {"items": [domain]}) + self.assertEqual(status, 200, body) + self.assertNotIn('"env"', json.loads(body)["diffs"][0]["diff"]) + deep = functools.reduce(lambda inner, _: {"x": inner}, range(600), {}) + for name, extra, reason in (("600 levels", deep, "nests deeper than 64"), ("lone surrogate", "\ud800", "lone surrogate")): + with self.subTest(name=name): + self.view([{**self.home_view(), "settings": {"permissions": {**PERMISSIONS, "extra": extra}, "sandbox": SANDBOX}}]) + status, body = self.post("/api/firewall/preview", {"items": [{**domain, "changes": [ADD]}]}) + self.assertEqual(status, 400, body) + self.assertIn(reason, body.decode("utf-8")) + + def test_groups_and_rule_sets_hold_known_agents_and_stored_rules(self): + for name, payload, expected in ( + ("unknown session", {"name": "builders", "members": ["s1", "s9"]}, 400), + ("every agent", {"name": "builders", "members": ["*"]}, 400), + ("unsorted", {"name": "builders", "members": ["s2", "s1"]}, 400), + ("empty name", {"name": " ", "members": ["s1"]}, 400), + ("extra key", {"name": "builders", "members": ["s1"], "rule": "x"}, 400), + ): + with self.subTest(name=name): + self.assertEqual(self.post("/api/firewall/groups", payload)[0], expected) + status, body = self.post("/api/firewall/groups", {"name": "builders", "members": ["s1", "s2"]}) + self.assertEqual(status, 201, body) + group = json.loads(body) + self.assertEqual(self.post("/api/firewall/groups", {"name": "builders", "members": ["s1"]})[0], 409) + self.assertEqual([(entry["name"], entry["members"]) for entry in self.firewall()["groups"]], [("builders", ["s1", "s2"])]) + signer = self.pin() + rule = self.signed_rule(signer, {"kind": "ask", "tool": "Bash", "pattern": "make *", "scope": "/repo*", "expires": time.time() + 3600, "agents": ["s1"]}) + for name, payload in (("no days", {"name": "ci", "days": 0, "rules": [rule["id"]]}), ("unknown rule", {"name": "ci", "days": 7, "rules": ["0123456789abcdef"]}), ("no rules", {"name": "ci", "days": 7, "rules": []})): + with self.subTest(name=name): + self.assertEqual(self.post("/api/firewall/sets", payload)[0], 400) + status, body = self.post("/api/firewall/sets", {"name": "ci", "days": 7, "rules": [rule["id"]]}) + self.assertEqual(status, 201, body) + stored = json.loads(body) + self.assertEqual((stored["days"], stored["rules"]), (7, [{"kind": "ask", "tool": "Bash", "pattern": "make *", "scope": "/repo*"}])) + for path, ident in (("groups", group["id"]), ("sets", stored["id"])): + with self.subTest(path=path): + self.assertEqual(self.call("POST", f"/api/firewall/{path}/{ident}/delete", b"", self.guard(self.host))[0], 200) + self.assertEqual(self.call("POST", f"/api/firewall/{path}/{ident}/delete", b"", self.guard(self.host))[0], 404) + + def signed_rule(self, signer, fields: dict) -> dict: + status, _, body = self.post_json("/api/rules/sign", fields, self.guard(self.host)) + self.assertEqual(status, 200, body) + payload = json.loads(body)["payload"] + status, _, body = self.post_json("/api/rules", {"rule": payload, "assertion": signer.assertion(payload, self.origin, "localhost", PRESENT, 0), "enabled": True}, self.guard(self.host)) + self.assertEqual(status, 201, body) + return json.loads(body) + + def test_a_rule_applies_to_one_agent_a_group_or_every_agent(self): + signer = self.pin() + now = time.time() + base = {"kind": "ask", "tool": "Bash", "scope": "/repo*", "expires": now + 3600} + for name, agents in (("unknown agent", ["s9"]), ("unsorted group", ["s2", "s1"]), ("pseudo agent", ["name:Docs"]), ("empty", [])): + with self.subTest(name=name): + self.assertEqual(self.post_json("/api/rules/sign", {**base, "pattern": "make *", "agents": agents}, self.guard(self.host))[0], 400) + self.assertEqual(self.post_json("/api/rules/sign", {"kind": "review", "tool": "", "pattern": "feature/*", "scope": "/repo/*", "expires": now + 3600, "agents": ["s1"]}, self.guard(self.host))[0], 400) + one = self.signed_rule(signer, {**base, "pattern": "make one", "agents": ["s1"]}) + group = self.signed_rule(signer, {**base, "pattern": "make group", "agents": ["s1", "s2"]}) + every = self.signed_rule(signer, {**base, "pattern": "make every"}) + self.assertEqual((one["agents"], group["agents"], every["agents"]), (["s1"], ["s1", "s2"], ["*"])) + self.assertEqual(one["proof"]["payload"]["agents"], ["s1"]) + with mock.patch.object(batches, "UNDO", 0.05): + asks = {(command, session): self.create_ask({**ASK, "command": command, "session": session}) for command in ("make one", "make group", "make every") for session in ("s1", "s2", "s3")} + time.sleep(0.6) + statuses = {pair: self.ask_status(ask_id, "0") for pair, ask_id in asks.items()} + self.assertEqual(statuses, { + ("make one", "s1"): "allowed", ("make one", "s2"): "pending", ("make one", "s3"): "pending", + ("make group", "s1"): "allowed", ("make group", "s2"): "allowed", ("make group", "s3"): "pending", + ("make every", "s1"): "allowed", ("make every", "s2"): "allowed", ("make every", "s3"): "allowed", + }) + hook = verifier.Trust(approvers=self.approvers, replay=self.replay / "hook.json", origin=self.origin) + relayed = json.loads(self.get(f"/api/asks/{asks[('make one', 's1')]}?wait=0")[2])["proof"] + verifier.check(relayed, signing.ask_item(asks[("make one", "s1")], "Bash", "make one", "/repo", "s1"), "", hook, time.time()) + with self.assertRaisesRegex(signing.Refused, "does not match"): + verifier.check(relayed, signing.ask_item(asks[("make one", "s2")], "Bash", "make one", "/repo", "s2"), "", hook, time.time()) + + def test_a_decision_rule_scoped_to_an_agent_decides_only_decisions_under_the_name_it_holds_now(self): + for session, name in (("s1", "Docs"), ("s1", "Main"), ("s1", "Skills"), ("s2", "Main")): + time.sleep(1.1) + self.claim(session, name) + self.snapshot() + held = {(entry["session"], entry["name"]) for entry in self.httpd.hub.names.listed()} + self.assertLessEqual({("s1", "Docs"), ("s1", "Main"), ("s1", "Skills"), ("s2", "Main")}, held) + signer = self.pin() + self.signed_rule(signer, {"kind": "decision", "tool": "", "pattern": "*", "scope": "*", "expires": time.time() + 3600, "agents": ["s1"]}) + options = [{"label": "Yes", "description": ""}, {"label": "No", "description": ""}] + with mock.patch.object(batches, "UNDO", 0.05): + asked = {name: self.tool("decision_request", {"question": "Merge?", "options": options, "agent": name, "recommended": "Yes"})["structuredContent"]["id"] for name in ("Skills", "Main", "Docs", "Stage")} + self.settle(lambda: self.choice(asked["Skills"]) == "Yes") + time.sleep(0.3) + self.assertEqual({name: self.choice(decision) for name, decision in asked.items()}, {"Skills": "Yes", "Main": None, "Docs": None, "Stage": None}) + self.claim("s3", "Skills") + self.snapshot() + with mock.patch.object(batches, "UNDO", 0.05): + shared = self.tool("decision_request", {"question": "Merge?", "options": options, "agent": "Skills", "recommended": "Yes"})["structuredContent"]["id"] + time.sleep(0.6) + self.assertIsNone(self.choice(shared)) + + def test_turning_a_rule_on_needs_a_fresh_passkey_signature_and_turning_it_off_none(self): + signer = self.pin() + rule = self.signed_rule(signer, {"kind": "ask", "tool": "Bash", "pattern": "make *", "scope": "/repo*", "expires": time.time() + 3600}) + other = self.signed_rule(signer, {"kind": "ask", "tool": "Bash", "pattern": "make other", "scope": "/repo*", "expires": time.time() + 3600}) + enable = f"/api/rules/{rule['id']}/enable" + self.assertEqual(self.call("POST", f"/api/rules/{rule['id']}/disable", b"", self.guard(self.host))[0], 200) + unsigned = (("no body", b""), ("no proof", b'{"proof": null}'), ("the rule's own signature", json.dumps({"proof": rule["proof"]}).encode())) + for name, body in unsigned: + with self.subTest(name=name): + self.assertEqual(self.call("POST", enable, body, self.guard(self.host))[0], 400) + self.assertFalse(self.httpd.hub.rules.get(rule["id"])["enabled"]) + offer = self.prepare([{"kind": "enable", "rule": rule["id"]}]) + self.assertEqual((offer["enables"], offer["payload"]["items"]), (rule["proof"]["payload"], [signing.item_hash(signing.enable_item(rule["proof"]["payload"]))])) + self.assertEqual(self.post("/api/sign", {"items": [{"kind": "enable", "rule": "0123456789abcdef"}]})[0], 404) + proof = signer.proof(offer["payload"], self.origin) + foreign = signer.proof(self.prepare([{"kind": "enable", "rule": other["id"]}])["payload"], self.origin) + for name, body in (("forged", {"proof": key(-7, "intruder").proof(offer["payload"], self.origin)}), ("another rule's approval", {"proof": foreign})): + with self.subTest(name=name): + self.assertEqual(self.post(enable, body)[0], 400) + self.assertFalse(self.httpd.hub.rules.get(rule["id"])["enabled"]) + with mock.patch.object(batches, "UNDO", 0.05): + waiting = self.create_ask({**ASK, "command": "make x", "session": "s1"}) + time.sleep(0.6) + self.assertEqual(self.ask_status(waiting, "0"), "pending") + status, body = self.post(enable, {"proof": proof}) + self.assertEqual((status, json.loads(body)["enabled"]), (200, True), body) + self.assertEqual(self.call("POST", f"/api/rules/{rule['id']}/disable", b"", self.guard(self.host))[0], 200) + self.assertEqual(self.post(enable, {"proof": proof})[0], 400) + self.assertFalse(self.httpd.hub.rules.get(rule["id"])["enabled"]) + + def test_a_deleted_rule_never_comes_back_with_its_old_proof_and_a_fresh_signature_does(self): + signer = self.pin() + fields = {"kind": "ask", "tool": "Bash", "pattern": "make *", "scope": "/repo*", "expires": time.time() + 3600} + rule = self.signed_rule(signer, fields) + old = {"rule": rule["proof"]["payload"], "assertion": rule["proof"]["assertion"], "enabled": True} + self.assertEqual(self.post("/api/rules", old)[0], 409) + self.assertEqual(self.call("POST", f"/api/rules/{rule['id']}/delete", b"", self.guard(self.host))[0], 200) + self.assertEqual(self.post("/api/rules", old)[0], 409) + self.assertIsNone(self.httpd.hub.rules.get(rule["id"])) + unsigned = json.loads(self.post("/api/rules/sign", fields)[1])["payload"] + object.__setattr__(self.httpd.hub, "offers", rules.Offers()) + self.assertEqual(self.post("/api/rules", {"rule": unsigned, "assertion": signer.assertion(unsigned, self.origin, "localhost", PRESENT, 0), "enabled": True})[0], 409) + fresh = self.signed_rule(signer, fields) + self.assertEqual([stored["id"] for stored in self.httpd.hub.rules.listed()], [fresh["id"]]) + with mock.patch.object(batches, "UNDO", 0.05): + asked = self.create_ask({**ASK, "command": "make x", "session": "s1"}) + time.sleep(0.6) + self.assertEqual(self.ask_status(asked, "0"), "allowed") + + def test_a_group_changed_after_signing_grants_nothing_beyond_the_signed_members(self): + self.claim("s3", "Evil") + self.snapshot() + signer = self.pin() + group = json.loads(self.post("/api/firewall/groups", {"name": "builders", "members": ["s1", "s2"]})[1]) + self.signed_rule(signer, {"kind": "ask", "tool": "Bash", "pattern": "make *", "scope": "/repo*", "expires": time.time() + 3600, "agents": group["members"]}) + self.assertEqual(self.call("POST", f"/api/firewall/groups/{group['id']}/delete", b"", self.guard(self.host))[0], 200) + self.assertEqual(self.post("/api/firewall/groups", {"name": "builders", "members": ["s1", "s2", "s3"]})[0], 201) + with mock.patch.object(batches, "UNDO", 0.05): + asks = {session: self.create_ask({**ASK, "command": "make x", "session": session}) for session in ("s1", "s3")} + time.sleep(0.6) + self.assertEqual({session: self.ask_status(ask_id, "0") for session, ask_id in asks.items()}, {"s1": "allowed", "s3": "pending"}) + + def test_the_signed_target_decides_and_an_untargeted_rule_reaches_every_agent(self): + now = time.time() + plain = signing.rule_payload("0123456789abcdef", ["ask"], "Bash", "make *", "*", int(now) + 600) + item = signing.ask_item("fedcba9876543210", "Bash", "make x", "/repo", "s2") + self.assertTrue(signing.rule_allows(plain, item, "", now)) + self.assertTrue(signing.rule_allows(signing.targeted(plain, ["*"]), item, "", now)) + self.assertTrue(signing.rule_allows(signing.targeted(plain, ["s1", "s2"]), item, "", now)) + self.assertFalse(signing.rule_allows(signing.targeted(plain, ["s1"]), item, "", now)) + review = signing.rule_payload("0123456789abcdef", ["review"], "", "*", "*", int(now) + 600) + commit = signing.review_item("fedcba9876543210", "/repo/.git", "/repo", "feature/x", "0" * 40) + self.assertTrue(signing.rule_allows(review, commit, "pass", now)) + self.assertFalse(signing.rule_allows(signing.targeted(review, ["s1"]), commit, "pass", now)) + for agents in ([], ["s2", "s1"], ["s1", "s1"], ["*", "s1"], "s1", [3]): + with self.subTest(agents=agents), self.assertRaises(signing.Refused): + signing.checked_rule(signing.targeted(plain, agents), now) + subject = {"recommended": "Yes", "agent": "Skills", "sessions": {"s1"}} + stored = {"kind": "decision", "pattern": "*", "enabled": True, "expires": now + 600} + targeted = {**stored, "proof": {"payload": signing.targeted(review, ["s2"])}} + self.assertFalse(rules.rule_matches(targeted, "decision", subject, now)) + self.assertTrue(rules.rule_matches({**stored, "proof": {"payload": signing.targeted(review, ["s1"])}}, "decision", subject, now)) diff --git a/tests/test_hub_gates.py b/tests/test_hub_gates.py index 01e0702..605727c 100644 --- a/tests/test_hub_gates.py +++ b/tests/test_hub_gates.py @@ -87,6 +87,14 @@ class TestHubGates(HubCase): self.assertEqual(sorted((job["kind"], job["argv"], job["command"].split()[0]) for job in jobs), sorted(("gate", item["argv"], "gates.py") for item in items)) self.assertEqual(self.get("/api/gates")[0], 405) + def test_a_branch_or_repo_with_a_hidden_character_is_never_offered_for_a_signature(self): + for name, argv, code in (("bidi branch", rule("commit", "test", branch=f"feature/x{chr(0x202E)}"), "U+202E"), ("zero-width repo", rule("commit", "test", repo=f"{REPO}{chr(0x200B)}"), "U+200B"), ("Hangul filler branch", rule("commit", "test", branch=f"main{chr(0x3164)}"), "U+3164"), ("no-break space repo", rule("commit", "test", repo=f"{REPO}{chr(0xA0)}"), "U+00A0")): + with self.subTest(name=name): + self.assertEqual(signing.gate_argv(argv), argv) + status, _, body = self.post_json("/api/sign", {"items": self.stage([argv])}, self.guard(self.host)) + self.assertEqual(status, 400) + self.assertIn(code, body.decode("utf-8")) + def test_the_hub_refuses_a_malformed_or_forged_gate_change_before_it_queues(self): for name, items in (("receipt", self.stage([["receipt", "add", "--repo", REPO, "--tree", "a" * 40, "--level", "e2e", "--log", "/x"]])), ("own parent", self.stage([parent("feature/x")])), ("twice", self.stage([parent("main"), parent("main")])), ("extra key", [{"kind": "gate", "argv": parent("main"), "id": "x"}])): with self.subTest(name=name): diff --git a/tests/test_hub_operator_tasks.py b/tests/test_hub_operator_tasks.py index 0ecfb78..97ac4f9 100644 --- a/tests/test_hub_operator_tasks.py +++ b/tests/test_hub_operator_tasks.py @@ -24,7 +24,7 @@ hub_limits = importlib.import_module("hub_limits") approval = importlib.import_module("approval") TEMPLATES = HUB / "templates" DIFFS = TEMPLATES / "diffs.js" -PAGE_MODULES = ["page-constants.js", "page-dom.js", "page-diff.js", "page-inbox.js", "page-stream.js", "page-screens.js", "page-keyboard.js"] +PAGE_MODULES = ["page-constants.js", "page-reveal.js", "page-dom.js", "page-diff.js", "page-inbox.js", "page-stream.js", "page-screens.js", "page-keyboard.js"] SECRET = 'api_key = "plain-example-words"' ARMOURED = "before\n-----BEGIN EXAMPLE-----\nplain words only\n-----END EXAMPLE-----\nafter\n" WITHHELD = "content withheld" @@ -96,7 +96,8 @@ const document = { querySelectorAll: () => [], }; document.activeElement = document.body; -const all = (node) => (typeof node === "string" ? [] : [node, ...node.children.flatMap(all)]); +document.getElementById("emoji-bases").textContent = "[]"; +const all =(node) => (typeof node === "string" ? [] : [node, ...node.children.flatMap(all)]); const classes = (node) => String(node.className || node.attrs.class || "").split(" ").filter(Boolean); const labels = (node) => all(node).filter((el) => el.tag === "button").map((el) => el.getAttribute("aria-label") || el.textContent); const calls = {proofs: [], posts: [], sheets: [], radio: []}; diff --git a/tests/test_hub_passkey_page.py b/tests/test_hub_passkey_page.py index 6e1f8b1..023c99b 100644 --- a/tests/test_hub_passkey_page.py +++ b/tests/test_hub_passkey_page.py @@ -25,7 +25,7 @@ const sendJson = async () => { const toast = (title) => toasts.push(title); const bytes = new Uint8Array([1]).buffer; const credential = {rawId: bytes, response: {clientDataJSON: bytes, authenticatorData: bytes, signature: bytes}}; -const P = new Function("window", "navigator", "sendJson", "toast", "fail", "h", `${source}\\nreturn {signedProof};`)(window, navigator, sendJson, toast, () => [], () => null); +const P = new Function("window", "navigator", "sendJson", "toast", "fail", "h", "promptText", "document", `${source}\\nreturn {signedProof};`)(window, navigator, sendJson, toast, () => [], () => ({remove: () => {}}), (label) => label, {body: {append: () => {}}}); const tick = () => new Promise((resolve) => setImmediate(resolve)); const nonce = (signed) => (signed ? signed.proof.payload.nonce : null); (async () => { diff --git a/tests/test_hub_reviews.py b/tests/test_hub_reviews.py index d7f3a8a..5f0206b 100644 --- a/tests/test_hub_reviews.py +++ b/tests/test_hub_reviews.py @@ -93,6 +93,24 @@ class TestHubReviews(HubCase): self.assertEqual(self.tool("review_list", {})["structuredContent"]["reviews"], []) self.assertEqual(list((self.state / "pages").iterdir()), []) + def test_a_review_with_a_look_alike_branch_title_or_path_is_refused_before_it_reaches_a_prompt(self): + source = self.page("bundle.html", PAGE) + plain = self.arguments("feat: x", "", source) + for name, value, code in ( + ("branch", f"main{chr(0x3164)}", "U+3164"), + ("branch", f"main{chr(0x115F)}", "U+115F"), + ("title", f"feat:{chr(0xA0)}x", "U+00A0"), + ("title", f"feat: x{chr(0x34F)}", "U+034F"), + ("worktree", f"/repo/.worktrees/x{chr(0xFE0F)}", "U+FE0F"), + ("repo", f"/repo{chr(0xE0100)}/.git", "U+E0100"), + ): + with self.subTest(name=name, code=code): + result = self.tool("review_request", {**plain, name: value}) + self.assertTrue(result["isError"], result) + self.assertIn(code, json.dumps(result, ensure_ascii=True)) + self.assertEqual(self.tool("review_list", {})["structuredContent"]["reviews"], []) + self.assertFalse(self.tool("review_request", plain).get("isError")) + def test_approve_flips_the_status_and_review_wait_returns_it(self): review_id = self.request("feat: approve me", "") results = [] diff --git a/tests/test_hub_rule_requests.py b/tests/test_hub_rule_requests.py index 203b865..0285861 100644 --- a/tests/test_hub_rule_requests.py +++ b/tests/test_hub_rule_requests.py @@ -48,9 +48,9 @@ class TestHubRuleRequests(HubCase): self.assertTrue(self.tool("rule_request", self.ask_rule(**change))["isError"]) for command, cwd in (("make test", "/repo"), ("make test-unit", "/repo/sub"), ("make deploy", "/repo"), ("make test && rm -rf /", "/repo"), ("make test", "/elsewhere")): self.create_ask({**ASK, "command": command, "cwd": cwd}) - request_id = self.requested(self.ask_rule()) - item = self.item(request_id) arguments = self.ask_rule() + request_id = self.requested(arguments) + item = self.item(request_id) self.assertEqual({name: item["rule"][name] for name in ("kind", "tool", "pattern", "scope")}, {name: arguments[name] for name in ("kind", "tool", "pattern", "scope")}) self.assertEqual((item["agent"], item["detail"], item["green"], item["deniable"], item["dry_run"]), ("Skills", "the gate runs make test all day", False, True, {"matched": 2, "total": 5, "days": 7})) self.assertEqual(self.waited(request_id), {"id": request_id, "status": "pending", "rule": None, "reason": "", "decided": None}) @@ -87,6 +87,28 @@ class TestHubRuleRequests(HubCase): matched = self.create_ask({**ASK, "command": "make test"}) self.settle(lambda: self.ask_status(matched, "0") == "allowed") + def test_a_text_with_an_invisible_format_character_is_refused_before_it_reaches_a_prompt(self): + for change, code in ( + ({"pattern": f"make test{chr(0x202E)}*"}, "U+202E"), + ({"pattern": f"make{chr(0x200B)} test*"}, "U+200B"), + ({"scope": f"/repo{chr(0x2066)}*"}, "U+2066"), + ({"tool": f"{chr(0xFEFF)}Bash"}, "U+FEFF"), + ({"agent": f"Skills{chr(0x200D)}"}, "U+200D"), + ): + with self.subTest(code=code): + result = self.tool("rule_request", self.ask_rule(**change)) + self.assertTrue(result["isError"]) + self.assertIn(code, json.dumps(result, ensure_ascii=True)) + if "agent" not in change: + form = {name: self.ask_rule(**change)[name] for name in ("kind", "tool", "pattern", "scope", "expires")} + status, _, body = self.post_json("/api/rules/sign", form, self.guard(self.host)) + self.assertEqual(status, 400) + self.assertIn(code, body.decode("utf-8")) + self.assertEqual([item for item in self.snapshot()["inbox"] if item["kind"] == "rule"], []) + self.assertEqual(self.httpd.hub.rule_requests.listed(), []) + request_id = self.requested(self.ask_rule()) + self.assertEqual(self.item(request_id)["rule"]["pattern"], "make test*") + def test_review_and_decision_requests_count_their_own_history(self): self.request("feat: green", "") self.tool("review_request", {**self.arguments("feat: red", "", self.page("red.html", PAGE)), "gate": "fail"}) diff --git a/tests/test_orchestrator_migration.py b/tests/test_orchestrator_migration.py index ad34036..f724a72 100644 --- a/tests/test_orchestrator_migration.py +++ b/tests/test_orchestrator_migration.py @@ -136,7 +136,7 @@ class TestOrchestratorMigration(unittest.TestCase): self.assertIn(f"{share / NEW}/client/permission_hook.py", hooks[1]["hooks"][0]["command"]) self.assertEqual(settings.stat().st_mode & 0o777, 0o640) self.assertIn(f"{OLD}/client", (self.home / ".claude" / f"settings.json.before-{NEW}-migration").read_text(encoding="utf-8")) - self.assertEqual((first["images"], first["mcp"], first["enabled"]), ([f"{NEW}:local"], {NEW: "http://127.0.0.1:8737/mcp"}, [f"{NEW}-offices.path", f"{NEW}-resources.timer", f"{NEW}-runner.service", f"{NEW}-sessions.timer"])) + self.assertEqual((first["images"], first["mcp"], first["enabled"]), ([f"{NEW}:local"], {NEW: "http://127.0.0.1:8737/mcp"}, [f"{NEW}-firewall.path", f"{NEW}-firewall.timer", f"{NEW}-offices.path", f"{NEW}-resources.timer", f"{NEW}-runner.service", f"{NEW}-sessions.timer"])) self.assertIn(["docker", "compose", "--file", str(share / OLD / "compose.yml"), "down", "--remove-orphans"], first["calls"]) before = snapshot(self.home) diff --git a/tests/test_page_prompt.py b/tests/test_page_prompt.py new file mode 100644 index 0000000..bf09332 --- /dev/null +++ b/tests/test_page_prompt.py @@ -0,0 +1,106 @@ +from __future__ import annotations + +import json +import shutil +import subprocess +import tempfile +import time +import unittest +from pathlib import Path + +TEMPLATES = Path(__file__).resolve().parent.parent / "orchestrator" / "templates" +FIREWALL = TEMPLATES / "page-firewall.js" +REVEAL = TEMPLATES / "page-reveal.js" +NOW = 1_800_000_000 +DAY = 86400 +NAMES = {"s1": "Skills", "s2": "Stage", "s3": "Evil"} +HARNESS = """const fs = require("fs"); +const [source, given] = [fs.readFileSync(process.argv[2], "utf8"), JSON.parse(fs.readFileSync(process.argv[3], "utf8"))]; +const revealed = new Function("document", `${fs.readFileSync(process.argv[4], "utf8")}\\nreturn revealed;`)({getElementById: () => ({textContent: "[]"})}); +const make = new Function("HUMAN", "people", "labelOf", "h", "state", "nowSeconds", "DAY", "revealed", `${source}\\nreturn {scopeLabel, promptText: typeof promptText === "function" ? promptText : () => "no promptText", firewall};`); +const page = make("human", () => given.names, (key) => given.names[key] || key, () => ({}), {rules: []}, () => given.now, given.day, revealed); +page.firewall.data = {groups: given.groups, files: [], sets: []}; +console.log(JSON.stringify({scope: given.scopes.map((agents) => page.scopeLabel(agents)), prompts: given.prompts.map(([label, prepared]) => page.promptText(label, prepared))})); +""" + + +def rule(agents: list[str] | None, days: int) -> dict: + """Returns: + A signed rule payload for asks of any command under /home/op, lasting + days from NOW, for agents or, with None, untargeted. + """ + payload = {"v": 1, "kind": "rule", "id": "0123456789abcdef", "kinds": ["ask"], "tool": "Bash", "pattern": "*", "scope": "/home/op/*", "expires": NOW + days * DAY} + return payload if agents is None else {**payload, "agents": agents} + + +def shown(source: Path, groups: list[dict], scopes: list[list[str]], prompts: list[list[object]], names: dict[str, str]) -> dict: + """Args: + source: the page script. + groups: the firewall groups the page knows. + scopes: the agents of each scope to name. + prompts: each [label, offer] to show in the passkey prompt. + names: the label of each session, as council claims give it. + + Returns: + {"scope", "prompts"}: what the page script names for each scope and + shows in the passkey prompt for each [label, offer]. + """ + with tempfile.TemporaryDirectory() as scratch: + harness, given = Path(scratch) / "harness.js", Path(scratch) / "given.json" + harness.write_text(HARNESS, encoding="utf-8") + given.write_text(json.dumps({"names": names, "now": NOW, "day": DAY, "groups": groups, "scopes": scopes, "prompts": prompts}), encoding="utf-8") + done = subprocess.run(["node", str(harness), str(source), str(given), str(REVEAL)], capture_output=True, text=True, timeout=60, check=False) + if done.returncode != 0: + raise AssertionError(done.stderr.strip()[:500]) + return json.loads(done.stdout) + + +@unittest.skipUnless(shutil.which("node"), "no node") +class TestPagePrompt(unittest.TestCase): + def test_a_group_redefined_after_it_was_picked_shows_every_signed_session(self): + redefined = [{"id": "ffffffffffffffff", "name": "builders", "members": ["s1", "s2", "s3"]}] + found = shown(FIREWALL, redefined, [["s1", "s2", "s3"], ["s1", "s2"], ["s3"], ["*"]], [], NAMES) + self.assertEqual(found["scope"], ["for group builders: Skills (s1), Stage (s2), Evil (s3)", "for agents Skills (s1), Stage (s2)", "for agent Evil (s3)", "for all agents"]) + + def test_the_prompt_shows_the_pattern_directory_and_lifetime_of_every_signed_rule(self): + end = time.strftime("%Y-%m-%d %H:%M", time.gmtime(NOW + 366 * DAY)) + approval = {"v": 1, "kind": "approve", "items": ["ab" * 32], "nonce": "cd" * 16, "expires": NOW + 900} + found = shown(FIREWALL, [], [], [ + ["Rule set ci", {"payload": rule(["s1"], 366)}], + ["Rule", {"payload": rule(None, 1)}], + ["Turn on rule", {"payload": approval, "enables": rule(["s1", "s3"], 366)}], + ["Settings", {"payload": approval}], + ], NAMES) + lines = ["ask Bash: *", "in /home/op/*", f"until {end} UTC, in 366.0 days"] + self.assertEqual([prompt.split("\n") for prompt in found["prompts"]], [ + ["Rule set ci", *lines, "for agent Skills (s1)"], + ["Rule", "ask Bash: *", "in /home/op/*", f"until {time.strftime('%Y-%m-%d %H:%M', time.gmtime(NOW + DAY))} UTC, in 1.0 days", "for all agents"], + ["Turn on rule", *lines, "for agents Skills (s1), Evil (s3)"], + ["Settings"], + ]) + + def test_a_hidden_character_of_a_group_set_claim_legacy_rule_or_pattern_shows_as_a_marker_in_the_prompt(self): + rlo, separator, nel = chr(0x202E), chr(0x2028), chr(0x85) + approval = {"v": 1, "kind": "approve", "items": ["ab" * 32], "nonce": "cd" * 16, "expires": NOW + 900} + groups = [{"id": "ffffffffffffffff", "name": f"ci{rlo}", "members": ["s1", "s3"]}] + legacy = {**rule(None, 1), "pattern": f"make test{rlo}*"} + separated = {**rule(["s2"], 1), "pattern": f"make test{separator}in /safe", "scope": f"/repo{nel}*"} + found = shown(FIREWALL, groups, [], [ + [f"Rule set ci{rlo}", {"payload": rule(["s1", "s3"], 1)}], + ["Turn on rule", {"payload": approval, "enables": legacy}], + ["Rule", {"payload": separated}], + ], {**NAMES, "s2": f"Main{rlo}"}) + until = f"until {time.strftime('%Y-%m-%d %H:%M', time.gmtime(NOW + DAY))} UTC, in 1.0 days" + self.assertEqual([prompt.split("\n") for prompt in found["prompts"]], [ + [f"Rule set ci{marker(0x202E)}", "ask Bash: *", "in /home/op/*", until, f"for group ci{marker(0x202E)}: Skills (s1), Evil (s3)"], + ["Turn on rule", f"ask Bash: make test{marker(0x202E)}*", "in /home/op/*", until, "for all agents"], + ["Rule", f"ask Bash: make test{marker(0x2028)}in /safe", f"in /repo{marker(0x85)}*", until, f"for agent Main{marker(0x202E)} (s2)"], + ]) + + +def marker(code: int) -> str: + return f"⟨U+{code:04X}⟩" + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_page_reveal.py b/tests/test_page_reveal.py new file mode 100644 index 0000000..ca0c865 --- /dev/null +++ b/tests/test_page_reveal.py @@ -0,0 +1,182 @@ +from __future__ import annotations + +import json +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +from hub_support import signing + +TEMPLATES = Path(__file__).resolve().parent.parent / "orchestrator" / "templates" +MODULES = ["page-constants.js", "ancestry.js", "page-reveal.js", "page-dom.js", "page-diff.js", "page-rule-request.js", "page-inbox.js", "page-firewall.js", "page-review.js", "page-cleanup.js", "page-offices.js", "page-ancestry.js", "page-tasks-prio.js"] +RLO, ZWSP, LRI, SEPARATOR, CARRIAGE = chr(0x202E), chr(0x200B), chr(0x2066), chr(0x2028), chr(0x0D) +FILLER, NBSP, CGJ, VS15, VS16 = chr(0x3164), chr(0xA0), chr(0x34F), chr(0xFE0E), chr(0xFE0F) +HIDDEN = (RLO, ZWSP, LRI, SEPARATOR, CARRIAGE, FILLER, NBSP, CGJ, VS16) +STOPWATCH, SHIELD, HEART = chr(0x23F1), chr(0x1F6E1), chr(0x2764) +CONTEXTS = ( + ("score line", f"🎯 fix 93 · {STOPWATCH}{VS16} time 20 · {SHIELD}{VS16} security 100", True), + ("text presentation", f"{STOPWATCH}{VS15} 20", True), + ("heart", f"thanks {HEART}{VS16}", True), + ("stacked selectors", f"{STOPWATCH}{VS16}{VS16} 20", False), + ("joiner after a sequence", f"{SHIELD}{VS16}{chr(0x200D)}", False), + ("leading selector", f"{VS16}stop {STOPWATCH}", False), + ("ASCII letter", f"a{VS16}", False), + ("combining mark", f"e{chr(0x301)}{VS16}", False), + ("keycap", f"1{VS16}{chr(0x20E3)}", False), + ("number sign", f"#{VS16}", False), + ("command", f"make test{VS16}", False), + ("emoji without a sequence", f"🎯{VS16}", False), + ("other selector", f"{STOPWATCH}{chr(0xFE00)}", False), + ("no-break space", f"rm{NBSP}-rf", False), + ("Hangul filler", f"main{FILLER}", False), +) +HARNESS = r"""const fs = require("fs"); +const [dir, given] = [process.argv[2], JSON.parse(fs.readFileSync(process.argv[3], "utf8"))]; +const source = given.files.map((name) => fs.readFileSync(`${dir}/${name}`, "utf8")).join("\n"); +class FakeNode { + constructor(tag) { this.tag = tag; this.attrs = {}; this.children = []; } + setAttribute(key, value) { this.attrs[key] = value; } + append(...children) { this.children.push(...children); } + replaceChildren(...children) { this.children = children; } + addEventListener() {} + focus() {} + get value() { return this.attrs.value ?? ""; } +} +const stub = new Proxy(function () {}, {get: (target, key) => (key === Symbol.toPrimitive ? () => "" : key === Symbol.iterator ? [][Symbol.iterator].bind([]) : stub), apply: () => stub}); +const sheet = new FakeNode("div"); +const provided = {document: {createElement: (tag) => new FakeNode(tag), createElementNS: (ns, tag) => new FakeNode(tag), getElementById: (id) => (id === "emoji-bases" ? {textContent: JSON.stringify(given.bases)} : sheet), addEventListener() {}}, Node: FakeNode, ...given.globals}; +const scope = new Proxy({}, {has: (target, key) => typeof key === "string" && (key in provided || !(key in globalThis)), get: (target, key) => (key === Symbol.unscopables ? undefined : key in provided ? provided[key] : stub)}); +const page = new Function("scope", `with (scope) {\n${source}\nreturn {${given.calls.map(([name]) => name).join(", ")}};\n}`)(scope); +const own = (node) => [node.attrs.title, node.textContent].filter((text) => typeof text === "string"); +const texts = (node) => (typeof node === "string" ? [node] : Array.isArray(node) ? node.flatMap(texts) : node instanceof FakeNode ? [...own(node), ...node.children.flatMap(texts)] : []); +console.log(JSON.stringify(given.calls.map(([name, args]) => { + const drawn = page[name](...args); + return texts(drawn === undefined ? sheet : drawn); +}))); +""" +PROBE = r"""const fs = require("fs"); +const given = JSON.parse(fs.readFileSync(process.argv[2], "utf8")); +const revealed = new Function("document", `${fs.readFileSync(process.argv[1], "utf8")}\nreturn revealed;`)({getElementById: () => ({textContent: JSON.stringify(given.bases)})}); +console.log(JSON.stringify(given.texts.map(revealed))); +""" + + +def shown(templates: Path, calls: list[list[object]], globals_: dict | None = None) -> list[list[str]]: + """Args: + templates: the folder of the page modules. + calls: each [function, arguments] of the page to render. + globals_: page state the calls read, such as state, ui and tree. + + Returns: + Every text, text content and title each rendered node shows, or the + sheet a call opens, in order. + """ + with tempfile.TemporaryDirectory() as scratch: + harness, given = Path(scratch) / "harness.js", Path(scratch) / "given.json" + harness.write_text(HARNESS, encoding="utf-8") + given.write_text(json.dumps({"files": MODULES, "calls": calls, "globals": globals_ or {}, "bases": sorted(signing.EMOJI_BASES)}), encoding="utf-8") + done = subprocess.run(["node", str(harness), str(templates), str(given)], capture_output=True, text=True, timeout=60, check=False) + if done.returncode != 0: + raise AssertionError(done.stderr.strip()[:800]) + return json.loads(done.stdout) + + +def item(kind: str, **fields: object) -> dict: + base = {"kind": kind, "id": "0123456789abcdef", "title": "Bash: make test", "command": "", "directory": "", "agent": "Skills", "to": "Skills", "ts": 1_800_000_000, "warnings": [], "options": [], "recommended": "", "held": False, "until": 0, "pinned": False, "diff": None, "job": None, "rule": None, "detail": "", "green": False, "gate": "", "score": "", "runnable": True, "deniable": True, "sha256": ""} + return {**base, **fields} + + +@unittest.skipUnless(shutil.which("node"), "no node") +class TestPageReveal(unittest.TestCase): + def assertRevealed(self, texts: list[str], codes: list[str]) -> None: + joined = "\n".join(texts) + for char in HIDDEN: + self.assertNotIn(char, joined) + for code in codes: + self.assertIn(f"⟨U+{code}⟩", joined) + + def test_the_sink_marks_every_control_format_separator_and_ignorable_character_but_the_tab(self): + probe = "".join(chr(code) for code in (0x202E, 0x200B, 0x2028, 0x2029, 0x85, 0x0B, 0x0D, 0x0A, 0x34F, 0xFE0F, 0x3164, 0xE0041, 0xAD, 0xA0, 0x3000)) + found = shown(TEMPLATES, [["revealed", [f"a{probe}\tb ✓"]], ["revealedLines", [f"one{NBSP}\ntwo"]]]) + self.assertEqual(found, [["a⟨U+202E⟩⟨U+200B⟩⟨U+2028⟩⟨U+2029⟩⟨U+0085⟩⟨U+000B⟩⟨U+000D⟩⟨U+000A⟩⟨U+034F⟩⟨U+FE0F⟩⟨U+3164⟩⟨U+E0041⟩⟨U+00AD⟩⟨U+00A0⟩⟨U+3000⟩\tb ✓"], ["one⟨U+00A0⟩\ntwo"]]) + + def test_both_sinks_accept_exactly_an_emoji_variation_sequence(self): + with tempfile.TemporaryDirectory() as scratch: + given = Path(scratch) / "given.json" + given.write_text(json.dumps({"bases": sorted(signing.EMOJI_BASES), "texts": [text for _, text, _ in CONTEXTS]}), encoding="utf-8") + done = subprocess.run(["node", "-e", PROBE, str(TEMPLATES / "page-reveal.js"), str(given)], capture_output=True, text=True, timeout=60, check=True) + for (name, text, accepted), page in zip(CONTEXTS, json.loads(done.stdout), strict=True): + with self.subTest(name=name): + self.assertEqual(signing.hidden_problem({"command": text}, "value") is None, accepted) + self.assertEqual(page == text, accepted) + self.assertEqual("⟨U+" in page, not accepted) + + def test_the_variation_table_names_its_source_and_holds_no_ascii_base(self): + table = json.loads(signing.EMOJI_TABLE.read_text(encoding="utf-8")) + self.assertEqual(sorted(table), ["bases", "source", "unicode"]) + self.assertEqual(table["source"], f"https://www.unicode.org/Public/{table['unicode']}/ucd/emoji/emoji-variation-sequences.txt") + self.assertEqual(min(signing.EMOJI_BASES), 0xA9) + self.assertLessEqual({0x23F1, 0x1F6E1, 0x2764, 0x26A0}, signing.EMOJI_BASES) + + def test_a_variation_table_with_a_wrong_base_stops_the_load(self): + with tempfile.TemporaryDirectory() as scratch: + path = Path(scratch) / "emoji-variation.json" + for name, bases in (("short ASCII base", ["41"]), ("number sign base", ["0023"]), ("digit base", ["0031"]),("string value", "23F1"), ("object value", {"23F1": 1}), ("number", [0x23F1]), ("C1 control", ["009B"]), ("joiner", ["200D"]), ("surrogate", ["D800"]), ("lower case", ["23f1"])): + with self.subTest(name=name): + path.write_text(json.dumps({"bases": bases}), encoding="utf-8") + with self.assertRaises(ValueError): + signing.variation_table(path) + path.write_text(json.dumps({"bases": ["23F1", "1F6E1"]}), encoding="utf-8") + self.assertEqual(signing.variation_table(path), {0x23F1, 0x1F6E1}) + path.unlink() + with self.assertRaises(OSError): + signing.variation_table(path) + + def test_a_review_cleanup_office_ancestry_and_task_preview_show_every_look_alike_character_as_a_marker(self): + review = {"id": "r1", "title": f"feat: x{RLO}", "notes": f"score{NBSP}90\nline{CGJ}", "created_ts": 1_800_000_000, "worktree": "/repo/.worktrees/x", "branch": f"main{FILLER}", "tree": "a" * 40, "gate": "pass", "score": f"🎯{VS16} 90", "status": "pending"} + repo = {"path": "/r", "name": "skills", "branches": [{"name": f"main{FILLER}", "plan": [f"git branch -D main{FILLER}"], "held": None}, {"name": "feature/a", "plan": [], "held": None}]} + page_state = { + "state": {"reviews": [review], "inbox": [], "jobs": [{"kind": "task", "task": "T1", "command": f"rasci.py set T1{RLO} --prio 3", "status": "done"}], "graph": {"nodes": []}, "claims": []}, + "ui": {"review": None, "file": 0}, + "tree": {"data": {"repos": [repo], "gates": {"/r": {"feature/a": {"parents": [{"parent": f"main{FILLER}", "source": "own"}], "rules": {}}}}}}, + } + calls = [ + ["reviewSheet", ["r1"]], + ["deleteDialog", [[{"repository": "/r", "branch": f"main{FILLER}", "tip": "b" * 40}]]], + ["officeSheet", ["takeover", {"office": f"work/x{FILLER}", "session": "s1", "work": "-"}]], + ["branchAncestry", [repo]], + ["priorityForm", [{"task": f"T1{FILLER}", "estimates": {}, "prio": 3}]], + ["taskJobs", [{"task": "T1"}]], + ] + found = shown(TEMPLATES, calls, page_state) + self.assertRevealed(found[0], ["202E", "3164", "00A0", "034F", "FE0F"]) + for drawn in found[1:5]: + self.assertRevealed(drawn, ["3164"]) + self.assertRevealed(found[5], ["202E"]) + + def test_an_inbox_ask_operator_row_and_decision_show_every_hidden_character_as_a_marker(self): + ask = item("ask", command=f"git status{RLO} && rm -rf ~", directory=f"/repo{ZWSP}", agent=f"Skills{RLO}") + operator = item("operator", id="operator/run", title=f"make{CARRIAGE} test", command=f"make{CARRIAGE} test", directory=f"/repo{LRI}", detail=f"why{SEPARATOR}not") + decision = item("decision", title=f"Merge{RLO}?", options=[{"label": f"Merge now{RLO}", "description": f"Gate{ZWSP} is green"}], recommended=f"Merge now{RLO}") + found = shown(TEMPLATES, [["inboxItem", [ask, 0]], ["inboxItem", [operator, 1]], ["inboxItem", [decision, 2]]]) + self.assertRevealed(found[0], ["202E", "200B"]) + self.assertRevealed(found[1], ["000D", "2066", "2028"]) + self.assertRevealed(found[2], ["202E", "200B"]) + + def test_a_diff_row_and_a_rule_request_show_every_hidden_character_as_a_marker(self): + rule = item("rule", detail=f"needed{ZWSP} daily", rule={"kind": "ask", "tool": "Bash", "pattern": f"make test{RLO}*", "scope": f"/repo{SEPARATOR}*", "expires": 1_800_000_000}, dry_run={"matched": 0, "total": 0, "days": 7}) + found = shown(TEMPLATES, [["patchRow", [{"kind": "add", "old": None, "new": 3, "text": f"access = 'user'{RLO} # admin"}]], ["ruleRequestBody", [rule]]]) + self.assertRevealed(found[0], ["202E"]) + self.assertRevealed(found[1], ["200B", "202E", "2028"]) + + def test_a_settings_entry_staged_for_removal_and_its_diff_show_every_hidden_character_as_a_marker(self): + entry = f"Read(~/.ssh/**{RLO})" + found = shown(TEMPLATES, [["entryRow", ["/home/op/.claude/settings.json", "permissions.deny", entry]], ["diffLines", [f'- "{entry}",\n+ "Bash(make{ZWSP} build:*)"']]]) + self.assertRevealed(found[0], ["202E"]) + self.assertRevealed(found[1], ["202E", "200B"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_page_scope.py b/tests/test_page_scope.py new file mode 100644 index 0000000..3a7572d --- /dev/null +++ b/tests/test_page_scope.py @@ -0,0 +1,75 @@ +from __future__ import annotations + +import re +import shutil +import subprocess +import tempfile +import unittest +from collections import defaultdict +from pathlib import Path + +TEMPLATES = Path(__file__).resolve().parent.parent / "orchestrator" / "templates" +INCLUDE = re.compile(r'\{% include "([^"]+\.js)" %\}') +DECLARED = re.compile(r"^ (?:async\s+)?(?:function\s*\*?\s*([A-Za-z_$][\w$]*)|(?:const|let|var|class)\s+([A-Za-z_$][\w$]*))", re.MULTILINE) +OPENING = '(() => {\n "use strict";' +CLOSING = "})();" + + +def page_block(templates: Path) -> str: + """Returns: + The shared page script of index.html.j2, from its opening arrow to + its closing call, with the include tags still in place. + """ + page = (templates / "index.html.j2").read_text(encoding="utf-8") + start = page.index(OPENING) + return page[start : page.index(CLOSING, start) + len(CLOSING)] + + +def clashes(templates: Path) -> dict[str, list[str]]: + """Returns: + Every top-level name the shared page script declares in more than one + included file, with those files in include order. + """ + owners: dict[str, list[str]] = defaultdict(list) + for name in INCLUDE.findall(page_block(templates)): + for match in DECLARED.finditer((templates / name).read_text(encoding="utf-8")): + owners[match.group(1) or match.group(2)].append(name) + return {name: files for name, files in owners.items() if len(files) > 1} + + +def assembled(templates: Path) -> str: + return INCLUDE.sub(lambda match: (templates / match.group(1)).read_text(encoding="utf-8"), page_block(templates)) + + +def parse_error(script: str) -> str: + """Returns: + What node --check reports for the script, "" when it parses. + """ + with tempfile.TemporaryDirectory() as scratch: + path = Path(scratch) / "page.js" + path.write_text(script, encoding="utf-8") + done = subprocess.run(["node", "--check", str(path)], capture_output=True, text=True, timeout=60, check=False) + return "" if done.returncode == 0 else done.stderr.strip()[:500] + + +class TestPageScope(unittest.TestCase): + def test_no_top_level_name_is_declared_in_two_page_files(self): + self.assertEqual(clashes(TEMPLATES), {}) + + @unittest.skipUnless(shutil.which("node"), "no node") + def test_the_assembled_page_script_parses(self): + self.assertEqual(parse_error(assembled(TEMPLATES)), "") + + def test_a_clash_between_two_page_files_is_caught(self): + with tempfile.TemporaryDirectory() as scratch: + copy = Path(scratch) + shutil.copytree(TEMPLATES, copy, dirs_exist_ok=True) + firewall = copy / "page-firewall.js" + firewall.write_text(firewall.read_text(encoding="utf-8") + "\n const pickOf = (key) => key;\n", encoding="utf-8") + self.assertEqual(clashes(copy), {"pickOf": ["page-firewall.js", "page-popup.js"]}) + if shutil.which("node"): + self.assertIn("'pickOf' has already been declared", parse_error(assembled(copy))) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_settings_writer.py b/tests/test_settings_writer.py new file mode 100644 index 0000000..e378a67 --- /dev/null +++ b/tests/test_settings_writer.py @@ -0,0 +1,504 @@ +from __future__ import annotations + +import contextlib +import errno +import hashlib +import importlib.util +import io +import json +import os +import secrets +import stat +import subprocess +import sys +import tempfile +import time +import unittest +from pathlib import Path +from unittest import mock + +import test_hub_spool_links as spool_links +from hub_support import HUB, ORIGIN, key, signing, verifier + +WRITER = HUB / "client" / "settings_writer.py" +WRITER_SPEC = importlib.util.spec_from_file_location("kvb_orchestrator_settings_writer", WRITER) +host = importlib.util.module_from_spec(WRITER_SPEC) +WRITER_SPEC.loader.exec_module(host) +ORIGINAL = { + "model": "x", + "env": {"EXAMPLE_FLAG": "fixture-value-not-a-secret"}, + "hooks": {"Stop": [{"matcher": "", "hooks": [{"type": "command", "command": "true"}]}]}, + "permissions": {"allow": ["Bash(make test:*)", "Bash(git status:*)"], "deny": ["Read(~/.ssh/**)"]}, + "sandbox": {"enabled": True, "filesystem": {"allowWrite": ["/tmp"]}}, +} +ADD_LINT = {"list": "permissions.allow", "op": "add", "entry": "Bash(make lint:*)"} + + +def sha(data: bytes) -> str: + return hashlib.sha256(data).hexdigest() + + +class TestSettingsWriter(unittest.TestCase): + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + self.base = Path(tmp.name) + self.home = self.base / "home" + self.settings = self.home / ".claude" / "settings.json" + self.project = self.base / "repo" + self.project_settings = self.project / ".claude" / "settings.json" + self.elsewhere = self.base / "elsewhere" / ".claude" / "settings.json" + for path in (self.settings, self.elsewhere): + path.parent.mkdir(parents=True) + path.write_text(json.dumps(ORIGINAL, indent=2) + "\n", encoding="utf-8") + path.chmod(0o640) + self.project_settings.parent.mkdir(parents=True) + self.tree = self.base / "tree" / "branch-tree.json" + self.tree.parent.mkdir() + self.tree.write_text(json.dumps({"repos": [{"path": str(self.project), "known": True, "default": "main"}, {"path": str(self.elsewhere.parent.parent), "known": False}]}), encoding="utf-8") + self.jobs, self.results = self.base / "jobs", self.base / "results" + self.jobs.mkdir(mode=0o700) + self.results.mkdir(mode=0o700) + self.approvers = self.base / "config" / "approvers.json" + self.trust = verifier.Trust(approvers=self.approvers, replay=self.base / "replay" / "firewall.json", origin=ORIGIN) + self.signer = key(-7, "main") + self.signer.pin(self.approvers, ORIGIN) + self.writer = host.Writer(self.trust, self.home, self.tree) + + def item(self, path: Path, changes: list[dict]) -> dict: + return {"file": str(path), "base": sha(path.read_bytes()) if path.exists() else "", "changes": changes} + + def proof(self, items: list[dict], signer) -> dict: + hashes = [signing.item_hash(signing.settings_item(**item)) for item in items] + return signer.proof(signing.approval_payload(hashes, secrets.token_hex(16), int(time.time()) + 600), ORIGIN) + + def write_job(self, job_id: str, items: list[dict], proof: object) -> None: + (self.jobs / f"{job_id}.json").write_text(json.dumps({"v": 1, "id": job_id, "created": "x", "items": items, "proof": proof}), encoding="utf-8") + + def result(self, job_id: str) -> dict: + return json.loads((self.results / f"{job_id}.json").read_text(encoding="utf-8")) + + def run_job(self, items: list[dict], proof: object) -> dict: + job_id = secrets.token_hex(8) + self.write_job(job_id, items, proof) + self.assertEqual(host.cycle(self.jobs, self.results, self.writer), 1) + self.assertEqual(list(self.jobs.iterdir()), []) + return self.result(job_id) + + def unit(self, view: Path, marker: dict[str, str]) -> subprocess.CompletedProcess: + trust = ["--approvers", str(self.approvers), "--replay", str(self.trust.replay)] + command = [sys.executable, str(WRITER), "--port", ORIGIN.rsplit(":", 1)[1], "--jobs", str(self.jobs), "--results", str(self.results), "--view", str(view), *trust, "--tree", str(self.tree)] + return subprocess.run(command, capture_output=True, text=True, timeout=60, check=False, env={"PATH": "/usr/bin:/bin", "HOME": str(self.home), **marker}) + + def signed(self, item: dict) -> dict: + return self.run_job([item], self.proof([item], self.signer))["items"][0] + + def files(self) -> dict[str, tuple[int, str]]: + found = {} + for root in (self.home, self.project, self.elsewhere.parent.parent): + for path in sorted(root.rglob("*")): + if path.is_file() or path.is_symlink(): + found[str(path)] = (path.lstat().st_mode, sha(path.read_bytes()) if path.is_file() else os.readlink(path)) + return found + + def test_a_signed_change_writes_exactly_it_keeps_every_other_key_and_a_backup_with_the_mode(self): + before = self.settings.read_bytes() + changes = [ADD_LINT, {"list": "permissions.deny", "op": "remove", "entry": "Read(~/.ssh/**)"}, {"list": "sandbox.network.allowedDomains", "op": "add", "entry": "example.org"}] + result = self.signed(self.item(self.settings, changes)) + self.assertEqual((result["status"], result["reason"]), ("done", ""), result) + expected = json.loads(json.dumps(ORIGINAL)) + expected["permissions"]["allow"].append("Bash(make lint:*)") + expected["permissions"]["deny"] = [] + expected["sandbox"]["network"] = {"allowedDomains": ["example.org"]} + written = self.settings.read_bytes() + self.assertEqual(json.loads(written), expected) + self.assertEqual(list(json.loads(written)), list(ORIGINAL)) + self.assertEqual(result["sha256"], sha(written)) + backup = Path(result["backup"]) + self.assertEqual((backup.parent, backup.read_bytes()), (self.settings.parent, before)) + self.assertEqual({path.name: path.stat().st_mode & 0o777 for path in self.settings.parent.iterdir()}, {"settings.json": 0o640, backup.name: 0o640}) + + def test_a_missing_project_file_is_created_private_with_only_the_change(self): + result = self.signed(self.item(self.project_settings, [ADD_LINT])) + self.assertEqual((result["status"], result["backup"]), ("done", ""), result) + self.assertEqual(json.loads(self.project_settings.read_text(encoding="utf-8")), {"permissions": {"allow": ["Bash(make lint:*)"]}}) + self.assertEqual(self.project_settings.stat().st_mode & 0o777, 0o600) + + def test_a_forged_changed_stale_unlisted_or_impossible_job_changes_nothing(self): + good = self.item(self.settings, [ADD_LINT]) + rule = self.signer.proof(signing.rule_payload(secrets.token_hex(8), ["settings"], "", "*", "*", int(time.time()) + 600), ORIGIN) + swapped = {**good, "changes": [{**ADD_LINT, "entry": "Bash(rm -rf:*)"}]} + stale = {**good, "base": sha(b"{}")} + unlisted = self.item(self.elsewhere, [ADD_LINT]) + twice = self.item(self.settings, [{**ADD_LINT, "entry": "Bash(make test:*)"}]) + absent = self.item(self.settings, [{**ADD_LINT, "op": "remove"}]) + cases = ( + ("forged signature", good, self.proof([good], key(-7, "intruder")), "no valid operator signature"), + ("missing signature", good, None, "no signature"), + ("rule instead of an approval", good, rule, "no valid operator signature"), + ("changed after signing", swapped, self.proof([good], self.signer), "does not list this item"), + ("stale base", stale, self.proof([stale], self.signer), "changed since the signature"), + ("unlisted file", unlisted, self.proof([unlisted], self.signer), "unlisted file"), + ("added twice", twice, self.proof([twice], self.signer), "lists Bash(make test:*)"), + ("removed absent", absent, self.proof([absent], self.signer), "does not list"), + ) + for name, item, proof, reason in cases: + with self.subTest(name=name): + before = self.files() + result = self.run_job([item], proof)["items"][0] + self.assertEqual(result["status"], "refused", result) + self.assertIn(reason, result["reason"]) + self.assertEqual(self.files(), before) + + def test_the_host_refuses_to_add_a_hidden_character_and_removes_one_already_in_the_file(self): + hidden = f"Bash(make{chr(0x202E)} build:*)" + before = self.files() + addition = self.item(self.settings, [{**ADD_LINT, "entry": hidden}]) + self.assertEqual(self.run_job([addition], self.proof([addition], self.signer))["error"], "malformed job: refused, nothing changed") + self.assertEqual(self.files(), before) + settings = json.loads(self.settings.read_text(encoding="utf-8")) + settings["permissions"]["allow"].append(hidden) + self.settings.write_text(json.dumps(settings, indent=2) + "\n", encoding="utf-8") + result = self.signed(self.item(self.settings, [{"list": "permissions.allow", "op": "remove", "entry": hidden}])) + self.assertEqual(result["status"], "done", result) + self.assertEqual(json.loads(self.settings.read_text(encoding="utf-8"))["permissions"]["allow"], ORIGINAL["permissions"]["allow"]) + + def test_a_replayed_signature_changes_nothing_even_when_the_file_is_back_at_its_base(self): + before = self.settings.read_bytes() + item = self.item(self.settings, [ADD_LINT]) + proof = self.proof([item], self.signer) + self.assertEqual(self.run_job([item], proof)["items"][0]["status"], "done") + self.settings.write_bytes(before) + again = self.run_job([item], proof)["items"][0] + self.assertEqual(again["status"], "refused") + self.assertIn("already used", again["reason"]) + self.assertEqual(self.settings.read_bytes(), before) + + def test_a_link_on_the_path_or_at_the_file_is_refused(self): + outside = self.base / "outside" + (outside / ".claude").mkdir(parents=True) + target = outside / ".claude" / "settings.json" + target.write_text("{}\n", encoding="utf-8") + self.project_settings.parent.rmdir() + (self.project / ".claude").symlink_to(outside / ".claude") + result = self.signed({"file": str(self.project_settings), "base": sha(b"{}\n"), "changes": [ADD_LINT]}) + self.assertEqual(result["status"], "refused", result) + self.assertIn("link", result["reason"]) + (self.project / ".claude").unlink() + (self.project / ".claude").mkdir() + self.project_settings.symlink_to(target) + result = self.signed({"file": str(self.project_settings), "base": sha(b"{}\n"), "changes": [ADD_LINT]}) + self.assertEqual(result["status"], "refused", result) + self.assertIn("link", result["reason"]) + self.assertEqual((target.read_text(encoding="utf-8"), self.project_settings.is_symlink()), ("{}\n", True)) + + def test_a_failed_replace_keeps_the_file_and_leaves_no_temporary(self): + before = self.settings.read_bytes() + replace = os.replace + + def failing(source: str, target: str, **kwargs: int) -> None: + if target == "settings.json": + raise OSError(errno.EIO, "disk gone") + replace(source, target, **kwargs) + + with mock.patch.object(host.os, "replace", failing): + result = self.signed(self.item(self.settings, [ADD_LINT])) + self.assertEqual(result["status"], "failed", result) + self.assertEqual(self.settings.read_bytes(), before) + self.assertEqual(sorted(path.name for path in self.settings.parent.iterdir() if path.name.startswith(".")), []) + + def test_a_malformed_or_linked_job_writes_nothing(self): + good = self.item(self.settings, [ADD_LINT]) + before = self.files() + for name, items in ( + ("command smuggled in", [{**good, "command": "rm -rf ~"}]), + ("local settings", [{**good, "file": str(self.settings.with_name("settings.local.json"))}]), + ("climbing path", [{**good, "file": f"{self.home}/../home/.claude/settings.json"}]), + ("no changes", [{**good, "changes": []}]), + ("hooks list", [{**good, "changes": [{**ADD_LINT, "list": "hooks.PermissionRequest"}]}]), + ("replace op", [{**good, "changes": [{**ADD_LINT, "op": "replace"}]}]), + ("two lines", [{**good, "changes": [{**ADD_LINT, "entry": "a\nb"}]}]), + ("lone surrogate entry", [{**good, "changes": [{**ADD_LINT, "entry": "Bash(make \ud800:*)"}]}]), + ("lone surrogate path", [{**good, "file": f"{self.home}/\ud800/.claude/settings.json"}]), + ("twice the same entry", [{**good, "changes": [ADD_LINT, ADD_LINT]}]), + ("no items", []), + ): + with self.subTest(name=name): + result = self.run_job(items, self.proof([good], self.signer)) + self.assertEqual(result["items"], []) + self.assertIn("malformed job", result["error"]) + job_id = secrets.token_hex(8) + planted = self.base / "planted.json" + planted.write_text(json.dumps({"v": 1, "id": job_id, "created": "x", "items": [good], "proof": self.proof([good], self.signer)}), encoding="utf-8") + (self.jobs / f"{job_id}.json").symlink_to(planted) + self.assertEqual(host.cycle(self.jobs, self.results, self.writer), 1) + self.assertIn("unreadable job", self.result(job_id)["error"]) + pipe = secrets.token_hex(8) + os.mkfifo(self.jobs / f"{pipe}.json") + renamed = secrets.token_hex(8) + self.write_job(renamed, [good], self.proof([good], self.signer)) + (self.jobs / f"{renamed}.json").rename(self.jobs / f"{'0' * 16}.json") + self.assertEqual(host.cycle(self.jobs, self.results, self.writer), 2) + self.assertIn("not a regular file", self.result(pipe)["error"]) + self.assertEqual((self.result("0" * 16)["items"], self.result("0" * 16)["error"]), ([], "malformed job: refused, nothing changed")) + self.assertEqual((list(self.jobs.iterdir()), self.files()), ([], before)) + + def test_a_file_json_readers_could_read_differently_fails_and_stays_byte_identical(self): + for name, text, reason in ( + ("overflowing number", '{"model": 1e400}', "beyond the double range"), + ("NaN", '{"model": NaN}', "holds NaN"), + ("Infinity", '{"model": Infinity}', "holds Infinity"), + ("minus Infinity", '{"model": -Infinity}', "holds -Infinity"), + ("repeated key", '{"permissions": {"allow": []}, "permissions": {"deny": []}}', "repeats a key"), + ("lone surrogate", '{"model": "\\ud800"}', "lone surrogate"), + ("600 levels", '{"model": ' + "[" * 600 + "]" * 600 + "}", "nests deeper than 64"), + ("no object", "[]", "no JSON object"), + ("no JSON", "{", "no strict JSON"), + ): + with self.subTest(name=name): + self.settings.write_text(text + "\n", encoding="utf-8") + before = self.files() + result = self.signed(self.item(self.settings, [ADD_LINT])) + self.assertEqual(result["status"], "failed", result) + self.assertIn(reason, result["reason"]) + self.assertNotIn("model", result["reason"]) + self.assertEqual(self.files(), before) + shown = self.writer.view()["files"][0] + self.assertEqual((reason in shown["error"], shown["settings"]), (True, {}), shown) + + def test_a_poisoned_file_fails_only_its_job_and_the_unit_applies_the_next_and_rewrites_the_view(self): + second = self.base / "second" / ".claude" / "settings.json" + second.parent.mkdir(parents=True) + self.tree.write_text(json.dumps({"repos": [{"path": str(self.project), "known": True, "default": "main"}, {"path": str(second.parent.parent), "known": True, "default": "main"}]}), encoding="utf-8") + self.project_settings.write_text('{"model": "\\ud800"}\n', encoding="utf-8") + second.write_text('{"model": ' + "[" * 600 + "]" * 600 + "}\n", encoding="utf-8") + poisoned = {path: path.read_bytes() for path in (self.project_settings, second)} + jobs = {"0" * 16: self.item(self.project_settings, [ADD_LINT]), "1" * 16: self.item(second, [ADD_LINT]), "f" * 16: self.item(self.settings, [ADD_LINT])} + for job_id, item in jobs.items(): + self.write_job(job_id, [item], self.proof([item], self.signer)) + view = self.base / "state" / "view.json" + done = self.unit(view, {}) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertEqual(list(self.jobs.iterdir()), []) + found = [(self.result(job_id)["items"][0]["status"], self.result(job_id)["items"][0]["reason"]) for job_id in jobs] + self.assertEqual([status for status, _ in found], ["failed", "failed", "done"], found) + self.assertIn("lone surrogate", found[0][1]) + self.assertIn("nests deeper than 64", found[1][1]) + self.assertEqual({path: path.read_bytes() for path in poisoned}, poisoned) + self.assertEqual([sorted(child.name for child in path.parent.iterdir()) for path in poisoned], [["settings.json"], ["settings.json"]]) + self.assertIn("Bash(make lint:*)", json.loads(self.settings.read_text(encoding="utf-8"))["permissions"]["allow"]) + shown = {entry["path"]: entry for entry in json.loads(view.read_text(encoding="utf-8"))["files"]} + self.assertEqual(shown[str(self.settings)]["sha256"], sha(self.settings.read_bytes())) + self.assertIn("lone surrogate", shown[str(self.project_settings)]["error"]) + self.assertIn("nests deeper than 64", shown[str(second)]["error"]) + + def test_a_bad_tree_path_or_a_deep_tree_marks_only_itself_and_the_unit_still_applies_and_rewrites_the_view(self): + nul, surrogate = f"{self.base}/nul\x00dir", f"{self.base}/\ud800dir" + for name, text, bad, error in ( + ("NUL in a path", json.dumps({"repos": [{"path": nul, "known": True}]}), f"{nul}{signing.SETTINGS_FILE}", "unreadable: ValueError"), + ("lone surrogate in a path", json.dumps({"repos": [{"path": surrogate, "known": True}]}), f"{surrogate}{signing.SETTINGS_FILE}", "unreadable: UnicodeEncodeError"), + ("tree nested 100000 levels", '{"repos": ' + "[" * 100000 + "]" * 100000 + "}", None, None), + ): + with self.subTest(name=name): + self.tree.write_text(text, encoding="utf-8") + self.settings.write_text(json.dumps(ORIGINAL, indent=2) + "\n", encoding="utf-8") + item = self.item(self.settings, [ADD_LINT]) + job_id = secrets.token_hex(8) + self.write_job(job_id, [item], self.proof([item], self.signer)) + view = self.base / "state" / f"{job_id}.json" + done = self.unit(view, {}) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertEqual(self.result(job_id)["items"][0]["status"], "done") + shown = {entry["path"]: entry for entry in json.loads(view.read_text(encoding="utf-8"))["files"]} + self.assertEqual(sorted(shown), sorted([str(self.settings), *([bad] if bad else [])])) + self.assertEqual((shown[str(self.settings)]["sha256"], shown[str(self.settings)]["error"]), (sha(self.settings.read_bytes()), "")) + if bad: + self.assertEqual((shown[bad]["error"], shown[bad]["settings"]), (error, {})) + + def test_the_view_is_rewritten_even_when_a_result_cannot_be_written(self): + item = self.item(self.settings, [ADD_LINT]) + job_id = secrets.token_hex(8) + self.write_job(job_id, [item], self.proof([item], self.signer)) + self.results.chmod(0o500) + self.addCleanup(self.results.chmod, 0o700) + view = self.base / "state" / "view.json" + done = self.unit(view, {}) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertIn(f"no result for {job_id}.json", done.stderr) + self.assertEqual((list(self.jobs.iterdir()), list(self.results.iterdir())), ([], [])) + self.assertEqual(json.loads(view.read_text(encoding="utf-8"))["files"][0]["sha256"], sha(self.settings.read_bytes())) + + def test_a_view_that_cannot_be_written_ends_the_unit_with_one_line_and_keeps_the_results(self): + item = self.item(self.settings, [ADD_LINT]) + job_id = secrets.token_hex(8) + self.write_job(job_id, [item], self.proof([item], self.signer)) + view = self.base / "state" / "view.json" + view.mkdir(mode=0o700, parents=True) + done = self.unit(view, {}) + self.assertEqual((done.returncode, "Traceback" in done.stderr), (0, False), done.stderr) + self.assertIn("view not written", done.stderr) + self.assertEqual((self.result(job_id)["items"][0]["status"], view.is_dir()), ("done", True)) + + def test_a_directory_sync_failing_after_the_replace_reports_the_written_change(self): + real = os.fsync + + def failing(fd: int) -> None: + if stat.S_ISDIR(os.fstat(fd).st_mode): + raise OSError(errno.EIO, "Input/output error") + real(fd) + + with mock.patch.object(host.os, "fsync", side_effect=failing): + result = self.signed(self.item(self.settings, [ADD_LINT])) + self.assertEqual(result["status"], "done", result) + self.assertIn("written, but its directory sync failed: Input/output error", result["reason"]) + self.assertIn("Bash(make lint:*)", json.loads(self.settings.read_text(encoding="utf-8"))["permissions"]["allow"]) + self.assertEqual(result["sha256"], sha(self.settings.read_bytes())) + + def test_the_unit_makes_no_directory_and_writes_nothing_through_a_linked_spool(self): + victim = self.base / "victim" + victim.mkdir(mode=0o700) + spool = self.base / "spool" + spool.symlink_to(victim) + self.jobs, self.results = spool / "jobs", spool / "results" + done = self.unit(spool / "view.json", {}) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertIn("refused, nothing done", done.stderr) + self.assertIn("view not written", done.stderr) + self.assertEqual(list(victim.iterdir()), []) + + def test_a_view_in_a_linked_or_foreign_spool_is_refused_and_a_linked_view_file_replaced(self): + victim = self.base / "victim" + victim.mkdir(mode=0o700) + (victim / "view.json").write_text("victim", encoding="utf-8") + linked, shared, real = self.base / "linked", self.base / "shared", self.base / "real" + linked.symlink_to(victim) + shared.mkdir() + shared.chmod(0o770) + real.mkdir(mode=0o700) + (real / "view.json").symlink_to(victim / "view.json") + for folder in (linked, shared, real): + stderr = io.StringIO() + with contextlib.redirect_stderr(stderr): + host.write_view(folder / "view.json", self.writer.view()) + with self.subTest(folder=folder.name): + self.assertEqual("view not written" in stderr.getvalue(), folder != real, stderr.getvalue()) + self.assertEqual(([path.name for path in victim.iterdir()], (victim / "view.json").read_text(encoding="utf-8"), list(shared.iterdir())), (["view.json"], "victim", [])) + self.assertFalse((real / "view.json").is_symlink()) + self.assertEqual(json.loads((real / "view.json").read_text(encoding="utf-8"))["files"][0]["path"], str(self.settings)) + + def test_an_unexpected_crash_fails_only_its_job_or_item_and_names_only_its_kind(self): + apply, problem = signing.apply_changes, signing.settings_problem + crashes = iter([RecursionError("planted depth"), UnicodeEncodeError("utf-8", "planted text", 0, 1, "planted")]) + checked = [] + + def crashing_apply(settings: dict, changes: list[dict]) -> dict: + error = next(crashes, None) + if error is not None: + raise error + return apply(settings, changes) + + def crashing_problem(item: object) -> str | None: + checked.append(item) + if len(checked) == 1: + raise KeyError("planted key") + return problem(item) + + item = self.item(self.settings, [ADD_LINT]) + for job_id in ("0" * 16, "1" * 16, "2" * 16, "f" * 16): + self.write_job(job_id, [item], self.proof([item], self.signer)) + with mock.patch.object(host.signing, "apply_changes", crashing_apply), mock.patch.object(host.signing, "settings_problem", crashing_problem): + self.assertEqual(host.cycle(self.jobs, self.results, self.writer), 4) + self.assertEqual((self.result("0" * 16)["items"], self.result("0" * 16)["error"]), ([], "failed: KeyError; nothing changed")) + found = [(self.result(job_id)["items"][0]["status"], self.result(job_id)["items"][0]["reason"]) for job_id in ("1" * 16, "2" * 16, "f" * 16)] + self.assertEqual(found, [("failed", "RecursionError; the file kept its content"), ("failed", "UnicodeEncodeError; the file kept its content"), ("done", "")]) + self.assertEqual(json.loads(self.settings.read_text(encoding="utf-8"))["permissions"]["allow"].count("Bash(make lint:*)"), 1) + self.assertEqual(list(self.jobs.iterdir()), []) + + def test_a_non_finite_number_never_reaches_the_file_even_past_the_parser(self): + before = self.files() + with mock.patch.object(host, "parsed", return_value={"model": float("inf")}): + result = self.signed(self.item(self.settings, [ADD_LINT])) + self.assertEqual((result["status"], result["reason"]), ("failed", "ValueError; the file kept its content")) + self.assertEqual(self.files(), before) + + def test_a_planted_temporary_link_is_never_followed(self): + outside = self.base / "outside.json" + outside.write_text("{}\n", encoding="utf-8") + before = self.settings.read_bytes() + item = self.item(self.settings, [ADD_LINT]) + job_id = secrets.token_hex(8) + self.write_job(job_id, [item], self.proof([item], self.signer)) + fixed = "0" * 16 + (self.settings.parent / f".settings.json.{fixed}").symlink_to(outside) + with mock.patch.object(host.secrets, "token_hex", return_value=fixed): + self.assertEqual(host.cycle(self.jobs, self.results, self.writer), 1) + self.assertEqual(self.result(job_id)["items"][0]["status"], "failed") + self.assertEqual((self.settings.read_bytes(), self.settings.is_symlink(), outside.read_text(encoding="utf-8")), (before, False, "{}\n")) + + def test_a_file_of_another_uid_is_refused(self): + before = self.files() + item = self.item(self.settings, [ADD_LINT]) + proof = self.proof([item], self.signer) + with mock.patch.object(host.os, "getuid", return_value=os.getuid() + 1): + result = self.run_job([item], proof)["items"][0] + self.assertEqual(result["status"], "refused", result) + self.assertIn(f"belongs to uid {os.getuid()}", result["reason"]) + self.assertEqual(self.files(), before) + + def test_a_settings_path_that_is_no_regular_file_is_refused(self): + os.mkfifo(self.project_settings) + result = self.signed({"file": str(self.project_settings), "base": sha(b""), "changes": [ADD_LINT]}) + self.assertEqual(result["status"], "refused", result) + self.assertIn("no regular file", result["reason"]) + self.assertTrue(stat.S_ISFIFO(self.project_settings.lstat().st_mode)) + + def test_the_view_lists_the_fixed_files_with_only_permissions_and_sandbox(self): + view = self.writer.view() + self.assertEqual([entry["path"] for entry in view["files"]], [str(self.settings), str(self.project_settings)]) + own, project = view["files"] + self.assertEqual((own["exists"], own["sha256"], own["error"]), (True, sha(self.settings.read_bytes()), "")) + self.assertEqual(own["settings"], {"permissions": ORIGINAL["permissions"], "sandbox": ORIGINAL["sandbox"]}) + self.assertEqual((project["exists"], project["sha256"], project["settings"]), (False, "", {})) + self.assertNotIn("fixture-value-not-a-secret", json.dumps(view)) + + def test_a_missing_directory_shows_in_the_view_as_the_writer_handles_it(self): + self.project_settings.parent.rmdir() + shown = self.writer.view()["files"][1] + self.assertEqual((shown["exists"], shown["error"]), (False, host.MISSING_DIRECTORY)) + result = self.signed({"file": str(self.project_settings), "base": "", "changes": [ADD_LINT]}) + self.assertEqual((result["status"], self.project_settings.parent.exists()), ("refused", False), result) + self.project_settings.parent.mkdir() + self.assertEqual(self.writer.view()["files"][1]["error"], "") + self.assertEqual(self.signed({"file": str(self.project_settings), "base": "", "changes": [ADD_LINT]})["status"], "done") + + def test_the_unit_runs_once_outside_any_agent_and_refuses_inside_one(self): + item = self.item(self.settings, [ADD_LINT]) + before = self.settings.read_bytes() + job_id = secrets.token_hex(8) + self.write_job(job_id, [item], self.proof([item], self.signer)) + view = self.base / "state" / "view.json" + for marker in host.AGENT_MARKERS: + with self.subTest(marker=marker): + inside = self.unit(view, {marker: "1"}) + self.assertEqual(inside.returncode, 2, inside.stderr) + self.assertIn("refused", inside.stderr) + self.assertEqual((self.settings.read_bytes(), view.exists(), len(list(self.jobs.iterdir()))), (before, False, 1)) + done = self.unit(view, {}) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertEqual(list(self.jobs.iterdir()), []) + result = self.result(job_id) + self.assertEqual(result["items"][0]["status"], "done", result) + self.assertIn("Bash(make lint:*)", json.loads(self.settings.read_text(encoding="utf-8"))["permissions"]["allow"]) + self.assertEqual(((self.results / f"{job_id}.json").stat().st_mode & 0o777, view.stat().st_mode & 0o777), (0o600, 0o600)) + self.assertEqual(json.loads(view.read_text(encoding="utf-8"))["files"][0]["sha256"], sha(self.settings.read_bytes())) + + +class TestSettingsWriterSpoolLinks(spool_links.SpoolLinks, unittest.TestCase): + module = host + + def unit(self, trust: verifier.Trust, base: Path): + return host.Writer(trust, base / "home", base / "tree.json") + + +if __name__ == "__main__": + unittest.main()