#!/usr/bin/env python3 """R-VULN-03/04: the rendered page. Assertions run against the actual HTML output.""" import base64 import copy import hashlib import json import re import sys from datetime import datetime, timedelta, timezone from html.parser import HTMLParser from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE.parents[3] / "platform/vuln-scanner/src")) import page # noqa: E402 FAILED = [] NOW = datetime(2026, 11, 9, 12, 0, tzinfo=timezone.utc) REPORT = json.loads((HERE / "fixtures/report.json").read_text()) CLEAN_RE = re.compile(r"(?i)\bclean\b|no vulnerabilit") def check(name, condition): print(("ok " if condition else "FAIL ") + name) if not condition: FAILED.append(name) def render(report=REPORT, query="", now=NOW): status, headers, body = page.render(report, query, now) return status, headers, body.decode("utf-8") class Tags(HTMLParser): def __init__(self): super().__init__() self.tags = [] self.attrs = [] def handle_starttag(self, tag, attrs): self.tags.append(tag) self.attrs.append((tag, dict(attrs))) def tags(body): parser = Tags() parser.feed(body) return parser def covers_everything(body, report): """Every fixture CVE id, workload name and container name appears in the document.""" missing = [f["id"] for f in report["findings"] if f["id"] not in body] for target in report["targets"]: for workload in target["workloads"]: if workload["name"] not in body: missing.append(workload["name"]) missing.extend(c for c in workload["containers"] if c not in body) return missing def workload_view_covers(body, report): """Every workload row with its containers, and every CVE in the expansions.""" missing = covers_everything(body, report) return missing for view in ("cves", "workloads", "inventory"): status, headers, body = render(query="view=" + view) check(view + " view returns 200", status == 200) check(view + " view has the Content-Security-Policy with no script source", "script-src" not in headers["Content-Security-Policy"] and headers["Content-Security-Policy"].startswith("default-src 'none'")) check(view + " view sets nosniff", headers["X-Content-Type-Options"] == "nosniff") check(view + " view has no script element", "script" not in tags(body).tags) style_body = re.search(r"", body, re.S).group(1) expected = base64.b64encode(hashlib.sha256(style_body.encode()).digest()).decode() check(view + " view's CSP hash matches its inline style", "'sha256-" + expected + "'" in headers["Content-Security-Policy"]) check(view + " view header shows every state count", all(state + ": " in body for state in page.report_mod.STATES)) check(view + " view shows a coverage warning", "INCOMPLETE COVERAGE" in body) check(view + " view makes no clean claim with unscanned targets", not CLEAN_RE.search(body)) status, headers, cves = render(query="view=cves") check("CVE view contains every CVE", all(f["id"] in cves for f in REPORT["findings"])) affected = {f["targetId"] for f in REPORT["findings"]} expected_in_cves = {(t["id"], w["name"]) for t in REPORT["targets"] if t["id"] in affected for w in t["workloads"]} check("CVE view expansions name every workload of an affected target", all(name in cves for _, name in expected_in_cves)) check("CVE view expansions name every container of an affected target", all(c in cves for t in REPORT["targets"] if t["id"] in affected for w in t["workloads"] for c in w["containers"])) for name in ("web", "api", "app", "server"): check("CVE view expansion names " + name, name in cves) status, headers, workloads = render(query="view=workloads") check("workloads view contains every workload and container, including zero-finding ones", covers_everything(workloads, REPORT) == []) status, headers, inventory = render(query="view=inventory") check("inventory view contains every target", all(t["id"] in inventory for t in REPORT["targets"])) check("inventory view contains every workload and container", covers_everything(inventory, REPORT) == []) mutated = copy.deepcopy(REPORT) mutated["findings"] = [f for f in mutated["findings"] if f["id"] != "CVE-2026-1003"] check("mutation: the coverage evaluator catches a dropped CVE", "CVE-2026-1003" in covers_everything(render(mutated, "view=workloads")[2], REPORT)) _, _, body = render(query="view=cves") check("hostile title is escaped", "<script>alert(1)</script>" in body) check("hostile title is never live", "