Files
skills/tests/test_page_prompt.py
T
Kevin Veen-BirkenbachandClaude Opus 5.5 9742884308 feat(orchestrator): firewall view with signed settings changes (001 S6)
Requirement 001 S6: the hub gets a Firewall view of the Claude Code permission and sandbox settings. The operator signs each change with the passkey; a host settings writer (systemd path unit) applies only exact, signed changes to allowlisted settings files through O_NOFOLLOW directory and file descriptors, and a timer refreshes the view. Every signed text and every page sink refuses or marks hidden characters: Cc, Cf, Zl, Zp, every space but U+0020 and the Default_Ignorable set, with U+FE0E/U+FE0F allowed only after one of the 359 non-ASCII emoji bases of Unicode 16 (shared table, validated at import, the same pair rule on the page). Removal of a deny, ask or denyRead entry is exempt only as an exact single remove. The permission hook prints the hub's refusal reason, cleaned of hidden characters. Accepted MEDUSA false positives (independent review, operator pick): 38 this round, mainly the Unicode defence tables read as attack patterns, generic MM-SCAN-058 heuristics, WEB-FLASK-004 at view.py:213 (ints from the validated table via tojson), EXF26-TOOL-004 on the non-secret WebAuthn challenge, PRIV-SCAN-060 on the office-action shape check and PQC007 on a picker key.

🎯 Fix: 95 %
💥 Break: 3 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: 100 %
🏆 Score: 92
🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 18:14:17 +00:00

107 lines
6.0 KiB
Python

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()