#!/usr/bin/env python3 """Adversarial tests for the packaged skill self-check (self_check.py).""" from __future__ import annotations import importlib.util import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent SELF_CHECK = ROOT / "skills/diagram-design/scripts/self_check.py" ASSET_DIR = ROOT / "skills/diagram-design/assets" TEMPLATE = ASSET_DIR / "template-motion.html" EXAMPLE = ASSET_DIR / "example-policy-trace-animated.html" STATIC_EXAMPLE = ASSET_DIR / "example-architecture.html" def load_self_check(): spec = importlib.util.spec_from_file_location("self_check", SELF_CHECK) assert spec is not None and spec.loader is not None module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def main() -> int: module = load_self_check() failures: list[str] = [] def check_pass(label: str, path: Path) -> None: errors = module.verify(path) if errors: failures.append(f"{label}: expected pass, got {errors}") else: print(f"OK: {label} passes") def check_fail(label: str, source: str, needle: str) -> None: with tempfile.TemporaryDirectory() as scratch: candidate = Path(scratch) / "candidate.html" candidate.write_text(source, encoding="utf-8") errors = module.verify(candidate) if not any(needle in error for error in errors): failures.append(f"{label}: expected an error containing {needle!r}, got {errors}") else: print(f"OK: {label} rejected") check_pass("shipped template", TEMPLATE) check_pass("shipped animated example", EXAMPLE) check_pass("shipped static example", STATIC_EXAMPLE) static = STATIC_EXAMPLE.read_text(encoding="utf-8") animated = EXAMPLE.read_text(encoding="utf-8") check_fail( "executable attribute", static.replace("", '', 1), "executable attribute", ) check_fail( "remote image", static.replace("", '', 1), "remote reference", ) check_fail( "lookalike fonts host", static.replace( "https://fonts.googleapis.com/css2", "https://fonts.googleapis.com.evil.tld/css2", 1, ), "approved Google Fonts", ) check_fail( "javascript URL", static.replace("", 'x', 1), "executable URL", ) check_fail( "data html URL", static.replace("", 'x', 1), "executable URL", ) check_fail( "iframe injection", static.replace("", "", 1), "not allowed", ) check_fail( "arbitrary script", static.replace("", "", 1), "canonical data-diagram-controls attribute", ) check_fail( "missing svg role", static.replace('role="img"', 'role="presentation"', 1), "role=img", ) check_fail( "modified controller", animated.replace("'motion-ready'", "'motion-ready' /* patched */", 1), "exactly match the controller", ) check_fail( "second script", animated.replace("", "", 1), "at most one script", ) check_fail( "hidden motion item", animated.replace( '", "
", 1).replace("", "
", 1), "