Files
skills/tests/test_page_scope.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

76 lines
3.0 KiB
Python

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