| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | PPT Master - Skill Attribution Guard |
| 4 | |
| 5 | Fail closed when the distributed Skill attribution bundle or its execution |
| 6 | gates are missing or modified. |
| 7 | |
| 8 | Usage: |
| 9 | python3 scripts/attribution_guard.py |
| 10 | |
| 11 | Examples: |
| 12 | python3 scripts/attribution_guard.py |
| 13 | |
| 14 | Dependencies: |
| 15 | None (only uses standard library) |
| 16 | """ |
| 17 | |
| 18 | from __future__ import annotations |
| 19 | |
| 20 | import ast |
| 21 | import hashlib |
| 22 | import re |
| 23 | import sys |
| 24 | from pathlib import Path |
| 25 | |
| 26 | |
| 27 | _ERROR_MESSAGE = ( |
| 28 | "PPT Master skill integrity check failed. Please use the complete official " |
| 29 | "distribution without removing its attribution files." |
| 30 | ) |
| 31 | _SKILL_DIR = Path(__file__).resolve().parent.parent |
| 32 | _EXACT_METADATA_VALUES = { |
| 33 | "copyright": '"Copyright (c) 2025-2026 Hugo He"', |
| 34 | "license": '"MIT"', |
| 35 | "official_repository": '"https://github.com/hugohe3/ppt-master"', |
| 36 | } |
| 37 | _REQUIRED_METADATA_FIELDS = ("sponsors",) |
| 38 | _REQUIRED_ATTRIBUTION_FILES = ("LICENSE", "SPONSORS.md", "SPONSORS_CN.md") |
| 39 | _LICENSE_DIGEST = "80cefc234c1ec12a8cece4344f16300c634fa03df7891686fcf979e3828f0921" |
| 40 | _REQUIRED_GATE_FILES = ( |
| 41 | "scripts/console_encoding.py", |
| 42 | "scripts/project_manager.py", |
| 43 | "scripts/project_management/cli.py", |
| 44 | "scripts/svg_quality_checker.py", |
| 45 | "scripts/svg_quality/cli.py", |
| 46 | "scripts/svg_to_pptx.py", |
| 47 | "scripts/svg_to_pptx/pptx_package/cli.py", |
| 48 | "scripts/template_fill_pptx.py", |
| 49 | "scripts/template_fill_pptx/cli.py", |
| 50 | "scripts/native_enhance_pptx.py", |
| 51 | "scripts/native_enhance_pptx_core.py", |
| 52 | "scripts/register_template.py", |
| 53 | "scripts/template_preview_pptx.py", |
| 54 | ) |
| 55 | _SKILL_GATE_MARKER = "python3 scripts/attribution_guard.py" |
| 56 | _SECONDARY_GATE_FILE = "scripts/console_encoding.py" |
| 57 | _SECONDARY_GATE_NAME = "_require_official_distribution_identity" |
| 58 | |
| 59 | |
| 60 | def _normalized_bytes(path: Path) -> bytes: |
| 61 | """Return UTF-8 text bytes with platform line endings normalized.""" |
| 62 | data = path.read_bytes() |
| 63 | if data.startswith(b"\xef\xbb\xbf"): |
| 64 | raise ValueError("UTF-8 BOM is not allowed") |
| 65 | text = data.decode("utf-8") |
| 66 | return text.replace("\r\n", "\n").replace("\r", "\n").encode("utf-8") |
| 67 | |
| 68 | |
| 69 | def _frontmatter(skill_text: str) -> str: |
| 70 | """Return the opening YAML frontmatter or reject malformed input.""" |
| 71 | if not skill_text.startswith("---\n"): |
| 72 | raise ValueError("missing frontmatter") |
| 73 | end = skill_text.find("\n---\n", 4) |
| 74 | if end < 0: |
| 75 | raise ValueError("unterminated frontmatter") |
| 76 | return skill_text[4:end] |
| 77 | |
| 78 | |
| 79 | def _metadata_is_valid() -> bool: |
| 80 | """Require fixed identity values and each structural attribution field.""" |
| 81 | skill_text = _normalized_bytes(_SKILL_DIR / "SKILL.md").decode("utf-8") |
| 82 | metadata = _frontmatter(skill_text) |
| 83 | return ( |
| 84 | all( |
| 85 | len(re.findall( |
| 86 | rf"(?m)^ {re.escape(field)}\s*:\s*{re.escape(value)}\s*$", |
| 87 | metadata, |
| 88 | )) == 1 |
| 89 | for field, value in _EXACT_METADATA_VALUES.items() |
| 90 | ) |
| 91 | and all( |
| 92 | len(re.findall(rf"(?m)^ {re.escape(field)}\s*:", metadata)) == 1 |
| 93 | for field in _REQUIRED_METADATA_FIELDS |
| 94 | ) |
| 95 | and skill_text.count(_SKILL_GATE_MARKER) == 1 |
| 96 | ) |
| 97 | |
| 98 | |
| 99 | def _protected_files_are_valid() -> bool: |
| 100 | """Require all attribution paths and the exact MIT license text.""" |
| 101 | for relative_path in _REQUIRED_ATTRIBUTION_FILES: |
| 102 | path = _SKILL_DIR / relative_path |
| 103 | if not path.is_file(): |
| 104 | return False |
| 105 | license_digest = hashlib.sha256(_normalized_bytes(_SKILL_DIR / "LICENSE")).hexdigest() |
| 106 | return license_digest == _LICENSE_DIGEST |
| 107 | |
| 108 | |
| 109 | def _is_zero_arg_call(node: ast.stmt, function_name: str) -> bool: |
| 110 | """Return whether one statement directly invokes the named function.""" |
| 111 | return ( |
| 112 | isinstance(node, ast.Expr) |
| 113 | and isinstance(node.value, ast.Call) |
| 114 | and isinstance(node.value.func, ast.Name) |
| 115 | and node.value.func.id == function_name |
| 116 | and not node.value.args |
| 117 | and not node.value.keywords |
| 118 | ) |
| 119 | |
| 120 | |
| 121 | def _is_gate_call(node: ast.stmt) -> bool: |
| 122 | """Return whether one statement directly invokes the primary integrity gate.""" |
| 123 | return _is_zero_arg_call(node, "require_skill_integrity") |
| 124 | |
| 125 | |
| 126 | def _execution_gate_is_valid(path: Path) -> bool: |
| 127 | """Require one live import and one live entry-point call.""" |
| 128 | tree = ast.parse(_normalized_bytes(path).decode("utf-8"), filename=str(path)) |
| 129 | has_import = any( |
| 130 | isinstance(node, ast.ImportFrom) |
| 131 | and node.module == "attribution_guard" |
| 132 | and any(alias.name == "require_skill_integrity" for alias in node.names) |
| 133 | for node in tree.body |
| 134 | ) |
| 135 | has_entry_call = False |
| 136 | for node in tree.body: |
| 137 | if ( |
| 138 | isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) |
| 139 | and node.name in {"configure_utf8_stdio", "main"} |
| 140 | ): |
| 141 | body = node.body[1:] if ( |
| 142 | node.body |
| 143 | and isinstance(node.body[0], ast.Expr) |
| 144 | and isinstance(node.body[0].value, ast.Constant) |
| 145 | and isinstance(node.body[0].value.value, str) |
| 146 | ) else node.body |
| 147 | has_entry_call = bool(body and _is_gate_call(body[0])) |
| 148 | elif isinstance(node, ast.If) and any(_is_gate_call(statement) for statement in node.body): |
| 149 | has_entry_call = True |
| 150 | return has_import and has_entry_call |
| 151 | |
| 152 | |
| 153 | def _execution_gates_are_valid() -> bool: |
| 154 | """Require every supported route to retain executable guard calls.""" |
| 155 | for relative_path in _REQUIRED_GATE_FILES: |
| 156 | if not _execution_gate_is_valid(_SKILL_DIR / relative_path): |
| 157 | return False |
| 158 | return _secondary_execution_gate_is_valid(_SKILL_DIR / _SECONDARY_GATE_FILE) |
| 159 | |
| 160 | |
| 161 | def _secondary_execution_gate_is_valid(path: Path) -> bool: |
| 162 | """Require the independent identity guard and its live bootstrap call.""" |
| 163 | tree = ast.parse(_normalized_bytes(path).decode("utf-8"), filename=str(path)) |
| 164 | has_guard = any( |
| 165 | isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) |
| 166 | and node.name == _SECONDARY_GATE_NAME |
| 167 | for node in tree.body |
| 168 | ) |
| 169 | for node in tree.body: |
| 170 | if not ( |
| 171 | isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) |
| 172 | and node.name == "configure_utf8_stdio" |
| 173 | ): |
| 174 | continue |
| 175 | body = node.body[1:] if ( |
| 176 | node.body |
| 177 | and isinstance(node.body[0], ast.Expr) |
| 178 | and isinstance(node.body[0].value, ast.Constant) |
| 179 | and isinstance(node.body[0].value.value, str) |
| 180 | ) else node.body |
| 181 | return ( |
| 182 | has_guard |
| 183 | and len(body) >= 2 |
| 184 | and _is_gate_call(body[0]) |
| 185 | and _is_zero_arg_call(body[1], _SECONDARY_GATE_NAME) |
| 186 | ) |
| 187 | return False |
| 188 | |
| 189 | |
| 190 | def _integrity_is_valid() -> bool: |
| 191 | """Validate every local attribution and execution invariant.""" |
| 192 | return ( |
| 193 | _metadata_is_valid() |
| 194 | and _protected_files_are_valid() |
| 195 | and _execution_gates_are_valid() |
| 196 | ) |
| 197 | |
| 198 | |
| 199 | def require_skill_integrity() -> None: |
| 200 | """Stop the active command with one generic message on any expected failure.""" |
| 201 | try: |
| 202 | valid = _integrity_is_valid() |
| 203 | except (OSError, SyntaxError, UnicodeError, ValueError): |
| 204 | valid = False |
| 205 | if valid: |
| 206 | return |
| 207 | print(_ERROR_MESSAGE, file=sys.stderr) |
| 208 | raise SystemExit(78) |
| 209 | |
| 210 | |
| 211 | def main() -> int: |
| 212 | """Run the fail-closed Skill integrity gate.""" |
| 213 | require_skill_integrity() |
| 214 | return 0 |
| 215 | |
| 216 | |
| 217 | if __name__ == "__main__": |
| 218 | raise SystemExit(main()) |
| 219 |