From 275170910ccfb3ca1afd8ba227345672b8aff0a1 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 06:07:55 +0800 Subject: [PATCH 01/12] refactor(scripts): simplify site checker and remove CSS layout engine --- scripts/check_site.py | 2003 ++++++----------------------------------- scripts/test_site.py | 437 ++++----- 2 files changed, 421 insertions(+), 2019 deletions(-) diff --git a/scripts/check_site.py b/scripts/check_site.py index c06a0fb..a577b2a 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Check QuantStrategyLab website assets, DOM/CSS integrity, and identifier leaks. +"""Check QuantStrategyLab website assets, links, and identifier leaks. Usage: python scripts/check_site.py [--root PATH] [--phase {1,2}] [--advisory] [--visibility {public,private}] @@ -10,15 +10,14 @@ import argparse import base64 import fnmatch -import hashlib import html import ipaddress +import json import posixpath import re import struct import subprocess import sys -import unicodedata import urllib.parse import zlib from html.parser import HTMLParser @@ -31,22 +30,15 @@ ".idea", ".vscode", } -# Section 13 says "non-placeholder buckets" and section 7 line 104 says the -# placeholder form is ``, ``, `$PROJECTS_ROOT`. So a -# `gs://` URI whose bucket segment is a placeholder is the correct form, not a -# finding. `synthetic-` is on the list because the organisation's fixtures are -# named that way; without it every fixture a doc quotes becomes a finding the -# day the doc is written. +# Section 13 says "non-placeholder buckets" and section 7 says the placeholder +# form is ``, ``, `$PROJECTS_ROOT`. A `gs://` URI whose +# bucket segment is a placeholder is the correct form, not a finding. PLACEHOLDER_BUCKET = re.compile( r"^[<{$]|[<>{}]|\.\.\." r"|^(?:your|my|example|sample|placeholder|shared|synthetic)[-_]" r"|^bucket(?:-|$)" ) -# Section 13 asks for a placeholder wherever a deployed subdomain would appear, -# so `YOUR_SUBDOMAIN.workers.dev` is the correct form, not a finding. Matching -# the domain shape alone reports the guide's own advice as a leak, which is how -# a check earns the reputation that lets the real leak through. PLACEHOLDER_SUBDOMAIN = re.compile( r"^[<{$]|[<>{}]" r"|^(?:your|my|example|sample|placeholder)[-_]" @@ -62,36 +54,25 @@ class FileContext(NamedTuple): """Per-file material the leak predicates need to judge a match.""" url_spans: list[tuple[int, int]] - prose: str # the text with fenced blocks blanked out, offsets preserved + prose: str # text with fenced blocks blanked out, offsets preserved masked: str # `prose` with inline code blanked out too def phrase_is_prose(text: str, match: re.Match, ctx: FileContext) -> bool: - """A rule about phrasing describes prose; backticks name the phrase. - - The style guide writes `this round` when it means the words, and a value - rule such as `gs://` is the opposite case: a bucket inside backticks is - still a bucket. That is why this is a per-rule predicate and not a blanket - skip on code spans. - """ + """Return True when a prose-only rule matched outside code spans and fences.""" return bool(ctx.masked[match.start():match.end()].strip()) def bucket_is_real(text: str, match: re.Match, ctx: FileContext) -> bool: + """Return True when a `gs://` URI names a non-placeholder bucket.""" bucket = match.group(0)[len("gs://"):].split("/", 1)[0].lower() - # A sentence-final period is punctuation, not part of the name. An ellipsis - # is the placeholder, so it must survive this. if bucket.endswith(".") and not bucket.endswith("..."): bucket = bucket[:-1] return bool(bucket) and not PLACEHOLDER_BUCKET.search(bucket) def subdomain_is_real(text: str, match: re.Match, ctx: FileContext) -> bool: - """A placeholder subdomain is the form section 13 asks for, not a leak. - - What survives this predicate is a subdomain that was actually deployed, - including inside fenced command or config examples. - """ + """Return True when a `*.workers.dev` host is not a documented placeholder.""" subdomain = match.group(0)[: -len(".workers.dev")] return not PLACEHOLDER_SUBDOMAIN.search(subdomain.lower()) @@ -108,7 +89,7 @@ def subdomain_is_real(text: str, match: re.Match, ctx: FileContext) -> bool: def home_path_is_real(text: str, match: re.Match, ctx: FileContext) -> bool: - """Reject workstation home paths in prose and fenced examples while allowing placeholders.""" + """Reject workstation home paths while allowing placeholders and URL `/home/` segments.""" if any(start <= match.start() < end for start, end in ctx.url_spans): return False return not PLACEHOLDER_HOME_USER.search(match.group(0)) @@ -136,44 +117,23 @@ def windows_path_is_real(text: str, match: re.Match, ctx: FileContext) -> bool: def hex_is_prose(text: str, match: re.Match, ctx: FileContext) -> bool: - """Section 5 forbids 40-character hex *in prose* — CI head SHAs pasted in. - - A revision reference is a different thing and the guide asks for it: line - 377 writes `<40-char sha>` and the cross-repo links pin `blob//`. So - the three forms that carry a revision are exempt, and what is left is the - case the rule was written for. - """ + """Reject bare 40-char hex SHAs in prose while exempting links, code, and placeholders.""" if ctx.prose[match.start()] == " ": - return False # inside a fenced block: a contract example, not prose + return False for start, end in ctx.url_spans: if start <= match.start() < end: - return False # a link target + return False line_start = text.rfind("\n", 0, match.start()) + 1 line_end = text.find("\n", match.end()) line = text[line_start: line_end if line_end != -1 else len(text)] column = match.start() - line_start if line[:column].count("`") % 2 == 1: - return False # a quoted identifier + return False if text[match.end(): match.end() + 2] == "](": - return False # the label of a link whose target carries the same sha - return len(set(match.group(0))) != 1 # 000...0 and friends, a placeholder - - -# Section 13 grep list plus section 5 categories that are recognisable. -# The lookbehind keeps URL paths such as https://example.com/home/legal/ out of -# the results; "/home/" inside a URL is not a local path. -# -# Two categories, because they carry different weight. A real home path is -# evidence that something leaked from a workstation. The rest are the guide's -# own "prompt to check, not a certain failure" greps. -# -# The last column is a predicate deciding whether a match is a finding at all. -# Both heuristics in this list match far more than they mean: `gs://` matches -# `gs:///...`, which is the form section 13 asks for, and a 40-character -# hex string matches every commit SHA, which is how a cross-repository reference -# is supposed to be pinned. Without the predicates the two rules reported 522 -# findings across the organisation and not one of them was a defect, which is -# the same as having no rule: the report gets scrolled past. + return False + return len(set(match.group(0))) != 1 + + LEAK_PATTERNS = [ (re.compile(r"(?|]{2,}"), "paths", "absolute home path (section 13)", home_path_is_real), @@ -189,9 +149,6 @@ def hex_is_prose(text: str, match: re.Match, ctx: FileContext) -> bool: "publish", "personal subdomain (section 13)", subdomain_is_real), (re.compile(r"\b[0-9a-f]{40}\b"), "content", "40-character hex string in prose (section 5)", hex_is_prose), - # `| PR |` is a table header; `| PR #12 |` and `| #12 |` are the rows this - # rule is about. Matching the header too reported the guide's own row - # describing the rule. (re.compile(r"\|\s*(?:PR\s*#?\d+|#\d+)"), "content", "PR-number status table (section 5)", None), (re.compile(r"\b(this round|this task|in this task)\b", re.IGNORECASE), @@ -201,9 +158,6 @@ def hex_is_prose(text: str, match: re.Match, ctx: FileContext) -> bool: ] -# Section 6: paths for notes that must never be committed. The four wildcard -# entries are spelled with [Aa] character classes because .gitignore is -# case-sensitive on some platforms and `*audit*.md` alone lets `Audit.md` past. INTERNAL_NOTE_GITIGNORE = [ "*[Aa][Uu][Dd][Ii][Tt]*.md", "*[Hh][Aa][Nn][Dd][Oo][Ff][Ff]*.md", @@ -223,37 +177,21 @@ def hex_is_prose(text: str, match: re.Match, ctx: FileContext) -> bool: "*.internal.md", ] -# Directories whose contents are working material, never reader-facing docs. INTERNAL_NOTE_DIRS = re.compile( r"(^|/)(docs/plans|docs/handoffs|docs/superpowers|docs/audits|docs/changes)/" ) -DATED_TITLE = re.compile(r"(audit|acceptance).*20\d{2}-\d{2}-\d{2}", re.IGNORECASE) - -# Blocking categories per phase. -# -# Phase 1 blocks only what is unambiguous: a leaked local path, a link a reader -# cannot follow, a missing rules file, a .gitignore that has stopped keeping -# working notes out. Everything else is reported as NOTE. Phase 2 blocks the -# rest once the naming cleanup is done. README structure never blocks, because -# section 3 is a default and section 4 says so. PHASE_BLOCKING = { 1: {"paths", "links", "agents", "hygiene", "publish"}, 2: {"paths", "links", "agents", "hygiene", "publish", "content", "naming", "language", "docs"}, } -# Rules whose purpose is to stop an identifier being published. -# -# The style guide governs published documentation. In a private repository -# nothing is published, and a deployment document may need the real host name to -# be usable at all — an OAuth callback URL with a placeholder in it cannot be -# configured. So these report as notes there and block in public repositories, -# which is the case the rule was written for. The default stays "public", so -# forgetting the flag cannot switch a leak rule off by accident. PUBLICATION_CATEGORIES = {"paths", "publish"} class Report: + """Collect categorized findings and separate blocking vs advisory items.""" + def __init__(self, blocking: set[str]) -> None: self.blocking = blocking self.findings: list[tuple[str, str]] = [] @@ -312,16 +250,17 @@ def is_untracked_gitignored(root: Path, rel_path: str) -> bool: def should_skip_path(root: Path, path: Path) -> bool: + """Skip tooling directories outside site/; Pages publishes the entire site/ tree.""" rel_parts = path.relative_to(root).parts if ".git" in rel_parts: return True - # Pages publishes the entire site/ directory, including site/dist or site/build. if rel_parts and rel_parts[0] == "site": return False return any(part in SKIP_DIRS for part in rel_parts) def iter_files(root: Path): + """Yield repository files subject to scanning.""" visible = git_visible_files(root) if visible is not None: for path in sorted(visible): @@ -340,28 +279,16 @@ def is_markdown(path: Path) -> bool: return path.suffix.lower() in {".md", ".markdown"} - - CODE_SPAN = re.compile(r"`[^`\n]+`") def blank_code_spans(text: str) -> str: - """Replace inline code with spaces, keeping offsets. - - A word in backticks is a mention: the style guide writes `robust` when it - means the word, and a research note writes a commit id as a value. Neither - is prose, and both were reported as prose. - """ + """Replace inline code with spaces while preserving character offsets.""" return CODE_SPAN.sub(lambda m: " " * len(m.group(0)), text) def blank_fenced_blocks(text: str) -> str: - """Replace fenced blocks with spaces, keeping every offset in place. - - `hex_is_prose` needs to know whether a match sits in a contract example or - in running text, and it needs the character offsets to stay usable for the - line and backtick lookups that follow. - """ + """Replace fenced code blocks with spaces while preserving character offsets.""" def blank(match: re.Match) -> str: return re.sub(r"[^\n]", " ", match.group(0)) @@ -369,56 +296,28 @@ def blank(match: re.Match) -> str: PNG_MAGIC = b"\x89PNG\r\n\x1a\n" -PNG_EXTRA_IMAGE_CHUNKS = { +PNG_ALLOWED_ANCILLARY_CHUNKS = { b"PLTE", b"tRNS", b"gAMA", b"cHRM", b"sRGB", - b"sBIT", b"bKGD", b"hIST", b"pHYs", b"tIME", + b"sBIT", b"bKGD", b"hIST", b"pHYs", } -PNG_CHANNELS = {0: 1, 2: 3, 4: 2, 6: 4} - -APPROVED_SITE_RASTERS: dict[str, list[tuple[int, int, int, int, int, str]]] = { - "site/favicon.ico": [ - (16, 16, 8, 6, 0, "fba8bde649a5423b2934c86a8705d476694fb712fb8da98a80496ccb715889a7"), - (32, 32, 8, 6, 0, "9e27f396de07f6e3052d6a197ae10408596f79dba02a70e67c929e080e8df292"), - (48, 48, 8, 6, 0, "1786525e5efbafbd75de650f5a2a53cfdc3fe39ff2d388eba49f87cb892908c2"), - ], - "site/landing-light-192.png": [ - (192, 192, 8, 6, 0, "26aa89b6369a45ace2c47db94d2eee28ed812caa92bc591fd59782e1aedf6124"), - ], - "site/landing-light-512.png": [ - (512, 512, 8, 6, 0, "bdaabe227116fc88c75bde2110a00e1e6a64448a853137cdc512c4d2ebc1251a"), - ], - "site/landing-light-apple-touch-180.png": [ - (180, 180, 8, 6, 0, "2db31b1e3ce7507ed4e9323d948d2bbc714d4033c6fd40c8cefbf995a6f053d5"), - ], - "site/landing-light-maskable-512.png": [ - (512, 512, 8, 6, 0, "61aa86a2070e1187bd23e501a7df0fb8152dbba1024e36db63d4f50806cde018"), - ], +PNG_TEXT_CHUNKS = {b"tEXt", b"zTXt", b"iTXt"} +PNG_CHANNELS = {0: 1, 2: 3, 3: 1, 4: 2, 6: 4} +PNG_VALID_BIT_DEPTHS = { + 0: {1, 2, 4, 8, 16}, + 2: {8, 16}, + 3: {1, 2, 4, 8}, + 4: {8, 16}, + 6: {8, 16}, } -SITE_SCRIPT_SUFFIXES = {".js", ".mjs", ".cjs"} -APPROVED_SITE_SCRIPT_FILES: dict[str, str] = { - "site/assets/app.js": "a38c89a342115396fc79960771a77eb63e314dc6874fdcad46529dead36a6408", - "site/assets/i18n.js": "4fdd39f086eed30377f5e2ff0c620a78d6f09d5bb63f785121db06c5685ebd9d", -} -APPROVED_SITE_INLINE_SCRIPTS: set[str] = { - "df216a14d05020b1d67c017b9252c0390dcfd70f727e188f42d8b92ac557637f", -} -APPROVED_SITE_VECTOR_FILES: dict[str, str] = { - "site/landing-light.svg": "82688b5a7a13ae562efffc5b3cebf9060ddf8d8c95a593441ada3cd5e3a92c85", -} -APPROVED_SITE_INLINE_SVGS: set[str] = { - "d2148efc230494e3ed92cfc84f356d3cbe3a2436e60a125640e981106d518c07", - "63ceec9a23ee08da79fcc3022b56d4908a34d0268982d1d5596892d09af3bb47", - "e89ab06f8dca3c8bfb330fa5f17eb3ce8f6c1efc0940b79ee691e66948c3aa70", -} ALLOWED_SITE_TEXT_SUFFIXES: set[str] = { - ".html", ".htm", ".css", ".json", ".webmanifest", - ".yaml", ".yml", ".toml", ".txt", ".md", ".map", + ".html", ".htm", ".xhtml", ".css", ".js", ".mjs", ".cjs", ".svg", ".xml", + ".json", ".webmanifest", ".yaml", ".yml", ".toml", ".txt", ".md", ".map", } def resolve_site_asset_href(page_rel: str, href: str) -> str | None: - """Resolve a site/-local HTML href against page_rel; return None for external or escaping targets.""" + """Resolve a site/-local HTML/CSS href against page_rel; return None for external or escaping targets.""" raw = href.strip() if not raw or raw.startswith("//") or "\\" in raw: return None @@ -437,55 +336,6 @@ def resolve_site_asset_href(page_rel: str, href: str) -> str | None: return "site/" + rel_in_site -def unfilter_png_scanlines( - decomp: bytes, width: int, height: int, bit_depth: int, color_type: int -) -> bytes | None: - """Reconstruct unfiltered raw pixel bytes for non-interlaced 8/16-bit PNGs.""" - channels = PNG_CHANNELS.get(color_type) - if channels is None or bit_depth not in (8, 16) or width < 1 or height < 1: - return None - bpp = channels * (bit_depth // 8) - stride = width * bpp - if len(decomp) != (stride + 1) * height: - return None - out = bytearray(stride * height) - for y in range(height): - row_start = y * (stride + 1) - ftype = decomp[row_start] - row = decomp[row_start + 1:row_start + 1 + stride] - base = y * stride - if ftype == 0: - out[base:base + stride] = row - elif ftype == 1: - for i in range(stride): - a = out[base + i - bpp] if i >= bpp else 0 - out[base + i] = (row[i] + a) & 0xFF - elif ftype == 2: - prev = base - stride - for i in range(stride): - b = out[prev + i] if y > 0 else 0 - out[base + i] = (row[i] + b) & 0xFF - elif ftype == 3: - prev = base - stride - for i in range(stride): - a = out[base + i - bpp] if i >= bpp else 0 - b = out[prev + i] if y > 0 else 0 - out[base + i] = (row[i] + ((a + b) >> 1)) & 0xFF - elif ftype == 4: - prev = base - stride - for i in range(stride): - a = out[base + i - bpp] if i >= bpp else 0 - b = out[prev + i] if y > 0 else 0 - c = out[prev + i - bpp] if (y > 0 and i >= bpp) else 0 - p = a + b - c - pa, pb, pc = abs(p - a), abs(p - b), abs(p - c) - pr = a if (pa <= pb and pa <= pc) else (b if pb <= pc else c) - out[base + i] = (row[i] + pr) & 0xFF - else: - return None - return bytes(out) - - def _zlib_decompress_exact(data: bytes) -> bytes | None: """Decompress a zlib stream and fail closed on truncation or trailing bytes.""" zobj = zlib.decompressobj() @@ -498,16 +348,53 @@ def _zlib_decompress_exact(data: bytes) -> bytes | None: return out -def inspect_png_asset( - raw: bytes, -) -> tuple[str, tuple[int, int, int, int, int, str], bool] | None: - """Validate PNG structure/CRCs/scanlines and return (metadata_text, sig, clean_chunks).""" +def _decode_png_text_chunk(ctype: bytes, cdata: bytes) -> list[str] | None: + """Decode a PNG tEXt/zTXt/iTXt chunk into text parts, or None if malformed.""" + if ctype == b"tEXt": + if b"\x00" not in cdata: + return None + return [cdata.replace(b"\x00", b"\n").decode("latin-1")] + if ctype == b"zTXt": + sep = cdata.find(b"\x00") + if sep == -1 or sep + 2 > len(cdata) or cdata[sep + 1] != 0: + return None + zraw = _zlib_decompress_exact(cdata[sep + 2:]) + if zraw is None: + return None + return [cdata[:sep].decode("latin-1"), zraw.decode("latin-1")] + sep1 = cdata.find(b"\x00") + if sep1 == -1 or sep1 + 3 > len(cdata): + return None + comp_flag, comp_method = cdata[sep1 + 1], cdata[sep1 + 2] + rest_parts = cdata[sep1 + 3:].split(b"\x00", 2) + if len(rest_parts) < 3: + return None + text_bytes = rest_parts[2] + if comp_flag == 1: + if comp_method != 0: + return None + text_bytes = _zlib_decompress_exact(text_bytes) + if text_bytes is None: + return None + elif comp_flag != 0: + return None + try: + return [cdata[:sep1].decode("utf-8"), text_bytes.decode("utf-8")] + except UnicodeDecodeError: + return None + + +def inspect_png_asset(raw: bytes) -> tuple[str, bool] | None: + """Validate PNG magic, chunk CRCs, IHDR, IDAT zlib stream, IEND, and ancillary text chunks. + + Returns (extracted_metadata_text, has_text_metadata), or None if malformed. + """ if not raw.startswith(PNG_MAGIC): return None pos = len(PNG_MAGIC) ihdr: tuple[int, int, int, int, int] | None = None seen_iend = False - extra_image_chunks = False + has_text_meta = False idat_parts: list[bytes] = [] parts: list[str] = [] while pos + 8 <= len(raw): @@ -525,7 +412,16 @@ def inspect_png_asset( if ctype != b"IHDR" or len(cdata) != 13: return None width, height, bit_depth, color_type, comp, flt, interlace = struct.unpack(">IIBBBBB", cdata) - if width < 1 or height < 1 or comp != 0 or flt != 0 or interlace != 0: + valid_depths = PNG_VALID_BIT_DEPTHS.get(color_type) + if ( + width < 1 + or height < 1 + or valid_depths is None + or bit_depth not in valid_depths + or comp != 0 + or flt != 0 + or interlace not in (0, 1) + ): return None ihdr = (width, height, bit_depth, color_type, interlace) continue @@ -539,63 +435,31 @@ def inspect_png_asset( if ctype == b"IDAT": idat_parts.append(cdata) continue - if ctype in PNG_EXTRA_IMAGE_CHUNKS: - extra_image_chunks = True + if ctype in PNG_ALLOWED_ANCILLARY_CHUNKS: continue - if ctype == b"tEXt": - if b"\x00" not in cdata: - return None - parts.append(cdata.replace(b"\x00", b"\n").decode("latin-1")) - elif ctype == b"zTXt": - sep = cdata.find(b"\x00") - if sep == -1 or sep + 2 > len(cdata) or cdata[sep + 1] != 0: - return None - parts.append(cdata[:sep].decode("latin-1")) - zraw = _zlib_decompress_exact(cdata[sep + 2:]) - if zraw is None: - return None - parts.append(zraw.decode("latin-1")) - elif ctype == b"iTXt": - sep1 = cdata.find(b"\x00") - if sep1 == -1 or sep1 + 3 > len(cdata): + if ctype in PNG_TEXT_CHUNKS: + has_text_meta = True + decoded = _decode_png_text_chunk(ctype, cdata) + if decoded is None: return None - comp_flag, comp_method = cdata[sep1 + 1], cdata[sep1 + 2] - rest_parts = cdata[sep1 + 3:].split(b"\x00", 2) - if len(rest_parts) < 3: - return None - text_bytes = rest_parts[2] - if comp_flag == 1: - if comp_method != 0: - return None - text_bytes = _zlib_decompress_exact(text_bytes) - if text_bytes is None: - return None - elif comp_flag != 0: - return None - try: - parts.append(cdata[:sep1].decode("utf-8")) - parts.append(text_bytes.decode("utf-8")) - except UnicodeDecodeError: - return None - else: - return None + parts.extend(decoded) + continue + return None if ihdr is None or not seen_iend or pos != len(raw) or not idat_parts: return None decomp = _zlib_decompress_exact(b"".join(idat_parts)) if decomp is None: return None width, height, bit_depth, color_type, interlace = ihdr - pixels = unfilter_png_scanlines(decomp, width, height, bit_depth, color_type) - if pixels is None: + row_bytes = (width * PNG_CHANNELS[color_type] * bit_depth + 7) // 8 + if interlace == 0 and len(decomp) != (row_bytes + 1) * height: return None - sig = (width, height, bit_depth, color_type, interlace, hashlib.sha256(pixels).hexdigest()) - clean_chunks = not extra_image_chunks and not parts - return "\n".join(parts), sig, clean_chunks + if interlace == 1 and not decomp: + return None + return "\n".join(parts), has_text_meta -def inspect_ico_asset( - raw: bytes, -) -> tuple[str, list[tuple[int, int, int, int, int, str]], bool] | None: +def inspect_ico_asset(raw: bytes) -> tuple[str, bool] | None: """Validate ICO directory structure and embedded PNG frames.""" if len(raw) < 6: return None @@ -615,8 +479,7 @@ def inspect_ico_asset( spans.sort() cursor = dir_end parts: list[str] = [] - sigs: list[tuple[int, int, int, int, int, str]] = [] - clean_all = True + has_text_meta = False for start, end in spans: if start != cursor: return None @@ -627,14 +490,13 @@ def inspect_ico_asset( png_info = inspect_png_asset(entry) if png_info is None: return None - png_txt, sig, png_clean = png_info - sigs.append(sig) - clean_all = clean_all and png_clean + png_txt, png_meta = png_info + has_text_meta = has_text_meta or png_meta if png_txt: parts.append(png_txt) if cursor != len(raw): return None - return "\n".join(parts), sigs, clean_all + return "\n".join(parts), has_text_meta def _decode_site_text(raw: bytes) -> tuple[str, str | None]: @@ -652,7 +514,7 @@ def _decode_site_text(raw: bytes) -> tuple[str, str | None]: def read_deployed_site_asset(root: Path, path: Path) -> tuple[str, str | None]: - """Return ('text', str), ('unapproved_raster', str), ('unapproved_vector', str), ('unapproved_script', str), ('skip', None), or ('unscanned_binary', None).""" + """Return ('text', str), ('png_metadata', str), ('skip', None), or ('unscanned_binary', None).""" rel = path.relative_to(root).as_posix() if not rel.startswith("site/"): return "skip", None @@ -662,37 +524,16 @@ def read_deployed_site_asset(root: Path, path: Path) -> tuple[str, str | None]: inspected = inspect_png_asset(raw) if inspected is None: return "unscanned_binary", None - text, sig, clean_chunks = inspected - if clean_chunks and [sig] == APPROVED_SITE_RASTERS.get(rel): - return "text", text - return "unapproved_raster", text + text, has_text_meta = inspected + return ("png_metadata", text) if has_text_meta else ("text", text) if suffix == ".ico": inspected = inspect_ico_asset(raw) if inspected is None: return "unscanned_binary", None - text, sigs, clean_chunks = inspected - if clean_chunks and sigs == APPROVED_SITE_RASTERS.get(rel): - return "text", text - return "unapproved_raster", text - if suffix == ".svg": - text_status, text = _decode_site_text(raw) - if text_status != "text" or text is None: - return "unscanned_binary", None - if hashlib.sha256(raw).hexdigest() == APPROVED_SITE_VECTOR_FILES.get(rel): - return "text", text - return "unapproved_vector", text - if suffix in SITE_SCRIPT_SUFFIXES: - text_status, text = _decode_site_text(raw) - if text_status != "text" or text is None: - return "unscanned_binary", None - if hashlib.sha256(raw).hexdigest() == APPROVED_SITE_SCRIPT_FILES.get(rel): - return "text", text - return "unapproved_script", text + text, has_text_meta = inspected + return ("png_metadata", text) if has_text_meta else ("text", text) if suffix in ALLOWED_SITE_TEXT_SUFFIXES: - text_status, text = _decode_site_text(raw) - if text_status != "text" or text is None: - return "unscanned_binary", None - return "text", text + return _decode_site_text(raw) return "unscanned_binary", None @@ -767,7 +608,6 @@ def account_field_is_real(text: str, match: re.Match, ctx: FileContext) -> bool: ) CSS_ESCAPE_RE = re.compile(r"\\([0-9a-fA-F]{1,6})(?:\r\n|[ \t\r\n\f])?") LINE_CONTINUATION_RE = re.compile(r"\\(?:\r\n|[\r\n\f])") -INLINE_DISPLAY_RE = re.compile(r"(?i)\bdisplay\s*:\s*(?:inline(?:-[a-z-]+)?|contents)\b") HTML_BLOCK_TAGS = { "address", "article", "aside", "blockquote", "body", "br", "caption", "dd", "details", "dialog", "div", "dl", "dt", "fieldset", "figcaption", "figure", @@ -777,68 +617,6 @@ def account_field_is_real(text: str, match: re.Match, ctx: FileContext) -> bool: "summary", "table", "tbody", "td", "template", "tfoot", "th", "thead", "title", "tr", "ul", "text", } -HTML_VOID_TAGS = { - "area", "base", "br", "col", "embed", "hr", "img", "input", - "link", "meta", "param", "source", "track", "wbr", -} -ALLOWED_SITE_HTML_TAGS = { - "a", "article", "aside", "b", "blockquote", "body", "br", "button", "caption", - "circle", "code", "dd", "div", "dl", "dt", "em", "footer", "h1", "h2", "h3", - "h4", "h5", "h6", "head", "header", "hr", "html", "i", "img", "li", "link", - "main", "meta", "nav", "ol", "p", "path", "pre", "rect", "s", "script", - "section", "small", "span", "strong", "style", "sub", "sup", "svg", - "table", "tbody", "td", "tfoot", "th", "thead", "time", "title", "tr", "u", "ul", "wbr", -} -SIMPLE_COMPOUND_RE = re.compile( - r"^(?:[a-zA-Z][a-zA-Z0-9-]*)?(?:(?:#[a-zA-Z0-9_-]+)|(?:\.[a-zA-Z0-9_-]+)|(?:\[[a-zA-Z0-9_-]+(?:=[a-zA-Z0-9_\"'-]+)?\]))*$" -) -DISALLOWED_SITE_CSS_RE = [ - (re.compile(r"(?i)@import\b"), "CSS @import external stylesheet"), - (re.compile(r"(?i)\burl\s*\("), "CSS url() resource reference"), - (re.compile(r"(?i)\bflex-direction\s*:\s*(?:row|column)-reverse\b"), "reverse flex visual ordering"), - (re.compile(r"(?i)\bflex-wrap\s*:\s*wrap-reverse\b"), "reverse flex-wrap visual ordering"), - (re.compile(r"(?i)(?]*\bhttp-equiv\b"), "HTML meta http-equiv element in site/"), - ( - re.compile( - r"(?i)<\s*(?:link\b[^>]*rel\s*=\s*[\"']?[^>]*\bstylesheet\b[^>]*\bhref|script\b[^>]*\bsrc)" - r"\s*=\s*[\"']?(?:https?:|//|data:|javascript:|vbscript:)" - ), - "remote or inline-scheme stylesheet or script in site/", - ), - ( - re.compile(r"(?i)<\s*(?:details|dialog|select|option|textarea|input|canvas|audio|video|marquee|math)\b"), - "unmodeled interactive or collapsible element in site/", - ), - ( - re.compile( - r"(?i)<\s*(?:animate|animateMotion|animateTransform|set|discard|use|switch|clipPath|mask|filter|pattern|marker)\b" - ), - "SVG animation, reference, or masking element in site/", - ), - (re.compile(r"(?i)<\s*(?:script|style|title)\b[^>]*/>"), "self-closing script or style tag in site/"), - ( - re.compile( - r"(?i)<\s*(?:img|source)\b[^>]*\b(?:src|srcset)\s*=\s*[\"']?\s*(?:https?:|//|data:|javascript:|vbscript:)" - ), - "remote or inline-scheme image source in site/", - ), -] def unescape_js_literals(text: str) -> str: @@ -913,896 +691,12 @@ def fold_js_expressions(text: str) -> str: return "\n".join([folded, *extras]) if extras else folded -def _is_safe_grid_track(tok: str) -> bool: - """Return True if a CSS grid track sizing token cannot collapse content to zero width.""" - t = tok.strip().lower() - if t in ("auto", "min-content", "max-content"): - return True - if t.startswith("minmax(") and t.endswith(")"): - inner = t[7:-1] - depth = 0 - comma_idx = -1 - for i, ch in enumerate(inner): - if ch == "(": - depth += 1 - elif ch == ")": - depth = max(0, depth - 1) - elif ch == "," and depth == 0: - comma_idx = i - break - if comma_idx == -1: - return False - min_t = inner[:comma_idx].strip() - max_t = inner[comma_idx + 1:].strip() - min_ok = min_t in ("0", "0px", "auto", "min-content", "max-content") or _is_safe_grid_track(min_t) - return min_ok and _is_safe_grid_track(max_t) - m = re.match(r"^(\d+(?:\.\d+)?)(fr|px|em|rem|ch|%)$", t) - if not m: - return False - num, unit = float(m.group(1)), m.group(2) - min_val = 0.25 if unit in ("fr", "em", "rem", "ch") else 2.0 - return num >= min_val +class _SiteHTMLParser(HTMLParser): + """Extract text, attributes, comments, scripts, and local asset links from HTML/SVG.""" - -def count_grid_columns(val: str) -> int | None: - """Count static tracks in a CSS grid-template-columns declaration, or -1 if dynamic or zero-width.""" - v = val.strip().lower() - if v in ("none", "auto"): - return None - parts: list[str] = [] - depth = 0 - cur: list[str] = [] - for ch in v: - if ch == "(": - depth += 1 - cur.append(ch) - elif ch == ")": - depth = max(0, depth - 1) - cur.append(ch) - elif ch.isspace() and depth == 0: - if cur: - parts.append("".join(cur)) - cur = [] - else: - cur.append(ch) - if cur: - parts.append("".join(cur)) - total = 0 - for tok in parts: - if tok.startswith("[") and tok.endswith("]"): - continue - m = re.match(r"^repeat\(\s*(\d+)\s*,\s*(.+)\)$", tok) - if m: - sub = count_grid_columns(m.group(2)) - if int(m.group(1)) < 1 or not sub or sub < 1: - return -1 - total += int(m.group(1)) * sub - elif tok.startswith("repeat(") or "subgrid" in tok or not _is_safe_grid_track(tok): - return -1 - else: - total += 1 - return total if total >= 1 else -1 - - -ALLOWED_SITE_CSS_PROPS = { - "-webkit-font-smoothing", - "align-items", "align-self", "animation", - "background", "background-color", - "border", "border-bottom", "border-color", "border-left", "border-radius", "border-right", "border-top", - "box-sizing", "clip", "color", "content", "counter-increment", "counter-reset", "cursor", - "display", "fill", "flex", "flex-direction", "flex-wrap", - "font", "font-family", "font-size", "font-style", "font-weight", - "gap", "row-gap", "column-gap", - "grid-column", "grid-column-start", "grid-row", "grid-row-start", "grid-template-columns", - "height", "justify-content", "letter-spacing", "line-height", "list-style", - "margin", "margin-bottom", "margin-left", "margin-right", "margin-top", - "max-height", "max-width", "min-height", "min-width", - "opacity", "order", "outline", "outline-offset", "overflow", - "padding", "padding-bottom", "padding-left", "padding-right", "padding-top", - "position", "stroke", "stroke-linecap", "stroke-linejoin", "stroke-width", - "text-align", "text-decoration", "text-decoration-thickness", "text-rendering", - "text-transform", "text-underline-offset", "transition", "visibility", "white-space", "width", -} -TRANSPARENT_COLOR_RE = re.compile( - r"(?i)\btransparent\b|color-mix\(" - r"|rgba?\([^)]*[,/]\s*0(?:\.0+)?%?\s*\)" - r"|hsla?\([^)]*[,/]\s*0(?:\.0+)?%?\s*\)" - r"|#(?:[0-9a-f]{3}0|[0-9a-f]{6}00)\b" -) -CSS_DYNAMIC_FUNC_RE = re.compile(r"(?i)\b(?:var|calc|min|max|clamp)\(") - - -def parse_style_decls( - decl_text: str, allow_zero_scrollbar: bool = False, prelude_sel: str = "" -) -> tuple[dict[str, tuple[object, bool]], list[str]]: - """Parse CSS declarations relevant to visual layout, ordering, and visibility.""" - props: dict[str, tuple[object, bool]] = {} - invalid: list[str] = [] - raw_decls: dict[str, str] = {} - raw_imp: dict[str, bool] = {} - small_box_dims: list[str] = [] - neg_margins: list[str] = [] - - def _set_prop(key: str, val: object, is_imp: bool) -> None: - prev = props.get(key) - if prev is not None and prev[1] and not is_imp: - return - props[key] = (val, is_imp) - - clean = CSS_COMMENT_RE.sub("", unescape_css_literals(decl_text)) - if "{" in clean or "}" in clean: - invalid.append("nested CSS block inside declarations") - return props, invalid - sel_norm = re.sub(r"\s+", " ", prelude_sel.strip().lower()) - for part in clean.split(";"): - if ":" not in part: - continue - k, raw_v = part.split(":", 1) - k = k.strip().lower() - imp = bool(re.search(r"(?i)!important\s*$", raw_v)) - v = re.sub(r"(?i)\s*!important\s*$", "", raw_v.strip()).strip() - vl = v.lower() - if k.startswith("--") and sel_norm not in {":root", "[data-theme=light]", "[data-theme=dark]"}: - invalid.append(f"scoped CSS custom property override ({k})") - continue - if not k.startswith("--") and k not in ALLOWED_SITE_CSS_PROPS: - invalid.append(f"unmodeled CSS property ({k})") - continue - if (k.startswith("--") or k in ("color", "fill")) and TRANSPARENT_COLOR_RE.search(v): - invalid.append(f"transparent color hiding ({k})") - if imp or not raw_imp.get(k, False): - raw_decls[k] = v - raw_imp[k] = imp - if k.startswith("--"): - _set_prop(k, vl, imp) - elif k == "display": - if vl in { - "none", "contents", "block", "inline", "inline-block", "flex", - "inline-flex", "grid", "inline-grid", "list-item", - }: - _set_prop("display", vl, imp) - else: - invalid.append(f"display:{v[:24]}") - elif k == "visibility": - if vl in {"visible", "hidden", "collapse"}: - _set_prop("visibility", vl, imp) - else: - invalid.append(f"visibility:{v[:24]}") - elif k == "opacity": - if re.match(r"^(?:0(?:\.0+)?|\.0+|1(?:\.0+)?|0?\.[2-9]\d*)$", vl): - _set_prop("opacity", "0" if float(vl) == 0.0 else vl, imp) - else: - invalid.append(f"opacity:{v[:24]}") - elif k == "position": - if vl in {"static", "relative", "absolute"}: - _set_prop("position", vl, imp) - else: - invalid.append(f"position:{v[:24]}") - elif k == "clip": - if re.match( - r"^rect\(\s*0(?:px)?(?:\s*,\s*|\s+)0(?:px)?(?:\s*,\s*|\s+)0(?:px)?(?:\s*,\s*|\s+)0(?:px)?\s*\)$", - vl, - ): - _set_prop("clip", "zero", imp) - else: - invalid.append(f"clip:{v[:24]}") - elif k == "overflow": - if vl in {"visible", "hidden"}: - _set_prop("overflow", vl, imp) - else: - invalid.append(f"overflow:{v[:24]}") - elif k == "flex": - if vl not in {"none", "1", "auto", "0 0 100%", "0 0 auto", "1 1 auto"}: - invalid.append(f"flex:{v[:24]}") - if re.search(r"(?:^|\s)0(?:\.0+)?(?:px|%|em|rem|ch)?$", vl): - _set_prop("zero-box", True, imp) - elif k == "color": - if ( - vl in {"inherit", "currentcolor", "var(--ink)", "var(--ink2)", "var(--ink3)", "var(--acc)"} - or (vl == "var(--bg)" and sel_norm == ".btn") - ): - _set_prop("fg-color", vl, imp) - else: - invalid.append(f"color:{v[:24]}") - elif k in ("background", "background-color"): - if vl in {"none", "var(--bg)", "var(--surf)", "var(--acc2)"} or (vl == "var(--ink)" and sel_norm == ".btn"): - _set_prop("bg-color", vl, imp) - else: - invalid.append(f"{k}:{v[:24]}") - elif k in ("animation", "transition"): - if vl != "none": - invalid.append(f"{k}:{v[:24]}") - elif k == "flex-direction": - if vl in {"row", "column"}: - _set_prop("flex-direction", vl, imp) - else: - invalid.append(f"flex-direction:{v[:24]}") - elif k == "flex-wrap": - if vl in {"nowrap", "wrap"}: - _set_prop("flex-wrap", vl, imp) - else: - invalid.append(f"flex-wrap:{v[:24]}") - elif k in ("gap", "row-gap", "column-gap"): - if CSS_DYNAMIC_FUNC_RE.search(vl) or "-" in vl: - invalid.append(f"{k}:{v[:24]}") - elif k == "content": - if re.match(r"""^(?:none|normal|""|'')$""", vl): - pass - elif ( - sel_norm == ".prin li::before" - and re.match(r"""^counter\(\s*p\s*,\s*upper-roman\s*\)\s*(?:"\."|'\.')$""", vl) - ): - pass - else: - invalid.append(f"content:{v[:24]}") - elif k == "counter-reset": - if not (sel_norm == ".prin" and vl == "p"): - invalid.append(f"counter-reset:{v[:24]}") - elif k == "counter-increment": - if not (sel_norm == ".prin li::before" and vl == "p"): - invalid.append(f"counter-increment:{v[:24]}") - elif k == "list-style": - if vl != "none": - invalid.append(f"list-style:{v[:24]}") - elif k == "font-size": - if CSS_DYNAMIC_FUNC_RE.search(vl): - invalid.append(f"font-size:{v[:24]}") - elif vl == "inherit": - pass - else: - m_fs = re.match(r"^([+-]?\d+(?:\.\d+)?)([a-z%]+)?$", vl) - if m_fs: - fs_num = float(m_fs.group(1)) - fs_unit = m_fs.group(2) or "px" - min_fs = 0.25 if fs_unit in ("em", "rem", "ch") else 4.0 - if fs_num < min_fs: - invalid.append(f"font-size:{v[:24]}") - else: - _set_prop("font-size", vl, imp) - else: - invalid.append(f"font-size:{v[:24]}") - elif k == "line-height": - if CSS_DYNAMIC_FUNC_RE.search(vl): - invalid.append(f"line-height:{v[:24]}") - else: - m_lh = re.match(r"^([+-]?\d+(?:\.\d+)?)$", vl) - if m_lh and float(m_lh.group(1)) < 0.5: - invalid.append(f"line-height:{v[:24]}") - elif k == "font": - if vl != "inherit": - stripped_v = re.sub(r"\bvar\(--[a-zA-Z0-9_-]+\)", "", vl) - if CSS_DYNAMIC_FUNC_RE.search(stripped_v) or re.search( - r"(? max_neg: - invalid.append(f"letter-spacing:{v[:24]}") - elif k in ("margin", "margin-top", "margin-right", "margin-bottom", "margin-left"): - if CSS_DYNAMIC_FUNC_RE.search(vl): - invalid.append(f"{k}:{v[:24]}") - elif "-" in vl and not (sel_norm == "nav.links" and k == "margin" and vl == "0 -20px"): - neg_margins.append(f"{k}:{v[:24]}") - elif k in ("width", "height", "max-width", "max-height", "min-width", "min-height"): - if CSS_DYNAMIC_FUNC_RE.search(vl): - invalid.append(f"{k}:{v[:24]}") - else: - m_dim = re.match(r"^([+-]?\d+(?:\.\d+)?)([a-z%]+)?$", vl) - if m_dim: - dim_num = float(m_dim.group(1)) - dim_unit = m_dim.group(2) or "px" - min_dim = 0.25 if dim_unit in ("em", "rem", "ch") else 2.0 - if dim_num < min_dim: - small_box_dims.append(f"{k}:{v[:24]}") - if k in ("width", "height", "max-width", "max-height"): - _set_prop("zero-box", True, imp) - elif k == "order": - m = re.match(r"^[+-]?\d+$", v) - if m: - _set_prop("order", int(m.group(0)), imp) - else: - invalid.append(f"order:{v[:24]}") - elif k in ("grid-row", "grid-row-start", "grid-column", "grid-column-start"): - prop_key = "grid-row" if k.startswith("grid-row") else "grid-column" - if vl == "auto": - _set_prop(prop_key, None, imp) - else: - m = re.match(r"^(\d+)(?:\s*/\s*(\d+))?$", v) - if m and int(m.group(1)) >= 1 and (m.group(2) is None or int(m.group(2)) == int(m.group(1)) + 1): - _set_prop(prop_key, int(m.group(1)), imp) - else: - invalid.append(f"{k}:{v[:24]}") - elif k == "grid-template-columns": - cols = count_grid_columns(v) - if cols == -1: - invalid.append(f"grid-template-columns:{v[:24]}") - elif cols is not None: - _set_prop("grid-cols", cols, imp) - has_zero_clip = props.get("clip", (None, False))[0] == "zero" - has_abs_pos = props.get("position", (None, False))[0] == "absolute" - has_sr_only_clip = has_zero_clip and has_abs_pos - if has_zero_clip and not has_abs_pos: - invalid.append("clip:rect(0,0,0,0) without position:absolute") - if has_abs_pos and not has_zero_clip: - invalid.append("position:absolute without clip:rect(0,0,0,0)") - if props.get("overflow", (None, False))[0] == "hidden" and not has_sr_only_clip: - invalid.append("overflow:hidden without clip:rect(0,0,0,0)") - if ( - props.get("flex-direction", (None, False))[0] == "column" - and props.get("flex-wrap", (None, False))[0] == "wrap" - ): - invalid.append("wrapped column flex visual reordering") - if small_box_dims and not (has_sr_only_clip or allow_zero_scrollbar): - invalid.append(f"sub-2px box dimension hiding ({small_box_dims[0]})") - if neg_margins and not (has_sr_only_clip and neg_margins == ["margin:-1px"]): - invalid.append(f"negative margin overlap hiding ({neg_margins[0]})") - if "color" in raw_decls: - c_val = raw_decls["color"].strip().lower() - bg_val = (raw_decls.get("background-color") or raw_decls.get("background") or "").strip().lower() - if (bg_val and c_val == bg_val) or (c_val == "var(--bg)" and bg_val != "var(--ink)"): - invalid.append("foreground color matching background hiding") - theme_bgs = {raw_decls[b].strip().lower() for b in ("--bg", "--surf") if b in raw_decls} - if theme_bgs and any( - raw_decls.get(fg, "").strip().lower() in theme_bgs - for fg in ("--ink", "--ink2", "--ink3", "--acc") - ): - invalid.append("foreground custom property matching background") - return props, invalid - - -def _check_css_safety(clean_css: str) -> list[str]: - """Flag CSS constructs capable of unanalyzable visual reordering or text injection.""" - issues: list[str] = [] - for rx, label in DISALLOWED_SITE_CSS_RE: - m = rx.search(clean_css) - if m: - issues.append(f"{label} ({m.group(0).strip()[:36]})") - for m in re.finditer(r"(?i)(?\s*|\s+)", sel.strip()) if t] - chain = [] - comb: str | None = None - spec_ids = spec_cls = spec_tags = 0 - for tok in tokens: - if ">" in tok: - comb = ">" - continue - if tok.isspace(): - if comb is None: - comb = " " - continue - if tok.lower() == ":root": - spec_cls += 1 - chain.append((comb, "html", [], [], [])) - comb = None - continue - if not SIMPLE_COMPOUND_RE.match(tok) or tok == "": - return None, (0, 0, 0) - tag_m = re.match(r"^([a-zA-Z][a-zA-Z0-9-]*)", tok) - tag = tag_m.group(1).lower() if tag_m else None - if tag: - spec_tags += 1 - ids = re.findall(r"#([a-zA-Z0-9_-]+)", tok) - spec_ids += len(ids) - classes = re.findall(r"\.([a-zA-Z0-9_-]+)", tok) - attrs: list[tuple[str, str | None]] = [] - for am in re.finditer(r"\[([a-zA-Z0-9_-]+)(?:=([a-zA-Z0-9_\"'-]+))?\]", tok): - ak = am.group(1).lower() - av = am.group(2).strip("\"'") if am.group(2) is not None else None - attrs.append((ak, av)) - spec_cls += len(classes) + len(attrs) - chain.append((comb, tag, ids, classes, attrs)) - comb = None - return (chain if chain else None), (spec_ids, spec_cls, spec_tags) - - -def parse_media_interval(prelude: str) -> tuple[float, float, bool]: - """Parse @media pixel-width bounds; mark non-pixel/complex queries unresolvable.""" - q = prelude.strip() - if q.lower().startswith("@media"): - q = q[6:].strip() - if not q or "," in q or " or " in f" {q.lower()} ": - return 0.0, float("inf"), True - parts = [p.strip() for p in re.split(r"(?i)\band\b", q) if p.strip()] - lo, hi = 0.0, float("inf") - unres = False - for part in parts: - pl = part.lower() - if pl in ("all", "screen", "only screen", "only all"): - continue - m = re.match( - r"^\(\s*(min-width|max-width|width)\s*:\s*([0-9]+(?:\.[0-9]+)?)\s*px\s*\)$", - pl, - ) - if m: - kind, val = m.group(1), float(m.group(2)) - if kind == "min-width": - lo = max(lo, val) - elif kind == "max-width": - hi = min(hi, val) - else: - lo = max(lo, val) - hi = min(hi, val) - continue - m_cmp = re.match( - r"^\(\s*width\s*(<=|>=|<|>|=)\s*([0-9]+(?:\.[0-9]+)?)\s*px\s*\)$", - pl, - ) - if m_cmp: - op, val = m_cmp.group(1), float(m_cmp.group(2)) - if op == "<=": - hi = min(hi, val) - elif op == "<": - hi = min(hi, max(0.0, val - 0.1)) - elif op == ">=": - lo = max(lo, val) - elif op == ">": - lo = max(lo, val + 0.1) - else: - lo = max(lo, val) - hi = min(hi, val) - continue - unres = True - return lo, hi, unres - - -def parse_css_stylesheet( - css_text: str, - media_iv: tuple[float, float] = (0.0, float("inf")), - unresolvable_cond: bool = False, - start_idx: int = 0, - in_at_rule: bool = False, -) -> tuple[list, list[str], int]: - """Parse stylesheet rules for visual-order reconstruction and flag unsafe layout rules.""" - clean = CSS_COMMENT_RE.sub("", unescape_css_literals(css_text)) - rules: list = [] - issues: list[str] = [] if in_at_rule else _check_css_safety(clean) - idx = start_idx - layout_keys = { - "display", "order", "grid-row", "grid-column", "grid-cols", - "visibility", "opacity", "position", "clip", "overflow", "bg-color", - "flex-direction", "flex-wrap", - } - i, n = 0, len(clean) - while i < n: - semi = clean.find(";", i) - brace = clean.find("{", i) - if semi != -1 and (brace == -1 or semi < brace): - stmt = clean[i:semi].strip() - if stmt and not re.match(r'(?i)^@charset\s+"utf-8"$', stmt): - issues.append(f"unmodeled CSS statement ({stmt[:36]})") - i = semi + 1 - continue - if brace == -1: - rem = clean[i:].strip() - if rem: - issues.append(f"unmodeled trailing CSS text ({rem[:36]})") - break - prelude = clean[i:brace].strip() - depth = 1 - j = brace + 1 - while j < n and depth > 0: - if clean[j] == "{": - depth += 1 - elif clean[j] == "}": - depth -= 1 - j += 1 - body = clean[brace + 1:j - 1] - i = j - if not prelude: - continue - if prelude.startswith("@"): - pl_low = prelude.lower() - if pl_low.startswith("@media"): - c_lo, c_hi, c_unres = parse_media_interval(prelude) - sub_iv = (max(media_iv[0], c_lo), min(media_iv[1], c_hi)) - sub_rules, sub_issues, idx = parse_css_stylesheet( - body, - media_iv=sub_iv, - unresolvable_cond=unresolvable_cond or c_unres, - start_idx=idx, - in_at_rule=True, - ) - rules.extend(sub_rules) - issues.extend(sub_issues) - elif pl_low.startswith(("@supports", "@layer", "@container", "@scope", "@starting-style")): - sub_rules, sub_issues, idx = parse_css_stylesheet( - body, - media_iv=media_iv, - unresolvable_cond=True, - start_idx=idx, - in_at_rule=True, - ) - rules.extend(sub_rules) - issues.extend(sub_issues) - else: - issues.append(f"unmodeled CSS at-rule ({prelude[:36]})") - continue - props, bad_vals = parse_style_decls( - body, - allow_zero_scrollbar=(prelude.strip().lower() == "::-webkit-scrollbar"), - prelude_sel=prelude, - ) - issues.extend(f"non-literal CSS visual-order value ({bv})" for bv in bad_vals) - fs_val = props.get("font-size", ("", False))[0] - has_zero_font = isinstance(fs_val, str) and bool(re.match(r"^0(?:\.0+)?(?:[a-z%]+)?$", fs_val)) - has_layout_prop = has_zero_font or any(k in props for k in layout_keys) - if has_layout_prop and unresolvable_cond: - issues.append(f"non-pixel conditional layout rule ({prelude[:36]})") - sel_parts: list[str] = [] - s_cur: list[str] = [] - s_depth = 0 - for ch in prelude: - if ch in "([": - s_depth += 1 - s_cur.append(ch) - elif ch in ")]": - s_depth = max(0, s_depth - 1) - s_cur.append(ch) - elif ch == "," and s_depth == 0: - sel_parts.append("".join(s_cur).strip()) - s_cur = [] - else: - s_cur.append(ch) - if s_cur: - sel_parts.append("".join(s_cur).strip()) - for sel in sel_parts: - if not sel: - continue - chain, spec = parse_simple_selector(sel) - if chain is None: - if has_layout_prop: - issues.append(f"complex CSS visual-order selector ({sel[:36]})") - else: - idx += 1 - rules.append((chain, spec, idx, props, media_iv)) - return rules, issues, idx - - -class _DOMNode: - def __init__(self, tag: str, attrs: dict[str, str], parent: "_DOMNode | None" = None) -> None: - self.tag = tag - self.attrs = attrs - self.parent = parent - self.children: list[str | _DOMNode] = [] - self.classes = set(attrs.get("class", "").split()) - self.id = attrs.get("id", "") - - -def _matches_compound(node: _DOMNode, tag, ids, classes, attrs) -> bool: - if tag and node.tag != tag: - return False - if any(i != node.id for i in ids): - return False - if any(c not in node.classes for c in classes): - return False - for ak, av in attrs: - if ak not in node.attrs: - return False - if av is not None and node.attrs[ak] != av: - return False - return True - - -def _matches_chain(node: _DOMNode, chain) -> bool: - if not _matches_compound(node, *chain[-1][1:]): - return False - cur = node - for idx in range(len(chain) - 1, 0, -1): - comb = chain[idx][0] - target = chain[idx - 1][1:] - if comb == ">": - if cur.parent is None or cur.parent.tag == "__root__" or not _matches_compound(cur.parent, *target): - return False - cur = cur.parent - else: - anc = cur.parent - while anc is not None and anc.tag != "__root__": - if _matches_compound(anc, *target): - break - anc = anc.parent - else: - return False - cur = anc - return True - - -def compute_node_style( - node: _DOMNode, rules: list | tuple, viewport_w: float | None = None -) -> dict[str, object]: - """Compute cascaded CSS properties on a DOM node using selector specificity and viewport width.""" - resolved: dict[str, tuple[tuple[int, int, int, int, int, int], object]] = {} - for chain, spec, r_idx, props, (lo, hi) in rules: - if viewport_w is None: - if lo > 0.0 or hi < float("inf"): - continue - elif not (lo <= viewport_w <= hi): - continue - if _matches_chain(node, chain): - for prop, (val, imp) in props.items(): - rank = (1 if imp else 0, 0, spec[0], spec[1], spec[2], r_idx) - if prop not in resolved or rank >= resolved[prop][0]: - resolved[prop] = (rank, val) - inline_s = node.attrs.get("style", "") - if inline_s: - iprops, _ = parse_style_decls(inline_s) - for prop, (val, imp) in iprops.items(): - rank = (1 if imp else 0, 1, 0, 0, 0, 10**9) - if prop not in resolved or rank >= resolved[prop][0]: - resolved[prop] = (rank, val) - return {k: v[1] for k, v in resolved.items()} - - -def _omits_layout_box(node: _DOMNode, st: dict[str, object]) -> bool: - if node.tag in ("template", "script", "style", "noscript", "svg", "defs", "symbol", "desc", "metadata"): - return True - if st.get("display") == "none": - return True - if "hidden" in node.attrs and st.get("display") in (None, "none"): - return True - if st.get("position") == "absolute" and st.get("clip") == "zero": - return True - return False - - -def _suppresses_subtree_paint(st: dict[str, object]) -> bool: - if st.get("opacity") in ("0", "0.0", ".0"): - return True - if st.get("overflow") == "hidden" and bool(st.get("zero-box")): - return True - return False - - -def _next_inherited_state( - st: dict[str, object], - inh: tuple[str, bool, str, str], -) -> tuple[str, bool, str, str]: - inh_vis, inh_zero_fs, inh_fg, inh_bg = inh - vis = str(st["visibility"]) if st.get("visibility") in ("visible", "hidden", "collapse") else inh_vis - fs = st.get("font-size") - if isinstance(fs, str) and re.match(r"^0(?:\.0+)?(?:[a-z%]+)?$", fs): - zero_fs = True - elif isinstance(fs, str) and fs: - zero_fs = True if (inh_zero_fs and bool(re.search(r"(?:%|(? bool: - vis, zero_fs, fg, bg = inh - if vis in ("hidden", "collapse") or zero_fs: - return True - if fg == "var(--bg)" and bg != "var(--ink)": - return True - if fg and bg and fg == bg: - return True - return False - - -_TABLE_SECTION_ORDER = { - "caption": 0, - "colgroup": 1, - "col": 1, - "thead": 2, - "tbody": 3, - "tr": 3, - "tfoot": 4, -} - - -def _to_upper_roman(num: int) -> str: - """Format a positive integer in uppercase Roman numerals matching CSS counter(p, upper-roman).""" - out: list[str] = [] - rem = max(1, min(num, 3999)) - for val, sym in ( - (1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), - (100, "C"), (90, "XC"), (50, "L"), (40, "XL"), - (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I"), - ): - while rem >= val: - out.append(sym) - rem -= val - return "".join(out) - - -def serialize_visual_tree( - root_node: _DOMNode, - rules: list | tuple, - viewport_w: float | None, - respect_order: bool, - skip_hidden: bool, - i18n_map: dict[str, str] | None = None, - include_pseudo: bool = False, -) -> tuple[str, str]: - """Serialize DOM text in computed visual order (flex order, grid tracks, table sections).""" - fused: list[str] = [] - compact: list[str] = [] - prin_counter = [0] - root_inh: tuple[str, bool, str, str] = ("visible", False, "", "") - - def expand_children( - container: _DOMNode, inh: tuple[str, bool, str, str] - ) -> list[tuple[str, object, dict[str, object], tuple[str, bool, str, str]]]: - items: list[tuple[str, object, dict[str, object], tuple[str, bool, str, str]]] = [] - for ch in container.children: - if isinstance(ch, str): - if ch and not (skip_hidden and _is_text_hidden(inh)): - items.append(("text", ch, {}, inh)) - else: - st = compute_node_style(ch, rules, viewport_w) - if skip_hidden and _omits_layout_box(ch, st): - continue - ch_inh = _next_inherited_state(st, inh) - if respect_order and st.get("display") == "contents": - if skip_hidden and _suppresses_subtree_paint(st): - continue - items.extend(expand_children(ch, ch_inh)) - else: - items.append(("node", ch, st, ch_inh)) - if not respect_order: - return items - if container.tag == "table": - meaningful = [ - (idx, kind, obj, st, item_inh) - for idx, (kind, obj, st, item_inh) in enumerate(items) - if not (kind == "text" and isinstance(obj, str) and not obj.strip()) - ] - meaningful.sort( - key=lambda x: ( - _TABLE_SECTION_ORDER.get(x[2].tag, 5) if isinstance(x[2], _DOMNode) else 5, - x[0], - ) - ) - return [(k, o, s, ii) for _, k, o, s, ii in meaningful] - - parent_st = compute_node_style(container, rules, viewport_w) if container.tag != "__root__" else {} - disp = str(parent_st.get("display", "")).lower() - is_flex = "flex" in disp - is_grid = "grid" in disp - if not (is_flex or is_grid): - return items - - meaningful_items: list[list[object]] = [] - for idx, (kind, obj, st, item_inh) in enumerate(items): - if kind == "text" and isinstance(obj, str) and not obj.strip(): - continue - raw_ord = st.get("order", 0) - ord_val = raw_ord if isinstance(raw_ord, int) else 0 - raw_gr = st.get("grid-row") - gr = raw_gr if isinstance(raw_gr, int) else None - raw_gc = st.get("grid-column") - gc = raw_gc if isinstance(raw_gc, int) else None - meaningful_items.append([idx, kind, obj, st, ord_val, gr, gc, item_inh]) - - if is_flex: - meaningful_items.sort(key=lambda x: (int(x[4]), int(x[0]))) # type: ignore[arg-type] - return [(m[1], m[2], m[3], m[7]) for m in meaningful_items] # type: ignore[misc] - - explicit_cols = [m[6] for m in meaningful_items if isinstance(m[6], int)] - parent_cols = parent_st.get("grid-cols") - ncols = parent_cols if isinstance(parent_cols, int) and parent_cols >= 1 else max([1, *explicit_cols]) - occupied: set[tuple[int, int]] = set() - for m in meaningful_items: - if isinstance(m[5], int) and isinstance(m[6], int): - r, c = m[5], m[6] - ncols = max(ncols, c) - occupied.add((r, c)) - for m in sorted(meaningful_items, key=lambda x: (int(x[4]), int(x[0]))): # type: ignore[arg-type] - if isinstance(m[5], int) and m[6] is None: - r = m[5] - c = 1 - while (r, c) in occupied: - c += 1 - m[6] = c - ncols = max(ncols, c) - occupied.add((r, c)) - cur_r, cur_c = 1, 1 - for m in sorted(meaningful_items, key=lambda x: (int(x[4]), int(x[0]))): # type: ignore[arg-type] - if m[5] is None: - if isinstance(m[6], int): - c = m[6] - ncols = max(ncols, c) - r = cur_r if c >= cur_c else cur_r + 1 - while (r, c) in occupied: - r += 1 - m[5] = r - occupied.add((r, c)) - cur_r, cur_c = r, c + 1 - if cur_c > ncols: - cur_r += 1 - cur_c = 1 - else: - while (cur_r, cur_c) in occupied: - cur_c += 1 - if cur_c > ncols: - cur_r += 1 - cur_c = 1 - m[5], m[6] = cur_r, cur_c - occupied.add((cur_r, cur_c)) - cur_c += 1 - if cur_c > ncols: - cur_r += 1 - cur_c = 1 - meaningful_items.sort(key=lambda x: (int(x[5]), int(x[6]), int(x[4]), int(x[0]))) # type: ignore[arg-type] - return [(m[1], m[2], m[3], m[7]) for m in meaningful_items] # type: ignore[misc] - - def walk(node: _DOMNode, st: dict[str, object], inh: tuple[str, bool, str, str]) -> None: - if skip_hidden and node.tag != "__root__" and _suppresses_subtree_paint(st): - return - if include_pseudo and node.tag != "__root__": - if "prin" in node.classes: - prin_counter[0] = 0 - elif node.tag == "li": - anc = node.parent - while anc is not None and anc.tag != "__root__": - if "prin" in anc.classes: - prin_counter[0] += 1 - if not (skip_hidden and _is_text_hidden(inh)): - pfx = f"{_to_upper_roman(prin_counter[0])}." - fused.append(pfx) - compact.append(pfx) - break - anc = anc.parent - if i18n_map and node.tag != "__root__": - k_txt = node.attrs.get("data-i18n") - k_htm = node.attrs.get("data-i18n-html") - if k_txt and i18n_map.get(k_txt): - if not (skip_hidden and _is_text_hidden(inh)): - val = i18n_map[k_txt] - fused.append(val) - if val.strip(): - compact.append(val.strip()) - return - if k_htm and i18n_map.get(k_htm): - if not (skip_hidden and _is_text_hidden(inh)): - val = re.sub(r"<[^>]+>", "", i18n_map[k_htm]) - fused.append(val) - if val.strip(): - compact.append(val.strip()) - return - for kind, obj, ch_st, ch_inh in expand_children(node, inh): - if kind == "text" and isinstance(obj, str): - fused.append(obj) - if obj.strip(): - compact.append(obj.strip()) - elif isinstance(obj, _DOMNode): - walk(obj, ch_st, ch_inh) - - walk(root_node, {}, root_inh) - return "".join(fused), "".join(compact) - - -class _RenderedHTMLNormalizer(HTMLParser): - def __init__(self, depth: int = 0, xml_mode: bool = False, page_rel: str = "site/index.html") -> None: + def __init__(self, depth: int = 0, page_rel: str = "site/index.html") -> None: super().__init__(convert_charrefs=True) self.depth = depth - self.xml_mode = xml_mode self.page_rel = page_rel self.inline_parts: list[str] = [] self.fused_parts: list[str] = [] @@ -1810,172 +704,97 @@ def __init__(self, depth: int = 0, xml_mode: bool = False, page_rel: str = "site self.attr_lines: list[str] = [] self.comment_lines: list[str] = [] self.subtrees: list[str] = [] - self.stylesheet_entries: list[tuple[str, str, str]] = [] - self.script_tags: list[tuple[str | None, str]] = [] - self.inline_css_issues: list[str] = [] - self.html_issues: list[str] = [] - self.dom_root = _DOMNode("__root__", {}) - self._dom_stack: list[_DOMNode] = [self.dom_root] - self._tag_stack: list[tuple[str, bool]] = [] + self.style_blocks: list[str] = [] + self.asset_refs: list[tuple[str, str]] = [] + self.element_ids: set[str] = set() + self.html_issues: list[tuple[str, str]] = [] self._raw_tag: str | None = None self._raw_chunks: list[str] = [] - self._raw_media: str = "" - self._raw_src: str | None = None - def _collect_attrs(self, tag: str, attrs: list[tuple[str, str | None]]) -> dict[str, str]: + def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: + t = tag.lower() + if t in HTML_BLOCK_TAGS: + self.inline_parts.append("\n") ad: dict[str, str] = {} - if tag not in ALLOWED_SITE_HTML_TAGS: - self.html_issues.append(f"unmodeled HTML element in site/ (<{tag}>)") for k, v in attrs: kl = k.lower() if kl in ad: - self.html_issues.append(f"duplicate HTML attribute in site/ ({kl})") + self.html_issues.append(("publish", f"duplicate HTML attribute in site/ ({kl})")) continue ad[kl] = v if v is not None else "" - return ad - - @staticmethod - def _has_inline_display(ad: dict[str, str]) -> bool: - style_v = ad.get("style", "") - return bool(style_v and INLINE_DISPLAY_RE.search(style_v)) - - def _handle_attrs( - self, tag: str, attrs: list[tuple[str, str | None]], ad: dict[str, str] - ) -> None: - for k, v in attrs: - kl = k.lower() - if kl in ("display", "visibility", "opacity", "clip", "clip-path", "transform", "filter", "mask"): - self.html_issues.append(f"presentation hiding or layout attribute in site/ ({kl})") if re.match(r"^on[a-z]{2,}$", kl): - self.html_issues.append(f"inline event handler attribute in site/ ({kl})") + self.html_issues.append(("publish", f"inline event handler attribute in site/ ({kl})")) if not v: continue compact_val = re.sub(r"[\x00-\x20]+", "", v) unq_compact = re.sub(r"[\x00-\x20]+", "", urllib.parse.unquote(compact_val)) for chk_val in (compact_val, unq_compact): if re.match(r"^(?:javascript|vbscript):", chk_val, re.IGNORECASE): - self.html_issues.append("javascript: or vbscript: URI attribute in site/") - if re.match(r"^data:", chk_val, re.IGNORECASE): - self.html_issues.append("embedded data URI asset requires direct file scan") + self.html_issues.append(("publish", "javascript: or vbscript: URI attribute in site/")) + elif re.match(r"^data:", chk_val, re.IGNORECASE): + self.html_issues.append(("publish", "embedded data URI asset requires direct file scan")) self.attr_lines.append(f"{k}={v}") if "%" in v: self.attr_lines.append(f"{k}={urllib.parse.unquote(v)}") - if kl == "style": - clean_s = CSS_COMMENT_RE.sub("", unescape_css_literals(v)) - self.inline_css_issues.extend(_check_css_safety(clean_s)) - _, bad_vals = parse_style_decls(clean_s) - self.inline_css_issues.extend(f"non-literal CSS visual-order value ({bv})" for bv in bad_vals) if kl == "srcdoc" and self.depth < 3: self.subtrees.append(normalize_html_text(v, self.depth + 1, page_rel=self.page_rel)) - if tag == "link" and "href" in ad: - if resolve_site_asset_href(self.page_rel, ad["href"]) is None: - self.html_issues.append(f"remote or inline-scheme stylesheet or link in site/ ({ad['href'][:36]})") - elif tag == "script" and "src" in ad: - if resolve_site_asset_href(self.page_rel, ad["src"]) is None: - self.html_issues.append(f"remote or inline-scheme stylesheet or script in site/ ({ad['src'][:36]})") - elif tag == "img": - if "src" in ad and resolve_site_asset_href(self.page_rel, ad["src"]) is None: - self.html_issues.append(f"remote or inline-scheme image source in site/ ({ad['src'][:36]})") + if ad.get("id"): + self.element_ids.add(ad["id"]) + if t == "link" and "href" in ad: + rel_tokens = {tok.lower() for tok in ad.get("rel", "").split()} + if rel_tokens & {"stylesheet", "icon", "apple-touch-icon", "manifest"}: + self.asset_refs.append(("link", ad["href"])) + elif t == "script" and "src" in ad: + self.asset_refs.append(("script", ad["src"])) + elif t == "img": + if "src" in ad: + self.asset_refs.append(("img", ad["src"])) if "srcset" in ad: for cand in ad["srcset"].split(","): parts = cand.strip().split() - if not parts or resolve_site_asset_href(self.page_rel, parts[0]) is None: - self.html_issues.append("remote or inline-scheme image source in site/ (srcset)") - break - - def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: - t = tag.lower() - ad = self._collect_attrs(t, attrs) - node = _DOMNode(t, ad, parent=self._dom_stack[-1]) - self._dom_stack[-1].children.append(node) - if t not in HTML_VOID_TAGS: - self._dom_stack.append(node) - is_block = (t in HTML_BLOCK_TAGS) and not self._has_inline_display(ad) - self._tag_stack.append((t, is_block)) - if is_block: - self.inline_parts.append("\n") - self._handle_attrs(t, attrs, ad) - if t == "link": - rel_tokens = {tok.lower() for tok in ad.get("rel", "").split()} - if "stylesheet" in rel_tokens: - self.stylesheet_entries.append(("link", ad.get("href", "").strip(), ad.get("media", "").strip())) - elif t == "style": - self._raw_tag = t - self._raw_chunks = [] - self._raw_media = ad.get("media", "").strip() - elif t == "script": - self._raw_tag = t - self._raw_chunks = [] - self._raw_src = ad["src"] if "src" in ad else None - elif t == "title": - self.set_cdata_mode("title") + self.asset_refs.append(("img", parts[0] if parts else "")) + elif t == "a" and "href" in ad: + self.asset_refs.append(("anchor", ad["href"])) + if t in ("script", "style", "title"): + if t == "title": + self.set_cdata_mode("title") self._raw_tag = t self._raw_chunks = [] def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: t = tag.lower() - in_svg = self.xml_mode or any(st_tag == "svg" for st_tag, _ in self._tag_stack) - if t in ("script", "style", "title") or (not in_svg and t not in HTML_VOID_TAGS): - if t in ("script", "style", "title"): - self.set_cdata_mode(t) - self.handle_starttag(tag, attrs) - return - ad = self._collect_attrs(t, attrs) - node = _DOMNode(t, ad, parent=self._dom_stack[-1]) - self._dom_stack[-1].children.append(node) - if (t in HTML_BLOCK_TAGS) and not self._has_inline_display(ad): - self.inline_parts.append("\n") - self._handle_attrs(t, attrs, ad) - if t == "link": - rel_tokens = {tok.lower() for tok in ad.get("rel", "").split()} - if "stylesheet" in rel_tokens: - self.stylesheet_entries.append(("link", ad.get("href", "").strip(), ad.get("media", "").strip())) - elif t == "script": - self.script_tags.append((ad["src"] if "src" in ad else None, "")) + if t in ("script", "style", "title"): + self.html_issues.append(("publish", "self-closing script or style tag in site/")) + self.set_cdata_mode(t) + self.handle_starttag(tag, attrs) def _flush_raw_tag(self, tag_name: str) -> None: raw_body = "".join(self._raw_chunks) if tag_name == "script": - self.script_tags.append((self._raw_src, raw_body)) - self._raw_src = None dec = fold_js_expressions(raw_body) self.subtrees.append(dec) if ("<" in dec or "&" in dec) and self.depth < 3: self.subtrees.append(normalize_html_text(dec, self.depth + 1, page_rel=self.page_rel)) elif tag_name == "style": - self.stylesheet_entries.append(("inline", raw_body, self._raw_media)) - self._raw_media = "" + self.style_blocks.append(unescape_css_literals(raw_body)) self.subtrees.append(unescape_css_literals(raw_body)) elif tag_name == "title": - if "<" in raw_body: - self.html_issues.append("markup inside HTML in site/") unesc = html.unescape(raw_body) self.inline_parts.append(unesc) self.fused_parts.append(unesc) if unesc.strip(): self.compact_parts.append(unesc.strip()) - self._dom_stack[-1].children.append(unesc) self._raw_tag = None self._raw_chunks = [] def handle_pi(self, data: str) -> None: - self.html_issues.append("HTML processing instruction in site/") + self.html_issues.append(("publish", "HTML processing instruction in site/")) def handle_endtag(self, tag: str) -> None: t = tag.lower() if self._raw_tag == t: self._flush_raw_tag(t) - is_block = t in HTML_BLOCK_TAGS - for idx in range(len(self._tag_stack) - 1, -1, -1): - if self._tag_stack[idx][0] == t: - _, is_block = self._tag_stack[idx] - del self._tag_stack[idx:] - break - for idx in range(len(self._dom_stack) - 1, 0, -1): - if self._dom_stack[idx].tag == t: - del self._dom_stack[idx:] - break - if is_block: + if t in HTML_BLOCK_TAGS: self.inline_parts.append("\n") def handle_data(self, data: str) -> None: @@ -1986,7 +805,6 @@ def handle_data(self, data: str) -> None: self.fused_parts.append(data) if data.strip(): self.compact_parts.append(data.strip()) - self._dom_stack[-1].children.append(data) def handle_comment(self, data: str) -> None: self.comment_lines.append(html.unescape(data)) @@ -1996,270 +814,24 @@ def unknown_decl(self, data: str) -> None: self.handle_data(data[len("CDATA["):]) -def _collect_page_stylesheet_rules( - page_rel: str, - stylesheet_entries: list[tuple[str, str, str]], - css_by_rel: dict[str, str] | None = None, -) -> tuple[list, list[str], set[float]]: - """Resolve linked and inline stylesheets for one page in document order.""" - rules: list = [] - issues: list[str] = [] - breakpoints: set[float] = set() - idx_base = 0 - for kind, content, media_attr in stylesheet_entries: - if media_attr.strip(): - m_lo, m_hi, m_unres = parse_media_interval("@media " + media_attr) - base_iv: tuple[float, float] = (m_lo, m_hi) - else: - base_iv = (0.0, float("inf")) - m_unres = False - css_src = "" - if kind == "link": - resolved = resolve_site_asset_href(page_rel, content) - if resolved is None: - issues.append(f"external or escaping stylesheet link ({content[:36]})") - continue - if not resolved.lower().endswith(".css"): - issues.append(f"non-CSS stylesheet link ({content[:36]})") - continue - if css_by_rel is not None: - if resolved not in css_by_rel: - issues.append(f"missing local stylesheet link ({content[:36]})") - continue - css_src = css_by_rel[resolved] - else: - css_src = content - if css_src: - p_rules, p_issues, idx_base = parse_css_stylesheet( - css_src, media_iv=base_iv, unresolvable_cond=m_unres, start_idx=idx_base - ) - rules.extend(p_rules) - issues.extend(p_issues) - for _, _, _, _, (lo, hi) in p_rules: - if 0.0 < lo < float("inf"): - breakpoints.add(lo) - if hi < float("inf"): - breakpoints.add(hi) - return rules, issues, breakpoints - - -def _collect_active_viewports(all_rules: list, breakpoints: set[float]) -> list[float]: - candidates = [320.0, 1440.0] - for bp in sorted(breakpoints)[:32]: - candidates.extend((max(0.0, bp - 0.5), bp, bp + 0.5)) - seen_sigs: set[tuple[int, ...]] = set() - active_viewports: list[float] = [] - for vw in candidates: - sig = tuple(r_idx for _, _, r_idx, _, (lo, hi) in all_rules if lo <= vw <= hi) - if sig not in seen_sigs: - seen_sigs.add(sig) - active_viewports.append(vw) - return active_viewports - - -def _ensure_html_root(parser: _RenderedHTMLNormalizer) -> None: - if parser.xml_mode or any(isinstance(c, _DOMNode) and c.tag == "html" for c in parser.dom_root.children): - return - html_node = _DOMNode("html", {}, parent=parser.dom_root) - html_node.children = list(parser.dom_root.children) - for ch in html_node.children: - if isinstance(ch, _DOMNode): - ch.parent = html_node - parser.dom_root.children = [html_node] - - -def _iter_runtime_dom_states(parser: _RenderedHTMLNormalizer, page_rel: str): - """Yield the initial parsed DOM state followed by all supported theme/locale/menu runtime states.""" - html_node: _DOMNode | None = None - nav_node: _DOMNode | None = None - menu_btn: _DOMNode | None = None - theme_btn: _DOMNode | None = None - lang_btn: _DOMNode | None = None - stack: list[_DOMNode] = [c for c in parser.dom_root.children if isinstance(c, _DOMNode)] - while stack: - node = stack.pop() - if html_node is None and node.tag == "html": - html_node = node - if nav_node is None and node.id == "primary-nav": - nav_node = node - if menu_btn is None and "js-menu" in node.classes: - menu_btn = node - if theme_btn is None and "js-theme" in node.classes: - theme_btn = node - if lang_btn is None and "js-lang" in node.classes: - lang_btn = node - stack.extend(reversed([c for c in node.children if isinstance(c, _DOMNode)])) - - snapshots: list[tuple[_DOMNode, dict[str, str], list[str | _DOMNode]]] = [] - seen_ids: set[int] = set() - for n in (html_node, nav_node, menu_btn, theme_btn, lang_btn): - if n is not None and id(n) not in seen_ids: - seen_ids.add(id(n)) - snapshots.append((n, dict(n.attrs), list(n.children))) - - def _restore() -> None: - for n, saved_attrs, saved_children in snapshots: - n.attrs = dict(saved_attrs) - n.children = list(saved_children) - - yield - has_bootstrap_js = any( - src is None - and raw_body.strip() - and hashlib.sha256(raw_body.encode("utf-8")).hexdigest() in APPROVED_SITE_INLINE_SCRIPTS - for src, raw_body in parser.script_tags - ) - loads_app_js = any( - src is not None and resolve_site_asset_href(page_rel, src) == "site/assets/app.js" - for src, _ in parser.script_tags - ) - if not (has_bootstrap_js or loads_app_js): - return - menus: tuple[str | None, ...] = ( - ("false", "true") if (loads_app_js and nav_node is not None and menu_btn is not None) else (None,) - ) - try: - for theme in ("light", "dark"): - for lang in ("en", "zh-CN"): - for menu_open in menus: - _restore() - if html_node is not None: - html_node.attrs["data-theme"] = theme - html_node.attrs["lang"] = lang - if loads_app_js: - if theme_btn is not None: - theme_btn.attrs["aria-pressed"] = "true" if theme == "dark" else "false" - if lang_btn is not None: - lang_btn.attrs["lang"] = "en" if lang == "zh-CN" else "zh-CN" - lang_btn.children = ["EN" if lang == "zh-CN" else "中文"] - if menu_open is not None and nav_node is not None and menu_btn is not None: - nav_node.attrs["data-open"] = menu_open - menu_btn.attrs["aria-expanded"] = menu_open - yield - finally: - _restore() - - -def _check_cascaded_dom_integrity( - dom_root: _DOMNode, rules: list | tuple, viewports: list[float | None] -) -> list[str]: - """Validate cascaded properties and explicit grid cell placement across DOM elements.""" - issues: list[str] = [] - seen_msgs: set[str] = set() - - def _rec(msg: str) -> None: - if msg not in seen_msgs: - seen_msgs.add(msg) - issues.append(msg) - - stack: list[_DOMNode] = [c for c in dom_root.children if isinstance(c, _DOMNode)] - while stack: - node = stack.pop() - child_nodes = [c for c in node.children if isinstance(c, _DOMNode)] - stack.extend(reversed(child_nodes)) - for vw in viewports: - st = compute_node_style(node, rules, vw) - has_zero_clip = st.get("clip") == "zero" - has_abs_pos = st.get("position") == "absolute" - has_sr_only_clip = has_zero_clip and has_abs_pos - if has_zero_clip and not has_abs_pos: - _rec("clip:rect(0,0,0,0) without position:absolute") - if has_abs_pos and not has_zero_clip: - _rec("position:absolute without clip:rect(0,0,0,0)") - if st.get("overflow") == "hidden" and not has_sr_only_clip: - _rec("overflow:hidden without clip:rect(0,0,0,0)") - if st.get("flex-direction") == "column" and st.get("flex-wrap") == "wrap": - _rec("wrapped column flex visual reordering") - if node.tag == "html": - theme_bgs = {st.get(b) for b in ("--bg", "--surf")} - {None} - if theme_bgs and any(st.get(fg) in theme_bgs for fg in ("--ink", "--ink2", "--ink3", "--acc")): - _rec("foreground custom property matching background") - if "grid" in str(st.get("display", "")).lower(): - used_cells: set[tuple[int, int]] = set() - for ch in child_nodes: - ch_st = compute_node_style(ch, rules, vw) - if _omits_layout_box(ch, ch_st): - continue - gr, gc = ch_st.get("grid-row"), ch_st.get("grid-column") - if isinstance(gr, int) and isinstance(gc, int): - if (gr, gc) in used_cells: - _rec("overlapping explicit CSS grid cell placement") - used_cells.add((gr, gc)) - return issues - - -def normalize_html_text( - text: str, - depth: int = 0, - page_rel: str = "site/index.html", - css_by_rel: dict[str, str] | None = None, - i18n_maps: list[dict[str, str]] | tuple[dict[str, str], ...] = (), -) -> str: - """Canonicalize rendered HTML/SVG text, CSS visual order, attributes, comments, and scripts.""" - parser = _RenderedHTMLNormalizer( - depth=depth, - xml_mode=Path(page_rel).suffix.lower() in (".svg", ".xml", ".xhtml"), - page_rel=page_rel, - ) +def _parse_site_html(text: str, depth: int = 0, page_rel: str = "site/index.html") -> _SiteHTMLParser: + parser = _SiteHTMLParser(depth=depth, page_rel=page_rel) parser.feed(text) parser.close() if parser._raw_tag is not None: parser._flush_raw_tag(parser._raw_tag) - _ensure_html_root(parser) - all_rules, _, breakpoints = _collect_page_stylesheet_rules( - page_rel, parser.stylesheet_entries, css_by_rel=css_by_rel - ) - active_viewports = _collect_active_viewports(all_rules, breakpoints) - - rendered_views: list[str] = [] - seen_rendered: set[str] = set() - has_i18n = "data-i18n" in text and bool(i18n_maps) - for _ in _iter_runtime_dom_states(parser, page_rel): - vis_dom_f, vis_dom_c = serialize_visual_tree( - parser.dom_root, all_rules, viewport_w=None, respect_order=False, skip_hidden=True - ) - for v in (vis_dom_f, vis_dom_c): - if v not in seen_rendered: - seen_rendered.add(v) - rendered_views.append(v) - for vw in active_viewports: - for skip_hidden in (True, False): - for inc_pseudo in (False, True): - vf, vc = serialize_visual_tree( - parser.dom_root, - all_rules, - viewport_w=vw, - respect_order=True, - skip_hidden=skip_hidden, - include_pseudo=inc_pseudo, - ) - for v in (vf, vc): - if v not in seen_rendered: - seen_rendered.add(v) - rendered_views.append(v) - if has_i18n: - for imap in i18n_maps: - if not imap: - continue - ivf, ivc = serialize_visual_tree( - parser.dom_root, - all_rules, - viewport_w=vw, - respect_order=True, - skip_hidden=skip_hidden, - i18n_map=imap, - include_pseudo=inc_pseudo, - ) - for v in (ivf, ivc): - if v not in seen_rendered: - seen_rendered.add(v) - rendered_views.append(v) + return parser + +def normalize_html_text( + text: str, depth: int = 0, page_rel: str = "site/index.html" +) -> str: + """Extract rendered HTML/SVG text, attributes, comments, and decoded scripts.""" + parser = _parse_site_html(text, depth=depth, page_rel=page_rel) combined = "\n".join([ "".join(parser.inline_parts), "".join(parser.fused_parts), "".join(parser.compact_parts), - *rendered_views, "\n".join(parser.attr_lines), "\n".join(parser.comment_lines), *parser.subtrees, @@ -2267,130 +839,111 @@ def normalize_html_text( return INVISIBLE_FORMAT_RE.sub("", combined) -def check_site_visual_integrity( +def check_site_links_and_safety( + root: Path, rel: str, path: Path, text: str, report: Report, seen: set[tuple[str, str]], - css_by_rel: dict[str, str] | None = None, - script_hashes_by_rel: dict[str, str] | None = None, ) -> None: - """Reject bidirectional controls and CSS/HTML/script constructs that obscure visual order or bypass static scans.""" - def _emit(msg: str) -> None: - item = ("publish", f"{rel}: {msg} (section 13)") + """Validate site/ asset link integrity, local resource resolution, and URI safety.""" + def _emit(category: str, msg: str) -> None: + item = (category, f"{rel}: {msg}") if item not in seen: seen.add(item) report.add(*item) if BIDI_CONTROL_RE.search(text): - _emit("bidirectional control character in published site/ asset") + _emit("publish", "bidirectional control character in published site/ asset (section 13)") + + def _check_css_text(css_text: str) -> None: + clean_css = CSS_COMMENT_RE.sub("", unescape_css_literals(css_text)) + if re.search(r"(?i)@import\b", clean_css): + _emit("publish", "CSS @import external stylesheet (section 13)") + for m in re.finditer(r"(?i)\burl\s*\(\s*([^)]+)\)", clean_css): + target = m.group(1).strip().strip("\"'").strip() + if not target or target.startswith("#"): + continue + if re.match(r"^(?:https?:|//|data:|javascript:|vbscript:)", target, re.IGNORECASE): + _emit("publish", f"remote or inline-scheme CSS url() resource ({target[:36]}) (section 13)") + continue + resolved = resolve_site_asset_href(rel, target) + if resolved is None: + _emit("publish", f"escaping CSS url() resource ({target[:36]}) (section 13)") + elif not (root / resolved).is_file(): + _emit("links", f"broken local CSS asset link -> {target}") + suffix = path.suffix.lower() if suffix == ".css": - _, issues, _ = parse_css_stylesheet(text) - for iss in issues: - _emit(iss) + _check_css_text(text) + return + + if suffix == ".webmanifest": + try: + manifest = json.loads(text) + except json.JSONDecodeError: + _emit("links", "invalid JSON in web manifest") + return + if isinstance(manifest, dict) and isinstance(manifest.get("icons"), list): + for icon in manifest["icons"]: + if not isinstance(icon, dict) or not isinstance(icon.get("src"), str): + continue + src = icon["src"] + resolved = resolve_site_asset_href(rel, src) + if resolved is None: + _emit("publish", f"remote or escaping manifest icon src ({src[:36]}) (section 13)") + elif not (root / resolved).is_file(): + _emit("links", f"broken manifest icon link -> {src}") + return + + if suffix not in (".html", ".htm", ".xhtml", ".svg", ".xml"): return - if suffix in (".html", ".htm", ".xhtml", ".svg", ".xml"): - for rx, label in DISALLOWED_SITE_HTML_RE: - m = rx.search(text) - if m: - _emit(f"{label}: {m.group(0).strip()[:36]}") - parser = _RenderedHTMLNormalizer(xml_mode=suffix in (".svg", ".xml", ".xhtml"), page_rel=rel) - parser.feed(text) - parser.close() - if parser._raw_tag is not None: - parser._flush_raw_tag(parser._raw_tag) - _ensure_html_root(parser) - for iss in parser.html_issues: - _emit(iss) - for iss in parser.inline_css_issues: - _emit(iss) - all_rules, css_issues, breakpoints = _collect_page_stylesheet_rules( - rel, parser.stylesheet_entries, css_by_rel=css_by_rel - ) - for iss in css_issues: - _emit(iss) - if len(breakpoints) > 32: - _emit(f"too many CSS @media breakpoints ({len(breakpoints)} > 32) in site/ page") - check_viewports: list[float | None] = [None, *_collect_active_viewports(all_rules, breakpoints)] - for _ in _iter_runtime_dom_states(parser, rel): - for iss in _check_cascaded_dom_integrity(parser.dom_root, all_rules, check_viewports): - _emit(iss) - if len(breakpoints) > 32: - _emit(f"too many CSS @media breakpoints ({len(breakpoints)} > 32) in site/ page") - if suffix != ".svg": - svg_matches = list(re.finditer(r"(?is)<\s*svg\b.*?</\s*svg\s*>", text)) - open_svgs = len(re.findall(r"(?i)<\s*svg\b", text)) - close_svgs = len(re.findall(r"(?i)</\s*svg\s*>", text)) - if open_svgs != close_svgs or len(svg_matches) != open_svgs: - _emit("unapproved inline vector graphic in site/") - for sm in svg_matches: - blk = sm.group(0) - svg_sha = hashlib.sha256(re.sub(r"\s+", " ", blk.strip()).encode("utf-8")).hexdigest() - if len(re.findall(r"(?i)<\s*svg\b", blk)) != 1 or svg_sha not in APPROVED_SITE_INLINE_SVGS: - _emit("unapproved inline vector graphic in site/") - non_svg_html = re.sub(r"(?is)<\s*svg\b.*?</\s*svg\s*>", "", text) - if re.search(r"(?i)<\s*(?:svg|path|polygon|polyline|rect|circle|ellipse|line)\b", non_svg_html): - _emit("unapproved inline vector graphic in site/") - for src, raw_body in parser.script_tags: - if src is not None: - resolved_src = resolve_site_asset_href(rel, src) - expected_sha = APPROVED_SITE_SCRIPT_FILES.get(resolved_src) if resolved_src else None - actual_sha = (script_hashes_by_rel or {}).get(resolved_src) if resolved_src else None - if resolved_src is None or expected_sha is None or actual_sha != expected_sha: - _emit(f"unapproved executable script src in site/ ({src[:36]})") - if raw_body.strip(): - _emit("unapproved executable inline script in site/") - else: - body_sha = hashlib.sha256(raw_body.encode("utf-8")).hexdigest() if raw_body.strip() else "" - if body_sha not in APPROVED_SITE_INLINE_SCRIPTS: - _emit("unapproved executable inline script in site/") - - -def collect_site_context(root: Path) -> tuple[dict[str, str], dict[str, str], list[dict[str, str]]]: - """Collect site/ CSS sources by relpath, script SHA-256 digests, and approved i18n dictionaries.""" - css_by_rel: dict[str, str] = {} - script_hashes_by_rel: dict[str, str] = {} - first_map: dict[str, str] = {} - last_map: dict[str, str] = {} - kv_re = re.compile( - r"(?:([a-zA-Z_][a-zA-Z0-9_]*)|\"([^\"]+)\"|'([^']+)')\s*:\s*" - r"(?:\"((?:[^\"\\]|\\.)*)\"|'((?:[^'\\]|\\.)*)')" - ) - for path in iter_files(root): - rel = path.relative_to(root).as_posix() - if not rel.startswith("site/"): - continue - suffix = path.suffix.lower() - if suffix not in (".css", *SITE_SCRIPT_SUFFIXES): - continue - raw = path.read_bytes() - status, text = _decode_site_text(raw) - if suffix == ".css": - if status == "text" and text is not None: - css_by_rel[rel] = text - else: - sha = hashlib.sha256(raw).hexdigest() - script_hashes_by_rel[rel] = sha - if status == "text" and text is not None and sha == APPROVED_SITE_SCRIPT_FILES.get(rel): - dec = fold_js_expressions(text) - for m in kv_re.finditer(dec): - k = m.group(1) or m.group(2) or m.group(3) - v = m.group(4) if m.group(4) is not None else (m.group(5) or "") - if k not in first_map: - first_map[k] = v - last_map[k] = v - i18n_maps = [m for m in (first_map, last_map) if m] - return css_by_rel, script_hashes_by_rel, i18n_maps + + parser = _parse_site_html(text, page_rel=rel) + for cat, msg in parser.html_issues: + _emit(cat, f"{msg} (section 13)") + for css_blk in parser.style_blocks: + _check_css_text(css_blk) + if suffix not in (".html", ".htm", ".xhtml"): + return + + for kind, target in parser.asset_refs: + if kind in ("link", "script", "img"): + if re.match(r"^(?:data|javascript|vbscript):", target.strip(), re.IGNORECASE): + continue + resolved = resolve_site_asset_href(rel, target) + if resolved is None: + _emit("publish", f"remote or escaping {kind} asset in site/ ({target[:36]}) (section 13)") + elif not (root / resolved).is_file(): + _emit("links", f"broken local {kind} link -> {target}") + elif kind == "anchor": + raw = target.strip() + if raw.startswith("#"): + frag = raw[1:] + if not frag or frag not in parser.element_ids: + _emit("links", f"broken fragment link -> {target}") + elif raw == "/" or raw.startswith(("/#", "/?")): + if not (root / "site/index.html").is_file(): + _emit("links", f"broken relative link -> {target}") + elif rel == "site/index.html" and raw.startswith("/#"): + frag = raw[2:] + if not frag or frag not in parser.element_ids: + _emit("links", f"broken fragment link -> {target}") + elif raw and not re.match(r"^(?:https?:|mailto:|tel:|javascript:|vbscript:|data:|//)", raw, re.IGNORECASE): + resolved = resolve_site_asset_href(rel, raw) + if resolved is None: + _emit("links", f"broken relative link -> {target}") + else: + target_path = root / resolved + if not (target_path.is_file() or (target_path / "index.html").is_file()): + _emit("links", f"broken relative link -> {target}") def normalized_site_views( path: Path, text: str, page_rel: str = "site/index.html", - css_by_rel: dict[str, str] | None = None, - i18n_maps: list[dict[str, str]] | tuple[dict[str, str], ...] = (), ) -> list[str]: """Build raw and decoded/rendered views of a deployed site/ text asset.""" views: list[str] = [] @@ -2400,17 +953,16 @@ def _add(candidate: str) -> None: views.append(candidate) _add(text) - stripped = INVISIBLE_FORMAT_RE.sub("", text) - _add(stripped) + _add(INVISIBLE_FORMAT_RE.sub("", text)) js_dec = INVISIBLE_FORMAT_RE.sub("", fold_js_expressions(text)) _add(js_dec) suffix = path.suffix.lower() if suffix in (".css", ".html", ".htm", ".xhtml", ".svg"): _add(INVISIBLE_FORMAT_RE.sub("", unescape_css_literals(text))) if suffix in (".html", ".htm", ".xhtml", ".svg", ".xml") or "<" in js_dec or "&" in js_dec: - _add(normalize_html_text(text, page_rel=page_rel, css_by_rel=css_by_rel, i18n_maps=i18n_maps)) + _add(normalize_html_text(text, page_rel=page_rel)) if js_dec != text: - _add(normalize_html_text(js_dec, page_rel=page_rel, css_by_rel=css_by_rel, i18n_maps=i18n_maps)) + _add(normalize_html_text(js_dec, page_rel=page_rel)) for base_view in list(views): cur = base_view for _ in range(2): @@ -2502,9 +1054,6 @@ def _scan_leak_patterns( ) -> None: for pattern, category, label, predicate in patterns: for match in pattern.finditer(text): - # A pattern without a predicate describes prose, and a fenced - # block is not prose. The style guide shows each of these - # shapes as an example of what to avoid. if predicate is None and not ctx.prose[match.start():match.end()].strip(): continue if predicate is not None and not predicate(text, match, ctx): @@ -2518,12 +1067,14 @@ def _scan_leak_patterns( def check_leaks(root: Path, report: Report) -> None: + """Scan published site/ assets and repository source files for leaked identifiers.""" site_path_patterns = [*LEAK_PATTERNS[:4], *SITE_LEAK_PATTERNS] - site_css_by_rel, site_script_hashes, site_i18n_maps = collect_site_context(root) for path in iter_files(root): rel = path.relative_to(root).as_posix() markdown = is_markdown(path) if rel.startswith("site/"): + if " " in path.name: + report.add("naming", f"{rel}: file name contains a space") seen_for_file: set[tuple[str, str]] = set() site_rel_path = "/" + rel[len("site/"):] path_views = [site_rel_path] @@ -2539,6 +1090,7 @@ def check_leaks(root: Path, report: Report) -> None: for pv in path_views: path_ctx = FileContext(url_spans=[], prose=pv, masked=pv) _scan_leak_patterns(rel, pv, site_path_patterns, path_ctx, report, seen_for_file) + status, site_text = read_deployed_site_asset(root, path) if status == "unscanned_binary": report.add( @@ -2546,47 +1098,20 @@ def check_leaks(root: Path, report: Report) -> None: f"{rel}: unscanned binary or encrypted asset in published site/ tree (section 13)", ) continue - if status == "unapproved_raster": - report.add( - "publish", - f"{rel}: unapproved or uninspected raster asset in published site/ tree (section 13)", - ) - if not site_text: - continue - elif status == "unapproved_vector": - report.add( - "publish", - f"{rel}: unapproved or uninspected vector asset in published site/ tree (section 13)", - ) - if not site_text: - continue - elif status == "unapproved_script": + if status == "png_metadata": report.add( "publish", - f"{rel}: unapproved or uninspected executable script in published site/ tree (section 13)", + f"{rel}: PNG ancillary text metadata chunk (tEXt/zTXt/iTXt) not allowed in published site/ tree (section 13)", ) if not site_text: continue elif status != "text" or site_text is None: continue - check_site_visual_integrity( - rel, - path, - site_text, - report, - seen_for_file, - css_by_rel=site_css_by_rel, - script_hashes_by_rel=site_script_hashes, - ) + + check_site_links_and_safety(root, rel, path, site_text, report, seen_for_file) patterns = list(LEAK_PATTERNS) if markdown else list(LEAK_PATTERNS[:4]) patterns.extend(SITE_LEAK_PATTERNS) - for view in normalized_site_views( - path, - site_text, - page_rel=rel, - css_by_rel=site_css_by_rel, - i18n_maps=site_i18n_maps, - ): + for view in normalized_site_views(path, site_text, page_rel=rel): prose = blank_fenced_blocks(view) if markdown else view masked = blank_code_spans(prose) if markdown else view ctx = FileContext( @@ -2596,6 +1121,7 @@ def check_leaks(root: Path, report: Report) -> None: ) _scan_leak_patterns(rel, view, patterns, ctx, report, seen_for_file) continue + if markdown: text = path.read_text(encoding="utf-8", errors="ignore") repo_patterns = [*LEAK_PATTERNS, *IDENTIFIER_LEAK_PATTERNS] @@ -2611,6 +1137,7 @@ def check_leaks(root: Path, report: Report) -> None: else decoded_txt ) repo_patterns = [*LEAK_PATTERNS[:4], *IDENTIFIER_LEAK_PATTERNS] + seen_repo: set[tuple[str, str]] = set() repo_views: list[str] = [] for base_cand in (text, html.unescape(text), fold_js_expressions(text)): @@ -2657,15 +1184,11 @@ def matching_internal_note_pattern(rel: str) -> str | None: def check_internal_paths(root: Path, report: Report) -> None: + """Verify .gitignore protections and reject tracked internal working notes.""" gitignore = root / ".gitignore" text = gitignore.read_text(encoding="utf-8", errors="ignore") if gitignore.exists() else "" missing = [pattern for pattern in INTERNAL_NOTE_GITIGNORE if pattern not in text] if missing: - # Blocking in both phases. This is the first of the three layers that - # keep working notes out of a public repository, and it is the only one - # that acts before the note is committed at all. A repository that - # loses it stops preventing the mistake and starts detecting it, which - # is a different and much later guarantee. report.add( "hygiene", ".gitignore: internal note paths not ignored — " + ", ".join(missing), @@ -2697,8 +1220,6 @@ def check_internal_paths(root: Path, report: Report) -> None: report.add("content", f"{rel}: committed output directory (section 5)") - - def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--root", default=".", help="repository root (default: cwd)") diff --git a/scripts/test_site.py b/scripts/test_site.py index a6b8f50..e0b2ded 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -1,16 +1,4 @@ -"""Regression tests for the documentation checker. - -This checker runs against every repository that adopts the rule set, from one -copy or many, so a rule that silently stops working fails everywhere at once -and nobody notices. Each test here pins one decision that was easy to get -wrong: - - * the leak heuristics must not report the placeholder forms the guide asks - for, or the report fills with non-defects and gets ignored - * the .gitignore block must be required, not suggested, or the layer that - stops a note being committed disappears without a trace - * phase 1 must stay quiet about naming, or every open pull request goes red - the day the checker lands +"""Regression tests for the QuantStrategyLab site publication and leak checker. Run with `python scripts/test_site.py`. """ @@ -20,9 +8,11 @@ import importlib.util import io import re +import struct import subprocess import tempfile import unittest +import zlib from contextlib import redirect_stdout from pathlib import Path @@ -36,6 +26,29 @@ SHA = "8e8ec51884bf8abb0a7ca699fc2da39fad134f89" +def _png_chunk(ctype: bytes, data: bytes) -> bytes: + crc = zlib.crc32(ctype + data) & 0xFFFFFFFF + return struct.pack(">I4s", len(data), ctype) + data + struct.pack(">I", crc) + + +def _make_png(*extra_chunks: bytes) -> bytes: + ihdr = struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0) + idat = zlib.compress(b"\x00\xff\x00\x00") + return ( + b"\x89PNG\r\n\x1a\n" + + _png_chunk(b"IHDR", ihdr) + + b"".join(extra_chunks) + + _png_chunk(b"IDAT", idat) + + _png_chunk(b"IEND", b"") + ) + + +def _make_ico(png_bytes: bytes) -> bytes: + header = struct.pack("<HHH", 0, 1, 1) + entry = struct.pack("<BBBBHHII", 1, 1, 0, 0, 1, 32, len(png_bytes), 22) + return header + entry + png_bytes + + class CheckerTestCase(unittest.TestCase): def setUp(self) -> None: self._temp = tempfile.TemporaryDirectory() @@ -66,8 +79,6 @@ def run_main(self, *extra: str) -> tuple[int, str]: return code, captured.getvalue() - - class LeakTest(CheckerTestCase): def findings(self, text: str) -> list[str]: self.write("docs/page.md", text) @@ -95,9 +106,6 @@ def test_real_bucket_is_reported(self): self.assertTrue(any("non-placeholder bucket" in m for m in found)) def test_placeholder_subdomain_is_not_reported(self): - # Section 13 asks for a placeholder where a deployed subdomain would - # appear. Reporting the guide's own recommended form is how a check - # teaches people to ignore it. for text in ( "Open https://YOUR_SUBDOMAIN.workers.dev/admin.\n", "Set the host to https://your-subdomain.workers.dev.\n", @@ -146,26 +154,8 @@ def test_windows_paths_are_reported_while_placeholders_pass(self): self.assertTrue(any("Windows deployment path" in m for m in found), found) def test_deployed_site_text_assets_are_scanned_for_identifier_leaks(self): - import pathlib - import struct - import zlib - - repo_root = pathlib.Path(check_site.__file__).resolve().parent.parent - - def png_chunk(ctype: bytes, data: bytes) -> bytes: - return struct.pack(">I4s", len(data), ctype) + data + struct.pack(">I", zlib.crc32(ctype + data) & 0xFFFFFFFF) - - def make_png(*extra_chunks: bytes) -> bytes: - ihdr = struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0) - idat = zlib.compress(b"\x00\xff\x00\x00") - return b"\x89PNG\r\n\x1a\n" + png_chunk(b"IHDR", ihdr) + b"".join(extra_chunks) + png_chunk(b"IDAT", idat) + png_chunk(b"IEND", b"") - - def make_ico(png_bytes: bytes) -> bytes: - header = struct.pack("<HHH", 0, 1, 1) - entry = struct.pack("<BBBBHHII", 1, 1, 0, 0, 1, 32, len(png_bytes), 22) - return header + entry + png_bytes - - clean_png = make_png() + repo_root = Path(check_site.__file__).resolve().parent.parent + clean_png = _make_png() approved_inline_svg = re.search( r"<svg\b.*?</svg>", (repo_root / "site/index.html").read_text(encoding="utf-8"), @@ -193,10 +183,13 @@ def make_ico(png_bytes: bytes) -> bytes: (self.root / "site/landing-light-192.png").write_bytes((repo_root / "site/landing-light-192.png").read_bytes()) (self.root / "site/landing-light.svg").write_bytes((repo_root / "site/landing-light.svg").read_bytes()) (self.root / "site/favicon.ico").write_bytes((repo_root / "site/favicon.ico").read_bytes()) + (self.root / "site/clean-extra.png").write_bytes(clean_png) + (self.root / "site/clean-extra.ico").write_bytes(_make_ico(clean_png)) (self.root / "site/assets").mkdir(parents=True, exist_ok=True) (self.root / "site/assets/app.js").write_bytes((repo_root / "site/assets/app.js").read_bytes()) (self.root / "site/assets/i18n.js").write_bytes((repo_root / "site/assets/i18n.js").read_bytes()) self.write("site/unused.css", "p { display: none; }\n") + clean_report = self.report() check_site.check_leaks(self.root, clean_report) self.assertEqual(clean_report.findings, []) @@ -218,57 +211,29 @@ def make_ico(png_bytes: bytes) -> bytes: ) self.write( "site/rendered-leak.html", - '<p>/home/<strong>operator</strong>/prod/config and ' - 'db-primary.<span>ops</span>.internal</p>' + "<p>/home/<strong>operator</strong>/prod/config and " + "db-primary.<span>ops</span>.internal</p>" '<a href="https://actual-private-app.workers.dev">x</a>\n', ) self.write( - "site/css-inline-leak.html", - '<style>.join{display:inline}</style>' - '<span>/home/</span><div class="join">operator</div><span>/prod/config</span>\n', - ) - self.write( - "site/css-flex-leak.html", - '<style>.row{display:flex}</style><div class="row">\n' - ' <div>db-primary.</div>\n' - ' <div>ops.internal</div>\n' - '</div>\n', - ) - self.write( - "site/css-order-leak.html", - '<style>.row{display:flex}.a{order:2}.b{order:1}.c{order:3}</style>' - '<div class="row"><span class="a">operator</span>' - '<span class="b">/home/</span><span class="c">/prod/config</span></div>\n', - ) - self.write( - "site/css-grid-leak.html", - '<div style="display:grid;grid-template-columns:1fr">' - '<span style="grid-row:2;grid-column:1">operator</span>' - '<span>/home/</span><span>/prod/config</span></div>\n', - ) - self.write( - "site/css-hidden-sep.html", - '<style>.h{display:none}</style>' - '<span>/home/</span><span class="h">SAFE</span><span>operator/prod/config</span>\n', - ) - self.write( - "site/css-reverse.html", - '<div style="display:flex;flex-direction:row-reverse"><span>a</span><span>b</span></div>\n', + "site/html-concat-leak.html", + "<span>/home/</span><div>operator</div><span>/prod/config</span>" + "<div>\n db-primary.\n ops.internal\n</div>\n", ) self.write( "site/escaped-leak.js", 'const p = "\\u002fhome\\u002foperator\\u002fprod\\u002fconfig";\n', ) + self.write("site/indirect.js", 'const p = ["/U", "sers/", "bob"].join("");\n') + self.write("site/inline-js.html", '<script>const p = ["/U", "sers/", "bob"].join("");</script>\n') self.write( - "site/data-uri.html", - '<img src="data:image/png;base64,iVBORw0KGgo=">\n', + "site/self-closing-script.html", + "<script/>const p = String.fromCharCode.apply(null, [47,104,111,109,101,47,111,112,101,114,97,116,111,114]);</script>\n", ) + self.write("site/data-uri.html", '<img src="data:image/png;base64,iVBORw0KGgo=">\n') self.write("site/actual-private-app.workers.dev/index.html", "<p>benign page content</p>\n") self.write("site/db-primary.ops.internal/cfg.html", "<p>benign internal path</p>\n") - self.write( - "site/assets/app.js.map", - '{"sourceRoot":"gs://actual-prod-bucket/maps"}\n', - ) + self.write("site/assets/app.js.map", '{"sourceRoot":"gs://actual-prod-bucket/maps"}\n') self.write( "site/dist/runtime-config.json", '{"path":"/home/operator/prod/config","url":"https://actual-private-app.workers.dev"}\n', @@ -279,41 +244,21 @@ def make_ico(png_bytes: bytes) -> bytes: "site/private-ip.html", "<!-- internal: http://10.23.45.67:8080, v6: http://[fd12:3456:789a::1]/api, meta: 169.254.169.254, ll: fe80::1 -->\n", ) - (self.root / "site/unapproved-pixel.png").write_bytes(clean_png) - (self.root / "site/unapproved.ico").write_bytes(make_ico(clean_png)) - (self.root / "site/landing-light-192.png").write_bytes(clean_png) - # Re-compress/append trailing payload inside IDAT of an otherwise approved PNG. - approved_bytes = (repo_root / "site/landing-light-512.png").read_bytes() - pos = 8 - tampered = bytearray(b"\x89PNG\r\n\x1a\n") - while pos < len(approved_bytes): - clen, ctype = struct.unpack(">I4s", approved_bytes[pos:pos + 8]) - cdata = approved_bytes[pos + 8:pos + 8 + clen] - if ctype == b"IDAT": - cdata = cdata + b"/home/operator/prod/config db-primary.ops.internal" - tampered.extend(png_chunk(ctype, cdata)) - pos += 12 + clen - (self.root / "site/landing-light-512.png").write_bytes(bytes(tampered)) - iend_src = (repo_root / "site/landing-light-apple-touch-180.png").read_bytes() - iend_out = bytearray(b"\x89PNG\r\n\x1a\n") - ipos = 8 - while ipos < len(iend_src): - clen, ctype = struct.unpack(">I4s", iend_src[ipos:ipos + 8]) - cdata = iend_src[ipos + 8:ipos + 8 + clen] - if ctype == b"IEND": - cdata = b"/home/operator/prod/config actual-private-app.workers.dev" - iend_out.extend(png_chunk(ctype, cdata)) - ipos += 12 + clen - (self.root / "site/landing-light-apple-touch-180.png").write_bytes(bytes(iend_out)) - (self.root / "site/leak-meta.png").write_bytes( - make_png( - png_chunk(b"tEXt", b"Comment\x00deploy /home/operator/prod/config http://10.23.45.67"), - png_chunk(b"zTXt", b"Host\x00\x00" + zlib.compress(b"https://actual-private-app.workers.dev")), - ) + self.write( + "site/mixed-case.json", + '{"url": "HTTPS://REAL-LEAK.WORKERS.DEV", "bucket": "GS://CORP-QUANT-BUCKET", ' + '"arn": "ARN:AWS:IAM::123456789012:ROLE/OPS", ' + '"sa": "DEPLOY@CORP-QUANT-PROD.IAM.GSERVICEACCOUNT.COM"}\n', ) + self.write( + "site/percent-config.json", + '{"config":"%2Fhome%2Foperator%2Fprod%2Fconfig","url":"https%3A%2F%2Factual-private-app.workers.dev"}\n', + ) + self.write("site/bad-handler.html", '<img src="landing-light-192.png" onclick="evil()"><a href="javascript:evil()">x</a>\n') + self.write("site/dup-script-src.html", '<script src="https://evil.invalid/p.js" src="assets/app.js"></script>\n') (self.root / "site/release.pdf").write_bytes(b"%PDF-1.4\n1 0 obj\n<<>>\nendobj\n%%EOF\n") (self.root / "site/bundle.zip").write_bytes(b"PK\x03\x04\x00\x00payload") - (self.root / "site/corrupt.png").write_bytes(b"not a real png\x00") + dirty_report = self.report() check_site.check_leaks(self.root, dirty_report) check_site.check_internal_paths(self.root, dirty_report) @@ -329,196 +274,133 @@ def make_ico(png_bytes: bytes) -> bytes: self.assertTrue(any("site/private-ip.html" in m and "private IPv4 address" in m and "169.254.169.254" in m for m in msgs)) self.assertTrue(any("site/private-ip.html" in m and "private IPv6 address" in m and "fd12:3456:789a::1" in m for m in msgs)) self.assertTrue(any("site/private-ip.html" in m and "private IPv6 address" in m and "fe80::1" in m for m in msgs)) - self.assertTrue(any("site/leak-meta.png" in m and "absolute home path" in m for m in msgs)) - self.assertTrue(any("site/leak-meta.png" in m and "private IPv4 address" in m for m in msgs)) - self.assertTrue(any("site/leak-meta.png" in m and "personal subdomain" in m for m in msgs)) - self.assertTrue(any("site/leak-meta.png" in m and "unapproved or uninspected raster asset" in m for m in msgs)) - self.assertTrue(any("site/unapproved-pixel.png" in m and "unapproved or uninspected raster asset" in m for m in msgs)) - self.assertTrue(any("site/unapproved.ico" in m and "unapproved or uninspected raster asset" in m for m in msgs)) - self.assertTrue(any("site/landing-light-192.png" in m and "unapproved or uninspected raster asset" in m for m in msgs)) - self.assertTrue(any("site/landing-light-512.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) - self.assertTrue(any("site/landing-light-apple-touch-180.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) self.assertEqual(sum(1 for m in msgs if "site/runtime-config.yaml" in m and "Windows deployment path" in m), 2) self.assertTrue(any("site/release.pdf" in m and "unscanned binary or encrypted asset" in m for m in msgs)) self.assertTrue(any("site/bundle.zip" in m and "unscanned binary or encrypted asset" in m for m in msgs)) - self.assertTrue(any("site/corrupt.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) self.assertTrue(any("site/runtime-config.yaml" in m and "internal hostname" in m for m in msgs)) self.assertEqual(sum(1 for m in msgs if "site/runtime-config.yaml" in m and "account identifier" in m), 3) self.assertEqual(sum(1 for m in msgs if "site/runtime-config.json" in m and "account identifier" in m), 2) self.assertTrue(any("site/rendered-leak.html" in m and "absolute home path" in m for m in msgs)) self.assertTrue(any("site/rendered-leak.html" in m and "internal hostname" in m for m in msgs)) self.assertTrue(any("site/rendered-leak.html" in m and "personal subdomain" in m for m in msgs)) - self.assertTrue(any("site/css-inline-leak.html" in m and "absolute home path" in m for m in msgs)) - self.assertTrue(any("site/css-flex-leak.html" in m and "internal hostname" in m for m in msgs)) - self.assertTrue(any("site/css-order-leak.html" in m and "absolute home path" in m for m in msgs)) - self.assertTrue(any("site/css-grid-leak.html" in m and "absolute home path" in m for m in msgs)) - self.assertTrue(any("site/css-hidden-sep.html" in m and "absolute home path" in m for m in msgs)) - self.assertTrue(any("site/css-reverse.html" in m and "reverse flex visual ordering" in m for m in msgs)) + self.assertTrue(any("site/html-concat-leak.html" in m and "absolute home path" in m for m in msgs)) + self.assertTrue(any("site/html-concat-leak.html" in m and "internal hostname" in m for m in msgs)) self.assertTrue(any("site/escaped-leak.js" in m and "absolute home path" in m for m in msgs)) + self.assertTrue(any("site/indirect.js" in m and "absolute home path" in m for m in msgs)) + self.assertTrue(any("site/inline-js.html" in m and "absolute home path" in m for m in msgs)) + self.assertTrue(any("site/self-closing-script.html" in m and "self-closing script or style tag" in m for m in msgs)) + self.assertTrue(any("site/self-closing-script.html" in m and "absolute home path" in m for m in msgs)) self.assertTrue(any("site/data-uri.html" in m and "embedded data URI asset" in m for m in msgs)) self.assertTrue(any("site/actual-private-app.workers.dev/index.html" in m and "personal subdomain" in m for m in msgs)) self.assertTrue(any("site/db-primary.ops.internal/cfg.html" in m and "internal hostname" in m for m in msgs)) + self.assertTrue(any("site/mixed-case.json" in m and "personal subdomain" in m for m in msgs)) + self.assertTrue(any("site/mixed-case.json" in m and "non-placeholder bucket" in m for m in msgs)) + self.assertTrue(any("site/mixed-case.json" in m and "account identifier" in m and "ARN:AWS:" in m for m in msgs)) + self.assertTrue(any("site/mixed-case.json" in m and "account identifier" in m and "DEPLOY@CORP-QUANT-PROD" in m for m in msgs)) + self.assertTrue(any("site/percent-config.json" in m and "absolute home path" in m for m in msgs)) + self.assertTrue(any("site/percent-config.json" in m and "personal subdomain" in m for m in msgs)) + self.assertTrue(any("site/bad-handler.html" in m and "inline event handler attribute" in m for m in msgs)) + self.assertTrue(any("site/bad-handler.html" in m and "javascript: or vbscript: URI" in m for m in msgs)) + self.assertTrue(any("site/dup-script-src.html" in m and "duplicate HTML attribute in site/ (src)" in m for m in msgs)) + self.assertTrue(any("site/dup-script-src.html" in m and "remote or escaping script asset in site/" in m for m in msgs)) + + def test_png_structure_and_metadata_chunks_are_enforced(self): + repo_root = Path(check_site.__file__).resolve().parent.parent - self.write( - "site/mixed-case.json", - '{"url": "HTTPS://REAL-LEAK.WORKERS.DEV", "bucket": "GS://CORP-QUANT-BUCKET", ' - '"arn": "ARN:AWS:IAM::123456789012:ROLE/OPS", ' - '"sa": "DEPLOY@CORP-QUANT-PROD.IAM.GSERVICEACCOUNT.COM"}\n', - ) - self.write("site/a.css", ".vis-box { display: flex; } .f1 { order: 2; } .f2 { order: 1; }\n") - self.write("site/z.css", ".vis-box { display: none; }\n") - self.write( - "site/linked-css.html", - '<link rel="stylesheet" href="a.css">\n' - '<div class="vis-box"><span class="f1">/alice/secret</span><span class="f2">/Users</span></div>\n', - ) - self.write("site/indirect.js", 'const p = ["/U", "sers/", "bob"].join("");\n') - self.write("site/inline-js.html", '<script>const p = ["/U", "sers/", "bob"].join("");</script>\n') - self.write( - "site/media-order.html", - "<style>\n" - ".mwrap { display: flex; }\n" - "@media (max-width: 600px) { .ma { order: 2; } .mb { order: 1; } }\n" - "@media (min-width: 601px) { .ma { order: 1; } .mb { order: 2; } }\n" - "</style>\n" - '<div class="mwrap"><span class="ma">/alice/mobile</span><span class="mb">/Users</span></div>\n', - ) - self.write("site/nonpixel-media.css", "@media (prefers-color-scheme: dark) { .hid { display: none; } }\n") - self.write("site/bad-handler.html", '<img src="landing-light-192.png" onclick="evil()"><a href="javascript:evil()">x</a>\n') - self.write( - "site/sr-only-sentinel.html", - "<style>.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0)}</style>\n" - '<p>actual-private-app.work<span class="sr-only">SAFE</span>ers.dev</p>\n', - ) - self.write( - "site/clip-path-hide.html", - "<style>.hidden{position:absolute;clip-path:inset(100%)}</style>\n" - '<p>actual-private-app.work<span class="hidden">SAFE</span>ers.dev</p>\n', - ) - self.write("site/zero-width-hide.html", "<style>.z{width:0;overflow:hidden}</style>\n<p>x</p>\n") - self.write( - "site/animation-order.css", - "@keyframes flip { from { order: 1; } to { order: 2; } }\n.a { animation: flip 1s infinite; }\n", - ) - self.write( - "site/animation-order.html", - '<link rel="stylesheet" href="animation-order.css">\n' - '<div style="display:flex"><span class="a">/prod/config</span><span>/home/operator</span></div>\n', - ) - self.write("site/stmt-desync.css", "@layer reset; .x { order: 2; }\n") - self.write( - "site/self-closing-script.html", - '<script/>const p = String.fromCharCode.apply(null, [47,104,111,109,101,47,111,112,101,114,97,116,111,114]);</script>\n', - ) - self.write( - "site/important-dup.html", - '<style>.h{display:none!important;display:block}</style>\n' - '<p>actual-private-app.work<span class="h">SAFE</span>ers.dev</p>\n', - ) - self.write( - "site/percent-config.json", - '{"config":"%2Fhome%2Foperator%2Fprod%2Fconfig","url":"https%3A%2F%2Factual-private-app.workers.dev"}\n', - ) - self.write( - "site/dot-pseudo.html", - '<style>.dot::before { content: "."; }</style>\n' - '<p>actual-private-app<span class="dot">workers</span><span class="dot">dev</span></p>\n', - ) - self.write( - "site/prin-abuse.html", - '<link rel="stylesheet" href="assets/styles.css">\n' - '<ol class="prin" style="display:flex"><span>actual-private-</span><li style="display:inline">workers.dev</li></ol>\n', - ) - self.write( - "site/flex-zero-clip.html", - "<style>.w{display:flex}.z{flex:0 0 0;overflow:hidden}</style>" - '<div class="w"><span>actual-private-app.work</span><span class="z">SAFE</span><span>ers.dev</span></div>\n', - ) - self.write( - "site/grid-zero-track.html", - '<div style="display:grid;grid-template-columns:auto 0px auto">' - "<span>actual-private-app.work</span><span>SAFE</span><span>ers.dev</span></div>\n", - ) - self.write( - "site/outlined-leak.svg", - '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 20"><path d="M1 1h10"/></svg>\n', + approved_bytes = (repo_root / "site/landing-light-512.png").read_bytes() + pos = 8 + tampered = bytearray(b"\x89PNG\r\n\x1a\n") + while pos < len(approved_bytes): + clen, ctype = struct.unpack(">I4s", approved_bytes[pos:pos + 8]) + cdata = approved_bytes[pos + 8:pos + 8 + clen] + if ctype == b"IDAT": + cdata = cdata + b"/home/operator/prod/config db-primary.ops.internal" + tampered.extend(_png_chunk(ctype, cdata)) + pos += 12 + clen + (self.root / "site").mkdir(parents=True, exist_ok=True) + (self.root / "site/tampered-idat.png").write_bytes(bytes(tampered)) + + iend_src = (repo_root / "site/landing-light-apple-touch-180.png").read_bytes() + iend_out = bytearray(b"\x89PNG\r\n\x1a\n") + ipos = 8 + while ipos < len(iend_src): + clen, ctype = struct.unpack(">I4s", iend_src[ipos:ipos + 8]) + cdata = iend_src[ipos + 8:ipos + 8 + clen] + if ctype == b"IEND": + cdata = b"/home/operator/prod/config actual-private-app.workers.dev" + iend_out.extend(_png_chunk(ctype, cdata)) + ipos += 12 + clen + (self.root / "site/nonempty-iend.png").write_bytes(bytes(iend_out)) + + (self.root / "site/corrupt.png").write_bytes(b"not a real png\x00") + (self.root / "site/leak-meta.png").write_bytes( + _make_png( + _png_chunk(b"tEXt", b"Comment\x00deploy /home/operator/prod/config http://10.23.45.67"), + _png_chunk(b"zTXt", b"Host\x00\x00" + zlib.compress(b"https://actual-private-app.workers.dev")), + ) ) - self.write( - "site/inline-vector.html", - '<p>x</p><svg viewBox="0 0 10 10"><path d="M0 0h5"/></svg>\n', + (self.root / "site/leak-itxt.png").write_bytes( + _make_png( + _png_chunk(b"iTXt", b"Description\x00\x01\x00en\x00\x00" + zlib.compress(b"gs://actual-prod-bucket/img")), + ) ) - self.write( - "site/vis-override.html", - '<div style="visibility:hidden">SAFE<span style="visibility:visible">' - 'actual-private-app.work<span style="display:none">SAFE</span>ers.dev</span></div>\n', + (self.root / "site/benign-meta.png").write_bytes( + _make_png(_png_chunk(b"tEXt", b"Software\x00Builder")) ) - self.write( - "site/dup-script-src.html", - '<script src="https://evil.invalid/p.js" src="assets/app.js"></script>\n', + (self.root / "site/ico-with-meta.ico").write_bytes( + _make_ico(_make_png(_png_chunk(b"tEXt", b"Author\x00/home/operator/secret"))) ) + + report = self.report() + check_site.check_leaks(self.root, report) + msgs = [m for _, m in report.findings] + self.assertTrue(any("site/tampered-idat.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) + self.assertTrue(any("site/nonempty-iend.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) + self.assertTrue(any("site/corrupt.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) + self.assertTrue(any("site/leak-meta.png" in m and "absolute home path" in m for m in msgs)) + self.assertTrue(any("site/leak-meta.png" in m and "private IPv4 address" in m for m in msgs)) + self.assertTrue(any("site/leak-meta.png" in m and "personal subdomain" in m for m in msgs)) + self.assertTrue(any("site/leak-meta.png" in m and "PNG ancillary text metadata chunk" in m for m in msgs)) + self.assertTrue(any("site/leak-itxt.png" in m and "PNG ancillary text metadata chunk" in m for m in msgs)) + self.assertTrue(any("site/leak-itxt.png" in m and "non-placeholder bucket" in m for m in msgs)) + self.assertTrue(any("site/benign-meta.png" in m and "PNG ancillary text metadata chunk" in m for m in msgs)) + self.assertTrue(any("site/ico-with-meta.ico" in m and "PNG ancillary text metadata chunk" in m for m in msgs)) + self.assertTrue(any("site/ico-with-meta.ico" in m and "absolute home path" in m for m in msgs)) + + def test_site_links_and_naming_are_checked(self): self.write( - "site/unmodeled-tag.html", - "<p>actual-private-app.work<noframes>SAFE</noframes>ers.dev</p>\n", + "site/index.html", + '<link rel="stylesheet" href="assets/missing.css">\n' + '<script src="assets/missing.js"></script>\n' + '<img src="missing.png">\n' + '<a href="#missing-id">jump</a>\n' + '<a href="missing-page.html">page</a>\n', ) self.write( - "site/ineffective-clip.html", - '<div style="clip:rect(0,0,0,0)">actual-private-app.work<span style="display:none">SAFE</span>ers.dev</div>\n', + "site/assets/styles.css", + '@import "https://cdn.example.invalid/x.css";\n' + 'body { background: url("missing-bg.png"); }\n', ) self.write( - "site/dark-theme-leak.html", - '<html data-theme="light"><head><style>html[data-theme=dark] .h{display:none}</style></head>' - '<body>actual-private-app.work<span class="h">SAFE</span>ers.dev' - '<script src="assets/app.js"></script></body></html>\n', + "site/manifest.webmanifest", + '{"icons": [{"src": "/missing-icon.png"}]}\n', ) - self.write( - "site/wrapped-col-flex.html", - '<div style="display:flex;flex-direction:column;flex-wrap:wrap;height:40px">' - "<span>actual-private-app.</span><span>SAFE</span><span>workers.dev</span><span>SAFE</span></div>\n", - ) - - extra_report = self.report() - check_site.check_leaks(self.root, extra_report) - extra_msgs = [f"{cat}: {detail}" for cat, detail in extra_report.findings] - self.assertTrue(any("site/mixed-case.json" in m and "personal subdomain" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/mixed-case.json" in m and "non-placeholder bucket" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/mixed-case.json" in m and "account identifier" in m and "ARN:AWS:" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/mixed-case.json" in m and "account identifier" in m and "DEPLOY@CORP-QUANT-PROD" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/linked-css.html" in m and "absolute home path" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/indirect.js" in m and "unapproved or uninspected executable script" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/inline-js.html" in m and "unapproved executable inline script" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/media-order.html" in m and "absolute home path" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/nonpixel-media.css" in m and "non-pixel conditional layout rule" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/bad-handler.html" in m and "inline event handler attribute" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/bad-handler.html" in m and "javascript: or vbscript: URI" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/sr-only-sentinel.html" in m and "personal subdomain" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/clip-path-hide.html" in m and "unmodeled CSS property (clip-path)" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/zero-width-hide.html" in m and "sub-2px box dimension hiding" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/animation-order.css" in m and "CSS @keyframes animation" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/animation-order.css" in m and "animation:" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/stmt-desync.css" in m and "unmodeled CSS statement" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/self-closing-script.html" in m and "self-closing script or style tag" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/self-closing-script.html" in m and "unapproved executable inline script" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/self-closing-script.html" in m and "absolute home path" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/important-dup.html" in m and "personal subdomain" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/percent-config.json" in m and "absolute home path" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/percent-config.json" in m and "personal subdomain" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/dot-pseudo.html" in m and "CSS content text injection" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/prin-abuse.html" in m and "personal subdomain" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/flex-zero-clip.html" in m and "flex:0 0 0" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/flex-zero-clip.html" in m and "overflow:hidden without clip:rect(0,0,0,0)" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/flex-zero-clip.html" in m and "personal subdomain" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/grid-zero-track.html" in m and "grid-template-columns:" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/outlined-leak.svg" in m and "unapproved or uninspected vector asset" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/inline-vector.html" in m and "unapproved inline vector graphic" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/vis-override.html" in m and "personal subdomain" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/dup-script-src.html" in m and "duplicate HTML attribute in site/ (src)" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/dup-script-src.html" in m and "remote or inline-scheme stylesheet or script in site/" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/dup-script-src.html" in m and "unapproved executable script src in site/" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/unmodeled-tag.html" in m and "unmodeled HTML element in site/ (<noframes>)" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/ineffective-clip.html" in m and "clip:rect(0,0,0,0) without position:absolute" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/ineffective-clip.html" in m and "personal subdomain" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/dark-theme-leak.html" in m and "personal subdomain" in m for m in extra_msgs), extra_msgs) - self.assertTrue(any("site/wrapped-col-flex.html" in m and "wrapped column flex visual reordering" in m for m in extra_msgs), extra_msgs) + self.write("site/bad name.txt", "hello\n") + + report = self.report() + check_site.check_leaks(self.root, report) + links = self.messages(report, "links") + publish = self.messages(report, "publish") + naming = self.messages(report, "naming") + self.assertTrue(any("assets/missing.css" in m for m in links), links) + self.assertTrue(any("assets/missing.js" in m for m in links), links) + self.assertTrue(any("missing.png" in m for m in links), links) + self.assertTrue(any("#missing-id" in m for m in links), links) + self.assertTrue(any("missing-page.html" in m for m in links), links) + self.assertTrue(any("missing-bg.png" in m for m in links), links) + self.assertTrue(any("/missing-icon.png" in m for m in links), links) + self.assertTrue(any("CSS @import external stylesheet" in m for m in publish), publish) + self.assertTrue(any("site/bad name.txt" in m and "space" in m for m in naming), naming) def test_tracked_workflow_and_config_files_scan_identifiers_with_placeholder_exemptions(self): self.write( @@ -642,7 +524,6 @@ def test_acceptance_policy_blocks_public_only(self): ) - class PhaseTest(CheckerTestCase): def test_phase_one_does_not_block_content(self): self.write("docs/release.md", "Default `gs://acme-prod-inputs/x.csv`.\n") @@ -721,7 +602,7 @@ def test_private_still_blocks_missing_internal_gitignore_rules(self): class GitVisibilityTest(CheckerTestCase): def test_gitignored_notes_do_not_block_while_staged_notes_do(self): self.write(".gitignore", GITIGNORE_BLOCK) - self.write("AGENTS.md", "# rules\n\n" + "\n") + self.write("AGENTS.md", "# rules\n\n\n") self.write("CLAUDE.md", "@AGENTS.md\n") self.write("SECURITY.md", "# Security\n") self.git("init", "--quiet") @@ -766,4 +647,4 @@ def test_gitignored_notes_do_not_block_while_staged_notes_do(self): if __name__ == "__main__": - unittest.main(verbosity=2) \ No newline at end of file + unittest.main(verbosity=2) From f8288d4ad6798944622edd4a50281182e5511fcd Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 07:30:11 +0800 Subject: [PATCH 02/12] fix(scripts): validate Adam7 PNGs, inline style URLs, and # links - Verify scanline lengths and filter bytes for non-interlaced and Adam7 interlaced PNG IDAT streams before marking them scanned. - Collect inline style attributes in _SiteHTMLParser for CSS resource checks and CSS-escape leak scanning. - Treat empty fragments (# and /#) as valid top-of-page navigation while continuing to flag missing element fragment targets. --- scripts/check_site.py | 43 +++++++++++++++++++++++++++++++++++------- scripts/test_site.py | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/scripts/check_site.py b/scripts/check_site.py index a577b2a..6ddcac7 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -309,6 +309,15 @@ def blank(match: re.Match) -> str: 4: {8, 16}, 6: {8, 16}, } +_ADAM7_PASSES = ( + (0, 8, 0, 8), + (4, 8, 0, 8), + (0, 4, 4, 8), + (2, 4, 0, 4), + (0, 2, 2, 4), + (1, 2, 0, 2), + (0, 1, 1, 2), +) ALLOWED_SITE_TEXT_SUFFIXES: set[str] = { ".html", ".htm", ".xhtml", ".css", ".js", ".mjs", ".cjs", ".svg", ".xml", @@ -384,6 +393,26 @@ def _decode_png_text_chunk(ctype: bytes, cdata: bytes) -> list[str] | None: return None +def _valid_png_scanlines( + decomp: bytes, width: int, height: int, bit_depth: int, color_type: int, interlace: int +) -> bool: + """Verify PNG decompressed scanline lengths and filter-type bytes for non-interlaced and Adam7 PNGs.""" + bits_per_pixel = PNG_CHANNELS[color_type] * bit_depth + passes = _ADAM7_PASSES if interlace == 1 else ((0, 1, 0, 1),) + offset = 0 + for xs, xstep, ys, ystep in passes: + if width <= xs or height <= ys: + continue + pass_w = (width - xs + xstep - 1) // xstep + pass_h = (height - ys + ystep - 1) // ystep + stride = (pass_w * bits_per_pixel + 7) // 8 + for _ in range(pass_h): + if offset + 1 + stride > len(decomp) or decomp[offset] > 4: + return False + offset += 1 + stride + return offset == len(decomp) + + def inspect_png_asset(raw: bytes) -> tuple[str, bool] | None: """Validate PNG magic, chunk CRCs, IHDR, IDAT zlib stream, IEND, and ancillary text chunks. @@ -451,10 +480,7 @@ def inspect_png_asset(raw: bytes) -> tuple[str, bool] | None: if decomp is None: return None width, height, bit_depth, color_type, interlace = ihdr - row_bytes = (width * PNG_CHANNELS[color_type] * bit_depth + 7) // 8 - if interlace == 0 and len(decomp) != (row_bytes + 1) * height: - return None - if interlace == 1 and not decomp: + if not _valid_png_scanlines(decomp, width, height, bit_depth, color_type, interlace): return None return "\n".join(parts), has_text_meta @@ -736,6 +762,9 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None self.attr_lines.append(f"{k}={v}") if "%" in v: self.attr_lines.append(f"{k}={urllib.parse.unquote(v)}") + if kl == "style": + self.style_blocks.append(v) + self.subtrees.append(unescape_css_literals(v)) if kl == "srcdoc" and self.depth < 3: self.subtrees.append(normalize_html_text(v, self.depth + 1, page_rel=self.page_rel)) if ad.get("id"): @@ -776,7 +805,7 @@ def _flush_raw_tag(self, tag_name: str) -> None: if ("<" in dec or "&" in dec) and self.depth < 3: self.subtrees.append(normalize_html_text(dec, self.depth + 1, page_rel=self.page_rel)) elif tag_name == "style": - self.style_blocks.append(unescape_css_literals(raw_body)) + self.style_blocks.append(raw_body) self.subtrees.append(unescape_css_literals(raw_body)) elif tag_name == "title": unesc = html.unescape(raw_body) @@ -921,14 +950,14 @@ def _check_css_text(css_text: str) -> None: raw = target.strip() if raw.startswith("#"): frag = raw[1:] - if not frag or frag not in parser.element_ids: + if frag and frag not in parser.element_ids: _emit("links", f"broken fragment link -> {target}") elif raw == "/" or raw.startswith(("/#", "/?")): if not (root / "site/index.html").is_file(): _emit("links", f"broken relative link -> {target}") elif rel == "site/index.html" and raw.startswith("/#"): frag = raw[2:] - if not frag or frag not in parser.element_ids: + if frag and frag not in parser.element_ids: _emit("links", f"broken fragment link -> {target}") elif raw and not re.match(r"^(?:https?:|mailto:|tel:|javascript:|vbscript:|data:|//)", raw, re.IGNORECASE): resolved = resolve_site_asset_href(rel, raw) diff --git a/scripts/test_site.py b/scripts/test_site.py index e0b2ded..403d0f9 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -350,6 +350,25 @@ def test_png_structure_and_metadata_chunks_are_enforced(self): (self.root / "site/ico-with-meta.ico").write_bytes( _make_ico(_make_png(_png_chunk(b"tEXt", b"Author\x00/home/operator/secret"))) ) + ihdr_interlaced = struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 1) + (self.root / "site/interlaced-valid.png").write_bytes( + b"\x89PNG\r\n\x1a\n" + + _png_chunk(b"IHDR", ihdr_interlaced) + + _png_chunk(b"IDAT", zlib.compress(b"\x00\xff\x00\x00")) + + _png_chunk(b"IEND", b"") + ) + (self.root / "site/interlaced-smuggled.png").write_bytes( + b"\x89PNG\r\n\x1a\n" + + _png_chunk(b"IHDR", ihdr_interlaced) + + _png_chunk(b"IDAT", zlib.compress(b"/home/operator/prod/config")) + + _png_chunk(b"IEND", b"") + ) + (self.root / "site/interlaced-bad-filter.png").write_bytes( + b"\x89PNG\r\n\x1a\n" + + _png_chunk(b"IHDR", ihdr_interlaced) + + _png_chunk(b"IDAT", zlib.compress(b"\x09\xff\x00\x00")) + + _png_chunk(b"IEND", b"") + ) report = self.report() check_site.check_leaks(self.root, report) @@ -357,6 +376,9 @@ def test_png_structure_and_metadata_chunks_are_enforced(self): self.assertTrue(any("site/tampered-idat.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) self.assertTrue(any("site/nonempty-iend.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) self.assertTrue(any("site/corrupt.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) + self.assertFalse(any("site/interlaced-valid.png" in m for m in msgs), msgs) + self.assertTrue(any("site/interlaced-smuggled.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) + self.assertTrue(any("site/interlaced-bad-filter.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) self.assertTrue(any("site/leak-meta.png" in m and "absolute home path" in m for m in msgs)) self.assertTrue(any("site/leak-meta.png" in m and "private IPv4 address" in m for m in msgs)) self.assertTrue(any("site/leak-meta.png" in m and "personal subdomain" in m for m in msgs)) @@ -402,6 +424,28 @@ def test_site_links_and_naming_are_checked(self): self.assertTrue(any("CSS @import external stylesheet" in m for m in publish), publish) self.assertTrue(any("site/bad name.txt" in m and "space" in m for m in naming), naming) + def test_inline_style_urls_and_empty_fragments(self): + self.write( + "site/index.html", + '<div style="border-bottom:0"><a href="#">Back to top</a><a href="/#">Home</a></div>\n', + ) + clean = self.report() + check_site.check_leaks(self.root, clean) + self.assertEqual(clean.findings, []) + + self.write( + "site/inline-style-leak.html", + '<div style="background-image:url(https://db-primary\2e ops\2e internal/x.png)">x</div>\n' + '<a href="#missing-id">jump</a>\n', + ) + dirty = self.report() + check_site.check_leaks(self.root, dirty) + publish = self.messages(dirty, "publish") + links = self.messages(dirty, "links") + self.assertTrue(any("remote or inline-scheme CSS url() resource" in m for m in publish), publish) + self.assertTrue(any("internal hostname" in m for m in publish), publish) + self.assertTrue(any("broken fragment link -> #missing-id" in m for m in links), links) + def test_tracked_workflow_and_config_files_scan_identifiers_with_placeholder_exemptions(self): self.write( ".github/workflows/ci.yml", From 88afcabdbbe9b24776728b86f470c5b47dc481c0 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 07:44:53 +0800 Subject: [PATCH 03/12] fix(scripts): validate PNG chunks and decode percent/query links - Validate spec-mandated sizes and fields on allowed PNG ancillary chunks (sRGB, gAMA, cHRM, pHYs, sBIT, bKGD, tRNS, hIST, PLTE). - Percent-decode local site paths and anchor fragments before checking target files and element IDs. - Resolve query-only anchors (?...) against the current document while checking any trailing fragment target. --- scripts/check_site.py | 77 ++++++++++++++++++++++++++++++++++++++++--- scripts/test_site.py | 28 ++++++++++++++-- 2 files changed, 99 insertions(+), 6 deletions(-) diff --git a/scripts/check_site.py b/scripts/check_site.py index 6ddcac7..f6a0886 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -333,6 +333,9 @@ def resolve_site_asset_href(page_rel: str, href: str) -> str | None: clean = raw.split("#", 1)[0].split("?", 1)[0].strip() if not clean or re.match(r"^[a-zA-Z][a-zA-Z0-9+.-]*:", clean): return None + clean = urllib.parse.unquote(clean) + if not clean or "\\" in clean or "\x00" in clean or re.match(r"^[a-zA-Z][a-zA-Z0-9+.-]*:", clean): + return None sub_page = page_rel[len("site/"):] if page_rel.startswith("site/") else page_rel if clean.startswith("/"): rel_in_site = posixpath.normpath(clean.lstrip("/")) @@ -413,6 +416,39 @@ def _valid_png_scanlines( return offset == len(decomp) +def _valid_ancillary_png_chunk( + ctype: bytes, cdata: bytes, bit_depth: int, color_type: int, plte_entries: int +) -> bool: + """Validate structure and spec-mandated byte sizes of allowed ancillary PNG chunks.""" + if ctype == b"sRGB": + return len(cdata) == 1 and cdata[0] <= 3 + if ctype == b"gAMA": + return len(cdata) == 4 and struct.unpack(">I", cdata)[0] > 0 + if ctype == b"cHRM": + return len(cdata) == 32 + if ctype == b"pHYs": + return len(cdata) == 9 and cdata[8] in (0, 1) + if ctype == b"sBIT": + expected = 3 if color_type == 3 else PNG_CHANNELS[color_type] + max_bits = 8 if color_type == 3 else bit_depth + return len(cdata) == expected and all(1 <= b <= max_bits for b in cdata) + if ctype == b"bKGD": + if color_type == 3: + return plte_entries > 0 and len(cdata) == 1 and cdata[0] < plte_entries + return len(cdata) == (2 if color_type in (0, 4) else 6) + if ctype == b"tRNS": + if color_type == 0: + return len(cdata) == 2 + if color_type == 2: + return len(cdata) == 6 + if color_type == 3: + return plte_entries > 0 and 1 <= len(cdata) <= plte_entries + return False + if ctype == b"hIST": + return plte_entries > 0 and len(cdata) == 2 * plte_entries + return False + + def inspect_png_asset(raw: bytes) -> tuple[str, bool] | None: """Validate PNG magic, chunk CRCs, IHDR, IDAT zlib stream, IEND, and ancillary text chunks. @@ -424,6 +460,8 @@ def inspect_png_asset(raw: bytes) -> tuple[str, bool] | None: ihdr: tuple[int, int, int, int, int] | None = None seen_iend = False has_text_meta = False + seen_ancillary: set[bytes] = set() + plte_entries = 0 idat_parts: list[bytes] = [] parts: list[str] = [] while pos + 8 <= len(raw): @@ -464,7 +502,24 @@ def inspect_png_asset(raw: bytes) -> tuple[str, bool] | None: if ctype == b"IDAT": idat_parts.append(cdata) continue + if ctype == b"PLTE": + if ( + ihdr[3] != 3 + or idat_parts + or ctype in seen_ancillary + or not (3 <= len(cdata) <= 3 * (1 << ihdr[2])) + or len(cdata) % 3 != 0 + ): + return None + seen_ancillary.add(ctype) + plte_entries = len(cdata) // 3 + continue if ctype in PNG_ALLOWED_ANCILLARY_CHUNKS: + if idat_parts or ctype in seen_ancillary: + return None + if not _valid_ancillary_png_chunk(ctype, cdata, ihdr[2], ihdr[3], plte_entries): + return None + seen_ancillary.add(ctype) continue if ctype in PNG_TEXT_CHUNKS: has_text_meta = True @@ -476,6 +531,8 @@ def inspect_png_asset(raw: bytes) -> tuple[str, bool] | None: return None if ihdr is None or not seen_iend or pos != len(raw) or not idat_parts: return None + if ihdr[3] == 3 and plte_entries == 0: + return None decomp = _zlib_decompress_exact(b"".join(idat_parts)) if decomp is None: return None @@ -948,18 +1005,30 @@ def _check_css_text(css_text: str) -> None: _emit("links", f"broken local {kind} link -> {target}") elif kind == "anchor": raw = target.strip() + if not raw: + continue + unq_raw = urllib.parse.unquote(raw) + if re.match(r"^(?:https?:|mailto:|tel:|javascript:|vbscript:|data:|//)", raw, re.IGNORECASE) or re.match( + r"^(?:https?:|mailto:|tel:|javascript:|vbscript:|data:|//)", unq_raw, re.IGNORECASE + ): + continue if raw.startswith("#"): - frag = raw[1:] + frag = urllib.parse.unquote(raw[1:]) if frag and frag not in parser.element_ids: _emit("links", f"broken fragment link -> {target}") + elif raw.startswith("?"): + if "#" in raw: + frag = urllib.parse.unquote(raw.split("#", 1)[1]) + if frag and frag not in parser.element_ids: + _emit("links", f"broken fragment link -> {target}") elif raw == "/" or raw.startswith(("/#", "/?")): if not (root / "site/index.html").is_file(): _emit("links", f"broken relative link -> {target}") - elif rel == "site/index.html" and raw.startswith("/#"): - frag = raw[2:] + elif rel == "site/index.html" and "#" in raw: + frag = urllib.parse.unquote(raw.split("#", 1)[1]) if frag and frag not in parser.element_ids: _emit("links", f"broken fragment link -> {target}") - elif raw and not re.match(r"^(?:https?:|mailto:|tel:|javascript:|vbscript:|data:|//)", raw, re.IGNORECASE): + else: resolved = resolve_site_asset_href(rel, raw) if resolved is None: _emit("links", f"broken relative link -> {target}") diff --git a/scripts/test_site.py b/scripts/test_site.py index 403d0f9..aca41f4 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -369,6 +369,15 @@ def test_png_structure_and_metadata_chunks_are_enforced(self): + _png_chunk(b"IDAT", zlib.compress(b"\x09\xff\x00\x00")) + _png_chunk(b"IEND", b"") ) + (self.root / "site/valid-ancillary.png").write_bytes( + _make_png( + _png_chunk(b"sRGB", b"\x00"), + _png_chunk(b"pHYs", struct.pack(">IIB", 2835, 2835, 1)), + ) + ) + (self.root / "site/smuggled-phys.png").write_bytes( + _make_png(_png_chunk(b"pHYs", b"/home/operator/prod/config")) + ) report = self.report() check_site.check_leaks(self.root, report) @@ -379,6 +388,8 @@ def test_png_structure_and_metadata_chunks_are_enforced(self): self.assertFalse(any("site/interlaced-valid.png" in m for m in msgs), msgs) self.assertTrue(any("site/interlaced-smuggled.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) self.assertTrue(any("site/interlaced-bad-filter.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) + self.assertFalse(any("site/valid-ancillary.png" in m for m in msgs), msgs) + self.assertTrue(any("site/smuggled-phys.png" in m and "unscanned binary or encrypted asset" in m for m in msgs)) self.assertTrue(any("site/leak-meta.png" in m and "absolute home path" in m for m in msgs)) self.assertTrue(any("site/leak-meta.png" in m and "private IPv4 address" in m for m in msgs)) self.assertTrue(any("site/leak-meta.png" in m and "personal subdomain" in m for m in msgs)) @@ -425,9 +436,20 @@ def test_site_links_and_naming_are_checked(self): self.assertTrue(any("site/bad name.txt" in m and "space" in m for m in naming), naming) def test_inline_style_urls_and_empty_fragments(self): + self.write("site/product-details.html", '<h1 id="产品">Details</h1>\n') + self.write( + "site/nested/guide.html", + '<a href="?lang=en">English</a><a href="../product%2Ddetails.html#%E4%BA%A7%E5%93%81">Details</a>\n', + ) self.write( "site/index.html", - '<div style="border-bottom:0"><a href="#">Back to top</a><a href="/#">Home</a></div>\n', + '<div id="产品" style="border-bottom:0">' + '<a href="#">Back to top</a>' + '<a href="/#">Home</a>' + '<a href="?lang=en">English</a>' + '<a href="#%E4%BA%A7%E5%93%81">产品</a>' + '<a href="product%2Ddetails.html">Product</a>' + "</div>\n", ) clean = self.report() check_site.check_leaks(self.root, clean) @@ -436,7 +458,8 @@ def test_inline_style_urls_and_empty_fragments(self): self.write( "site/inline-style-leak.html", '<div style="background-image:url(https://db-primary\2e ops\2e internal/x.png)">x</div>\n' - '<a href="#missing-id">jump</a>\n', + '<a href="#missing-id">jump</a>\n' + '<a href="?lang=en#missing-id">query-missing</a>\n', ) dirty = self.report() check_site.check_leaks(self.root, dirty) @@ -445,6 +468,7 @@ def test_inline_style_urls_and_empty_fragments(self): self.assertTrue(any("remote or inline-scheme CSS url() resource" in m for m in publish), publish) self.assertTrue(any("internal hostname" in m for m in publish), publish) self.assertTrue(any("broken fragment link -> #missing-id" in m for m in links), links) + self.assertTrue(any("broken fragment link -> ?lang=en#missing-id" in m for m in links), links) def test_tracked_workflow_and_config_files_scan_identifiers_with_placeholder_exemptions(self): self.write( From 9e19fe118ac19b51092e3f017e8c353eeb8d4b89 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 07:52:52 +0800 Subject: [PATCH 04/12] fix(scripts): reject <base> tags and accept named anchor targets - Reject <base> elements in site HTML so relative script, stylesheet, and image paths cannot be redirected to an external base URL. - Collect <a name="..."> attributes alongside id attributes when validating same-document fragment links. --- scripts/check_site.py | 4 ++++ scripts/test_site.py | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/scripts/check_site.py b/scripts/check_site.py index f6a0886..c39b689 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -796,6 +796,8 @@ def __init__(self, depth: int = 0, page_rel: str = "site/index.html") -> None: def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: t = tag.lower() + if t == "base": + self.html_issues.append(("publish", "HTML base href element in site/")) if t in HTML_BLOCK_TAGS: self.inline_parts.append("\n") ad: dict[str, str] = {} @@ -826,6 +828,8 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None self.subtrees.append(normalize_html_text(v, self.depth + 1, page_rel=self.page_rel)) if ad.get("id"): self.element_ids.add(ad["id"]) + if t == "a" and ad.get("name"): + self.element_ids.add(ad["name"]) if t == "link" and "href" in ad: rel_tokens = {tok.lower() for tok in ad.get("rel", "").split()} if rel_tokens & {"stylesheet", "icon", "apple-touch-icon", "manifest"}: diff --git a/scripts/test_site.py b/scripts/test_site.py index aca41f4..152d51a 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -444,6 +444,8 @@ def test_inline_style_urls_and_empty_fragments(self): self.write( "site/index.html", '<div id="产品" style="border-bottom:0">' + '<a name="details"></a>' + '<a href="#details">Details</a>' '<a href="#">Back to top</a>' '<a href="/#">Home</a>' '<a href="?lang=en">English</a>' @@ -461,12 +463,19 @@ def test_inline_style_urls_and_empty_fragments(self): '<a href="#missing-id">jump</a>\n' '<a href="?lang=en#missing-id">query-missing</a>\n', ) + self.write( + "site/base-hijack.html", + '<base href="https://cdn.example.com/">\n' + '<script src="assets/app.js"></script>\n', + ) + self.write("site/assets/app.js", "console.log('ok');\n") dirty = self.report() check_site.check_leaks(self.root, dirty) publish = self.messages(dirty, "publish") links = self.messages(dirty, "links") self.assertTrue(any("remote or inline-scheme CSS url() resource" in m for m in publish), publish) self.assertTrue(any("internal hostname" in m for m in publish), publish) + self.assertTrue(any("site/base-hijack.html" in m and "HTML base href element in site/" in m for m in publish), publish) self.assertTrue(any("broken fragment link -> #missing-id" in m for m in links), links) self.assertTrue(any("broken fragment link -> ?lang=en#missing-id" in m for m in links), links) From 7242b8abfbd01a494f0b0065e81c6df830c3c827 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:00:49 +0800 Subject: [PATCH 05/12] fix(scripts): support site-root anchors and source srcset checks - Allow relative directory anchors (./, ../) resolving to the site root index while still rejecting paths that escape site/. - Check <source> src and srcset candidates alongside <img> elements. --- scripts/check_site.py | 23 +++++++++++------------ scripts/test_site.py | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/scripts/check_site.py b/scripts/check_site.py index c39b689..aa3c8ba 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -325,7 +325,7 @@ def blank(match: re.Match) -> str: } -def resolve_site_asset_href(page_rel: str, href: str) -> str | None: +def resolve_site_asset_href(page_rel: str, href: str, allow_root: bool = False) -> str | None: """Resolve a site/-local HTML/CSS href against page_rel; return None for external or escaping targets.""" raw = href.strip() if not raw or raw.startswith("//") or "\\" in raw: @@ -343,7 +343,9 @@ def resolve_site_asset_href(page_rel: str, href: str) -> str | None: base_dir = posixpath.dirname(sub_page) joined = posixpath.join(base_dir, clean) if base_dir else clean rel_in_site = posixpath.normpath(joined) - if rel_in_site in ("", ".", "..") or rel_in_site.startswith("../"): + if rel_in_site == ".": + return "site" if allow_root else None + if rel_in_site in ("", "..") or rel_in_site.startswith("../"): return None return "site/" + rel_in_site @@ -836,10 +838,10 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None self.asset_refs.append(("link", ad["href"])) elif t == "script" and "src" in ad: self.asset_refs.append(("script", ad["src"])) - elif t == "img": + elif t in ("img", "source"): if "src" in ad: self.asset_refs.append(("img", ad["src"])) - if "srcset" in ad: + if "srcset" in ad and not re.match(r"^(?:data|javascript|vbscript):", ad["srcset"].strip(), re.IGNORECASE): for cand in ad["srcset"].split(","): parts = cand.strip().split() self.asset_refs.append(("img", parts[0] if parts else "")) @@ -1025,21 +1027,18 @@ def _check_css_text(css_text: str) -> None: frag = urllib.parse.unquote(raw.split("#", 1)[1]) if frag and frag not in parser.element_ids: _emit("links", f"broken fragment link -> {target}") - elif raw == "/" or raw.startswith(("/#", "/?")): - if not (root / "site/index.html").is_file(): - _emit("links", f"broken relative link -> {target}") - elif rel == "site/index.html" and "#" in raw: - frag = urllib.parse.unquote(raw.split("#", 1)[1]) - if frag and frag not in parser.element_ids: - _emit("links", f"broken fragment link -> {target}") else: - resolved = resolve_site_asset_href(rel, raw) + resolved = resolve_site_asset_href(rel, raw, allow_root=True) if resolved is None: _emit("links", f"broken relative link -> {target}") else: target_path = root / resolved if not (target_path.is_file() or (target_path / "index.html").is_file()): _emit("links", f"broken relative link -> {target}") + elif resolved == "site" and rel == "site/index.html" and "#" in raw: + frag = urllib.parse.unquote(raw.split("#", 1)[1]) + if frag and frag not in parser.element_ids: + _emit("links", f"broken fragment link -> {target}") def normalized_site_views( diff --git a/scripts/test_site.py b/scripts/test_site.py index 152d51a..13447e4 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -438,8 +438,9 @@ def test_site_links_and_naming_are_checked(self): def test_inline_style_urls_and_empty_fragments(self): self.write("site/product-details.html", '<h1 id="产品">Details</h1>\n') self.write( - "site/nested/guide.html", - '<a href="?lang=en">English</a><a href="../product%2Ddetails.html#%E4%BA%A7%E5%93%81">Details</a>\n', + "site/nested/index.html", + '<a href="../">Root</a><a href="?lang=en">English</a>' + '<a href="../product%2Ddetails.html#%E4%BA%A7%E5%93%81">Details</a>\n', ) self.write( "site/index.html", @@ -447,6 +448,7 @@ def test_inline_style_urls_and_empty_fragments(self): '<a name="details"></a>' '<a href="#details">Details</a>' '<a href="#">Back to top</a>' + '<a href="./">Self</a>' '<a href="/#">Home</a>' '<a href="?lang=en">English</a>' '<a href="#%E4%BA%A7%E5%93%81">产品</a>' @@ -461,7 +463,13 @@ def test_inline_style_urls_and_empty_fragments(self): "site/inline-style-leak.html", '<div style="background-image:url(https://db-primary\2e ops\2e internal/x.png)">x</div>\n' '<a href="#missing-id">jump</a>\n' - '<a href="?lang=en#missing-id">query-missing</a>\n', + '<a href="?lang=en#missing-id">query-missing</a>\n' + '<a href="../outside.html">escape</a>\n', + ) + self.write( + "site/picture-remote.html", + '<picture><source srcset="https://cdn.example.com/hero.webp">' + '<img src="landing-light-192.png"></picture>\n', ) self.write( "site/base-hijack.html", @@ -469,15 +477,18 @@ def test_inline_style_urls_and_empty_fragments(self): '<script src="assets/app.js"></script>\n', ) self.write("site/assets/app.js", "console.log('ok');\n") + (self.root / "site/landing-light-192.png").write_bytes(_make_png()) dirty = self.report() check_site.check_leaks(self.root, dirty) publish = self.messages(dirty, "publish") links = self.messages(dirty, "links") self.assertTrue(any("remote or inline-scheme CSS url() resource" in m for m in publish), publish) self.assertTrue(any("internal hostname" in m for m in publish), publish) + self.assertTrue(any("site/picture-remote.html" in m and "remote or escaping img asset" in m for m in publish), publish) self.assertTrue(any("site/base-hijack.html" in m and "HTML base href element in site/" in m for m in publish), publish) self.assertTrue(any("broken fragment link -> #missing-id" in m for m in links), links) self.assertTrue(any("broken fragment link -> ?lang=en#missing-id" in m for m in links), links) + self.assertTrue(any("broken relative link -> ../outside.html" in m for m in links), links) def test_tracked_workflow_and_config_files_scan_identifiers_with_placeholder_exemptions(self): self.write( From c52a7b43908fb63a6042c64d452c120bfc6304c3 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:25:18 +0800 Subject: [PATCH 06/12] fix(scripts): harden site link, CSS, SVG, and r2 asset checks - Accept implicit case-insensitive #top fragments across fragment checks - Validate autoloading HTML/SVG resources (iframe, srcdoc, object, embed, audio, video, input, track, preload, meta refresh, image, SVG hrefs) - Exclude external URI schemes (sms:, geo:, etc.) from relative file checks - Decode simple CSS escapes, fold adjacent strings, validate image-set(), preserve quoted url() parens, and reject HTML inline hiding rules - Reject custom XML DTD entities and xml:base in site/ assets - Gate site JS/CSS/PNG/ICO in policy.json r2_paths and add regression tests --- .github/governance/policy.json | 8 +- scripts/check_site.py | 174 ++++++++++++++++++++++++++------- scripts/test_site.py | 162 ++++++++++++++++++++++++++++++ 3 files changed, 306 insertions(+), 38 deletions(-) diff --git a/.github/governance/policy.json b/.github/governance/policy.json index f75c670..9776691 100644 --- a/.github/governance/policy.json +++ b/.github/governance/policy.json @@ -16,7 +16,13 @@ "r2_paths": [ "scripts/", ".github/governance/", - ".github/scripts/" + ".github/scripts/", + "site/**/*.js", + "site/**/*.mjs", + "site/**/*.cjs", + "site/**/*.css", + "site/**/*.png", + "site/**/*.ico" ], "dependabot_template_exemption": true, "max_evidence_age_hours": 168 diff --git a/scripts/check_site.py b/scripts/check_site.py index aa3c8ba..898d04f 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -325,6 +325,32 @@ def blank(match: re.Match) -> str: } +def _parse_srcset_urls(srcset: str) -> list[str]: + """Extract candidate URLs from an HTML srcset attribute per spec.""" + urls: list[str] = [] + i, n = 0, len(srcset) + while i < n: + while i < n and srcset[i] in " \t\n\r\f,": + i += 1 + if i >= n: + break + start = i + while i < n and srcset[i] not in " \t\n\r\f": + i += 1 + url = srcset[start:i] + if url.endswith(","): + urls.append(url.rstrip(",")) + continue + urls.append(url) + while i < n and srcset[i] in " \t\n\r\f": + i += 1 + while i < n and srcset[i] != ",": + i += 1 + if i < n and srcset[i] == ",": + i += 1 + return urls + + def resolve_site_asset_href(page_rel: str, href: str, allow_root: bool = False) -> str | None: """Resolve a site/-local HTML/CSS href against page_rel; return None for external or escaping targets.""" raw = href.strip() @@ -719,14 +745,21 @@ def _repl(match: re.Match) -> str: def unescape_css_literals(text: str) -> str: - """Decode CSS hex codepoint escapes for identifier scanning.""" + """Decode CSS hex and simple escapes for identifier scanning.""" def _repl(match: re.Match) -> str: cp = int(match.group(1), 16) if 0 < cp <= 0x10FFFF and not (0xD800 <= cp <= 0xDFFF): return chr(cp) return match.group(0) - return CSS_ESCAPE_RE.sub(_repl, LINE_CONTINUATION_RE.sub("", text)) + decoded = CSS_ESCAPE_RE.sub(_repl, LINE_CONTINUATION_RE.sub("", text)) + decoded = re.sub(r"\\([^\n\r\f0-9a-fA-F])", r"\1", decoded) + while True: + folded = re.sub(r'"([^"\n]*)"\s+"([^"\n]*)"', r'"\1\2"', decoded) + folded = re.sub(r"'([^'\n]*)'\s+'([^'\n]*)'", r"'\1\2'", folded) + if folded == decoded: + return folded + decoded = folded def fold_js_expressions(text: str) -> str: @@ -797,7 +830,7 @@ def __init__(self, depth: int = 0, page_rel: str = "site/index.html") -> None: self._raw_chunks: list[str] = [] def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: - t = tag.lower() + t = tag.lower().rsplit(":", 1)[-1] if t == "base": self.html_issues.append(("publish", "HTML base href element in site/")) if t in HTML_BLOCK_TAGS: @@ -805,6 +838,8 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None ad: dict[str, str] = {} for k, v in attrs: kl = k.lower() + if kl == "xml:base": + self.html_issues.append(("publish", "XML base attribute in site/")) if kl in ad: self.html_issues.append(("publish", f"duplicate HTML attribute in site/ ({kl})")) continue @@ -826,25 +861,69 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None if kl == "style": self.style_blocks.append(v) self.subtrees.append(unescape_css_literals(v)) - if kl == "srcdoc" and self.depth < 3: - self.subtrees.append(normalize_html_text(v, self.depth + 1, page_rel=self.page_rel)) + if kl == "srcdoc": + if self.depth < 3: + nested = _parse_site_html(v, depth=self.depth + 1, page_rel=self.page_rel) + self.html_issues.extend(nested.html_issues) + self.style_blocks.extend(nested.style_blocks) + self.asset_refs.extend(nested.asset_refs) + self.subtrees.append(normalize_html_text(v, self.depth + 1, page_rel=self.page_rel)) + else: + self.html_issues.append(("publish", "deeply nested iframe srcdoc in site/")) + if "hidden" in ad: + self.html_issues.append(("publish", "hidden HTML attribute in site/")) if ad.get("id"): self.element_ids.add(ad["id"]) if t == "a" and ad.get("name"): self.element_ids.add(ad["name"]) if t == "link" and "href" in ad: rel_tokens = {tok.lower() for tok in ad.get("rel", "").split()} - if rel_tokens & {"stylesheet", "icon", "apple-touch-icon", "manifest"}: + if rel_tokens & { + "stylesheet", "icon", "apple-touch-icon", "manifest", + "preload", "modulepreload", "prefetch", "dns-prefetch", + "preconnect", "prerender", "mask-icon", + }: self.asset_refs.append(("link", ad["href"])) - elif t == "script" and "src" in ad: - self.asset_refs.append(("script", ad["src"])) + elif t == "script": + script_src = ad.get("src") or ad.get("href") or ad.get("xlink:href") + if script_src is not None: + self.asset_refs.append(("script", script_src)) elif t in ("img", "source"): if "src" in ad: self.asset_refs.append(("img", ad["src"])) if "srcset" in ad and not re.match(r"^(?:data|javascript|vbscript):", ad["srcset"].strip(), re.IGNORECASE): - for cand in ad["srcset"].split(","): - parts = cand.strip().split() - self.asset_refs.append(("img", parts[0] if parts else "")) + for cand_url in _parse_srcset_urls(ad["srcset"]): + self.asset_refs.append(("img", cand_url)) + elif t in ("iframe", "embed", "audio", "video", "input", "track"): + if "src" in ad: + self.asset_refs.append((t, ad["src"])) + if t == "video" and "poster" in ad: + self.asset_refs.append(("video", ad["poster"])) + elif t == "object" and "data" in ad: + self.asset_refs.append(("object", ad["data"])) + elif t == "meta" and ad.get("http-equiv", "").strip().lower() == "refresh" and "content" in ad: + m = re.match( + r"^\s*\d+(?:\.\d*)?\s*[;,]\s*(?:url\s*=\s*)?(?:\"([^\"]*)\"|'([^']*)'|(.+?))\s*$", + ad["content"], + re.IGNORECASE, + ) + if m: + refresh_url = next((g for g in m.groups() if g is not None), "").strip() + if refresh_url: + compact_ref = re.sub(r"[\x00-\x20]+", "", refresh_url) + unq_ref = re.sub(r"[\x00-\x20]+", "", urllib.parse.unquote(compact_ref)) + for chk_val in (compact_ref, unq_ref): + if re.match(r"^(?:javascript|vbscript):", chk_val, re.IGNORECASE): + self.html_issues.append(("publish", "javascript: or vbscript: URI attribute in site/")) + elif re.match(r"^data:", chk_val, re.IGNORECASE): + self.html_issues.append(("publish", "embedded data URI asset requires direct file scan")) + self.asset_refs.append(("meta", refresh_url)) + elif t in ("image", "use", "feimage"): + if t == "image" and "src" in ad: + self.asset_refs.append(("img", ad["src"])) + svg_href = ad.get("href") or ad.get("xlink:href") + if svg_href and not svg_href.strip().startswith("#"): + self.asset_refs.append(("img", svg_href)) elif t == "a" and "href" in ad: self.asset_refs.append(("anchor", ad["href"])) if t in ("script", "style", "title"): @@ -854,7 +933,7 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None self._raw_chunks = [] def handle_startendtag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: - t = tag.lower() + t = tag.lower().rsplit(":", 1)[-1] if t in ("script", "style", "title"): self.html_issues.append(("publish", "self-closing script or style tag in site/")) self.set_cdata_mode(t) @@ -883,7 +962,7 @@ def handle_pi(self, data: str) -> None: self.html_issues.append(("publish", "HTML processing instruction in site/")) def handle_endtag(self, tag: str) -> None: - t = tag.lower() + t = tag.lower().rsplit(":", 1)[-1] if self._raw_tag == t: self._flush_raw_tag(t) if t in HTML_BLOCK_TAGS: @@ -949,22 +1028,35 @@ def _emit(category: str, msg: str) -> None: if BIDI_CONTROL_RE.search(text): _emit("publish", "bidirectional control character in published site/ asset (section 13)") - def _check_css_text(css_text: str) -> None: + def _validate_css_target(target: str) -> None: + if not target or target.startswith("#"): + return + if re.match(r"^(?:https?:|//|data:|javascript:|vbscript:)", target, re.IGNORECASE): + _emit("publish", f"remote or inline-scheme CSS url() resource ({target[:36]}) (section 13)") + return + resolved = resolve_site_asset_href(rel, target) + if resolved is None: + _emit("publish", f"escaping CSS url() resource ({target[:36]}) (section 13)") + elif not (root / resolved).is_file(): + _emit("links", f"broken local CSS asset link -> {target}") + + def _check_css_text(css_text: str, *, allow_hiding: bool = True) -> None: clean_css = CSS_COMMENT_RE.sub("", unescape_css_literals(css_text)) if re.search(r"(?i)@import\b", clean_css): _emit("publish", "CSS @import external stylesheet (section 13)") - for m in re.finditer(r"(?i)\burl\s*\(\s*([^)]+)\)", clean_css): - target = m.group(1).strip().strip("\"'").strip() - if not target or target.startswith("#"): - continue - if re.match(r"^(?:https?:|//|data:|javascript:|vbscript:)", target, re.IGNORECASE): - _emit("publish", f"remote or inline-scheme CSS url() resource ({target[:36]}) (section 13)") - continue - resolved = resolve_site_asset_href(rel, target) - if resolved is None: - _emit("publish", f"escaping CSS url() resource ({target[:36]}) (section 13)") - elif not (root / resolved).is_file(): - _emit("links", f"broken local CSS asset link -> {target}") + if not allow_hiding and re.search( + r"(?i)(?:display\s*:\s*none|visibility\s*:\s*hidden|opacity\s*:\s*0(?:\.0+)?(?:\s*[;}]|$))", + clean_css, + ): + _emit("publish", "hidden CSS text rule in HTML site/ asset (section 13)") + for m in re.finditer(r'(?i)\burl\s*\(\s*(?:"([^"]*)"|\'([^\']*)\'|([^)]+))\s*\)', clean_css): + target = next((g for g in m.groups() if g is not None), "").strip() + _validate_css_target(target) + for m in re.finditer(r"(?i)(?:-webkit-)?image-set\s*\(((?:[^()]|\([^()]*\))*)\)", clean_css): + inner = re.sub(r"(?i)\b(?:type|url)\s*\([^)]*\)", "", m.group(1)) + for sm in re.finditer(r'"([^"]+)"|\'([^\']+)\'', inner): + target = (sm.group(1) if sm.group(1) is not None else sm.group(2)).strip() + _validate_css_target(target) suffix = path.suffix.lower() if suffix == ".css": @@ -992,16 +1084,24 @@ def _check_css_text(css_text: str) -> None: if suffix not in (".html", ".htm", ".xhtml", ".svg", ".xml"): return + if re.search(r"(?is)<!(?:ENTITY\b|DOCTYPE\b[^>\[]*\[)", text): + _emit("publish", "custom XML entity or internal DTD subset in site/ asset (section 13)") + parser = _parse_site_html(text, page_rel=rel) for cat, msg in parser.html_issues: _emit(cat, f"{msg} (section 13)") for css_blk in parser.style_blocks: - _check_css_text(css_blk) - if suffix not in (".html", ".htm", ".xhtml"): - return + _check_css_text(css_blk, allow_hiding=False) + + def _is_valid_fragment(frag: str) -> bool: + return not frag or frag.lower() == "top" or frag in parser.element_ids + resource_kinds = { + "link", "script", "img", "iframe", "object", "embed", + "audio", "video", "input", "track", "meta", + } for kind, target in parser.asset_refs: - if kind in ("link", "script", "img"): + if kind in resource_kinds: if re.match(r"^(?:data|javascript|vbscript):", target.strip(), re.IGNORECASE): continue resolved = resolve_site_asset_href(rel, target) @@ -1009,23 +1109,23 @@ def _check_css_text(css_text: str) -> None: _emit("publish", f"remote or escaping {kind} asset in site/ ({target[:36]}) (section 13)") elif not (root / resolved).is_file(): _emit("links", f"broken local {kind} link -> {target}") - elif kind == "anchor": + elif kind == "anchor" and suffix in (".html", ".htm", ".xhtml"): raw = target.strip() if not raw: continue unq_raw = urllib.parse.unquote(raw) - if re.match(r"^(?:https?:|mailto:|tel:|javascript:|vbscript:|data:|//)", raw, re.IGNORECASE) or re.match( - r"^(?:https?:|mailto:|tel:|javascript:|vbscript:|data:|//)", unq_raw, re.IGNORECASE + if re.match(r"^(?:[a-zA-Z][a-zA-Z0-9+.-]*:|//)", raw) or re.match( + r"^(?:[a-zA-Z][a-zA-Z0-9+.-]*:|//)", unq_raw ): continue if raw.startswith("#"): frag = urllib.parse.unquote(raw[1:]) - if frag and frag not in parser.element_ids: + if not _is_valid_fragment(frag): _emit("links", f"broken fragment link -> {target}") elif raw.startswith("?"): if "#" in raw: frag = urllib.parse.unquote(raw.split("#", 1)[1]) - if frag and frag not in parser.element_ids: + if not _is_valid_fragment(frag): _emit("links", f"broken fragment link -> {target}") else: resolved = resolve_site_asset_href(rel, raw, allow_root=True) @@ -1035,9 +1135,9 @@ def _check_css_text(css_text: str) -> None: target_path = root / resolved if not (target_path.is_file() or (target_path / "index.html").is_file()): _emit("links", f"broken relative link -> {target}") - elif resolved == "site" and rel == "site/index.html" and "#" in raw: + elif (resolved == rel or (resolved == "site" and rel == "site/index.html")) and "#" in raw: frag = urllib.parse.unquote(raw.split("#", 1)[1]) - if frag and frag not in parser.element_ids: + if not _is_valid_fragment(frag): _emit("links", f"broken fragment link -> {target}") diff --git a/scripts/test_site.py b/scripts/test_site.py index 13447e4..6c93d6e 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -455,6 +455,11 @@ def test_inline_style_urls_and_empty_fragments(self): '<a href="product%2Ddetails.html">Product</a>' "</div>\n", ) + (self.root / "site/ok.png").write_bytes(_make_png()) + self.write( + "site/ok-imageset.css", + 'p { background-image: image-set(url("ok.png") type("image/png") 1x); }\n', + ) clean = self.report() check_site.check_leaks(self.root, clean) self.assertEqual(clean.findings, []) @@ -490,6 +495,163 @@ def test_inline_style_urls_and_empty_fragments(self): self.assertTrue(any("broken fragment link -> ?lang=en#missing-id" in m for m in links), links) self.assertTrue(any("broken relative link -> ../outside.html" in m for m in links), links) + def test_implicit_top_fragment_and_autoloading_resources_and_hidden_css(self): + self.write( + "site/index.html", + '<a href="#top">Top</a>' + '<a href="#TOP">TOP</a>' + '<a href="?lang=en#Top">Query Top</a>' + '<a href="/#top">Root Top</a>' + '<a href="./#TOP">Dot Top</a>' + '<a href="index.html#top">Index Top</a>' + '<a href="sms:+15555550100">SMS</a>' + '<a href="geo:37.786971,-122.399677">Map</a>' + '<img srcset="ok.png?variant=a,b 1x">\n', + ) + (self.root / "site/ok.png").write_bytes(_make_png()) + (self.root / "site/ok(1).png").write_bytes(_make_png()) + self.write( + "site/ok-imageset.css", + 'p { background-image: image-set(url("ok.png") type("image/png") 1x); }\n' + 'div { background-image: url("ok(1).png"); }\n', + ) + clean = self.report() + check_site.check_leaks(self.root, clean) + self.assertEqual(clean.findings, []) + + self.write( + "site/autoload-remote.html", + '<iframe src="https://cdn.example.com/frame.html"></iframe>\n' + '<object data="https://cdn.example.com/obj.swf"></object>\n' + '<embed src="https://cdn.example.com/emb.swf">\n' + '<audio src="https://cdn.example.com/a.mp3"></audio>\n' + '<video src="https://cdn.example.com/v.mp4" poster="missing-poster.png"></video>\n' + '<input type="image" src="missing-btn.png">\n' + '<link rel="preload" as="script" href="https://cdn.example.com/pre.js">\n' + '<meta http-equiv="refresh" content="0;url=https://cdn.example.com/redir">\n' + '<meta http-equiv="refresh" content="0; https://collector.invalid/replaced">\n' + '<image src="https://cdn.example.com/legacy.png">\n', + ) + self.write( + "site/autoload-js-refresh.html", + '<meta http-equiv="refresh" content="0;url=javascript:alert(1)">\n', + ) + self.write( + "site/decoy-hidden.html", + '<style>.mask{display:none}</style>\n' + '<span style="visibility:hidden">a</span>\n' + '<span style="opacity: 0">b</span>\n' + '<span hidden>c</span>\n', + ) + self.write( + "site/srcdoc-remote.html", + '<iframe srcdoc="<script src="https://cdn.example.com/x.js"></script>"></iframe>\n', + ) + self.write( + "site/foreign-remote.svg", + '<svg xmlns="http://www.w3.org/2000/svg">' + '<foreignObject width="100" height="100">' + '<img xmlns="http://www.w3.org/1999/xhtml" src="https://cdn.example.com/x.png" />' + '</foreignObject>' + '<image href="https://cdn.example.com/y.png" />' + '<script href="https://cdn.example.com/x.js"></script>' + '</svg>\n', + ) + self.write( + "site/css-escapes-and-imageset.css", + 'p::after { content: "db-primary.ops.in\\ternal"; }\n' + 'p::before { content: "/home/" "operator/prod/config"; }\n' + 'p { background-image: image-set("https://cdn.example.com/x.png" 1x, "missing-set.png" 2x); }\n', + ) + self.write( + "site/entity-smuggle.svg", + '<!DOCTYPE svg [<!ENTITY home "/home/">]><svg xmlns="http://www.w3.org/2000/svg"><text>&home;</text></svg>\n', + ) + self.write( + "site/ns-and-xmlbase.svg", + '<s:svg xmlns:s="http://www.w3.org/2000/svg" xml:base="https://cdn.example.com/">' + '<s:script href="https://cdn.example.com/x.js"></s:script></s:svg>\n', + ) + dirty = self.report() + check_site.check_leaks(self.root, dirty) + publish = self.messages(dirty, "publish") + links = self.messages(dirty, "links") + for kind in ("iframe", "object", "embed", "audio", "video", "link", "meta", "script", "img"): + self.assertTrue( + any(f"remote or escaping {kind} asset" in m for m in publish), + (kind, publish), + ) + self.assertTrue( + any("site/srcdoc-remote.html" in m and "remote or escaping script asset" in m for m in publish), + publish, + ) + self.assertTrue( + any("site/foreign-remote.svg" in m and "https://cdn.example.com/x.png" in m for m in publish), + publish, + ) + self.assertTrue( + any("site/foreign-remote.svg" in m and "https://cdn.example.com/y.png" in m for m in publish), + publish, + ) + self.assertTrue( + any("site/foreign-remote.svg" in m and "https://cdn.example.com/x.js" in m for m in publish), + publish, + ) + self.assertTrue( + any("site/css-escapes-and-imageset.css" in m and "internal hostname" in m for m in publish), + publish, + ) + self.assertTrue( + any("site/css-escapes-and-imageset.css" in m and "absolute home path" in m for _, m in dirty.findings), + dirty.findings, + ) + self.assertTrue( + any("site/entity-smuggle.svg" in m and "custom XML entity" in m for m in publish), + publish, + ) + self.assertTrue( + any("site/ns-and-xmlbase.svg" in m and "XML base attribute in site/" in m for m in publish), + publish, + ) + self.assertTrue( + any("site/ns-and-xmlbase.svg" in m and "remote or escaping script asset" in m for m in publish), + publish, + ) + self.assertTrue( + any("site/css-escapes-and-imageset.css" in m and "https://cdn.example.com/x.png" in m for m in publish), + publish, + ) + self.assertTrue( + any("site/css-escapes-and-imageset.css" in m and "missing-set.png" in m for m in links), + links, + ) + self.assertTrue( + any("site/autoload-remote.html" in m and "https://collector.invalid/replaced" in m for m in publish), + publish, + ) + self.assertTrue( + any("site/autoload-remote.html" in m and "https://cdn.example.com/legacy.png" in m for m in publish), + publish, + ) + self.assertTrue(any("javascript: or vbscript: URI attribute" in m for m in publish), publish) + self.assertTrue(any("broken local video link -> missing-poster.png" in m for m in links), links) + self.assertTrue(any("broken local input link -> missing-btn.png" in m for m in links), links) + self.assertTrue(any("hidden CSS text rule in HTML site/ asset" in m for m in publish), publish) + self.assertTrue(any("hidden HTML attribute in site/" in m for m in publish), publish) + + policy_path = Path(__file__).resolve().parents[1] / ".github" / "governance" / "policy.json" + import json + policy = json.loads(policy_path.read_text(encoding="utf-8")) + for pattern in ( + "site/**/*.js", + "site/**/*.mjs", + "site/**/*.cjs", + "site/**/*.css", + "site/**/*.png", + "site/**/*.ico", + ): + self.assertIn(pattern, policy["r2_paths"]) + def test_tracked_workflow_and_config_files_scan_identifiers_with_placeholder_exemptions(self): self.write( ".github/workflows/ci.yml", From de5987d7333d31dfe0e1d1b7866ce722e04f59bb Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:31:38 +0800 Subject: [PATCH 07/12] fix(scripts): validate SVG presentation-attribute URLs - Route URL-bearing SVG presentation attributes (fill, stroke, clip-path, mask, filter, marker-*) through the CSS resource validator - Add regression test in scripts/test_site.py --- scripts/check_site.py | 6 ++++++ scripts/test_site.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/scripts/check_site.py b/scripts/check_site.py index 898d04f..1e628fd 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -861,6 +861,12 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None if kl == "style": self.style_blocks.append(v) self.subtrees.append(unescape_css_literals(v)) + elif kl in { + "fill", "stroke", "clip-path", "mask", "filter", + "marker-start", "marker-mid", "marker-end", + } and "url(" in unescape_css_literals(v).lower(): + self.style_blocks.append(f"{kl}: {v}") + self.subtrees.append(unescape_css_literals(v)) if kl == "srcdoc": if self.depth < 3: nested = _parse_site_html(v, depth=self.depth + 1, page_rel=self.page_rel) diff --git a/scripts/test_site.py b/scripts/test_site.py index 6c93d6e..e215f78 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -554,6 +554,7 @@ def test_implicit_top_fragment_and_autoloading_resources_and_hidden_css(self): '<img xmlns="http://www.w3.org/1999/xhtml" src="https://cdn.example.com/x.png" />' '</foreignObject>' '<image href="https://cdn.example.com/y.png" />' + '<rect fill="url(https://collector.invalid/paint.svg#p)" />' '<script href="https://cdn.example.com/x.js"></script>' '</svg>\n', ) @@ -597,6 +598,10 @@ def test_implicit_top_fragment_and_autoloading_resources_and_hidden_css(self): any("site/foreign-remote.svg" in m and "https://cdn.example.com/x.js" in m for m in publish), publish, ) + self.assertTrue( + any("site/foreign-remote.svg" in m and "https://collector.invalid/paint.svg#" in m for m in publish), + publish, + ) self.assertTrue( any("site/css-escapes-and-imageset.css" in m and "internal hostname" in m for m in publish), publish, From 777ded79afabc51a7328333487bcdfc27b885040 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:37:52 +0800 Subject: [PATCH 08/12] fix(scripts): scope srcdoc fragment checks to nested document - Validate iframe srcdoc anchor fragments against the nested parser's element_ids instead of flattening them into the outer document - Add regression test in scripts/test_site.py --- scripts/check_site.py | 83 +++++++++++++++++++++++-------------------- scripts/test_site.py | 1 + 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/scripts/check_site.py b/scripts/check_site.py index 1e628fd..d853d19 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -824,6 +824,7 @@ def __init__(self, depth: int = 0, page_rel: str = "site/index.html") -> None: self.subtrees: list[str] = [] self.style_blocks: list[str] = [] self.asset_refs: list[tuple[str, str]] = [] + self.nested_parsers: list[_SiteHTMLParser] = [] self.element_ids: set[str] = set() self.html_issues: list[tuple[str, str]] = [] self._raw_tag: str | None = None @@ -872,7 +873,7 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None nested = _parse_site_html(v, depth=self.depth + 1, page_rel=self.page_rel) self.html_issues.extend(nested.html_issues) self.style_blocks.extend(nested.style_blocks) - self.asset_refs.extend(nested.asset_refs) + self.nested_parsers.append(nested) self.subtrees.append(normalize_html_text(v, self.depth + 1, page_rel=self.page_rel)) else: self.html_issues.append(("publish", "deeply nested iframe srcdoc in site/")) @@ -1099,52 +1100,58 @@ def _check_css_text(css_text: str, *, allow_hiding: bool = True) -> None: for css_blk in parser.style_blocks: _check_css_text(css_blk, allow_hiding=False) - def _is_valid_fragment(frag: str) -> bool: - return not frag or frag.lower() == "top" or frag in parser.element_ids - resource_kinds = { "link", "script", "img", "iframe", "object", "embed", "audio", "video", "input", "track", "meta", } - for kind, target in parser.asset_refs: - if kind in resource_kinds: - if re.match(r"^(?:data|javascript|vbscript):", target.strip(), re.IGNORECASE): - continue - resolved = resolve_site_asset_href(rel, target) - if resolved is None: - _emit("publish", f"remote or escaping {kind} asset in site/ ({target[:36]}) (section 13)") - elif not (root / resolved).is_file(): - _emit("links", f"broken local {kind} link -> {target}") - elif kind == "anchor" and suffix in (".html", ".htm", ".xhtml"): - raw = target.strip() - if not raw: - continue - unq_raw = urllib.parse.unquote(raw) - if re.match(r"^(?:[a-zA-Z][a-zA-Z0-9+.-]*:|//)", raw) or re.match( - r"^(?:[a-zA-Z][a-zA-Z0-9+.-]*:|//)", unq_raw - ): - continue - if raw.startswith("#"): - frag = urllib.parse.unquote(raw[1:]) - if not _is_valid_fragment(frag): - _emit("links", f"broken fragment link -> {target}") - elif raw.startswith("?"): - if "#" in raw: - frag = urllib.parse.unquote(raw.split("#", 1)[1]) + + def _validate_parsed_refs(parsed: _SiteHTMLParser) -> None: + def _is_valid_fragment(frag: str) -> bool: + return not frag or frag.lower() == "top" or frag in parsed.element_ids + + for kind, target in parsed.asset_refs: + if kind in resource_kinds: + if re.match(r"^(?:data|javascript|vbscript):", target.strip(), re.IGNORECASE): + continue + resolved = resolve_site_asset_href(rel, target) + if resolved is None: + _emit("publish", f"remote or escaping {kind} asset in site/ ({target[:36]}) (section 13)") + elif not (root / resolved).is_file(): + _emit("links", f"broken local {kind} link -> {target}") + elif kind == "anchor" and suffix in (".html", ".htm", ".xhtml"): + raw = target.strip() + if not raw: + continue + unq_raw = urllib.parse.unquote(raw) + if re.match(r"^(?:[a-zA-Z][a-zA-Z0-9+.-]*:|//)", raw) or re.match( + r"^(?:[a-zA-Z][a-zA-Z0-9+.-]*:|//)", unq_raw + ): + continue + if raw.startswith("#"): + frag = urllib.parse.unquote(raw[1:]) if not _is_valid_fragment(frag): _emit("links", f"broken fragment link -> {target}") - else: - resolved = resolve_site_asset_href(rel, raw, allow_root=True) - if resolved is None: - _emit("links", f"broken relative link -> {target}") - else: - target_path = root / resolved - if not (target_path.is_file() or (target_path / "index.html").is_file()): - _emit("links", f"broken relative link -> {target}") - elif (resolved == rel or (resolved == "site" and rel == "site/index.html")) and "#" in raw: + elif raw.startswith("?"): + if "#" in raw: frag = urllib.parse.unquote(raw.split("#", 1)[1]) if not _is_valid_fragment(frag): _emit("links", f"broken fragment link -> {target}") + else: + resolved = resolve_site_asset_href(rel, raw, allow_root=True) + if resolved is None: + _emit("links", f"broken relative link -> {target}") + else: + target_path = root / resolved + if not (target_path.is_file() or (target_path / "index.html").is_file()): + _emit("links", f"broken relative link -> {target}") + elif (resolved == rel or (resolved == "site" and rel == "site/index.html")) and "#" in raw: + frag = urllib.parse.unquote(raw.split("#", 1)[1]) + if not _is_valid_fragment(frag): + _emit("links", f"broken fragment link -> {target}") + for nested in parsed.nested_parsers: + _validate_parsed_refs(nested) + + _validate_parsed_refs(parser) def normalized_site_views( diff --git a/scripts/test_site.py b/scripts/test_site.py index e215f78..a2c53c3 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -506,6 +506,7 @@ def test_implicit_top_fragment_and_autoloading_resources_and_hidden_css(self): '<a href="index.html#top">Index Top</a>' '<a href="sms:+15555550100">SMS</a>' '<a href="geo:37.786971,-122.399677">Map</a>' + '<iframe srcdoc="<p id=inner>x</p><a href=#inner>go</a>"></iframe>' '<img srcset="ok.png?variant=a,b 1x">\n', ) (self.root / "site/ok.png").write_bytes(_make_png()) From 987f39d80ddc1f48a13568d8d18310a6549a7540 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:44:12 +0800 Subject: [PATCH 09/12] fix(scripts): decode XML character references in SVG style blocks - Unescape XML character references in <style> blocks for .svg/.xml files - Add regression test in scripts/test_site.py --- scripts/check_site.py | 5 +++-- scripts/test_site.py | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/scripts/check_site.py b/scripts/check_site.py index d853d19..ab9067f 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -954,8 +954,9 @@ def _flush_raw_tag(self, tag_name: str) -> None: if ("<" in dec or "&" in dec) and self.depth < 3: self.subtrees.append(normalize_html_text(dec, self.depth + 1, page_rel=self.page_rel)) elif tag_name == "style": - self.style_blocks.append(raw_body) - self.subtrees.append(unescape_css_literals(raw_body)) + style_body = html.unescape(raw_body) if self.page_rel.lower().endswith((".svg", ".xml")) else raw_body + self.style_blocks.append(style_body) + self.subtrees.append(unescape_css_literals(style_body)) elif tag_name == "title": unesc = html.unescape(raw_body) self.inline_parts.append(unesc) diff --git a/scripts/test_site.py b/scripts/test_site.py index a2c53c3..a6b5806 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -572,6 +572,7 @@ def test_implicit_top_fragment_and_autoloading_resources_and_hidden_css(self): self.write( "site/ns-and-xmlbase.svg", '<s:svg xmlns:s="http://www.w3.org/2000/svg" xml:base="https://cdn.example.com/">' + '<style>rect { fill: url(https://cdn.example.com/charref.svg#p); }</style>' '<s:script href="https://cdn.example.com/x.js"></s:script></s:svg>\n', ) dirty = self.report() @@ -623,6 +624,10 @@ def test_implicit_top_fragment_and_autoloading_resources_and_hidden_css(self): any("site/ns-and-xmlbase.svg" in m and "remote or escaping script asset" in m for m in publish), publish, ) + self.assertTrue( + any("site/ns-and-xmlbase.svg" in m and "https://cdn.example.com/charref.svg#" in m for m in publish), + publish, + ) self.assertTrue( any("site/css-escapes-and-imageset.css" in m and "https://cdn.example.com/x.png" in m for m in publish), publish, From 21260a04de5437f7bfd759c980366579913f0809 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:50:24 +0800 Subject: [PATCH 10/12] fix(scripts): strip text-fragment directives from anchor targets - Strip :~: text-fragment directives before validating element IDs - Add regression tests in scripts/test_site.py --- scripts/check_site.py | 3 ++- scripts/test_site.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/check_site.py b/scripts/check_site.py index ab9067f..3070eee 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -1108,7 +1108,8 @@ def _check_css_text(css_text: str, *, allow_hiding: bool = True) -> None: def _validate_parsed_refs(parsed: _SiteHTMLParser) -> None: def _is_valid_fragment(frag: str) -> bool: - return not frag or frag.lower() == "top" or frag in parsed.element_ids + base_frag = frag.split(":~:", 1)[0] + return not base_frag or base_frag.lower() == "top" or base_frag in parsed.element_ids for kind, target in parsed.asset_refs: if kind in resource_kinds: diff --git a/scripts/test_site.py b/scripts/test_site.py index a6b5806..0e8ba78 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -504,6 +504,9 @@ def test_implicit_top_fragment_and_autoloading_resources_and_hidden_css(self): '<a href="/#top">Root Top</a>' '<a href="./#TOP">Dot Top</a>' '<a href="index.html#top">Index Top</a>' + '<p id="details">Product details</p>' + '<a href="#details:~:text=Product">Text Fragment</a>' + '<a href="#:~:text=Product">Directive Only</a>' '<a href="sms:+15555550100">SMS</a>' '<a href="geo:37.786971,-122.399677">Map</a>' '<iframe srcdoc="<p id=inner>x</p><a href=#inner>go</a>"></iframe>' From 451d33d8bee232744ba93269ac1eff09931de991 Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:58:22 +0800 Subject: [PATCH 11/12] fix(scripts): strip embedded tabs and newlines from site URLs - Strip ASCII tabs and newlines in resolve_site_asset_href and anchor checks - Add regression tests in scripts/test_site.py --- scripts/check_site.py | 4 ++-- scripts/test_site.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/check_site.py b/scripts/check_site.py index 3070eee..718d49c 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -353,7 +353,7 @@ def _parse_srcset_urls(srcset: str) -> list[str]: def resolve_site_asset_href(page_rel: str, href: str, allow_root: bool = False) -> str | None: """Resolve a site/-local HTML/CSS href against page_rel; return None for external or escaping targets.""" - raw = href.strip() + raw = re.sub(r"[\t\n\r]+", "", href).strip() if not raw or raw.startswith("//") or "\\" in raw: return None clean = raw.split("#", 1)[0].split("?", 1)[0].strip() @@ -1121,7 +1121,7 @@ def _is_valid_fragment(frag: str) -> bool: elif not (root / resolved).is_file(): _emit("links", f"broken local {kind} link -> {target}") elif kind == "anchor" and suffix in (".html", ".htm", ".xhtml"): - raw = target.strip() + raw = re.sub(r"[\t\n\r]+", "", target).strip() if not raw: continue unq_raw = urllib.parse.unquote(raw) diff --git a/scripts/test_site.py b/scripts/test_site.py index 0e8ba78..35dbd7a 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -507,6 +507,8 @@ def test_implicit_top_fragment_and_autoloading_resources_and_hidden_css(self): '<p id="details">Product details</p>' '<a href="#details:~:text=Product">Text Fragment</a>' '<a href="#:~:text=Product">Directive Only</a>' + '<a href="#det\tails">Tab Fragment</a>' + '<a href="in\ndex.html">Newline Path</a>' '<a href="sms:+15555550100">SMS</a>' '<a href="geo:37.786971,-122.399677">Map</a>' '<iframe srcdoc="<p id=inner>x</p><a href=#inner>go</a>"></iframe>' From 60f10c2a23f846725a3a8783bfea5db8061282df Mon Sep 17 00:00:00 2001 From: Pigbibi <20649888+Pigbibi@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:17:45 +0800 Subject: [PATCH 12/12] fix(scripts): validate legacy <frame> src resources - Collect and validate <frame src=...> resources alongside iframe/object/embed - Add regression test in scripts/test_site.py --- scripts/check_site.py | 4 ++-- scripts/test_site.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/check_site.py b/scripts/check_site.py index 718d49c..f8762f7 100644 --- a/scripts/check_site.py +++ b/scripts/check_site.py @@ -901,7 +901,7 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None if "srcset" in ad and not re.match(r"^(?:data|javascript|vbscript):", ad["srcset"].strip(), re.IGNORECASE): for cand_url in _parse_srcset_urls(ad["srcset"]): self.asset_refs.append(("img", cand_url)) - elif t in ("iframe", "embed", "audio", "video", "input", "track"): + elif t in ("iframe", "frame", "embed", "audio", "video", "input", "track"): if "src" in ad: self.asset_refs.append((t, ad["src"])) if t == "video" and "poster" in ad: @@ -1102,7 +1102,7 @@ def _check_css_text(css_text: str, *, allow_hiding: bool = True) -> None: _check_css_text(css_blk, allow_hiding=False) resource_kinds = { - "link", "script", "img", "iframe", "object", "embed", + "link", "script", "img", "iframe", "frame", "object", "embed", "audio", "video", "input", "track", "meta", } diff --git a/scripts/test_site.py b/scripts/test_site.py index 35dbd7a..7136d11 100644 --- a/scripts/test_site.py +++ b/scripts/test_site.py @@ -528,6 +528,7 @@ def test_implicit_top_fragment_and_autoloading_resources_and_hidden_css(self): self.write( "site/autoload-remote.html", '<iframe src="https://cdn.example.com/frame.html"></iframe>\n' + '<frameset><frame src="https://collector.invalid/remote"></frameset>\n' '<object data="https://cdn.example.com/obj.swf"></object>\n' '<embed src="https://cdn.example.com/emb.swf">\n' '<audio src="https://cdn.example.com/a.mp3"></audio>\n' @@ -584,7 +585,7 @@ def test_implicit_top_fragment_and_autoloading_resources_and_hidden_css(self): check_site.check_leaks(self.root, dirty) publish = self.messages(dirty, "publish") links = self.messages(dirty, "links") - for kind in ("iframe", "object", "embed", "audio", "video", "link", "meta", "script", "img"): + for kind in ("iframe", "frame", "object", "embed", "audio", "video", "link", "meta", "script", "img"): self.assertTrue( any(f"remote or escaping {kind} asset" in m for m in publish), (kind, publish),