Restrict public benchmark pages to generated HTML
next-site-candidate / validate-and-candidate (push) Canceled after 0s

This commit is contained in:
Alkane CI
2026-10-16 08:41:57 -06:00
parent 78f1f82b38
commit 334b993d52
2 changed files with 60 additions and 12 deletions
+27 -1
View File
@@ -37,7 +37,7 @@ class SiteValidationTests(unittest.TestCase):
PAGE + '<img src="https://example.com/a.png">GITEA_TOKEN'
)
errors = validate(self.root)
self.assertTrue(any("external or active reference" in error for error in errors))
self.assertTrue(any("forbidden tag: img" in error for error in errors))
self.assertTrue(any("credential marker" in error for error in errors))
def test_allows_only_pinned_source_commit_links(self):
@@ -63,6 +63,32 @@ class SiteValidationTests(unittest.TestCase):
(self.root / "os/index.html").write_text(PAGE + "<style>@import 'https://example.com/x.css';</style>")
self.assertTrue(any("external CSS reference" in error for error in validate(self.root)))
def test_rejects_css_escaped_import(self):
(self.root / "os/index.html").write_text(
PAGE + r"<style>@im\70ort 'https://example.com/x.css';</style>"
)
self.assertTrue(any("external CSS reference" in error for error in validate(self.root)))
def test_rejects_meta_refresh(self):
(self.root / "os/index.html").write_text(
PAGE + '<meta http-equiv="refresh" content="0;url=https://example.com/">'
)
self.assertTrue(any("unexpected meta directive" in error for error in validate(self.root)))
def test_rejects_base_and_unexpected_attributes(self):
(self.root / "os/index.html").write_text(
PAGE + '<base href="https://example.com/"><a href="../" ping="https://example.com/">Back</a>'
)
errors = validate(self.root)
self.assertTrue(any("forbidden tag: base" in error for error in errors))
self.assertTrue(any("forbidden attribute on a: ping" in error for error in errors))
def test_rejects_unlisted_local_link(self):
(self.root / "os/index.html").write_text(
PAGE + '<a href="../../private/">Source</a>'
)
self.assertTrue(any("external or active reference" in error for error in validate(self.root)))
def test_rejects_symlink(self):
(self.root / "private").symlink_to(self.root / "os/index.html")
self.assertTrue(any("contains a symlink" in error for error in validate(self.root)))
+33 -11
View File
@@ -8,7 +8,12 @@ import re
PAGES = ("index.html", "sdk/index.html", "os/index.html", "toolkit/index.html")
FORBIDDEN_TAGS = {"script", "iframe", "object", "embed", "form"}
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}"
@@ -23,9 +28,10 @@ class PageParser(HTMLParser):
self.tables = 0
self.links = set()
self.errors = []
self.in_style = False
def handle_starttag(self, tag, attrs):
if tag in FORBIDDEN_TAGS:
if tag not in ALLOWED_TAGS:
self.errors.append(f"forbidden tag: {tag}")
if tag == "title":
self.title = True
@@ -33,18 +39,34 @@ class PageParser(HTMLParser):
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 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:")
):
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}")
if name == "href":
self.links.add(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():