#!/bin/sh # SPIKE (dynamic specialists slice 1, task 1). Not production code: it # answers one question before the renderer is built. Can Debian's # chromium-headless-shell, its own sandbox ON, render a page inside a # bubblewrap launch with every namespace unshared and nothing from the # host's /usr? # # scripts/renderer-spike.sh DIR [render|no-loader|no-userns|evidence] # # DIR is an empty scratch directory you remove afterwards (a few hundred # MB). The first run fills DIR/tree with the pinned browser packages of # scripts/ci-chromium.json plus every package they need, essential ones # included (the pin script's resolver with no pruning), each deb checked # against the SHA-256 of the signed snapshot index; DIR/packages.txt lists # them as name=version. # # render render DIR/site/index.html to DIR/out/page.png and check it # no-loader the same launch without the loader path: must fail # no-userns the same launch, nested user namespaces denied: the browser # must refuse to start (its sandbox is on, not falling back) # evidence keep a browser open and print each process's namespaces and # seccomp mode, read from the host's /proc set -eu dir=$(cd "${1:?usage: renderer-spike.sh DIR [render|no-loader|no-userns|evidence]}" && pwd) mode=${2:-render} here=$(cd "$(dirname "$0")" && pwd) tree=$dir/tree if [ ! -x "$tree/usr/lib/chromium/chromium-headless-shell" ]; then mkdir -p "$dir/debs" "$tree" python3 -I - "$here" "$dir" <<'PY' import hashlib, importlib.util, json, os, sys, urllib.request from concurrent.futures import ThreadPoolExecutor here, out = sys.argv[1], sys.argv[2] spec = importlib.util.spec_from_file_location("pin", os.path.join(here, "ci-chromium-pin.py")) pin = importlib.util.module_from_spec(spec) spec.loader.exec_module(pin) ci = json.load(open(os.path.join(here, "ci-chromium.json"))) s = ci["snapshots"] packages = pin.index("debian", s["debian"], "trixie", "amd64") packages.update(pin.index("debian-security", s["debian-security"], "trixie-security", "amd64")) wanted = sorted(pin.resolver(packages)(pin.TOP, skip=pin.SKIP) - pin.SKIP) for p in ci["amd64"]: # the browser packages, at exactly CI's versions if p["package"] not in wanted or packages[p["package"]]["Version"] != p["version"]: sys.exit("closure disagrees with ci-chromium.json on " + p["package"]) def get(name): p = packages[name] data = pin.fetch(p["_base"] + p["Filename"]) if hashlib.sha256(data).hexdigest() != p["SHA256"]: sys.exit("%s: SHA-256 mismatch" % name) with open(os.path.join(out, "debs", name + ".deb"), "wb") as f: f.write(data) with ThreadPoolExecutor(max_workers=4) as pool: list(pool.map(get, wanted)) ours = {p["package"] for p in ci["amd64"]} with open(os.path.join(out, "packages.txt"), "w") as f: for n in wanted: f.write("%s=%s%s\n" % (n, packages[n]["Version"], "" if n in ours else " (essential)")) print("%d packages, %d beyond ci-chromium.json" % (len(wanted), len(set(wanted) - ours))) PY for deb in "$dir"/debs/*.deb; do dpkg-deb -x "$deb" "$tree"; done rm -r "$dir/debs" fi mkdir -p "$dir/site" "$dir/out" cat >"$dir/site/index.html" <<'HTML' spike

Rendered inside the sandbox

HTML rm -f "$dir/out/page.png" # The launch: task 5's shape. Only the pinned tree is mounted, read only, # as /usr (and its fonts config at /etc/fonts); /lib, /lib64 and /bin are # links into it. loader="--symlink usr/lib64 /lib64" userns="" case $mode in no-loader) loader="" ;; no-userns) userns="--disable-userns" ;; render | evidence) ;; *) echo "unknown mode $mode" >&2; exit 2 ;; esac set -- bwrap --unshare-all --unshare-user $userns --die-with-parent --new-session \ --clearenv --cap-drop ALL \ --ro-bind "$tree/usr" /usr --symlink usr/lib /lib $loader --symlink usr/bin /bin \ --ro-bind "$tree/etc/fonts" /etc/fonts \ --ro-bind "$dir/site" /site --bind "$dir/out" /out \ --tmpfs /tmp --proc /proc --dev /dev --setenv HOME /tmp \ -- /usr/lib/chromium/chromium-headless-shell --user-data-dir=/tmp/profile \ --disable-crash-reporter --enable-logging=stderr --v=0 if [ "$mode" = evidence ]; then "$@" file:///site/index.html 2>/dev/null & top=$! sleep 4 tree_of() { echo "$1"; for c in $(pgrep -P "$1"); do tree_of "$c"; done; } for p in $(tree_of "$top"); do printf '%s %s user=%s pid=%s net=%s %s\n' "$p" \ "$(cat /proc/$p/comm; tr '\0' ' ' II", png[16:24]) idat, i = b"", 8 while i < len(png): n, kind = struct.unpack(">I4s", png[i:i + 8]) if kind == b"IDAT": idat += png[i + 8:i + 8 + n] i += 12 + n # Whatever row 0's filter, its first pixel's bytes are the pixel itself. px = tuple(zlib.decompress(idat)[1:4]) print("PNG %dx%d, pixel (0,0) = #%02x%02x%02x" % ((w, h) + px)) sys.exit(0 if (w, h, px) == (1440, 900, (0x33, 0x66, 0xcc)) else "FAIL: not the fixture") PY