Files
Cole W. LeavittandClaude Opus 5.5 947685a0c0 patch-guards: check that a bare launch on Linux renders the host's fonts (#1044)
camoufox-bin started without the launcher's config claims no OS, so its
fonts must be the host's plus Twemoji Mozilla, the font stock Firefox
bundles. Up to 158.0.1-beta.41, Linux added fonts/ to fontconfig
recursively and so registered every OS's font set. A bare launch then
resolved Segoe UI, Lucida Console and Cambria Math under a Linux
User-Agent. Akamai's detectOSFontsSignal reads that as Windows (1 where
stock gives 5), and sans-serif measured 99 px high where stock measures
98. #997 changed font-hijacker.patch to add only the directory's own
files, which fixed this, but no test covers it.

The guard launches the binary twice: once as shipped, and once on a
profile with gfx.bundled-fonts.activate=0, which registers nothing from
fonts/. Every family in fonts.json and each generic's 72 px span must
measure the same in both. The only exception is Twemoji Mozilla, which
must render only in the default launch. The pref goes in prefs.js
because the font list reads it once, at startup.

On 158.0.1-beta.41 it fails: 646 extra families, and sans-serif is 99
px high. On a main build (159.0-beta.35) it passes.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 21:13:20 +00:00

244 lines
8.6 KiB
Python

#!/usr/bin/env python3
"""Patch-guard gates: browser/tests/playwright/patches/*.py, one standalone guard per shipped behaviour.
Whether the patches *apply* is the build's job (browser/scripts/patch.py fails it).
These check that what they do still works in the built browser -- the most
direct evidence that a Firefox bump did not quietly neuter a patch that still
applies cleanly, which is the failure mode a compile check cannot catch.
The guards fall into three groups, each its own suite and CI job:
spoofing a spoofed value still reaches the page and holds together
automation Playwright stays invisible to the page and never deadlocks it
parity what a page, or the OS, can observe matches stock Firefox
Each guard is a standalone script exiting 0 or 1. Policy allows zero failures.
Every guard belongs to exactly one group (ci/tests checks this), so a new one
cannot be left out of CI.
Run:
python3 -m ci.run_patch_guards --binary /path/to/camoufox-bin
python3 -m ci.run_patch_guards --binary /path/to/camoufox-bin --group automation
"""
from __future__ import annotations
import argparse
import os
import sys
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from . import results as evidence
from ._util import EVIDENCE_DIR, REPO_ROOT, log, run
GUARD_DIR = REPO_ROOT / "browser" / "tests" / "playwright" / "patches"
GROUPS: Dict[str, Tuple[str, ...]] = {
"spoofing": (
"animation-timing",
"audio-matches-stock",
"baseline-protections",
"context-app-version",
"context-http-user-agent",
"context-lists-across-processes",
"context-os-system-font",
"cpu-vendor-estimates",
"detached-window-stock-values",
"display-scale-resolution",
"fingerprint-setter-seal",
"ipv4-only-network",
"locale-explicit-names",
"media-devices-coherence",
"navigation-preconnect-proxied",
"offscreen-webgl2-follows-canvas",
"spoofed-voice-speaks",
"startup-prefs",
"startup-size-frame-extents",
"system-font-alias-stall",
"system-ui-font-spoofing",
"timezone-stable",
"touchscreen-digitizer",
"webgl-live-state",
"webgl-renderer-pair",
"webgl-spoofed-limit-cost",
"webrtc-context-ip",
"webrtc-port-range",
"webrtc-proxy-local-network",
"webrtc-proxy-no-udp-around",
"webrtc-socks5-udp",
"worker-config-reads",
),
"automation": (
"addons-install-once",
"context-process-isolation",
"debug-logging-off",
"driver-stack-frames",
"fonts-ready-driver",
"force-scope-access",
"humanize-edge-deadlock",
"humanize-mouse-trajectory",
"humanize-pacing",
"input-ack-backstop",
"isolated-evaluate",
"long-press-no-link-preview",
"main-world-eval",
"main-world-eval-globals",
"main-world-init-script",
"mouse-boundary-sweep",
"near-edge-mouse-deadlock",
"noop-mousemove-deadlock",
"occluded-window-active",
"pointer-lock-offscreen",
"screenshot-canvas-release",
"socks5-proxy-auth",
"trusted-events",
"ubo-lists-frozen",
"visible-automation-cues",
"webdriver-bidi-tabs",
),
"parity": (
"bare-launch-host-fonts",
"certificate-revocations",
"contentaccessible-parity",
"css-device-size-fractional",
"driven-click-hold",
"driven-click-throughput",
"driven-dblclick-gap",
"driven-drag-gtk",
"driven-evaluate-globals",
"driven-fill-paste",
"driven-global-order",
"driven-move-coalescing",
"driven-page-real-focus",
"driven-wheel-notches",
"enterprise-policies",
"firefox-version-pin",
"getter-cost-parity",
"gfx-probes-packaged",
"headless-software-webgl",
"juggler-initial-blank-context",
"juggler-input-gate",
"nimbus-without-normandy",
"no-background-traffic",
"playwright-window-size",
"popup-blocker-parity",
"popup-covered-click",
"popup-window-size",
"release-rollouts",
"screenshot-leaves-page",
"search-service-init",
"startup-size-hold-yields",
"stock-gpu-parity",
"stock-parity-probes",
"system-font-size-windows",
"tracking-protection-lists",
"viewport-no-rdm",
"virtual-display-window-visible",
"windows-crt-math",
"windows-exe-manifest",
"windows-keyboard",
),
}
def gate_name(group: Optional[str]) -> str:
"""patch_guards for the whole set, patch_guards_<group> for one group."""
return f"patch_guards_{group}" if group else "patch_guards"
def guards() -> List[Path]:
"""Every guard script. helpers.py is a library, not a guard."""
return sorted(p for p in GUARD_DIR.glob("*.py") if p.name != "helpers.py")
def leftover_browsers(binary: Path) -> List[str]:
"""Processes of the browser still running after a guard, with CPU seconds.
Guards share one runner. A browser a guard failed to close keeps its
content processes -- and any spinning workers -- alive into every later
guard, which then sees a slower machine than it was written for. On Linux
those content processes run plugin-container, which sits beside the binary.
"""
binary = binary.resolve()
exes = {str(binary), str(binary.parent / "plugin-container")}
ticks = os.sysconf("SC_CLK_TCK")
found = []
for proc in Path("/proc").glob("[0-9]*"):
try:
if os.readlink(proc / "exe") not in exes:
continue
fields = (proc / "stat").read_text().rsplit(")", 1)[1].split()
except OSError:
continue
cpu = (int(fields[11]) + int(fields[12])) / ticks
found.append(f"pid {proc.name} cpu {cpu:.1f}s")
return found
def main(argv: Optional[List[str]] = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--binary", type=Path)
parser.add_argument("--evidence-dir", type=Path, default=EVIDENCE_DIR)
parser.add_argument("--timeout", type=int, default=600, help="per guard")
parser.add_argument("--only", nargs="*", help="run only these guard names")
parser.add_argument("--group", choices=sorted(GROUPS), help="run one group (default: all)")
args = parser.parse_args(argv)
from ._pytest import built_binary
result = evidence.GateResult(gate=gate_name(args.group))
binary = args.binary or built_binary()
if not binary.exists():
result.note(f"no built binary at {binary}")
result.finish(evidence.ERROR).save(args.evidence_dir)
return 1
env = {
"CAMOUFOX_EXECUTABLE_PATH": str(binary),
# The guards drive the browser through the Python package, which resolves
# the binary from this variable rather than a packaged install.
"PYTHONPATH": os.pathsep.join(
filter(None, [str(REPO_ROOT / "python" / "src"), os.environ.get("PYTHONPATH", "")])
),
}
selected = [
g for g in guards()
if (not args.only or g.stem in args.only)
and (not args.group or g.stem in GROUPS[args.group])
]
if not selected:
result.note("no guards found -- browser/tests/playwright/patches/ is empty or the filter matched nothing")
result.finish(evidence.ERROR).save(args.evidence_dir)
return 1
failed: List[str] = []
for guard in selected:
proc = run([sys.executable, str(guard)], cwd=REPO_ROOT, env=env, timeout=args.timeout)
outcome = evidence.PASS if proc.ok else evidence.FAIL
result.record(f"patches/{guard.name}", outcome)
if not proc.ok:
failed.append(guard.name)
tail = proc.combined().strip().splitlines()[-6:]
result.note(f"{guard.name} failed ({proc.code}): " + " | ".join(t.strip() for t in tail))
# The note keeps six lines; the job log gets all of it.
log(f" ✗ {guard.name} output:\n{proc.combined().strip()}")
else:
log(f" ✓ {guard.name}")
leftovers = leftover_browsers(binary)
if leftovers:
log(f" {guard.name} left {len(leftovers)} browser processes running: {', '.join(leftovers)}",
level="WARN")
passed = len(selected) - len(failed)
result.note(f"{passed}/{len(selected)} guards passed")
status = evidence.PASS if not failed else evidence.FAIL
result.finish(status).save(args.evidence_dir)
return 0 if status == evidence.PASS else 1
if __name__ == "__main__":
sys.exit(main())