#!/usr/bin/env python3 """Validate the generated public benchmark pages before publication.""" import argparse from html.parser import HTMLParser from pathlib import Path import re PAGES = ("index.html", "sdk/index.html", "os/index.html", "toolkit/index.html") FORBIDDEN_TAGS = {"script", "iframe", "object", "embed", "form"} FORBIDDEN_TEXT = ("BEGIN PRIVATE KEY", "GITEA_TOKEN", "ghp_", "glpat-") COMMIT_LINK = re.compile( r"https://gitea\.bluefla\.re/blueflare/alkane-(?:sdk|os|toolkit)/commit/[0-9a-f]{40}" ) class PageParser(HTMLParser): def __init__(self): super().__init__(convert_charrefs=True) self.title = False self.heading = False self.tables = 0 self.links = set() self.errors = [] def handle_starttag(self, tag, attrs): if tag in FORBIDDEN_TAGS: self.errors.append(f"forbidden tag: {tag}") if tag == "title": self.title = True if tag == "h1": self.heading = True if tag == "table": self.tables += 1 for name, value in attrs: value = value or "" if name.startswith("on"): self.errors.append(f"event handler: {name}") if name in {"href", "src", "action"}: allowed_commit = name == "href" and COMMIT_LINK.fullmatch(value) if not allowed_commit and value.lower().startswith( ("http:", "https:", "//", "javascript:", "data:") ): self.errors.append(f"external or active reference: {value}") if name == "href": self.links.add(value) def validate_page(path: Path, relative: str) -> list[str]: if not path.is_file() or path.is_symlink(): return [f"{relative}: missing regular page"] if path.stat().st_size > 2_000_000: return [f"{relative}: page exceeds 2 MB"] text = path.read_text(encoding="utf-8") parser = PageParser() parser.feed(text) errors = [f"{relative}: {item}" for item in parser.errors] if not text.lstrip().lower().startswith(""): errors.append(f"{relative}: missing HTML doctype") if not parser.title or not parser.heading: errors.append(f"{relative}: missing title or heading") if any(token in text for token in FORBIDDEN_TEXT): errors.append(f"{relative}: credential marker in public output") if re.search(r"url\s*\(|@import", text, re.IGNORECASE): errors.append(f"{relative}: external CSS reference is not allowed") if relative == "index.html": if not {"sdk/", "os/", "toolkit/"}.issubset(parser.links): errors.append("index.html: missing project links") elif not parser.tables and "Waiting for a completed measured CI run." not in text: errors.append(f"{relative}: no measurements or pending notice") return errors def validate(root: Path) -> list[str]: if not root.is_dir() or root.is_symlink(): return ["site root is not a directory"] paths = {p.relative_to(root).as_posix() for p in root.rglob("*") if p.is_file()} errors = [] if any(p.is_symlink() for p in root.rglob("*")): errors.append("published site contains a symlink") for relative in PAGES: errors.extend(validate_page(root / relative, relative)) unexpected = paths - set(PAGES) if unexpected: errors.append("unexpected published files: " + ", ".join(sorted(unexpected))) return errors def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("site", type=Path) args = parser.parse_args() errors = validate(args.site) for error in errors: print(error) if errors: return 1 print(f"Validated {len(PAGES)} public benchmark pages") return 0 if __name__ == "__main__": raise SystemExit(main())