#!/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") ALLOWED_TAGS = {"html", "head", "meta", "title", "style", "body", "h1", "p", "a", "ul", "li", "div", "small", "table", "thead", "tbody", "tr", "th", "td"} ALLOWED_ATTRIBUTES = {"html": {"lang"}, "a": {"href"}, "div": {"class"}, "th": {"scope"}} LOCAL_LINKS = {"sdk/", "os/", "toolkit/", "../"} 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 = [] self.in_style = False def handle_starttag(self, tag, attrs): if tag not in ALLOWED_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 if tag == "style": self.in_style = True names = [name for name, _ in attrs] if len(names) != len(set(names)): self.errors.append(f"duplicate attribute on {tag}") if tag == "meta" and attrs not in ( [("charset", "utf-8")], [("name", "viewport"), ("content", "width=device-width, initial-scale=1")], ): self.errors.append("unexpected meta directive") for name, value in attrs: value = value or "" if tag != "meta" and name not in ALLOWED_ATTRIBUTES.get(tag, set()): self.errors.append(f"forbidden attribute on {tag}: {name}") if tag == "a" and name == "href": if value not in LOCAL_LINKS and not COMMIT_LINK.fullmatch(value): self.errors.append(f"external or active reference: {value}") self.links.add(value) def handle_endtag(self, tag): if tag == "style": self.in_style = False def handle_data(self, data): if self.in_style and (re.search(r"url\s*\(|@import", data, re.IGNORECASE) or "\\" in data): self.errors.append("external CSS reference is not allowed") 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) parser.add_argument("--page", choices=("sdk", "os", "toolkit"), help="validate one generated report before publication") args = parser.parse_args() errors = (validate_page(args.site, f"{args.page}/index.html") if args.page else validate(args.site)) for error in errors: print(error) if errors: return 1 print(f"Validated {args.page or len(PAGES)} public benchmark pages") return 0 if __name__ == "__main__": raise SystemExit(main())