mirror of
https://github.com/beenuar/AiSOC.git
synced 2026-11-24 16:48:33 +08:00
* fix(web): self-host the five webfonts so `next build` never calls Google `apps/web` loaded Inter, JetBrains Mono, DM Serif Display, Poppins and Source Code Pro through `next/font/google`, which resolves a face by fetching `fonts.googleapis.com` for the stylesheet and `fonts.gstatic.com` for each file **during `next build`**. Every build surface therefore depended on a third party being reachable *and* on its response not changing, and both halves have now failed. Unreachable: a build whose egress to `fonts.gstatic.com` is blocked fails with `Module not found: Can't resolve '@vercel/turbopack-next/internal/font/google/font'`, which reads like a bad import and is a network error. That is the flake that has reddened `Web — Build` on pull requests touching no web code. Changed: Google moved Source Code Pro to v32 and now serves *one* variable file for the two weights this app requests, so two `@font-face` blocks point at the same URL. Turbopack cannot resolve a font file referenced twice and fails with the same message — which is why `main` is red today with no commit having touched the web app. The five families are vendored under `apps/web/public/fonts` (39 woff2 files, 780 KB) and declared in a generated stylesheet. Nothing about the rendered typography changes: the 39 binaries are byte-identical to the ones `next/font/google` emits, and the 51 `@font-face` declarations match the baseline on family, style, weight, display, unicode-range and fallback metrics, including the per-subset `unicode-range` splitting that keeps Cyrillic, Greek, Vietnamese and Devanagari text on the real face. Chose self-hosting over `NEXT_FONT_GOOGLE_MOCKED_RESPONSES` because that env var does not do what it appears to: `fetchFontFile` returns `Buffer.from(url)` for any non-absolute path, so a mocked build emits the URL string as the font file and ships a corrupt woff2 — green build, broken typography, in the published image. It also only covers surfaces where it is set, and the list is open-ended (CI, both publish-images architectures, the production Dockerfile, release, compose-smoke, visual-regression, the devcontainer, the Fly/Railway recipes, and every contributor's laptop). - `scripts/fetch_web_fonts.py` refreshes the payload from Google; it is the only thing that talks to the network, and it is never run by a build. - `scripts/generate_web_font_css.py` turns the manifest into `fonts.css` and the preload lists; `--check` is an offline CI gate wired into `ci.yml` that also verifies every vendored file against its own content hash. - Preload tags are emitted explicitly, reproducing the exact 8 files `next/font` marked `.p.` and the routes each was scoped to. - `public/fonts` gets an immutable cache header; filenames are content-hashed. * fix(web): make the font fetcher structurally unable to open file:// URLs Semgrep's `dynamic-urllib-use-detected` fired on `_get()` and the finding is fair rather than noise: the font-file URLs are parsed out of a stylesheet Google returns, so they are attacker-influenced in principle, and `urllib` honours `file://`. An odd response could have turned the refresh into "read a local file and commit it as a font". Fixed by removing the capability rather than suppressing the rule. The opener is built from `OpenerDirector` with only the handlers an https GET needs — no FileHandler, no FTPHandler, no plain HTTPHandler — which both `build_opener()` and the default `urlopen` would have installed. A host and scheme allowlist sits in front of it as the legible second layer. Verified all three ways: the allowlist refuses `file:///etc/passwd`, plain http and a foreign host; the opener still refuses `file://` with `unknown url type: file` when the allowlist is bypassed entirely; and the real fetch against fonts.googleapis.com still works. Semgrep now reports 0 findings on both new scripts, so the ratchet ceiling stays where it is. * fix(web): type the font manifest properly instead of dict[str, object] `check_mypy_baseline` failed on three findings in `fetch_web_fonts.py` (`attr-defined`, `index`, `misc`), all from the manifest being typed as `dict[str, object]` and then indexed and iterated as if it were structured. The existing `# type: ignore[index]` was already a symptom — it silenced one of the three and the gate correctly reported that the other two were not covered by it. Replaced with four TypedDicts describing what the manifest actually is: `RemoteFace` (a face as Google's stylesheet describes it, carrying `url`), `VendoredFace` (the same face once the file is local, carrying `file`), `FamilyRecord` and `Manifest`. The `str(face["url"])` coercion and the `type: ignore` both go away because the types now say what the values are. mypy reports no findings for either script and the baseline is back to "no tree got worse". Verified the refactor is behaviour-preserving: `fetch_web_fonts.py --check` still round-trips against Google and no vendored file or manifest byte changed. * fix(web): make the two font scripts satisfy the gate contract `check_gate_contract` reported four findings against the new scripts, both of them real conventions this repository enforces on everything under `scripts/`: * `[root-from-file]` — both derived the repository root from `__file__` rather than asking git, so "two levels up" was whatever happened to be there. Both now use `gate_toolkit.repo_root()`. * `[no-self-test]` — neither declared `--self-test`, so nothing proved they still detect what they claim to. `fetch_web_fonts.py` takes the shared `self_test_if_requested` hook. `generate_web_font_css.py` is the CI gate, so it gets a bespoke self-test that hand-breaks each comparison in turn and requires the gate to notice: a drifted unicode-range must change the generated stylesheet, a manifest face naming a missing file must be refused, a woff2 whose bytes no longer match its filename hash must be refused, and an orphan file in public/fonts must be refused — alongside a positive control asserting an untouched payload reports nothing, so the gate cannot pass by refusing everything. Wired as its own step in `ci.yml` ahead of the check, so a green check means the comparison still happens rather than that it found nothing. * docs(matrix): recompute the tally after the merge, don't retype it `main`'s summary stanza said 356 rows / 351 GATED against a table that parses 358 / 353, so the ratchet failed directly and `Python — Tests` failed through `test_readme_figures_gate.py` and `test_check_claim_gate_matrix.py`. This branch touches no claim rows; it inherited the drift by merging `main`. Restated from the gate's own parse rather than typed, in all three surfaces that publish the figure — the stanza, `ROADMAP.md` and `evidence-pack.md` — which is the only form of this fix that does not go stale again at the next merge.
313 lines
12 KiB
Python
313 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""Download the web console's webfonts from Google Fonts into the repository.
|
|
|
|
``apps/web`` used to load its five faces through ``next/font/google``, which
|
|
resolves them **at compile time**: every ``next build`` issued a request to
|
|
``fonts.googleapis.com`` for the stylesheet and one to ``fonts.gstatic.com``
|
|
per font file. That made the build depend on a third party being both
|
|
reachable *and* unchanged, and both halves bit us:
|
|
|
|
* unreachable — a build on a runner with no egress to Google fails with
|
|
``Module not found: Can't resolve
|
|
'@vercel/turbopack-next/internal/font/google/font'``, which reads like a bad
|
|
import and is a network error;
|
|
* changed — Google moved Source Code Pro from v30 to v32 and began serving a
|
|
single variable file for the two weights ``apps/web`` asks for, so the two
|
|
``@font-face`` blocks pointed at one URL. Turbopack cannot resolve a font
|
|
file referenced twice and failed the same way, on a tree nobody had touched.
|
|
|
|
So the fonts are vendored and loaded with ``next/font/local``. This script is
|
|
how they were produced and how they are refreshed; ``check_web_fonts.py`` is
|
|
the gate that keeps the vendored set and the TypeScript declarations honest,
|
|
and it runs offline so refreshing is a deliberate act rather than a build-time
|
|
accident.
|
|
|
|
Usage::
|
|
|
|
python3 scripts/fetch_web_fonts.py # refresh apps/web/src/fonts
|
|
python3 scripts/fetch_web_fonts.py --check # fail if a refresh would change anything
|
|
|
|
The request is byte-for-byte the one ``next/font/google`` would have made: the
|
|
URLs below are what ``getGoogleFontsUrl()`` builds for the options each face is
|
|
declared with, and the user agent is the one
|
|
``next/dist/compiled/@next/font/dist/google/fetch-resource.js`` sends in order
|
|
to be served ``woff2`` rather than ``ttf``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import re
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import urllib.parse
|
|
import urllib.request
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import TypedDict
|
|
|
|
# `scripts/` is on sys.path when this file is run as a program, but not when a
|
|
# test loads it by path with importlib. gate_toolkit sits beside it either way.
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
|
|
from gate_toolkit import repo_root, self_test_if_requested # noqa: E402 - needs the sys.path line above
|
|
|
|
self_test_if_requested(__file__)
|
|
|
|
REPO_ROOT = repo_root()
|
|
# The woff2 payload is served from `public/` rather than imported through the
|
|
# bundler so that the `<link rel="preload">` tags in the layouts can name a
|
|
# stable URL. Filenames carry a content hash, which is what makes the
|
|
# immutable cache header in `next.config.js` safe.
|
|
FONT_DIR = REPO_ROOT / "apps" / "web" / "public" / "fonts"
|
|
# The declarations derived from those files live beside the stylesheet they
|
|
# generate, under `src/`, so nothing describing the build is served to users.
|
|
MANIFEST = REPO_ROOT / "apps" / "web" / "src" / "fonts" / "manifest.json"
|
|
|
|
# Chrome UA. Google serves `ttf` to clients it does not recognise, so this is
|
|
# load-bearing rather than cosmetic — it is what gets us `woff2`.
|
|
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
|
|
|
|
# The only two hosts this script may contact: the stylesheet API and the font
|
|
# CDN that stylesheet points at.
|
|
ALLOWED_HOSTS = frozenset({"fonts.googleapis.com", "fonts.gstatic.com"})
|
|
|
|
# An opener carrying *only* the handlers an https GET needs. Both
|
|
# `build_opener()` and the default `urlopen` install FileHandler and
|
|
# FTPHandler; omitting them means a `file://` or `ftp://` URL arriving in a
|
|
# Google response has no handler to serve it and raises, rather than being
|
|
# fetched and written into the repository as a font. UnknownHandler is here
|
|
# so that refusal is a clean URLError instead of `open()` returning None.
|
|
# HTTPHandler is absent too, so plain http cannot be used either.
|
|
_OPENER = urllib.request.OpenerDirector()
|
|
for _handler in (
|
|
urllib.request.HTTPSHandler(),
|
|
urllib.request.HTTPRedirectHandler(),
|
|
urllib.request.HTTPErrorProcessor(),
|
|
urllib.request.UnknownHandler(),
|
|
):
|
|
_OPENER.add_handler(_handler)
|
|
|
|
|
|
class RemoteFace(TypedDict):
|
|
"""One `@font-face` block as Google's stylesheet describes it."""
|
|
|
|
subset: str
|
|
weight: str
|
|
style: str
|
|
unicodeRange: str
|
|
url: str
|
|
|
|
|
|
class VendoredFace(TypedDict):
|
|
"""The same face once its file is vendored — `url` becomes `file`."""
|
|
|
|
file: str
|
|
subset: str
|
|
weight: str
|
|
style: str
|
|
unicodeRange: str
|
|
|
|
|
|
class FamilyRecord(TypedDict):
|
|
cssName: str
|
|
googleQuery: str
|
|
faces: list[VendoredFace]
|
|
|
|
|
|
class Manifest(TypedDict):
|
|
_comment: str
|
|
families: dict[str, FamilyRecord]
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Family:
|
|
"""One font family, and the Google query the old call site resolved to."""
|
|
|
|
# Slug used for the vendored filenames and the manifest key.
|
|
slug: str
|
|
# Family name as it appears in the stylesheet Google returns.
|
|
css_name: str
|
|
# The `family=` value `getGoogleFontsUrl()` builds for this face's options.
|
|
query: str
|
|
|
|
|
|
FAMILIES = (
|
|
# `Inter({ subsets: ['latin'] })` — no weight given, so the variable axis.
|
|
Family("inter", "Inter", "Inter:wght@100..900"),
|
|
# `JetBrains_Mono({ subsets: ['latin'] })` — likewise variable.
|
|
Family("jetbrains-mono", "JetBrains Mono", "JetBrains+Mono:wght@100..800"),
|
|
# `DM_Serif_Display({ weight: '400', style: ['normal', 'italic'] })`.
|
|
Family("dm-serif-display", "DM Serif Display", "DM+Serif+Display:ital,wght@0,400;1,400"),
|
|
# `Poppins({ weight: ['300', '400', '500', '600', '700'] })` — static cuts.
|
|
Family("poppins", "Poppins", "Poppins:wght@300;400;500;600;700"),
|
|
# `Source_Code_Pro({ weight: ['400', '500'] })` — the face that broke.
|
|
Family("source-code-pro", "Source Code Pro", "Source+Code+Pro:wght@400;500"),
|
|
)
|
|
|
|
# `@font-face` blocks in a Google stylesheet are preceded by a comment naming
|
|
# the subset they cover. That comment is the only place the subset name appears,
|
|
# so it is parsed rather than inferred from the unicode-range.
|
|
_FACE_RE = re.compile(
|
|
r"/\*\s*(?P<subset>[a-z0-9-]+)\s*\*/\s*@font-face\s*\{(?P<body>[^}]*)\}",
|
|
re.IGNORECASE,
|
|
)
|
|
|
|
|
|
def _declaration(body: str, prop: str) -> str:
|
|
match = re.search(rf"\b{re.escape(prop)}\s*:\s*(?P<value>[^;]+);", body)
|
|
if match is None:
|
|
raise SystemExit(f"Google stylesheet has a @font-face with no `{prop}`:\n{body}")
|
|
return match.group("value").strip()
|
|
|
|
|
|
def _get(url: str) -> bytes:
|
|
"""Fetch `url`, which must be https on one of the two Google font hosts.
|
|
|
|
The font-file URLs are read out of a stylesheet Google returns, so they
|
|
are attacker-influenced in principle. `urllib` honours `file://`, which
|
|
would turn an odd response into "read a local file and commit it as a
|
|
font", so the opener below is built with no file, ftp or plain-http
|
|
handler at all — those schemes are not merely rejected, they are
|
|
unrepresentable. The host allowlist is the second, legible layer.
|
|
"""
|
|
parsed = urllib.parse.urlsplit(url)
|
|
if parsed.scheme != "https" or parsed.hostname not in ALLOWED_HOSTS:
|
|
raise SystemExit(f"Refusing to fetch {url!r}: not https on {sorted(ALLOWED_HOSTS)}")
|
|
request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
|
|
with _OPENER.open(request, timeout=30) as response:
|
|
return response.read()
|
|
|
|
|
|
def fetch_family(family: Family) -> list[RemoteFace]:
|
|
"""Return one record per `@font-face` Google serves for `family`."""
|
|
url = f"https://fonts.googleapis.com/css2?family={family.query}&display=swap"
|
|
css = _get(url).decode("utf-8")
|
|
|
|
faces: list[RemoteFace] = []
|
|
for match in _FACE_RE.finditer(css):
|
|
body = match.group("body")
|
|
src = _declaration(body, "src")
|
|
href = re.search(r"url\((?P<href>https://[^)]+)\)", src)
|
|
if href is None:
|
|
raise SystemExit(f"@font-face src is not a remote url: {src}")
|
|
faces.append(
|
|
{
|
|
"subset": match.group("subset"),
|
|
"weight": _declaration(body, "font-weight"),
|
|
"style": _declaration(body, "font-style"),
|
|
"unicodeRange": _declaration(body, "unicode-range"),
|
|
"url": href.group("href"),
|
|
}
|
|
)
|
|
|
|
if not faces:
|
|
raise SystemExit(f"No @font-face blocks found for {family.css_name} at {url}")
|
|
return faces
|
|
|
|
|
|
def build(destination: Path) -> Manifest:
|
|
"""Download every face into `destination` and return the manifest."""
|
|
destination.mkdir(parents=True, exist_ok=True)
|
|
manifest: Manifest = {
|
|
"_comment": (
|
|
"Generated by scripts/fetch_web_fonts.py. Vendored so `next build` "
|
|
"never contacts Google. Refresh with that script, never by hand."
|
|
),
|
|
"families": {},
|
|
}
|
|
|
|
for family in FAMILIES:
|
|
faces = fetch_family(family)
|
|
# Google serves one *variable* file per subset, so the same URL is
|
|
# reused across the weights that file covers. Download each distinct
|
|
# URL once and let several faces point at the same vendored file —
|
|
# exactly the sharing that Turbopack could not resolve remotely.
|
|
by_url: dict[str, str] = {}
|
|
records: list[VendoredFace] = []
|
|
for face in faces:
|
|
url = face["url"]
|
|
if url not in by_url:
|
|
payload = _get(url)
|
|
if payload[:4] != b"wOF2":
|
|
raise SystemExit(f"{url} did not return a woff2 file")
|
|
digest = hashlib.sha256(payload).hexdigest()
|
|
name = f"{family.slug}-{face['subset']}-{digest[:8]}.woff2"
|
|
(destination / name).write_bytes(payload)
|
|
by_url[url] = name
|
|
records.append(
|
|
{
|
|
"file": by_url[url],
|
|
"subset": face["subset"],
|
|
"weight": face["weight"],
|
|
"style": face["style"],
|
|
"unicodeRange": face["unicodeRange"],
|
|
}
|
|
)
|
|
manifest["families"][family.slug] = {
|
|
"cssName": family.css_name,
|
|
"googleQuery": family.query,
|
|
"faces": records,
|
|
}
|
|
|
|
return manifest
|
|
|
|
|
|
def _written_state(directory: Path) -> dict[str, str]:
|
|
"""Map every vendored file to its sha256, for comparing two directories."""
|
|
return {path.name: hashlib.sha256(path.read_bytes()).hexdigest() for path in sorted(directory.glob("*.woff2"))}
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument(
|
|
"--check",
|
|
action="store_true",
|
|
help="Download into a temporary directory and report whether the vendored set is stale. Requires network.",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
if args.check:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
scratch = Path(tmp)
|
|
fresh = build(scratch)
|
|
stale = []
|
|
if _written_state(scratch) != _written_state(FONT_DIR):
|
|
stale.append("font files differ")
|
|
if not MANIFEST.exists():
|
|
stale.append("manifest.json is missing")
|
|
elif json.loads(MANIFEST.read_text()) != fresh:
|
|
stale.append("manifest.json differs")
|
|
if stale:
|
|
print("Vendored fonts are stale: " + "; ".join(stale))
|
|
print("Refresh with: python3 scripts/fetch_web_fonts.py")
|
|
return 1
|
|
print("Vendored fonts match Google Fonts.")
|
|
return 0
|
|
|
|
# Write into a scratch directory first so a failed download cannot leave
|
|
# the tree with half a font set.
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
scratch = Path(tmp)
|
|
manifest = build(scratch)
|
|
if FONT_DIR.exists():
|
|
for stale_file in FONT_DIR.glob("*.woff2"):
|
|
stale_file.unlink()
|
|
FONT_DIR.mkdir(parents=True, exist_ok=True)
|
|
for produced in sorted(scratch.glob("*.woff2")):
|
|
shutil.copy2(produced, FONT_DIR / produced.name)
|
|
MANIFEST.parent.mkdir(parents=True, exist_ok=True)
|
|
MANIFEST.write_text(json.dumps(manifest, indent=2) + "\n")
|
|
|
|
total = sum(len(f["faces"]) for f in manifest["families"].values())
|
|
files = len(list(FONT_DIR.glob("*.woff2")))
|
|
print(f"Vendored {files} woff2 files covering {total} @font-face declarations.")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|