| 1 | #!/bin/sh |
| 2 | # SPIKE (dynamic specialists slice 1, task 1). Not production code: it |
| 3 | # answers one question before the renderer is built. Can Debian's |
| 4 | # chromium-headless-shell, its own sandbox ON, render a page inside a |
| 5 | # bubblewrap launch with every namespace unshared and nothing from the |
| 6 | # host's /usr? |
| 7 | # |
| 8 | # scripts/renderer-spike.sh DIR [render|no-loader|no-userns|evidence] |
| 9 | # |
| 10 | # DIR is an empty scratch directory you remove afterwards (a few hundred |
| 11 | # MB). The first run fills DIR/tree with the pinned browser packages of |
| 12 | # scripts/ci-chromium.json plus every package they need, essential ones |
| 13 | # included (the pin script's resolver with no pruning), each deb checked |
| 14 | # against the SHA-256 of the signed snapshot index; DIR/packages.txt lists |
| 15 | # them as name=version. |
| 16 | # |
| 17 | # render render DIR/site/index.html to DIR/out/page.png and check it |
| 18 | # no-loader the same launch without the loader path: must fail |
| 19 | # no-userns the same launch, nested user namespaces denied: the browser |
| 20 | # must refuse to start (its sandbox is on, not falling back) |
| 21 | # evidence keep a browser open and print each process's namespaces and |
| 22 | # seccomp mode, read from the host's /proc |
| 23 | set -eu |
| 24 | dir=$(cd "${1:?usage: renderer-spike.sh DIR [render|no-loader|no-userns|evidence]}" && pwd) |
| 25 | mode=${2:-render} |
| 26 | here=$(cd "$(dirname "$0")" && pwd) |
| 27 | tree=$dir/tree |
| 28 | |
| 29 | if [ ! -x "$tree/usr/lib/chromium/chromium-headless-shell" ]; then |
| 30 | mkdir -p "$dir/debs" "$tree" |
| 31 | python3 -I - "$here" "$dir" <<'PY' |
| 32 | import hashlib, importlib.util, json, os, sys, urllib.request |
| 33 | from concurrent.futures import ThreadPoolExecutor |
| 34 | here, out = sys.argv[1], sys.argv[2] |
| 35 | spec = importlib.util.spec_from_file_location("pin", os.path.join(here, "ci-chromium-pin.py")) |
| 36 | pin = importlib.util.module_from_spec(spec) |
| 37 | spec.loader.exec_module(pin) |
| 38 | ci = json.load(open(os.path.join(here, "ci-chromium.json"))) |
| 39 | s = ci["snapshots"] |
| 40 | packages = pin.index("debian", s["debian"], "trixie", "amd64") |
| 41 | packages.update(pin.index("debian-security", s["debian-security"], "trixie-security", "amd64")) |
| 42 | wanted = sorted(pin.resolver(packages)(pin.TOP, skip=pin.SKIP) - pin.SKIP) |
| 43 | for p in ci["amd64"]: # the browser packages, at exactly CI's versions |
| 44 | if p["package"] not in wanted or packages[p["package"]]["Version"] != p["version"]: |
| 45 | sys.exit("closure disagrees with ci-chromium.json on " + p["package"]) |
| 46 | def get(name): |
| 47 | p = packages[name] |
| 48 | data = pin.fetch(p["_base"] + p["Filename"]) |
| 49 | if hashlib.sha256(data).hexdigest() != p["SHA256"]: |
| 50 | sys.exit("%s: SHA-256 mismatch" % name) |
| 51 | with open(os.path.join(out, "debs", name + ".deb"), "wb") as f: |
| 52 | f.write(data) |
| 53 | with ThreadPoolExecutor(max_workers=4) as pool: |
| 54 | list(pool.map(get, wanted)) |
| 55 | ours = {p["package"] for p in ci["amd64"]} |
| 56 | with open(os.path.join(out, "packages.txt"), "w") as f: |
| 57 | for n in wanted: |
| 58 | f.write("%s=%s%s\n" % (n, packages[n]["Version"], "" if n in ours else " (essential)")) |
| 59 | print("%d packages, %d beyond ci-chromium.json" % (len(wanted), len(set(wanted) - ours))) |
| 60 | PY |
| 61 | for deb in "$dir"/debs/*.deb; do dpkg-deb -x "$deb" "$tree"; done |
| 62 | rm -r "$dir/debs" |
| 63 | fi |
| 64 | |
| 65 | mkdir -p "$dir/site" "$dir/out" |
| 66 | cat >"$dir/site/index.html" <<'HTML' |
| 67 | <!doctype html><title>spike</title> |
| 68 | <style>html,body{margin:0;background:#3366cc;color:#fff;font:48px sans-serif}</style> |
| 69 | <p>Rendered inside the sandbox</p> |
| 70 | HTML |
| 71 | rm -f "$dir/out/page.png" |
| 72 | |
| 73 | # The launch: task 5's shape. Only the pinned tree is mounted, read only, |
| 74 | # as /usr (and its fonts config at /etc/fonts); /lib, /lib64 and /bin are |
| 75 | # links into it. |
| 76 | loader="--symlink usr/lib64 /lib64" |
| 77 | userns="" |
| 78 | case $mode in |
| 79 | no-loader) loader="" ;; |
| 80 | no-userns) userns="--disable-userns" ;; |
| 81 | render | evidence) ;; |
| 82 | *) echo "unknown mode $mode" >&2; exit 2 ;; |
| 83 | esac |
| 84 | set -- bwrap --unshare-all --unshare-user $userns --die-with-parent --new-session \ |
| 85 | --clearenv --cap-drop ALL \ |
| 86 | --ro-bind "$tree/usr" /usr --symlink usr/lib /lib $loader --symlink usr/bin /bin \ |
| 87 | --ro-bind "$tree/etc/fonts" /etc/fonts \ |
| 88 | --ro-bind "$dir/site" /site --bind "$dir/out" /out \ |
| 89 | --tmpfs /tmp --proc /proc --dev /dev --setenv HOME /tmp \ |
| 90 | -- /usr/lib/chromium/chromium-headless-shell --user-data-dir=/tmp/profile \ |
| 91 | --disable-crash-reporter --enable-logging=stderr --v=0 |
| 92 | if [ "$mode" = evidence ]; then |
| 93 | "$@" file:///site/index.html 2>/dev/null & |
| 94 | top=$! |
| 95 | sleep 4 |
| 96 | tree_of() { echo "$1"; for c in $(pgrep -P "$1"); do tree_of "$c"; done; } |
| 97 | for p in $(tree_of "$top"); do |
| 98 | printf '%s %s user=%s pid=%s net=%s %s\n' "$p" \ |
| 99 | "$(cat /proc/$p/comm; tr '\0' ' ' </proc/$p/cmdline | grep -oE -- '--(type|utility-sub-type)=[^ ]*|--[a-z-]*sandbox[a-z-]*' | tr '\n' ' ')" \ |
| 100 | "$(readlink /proc/$p/ns/user)" "$(readlink /proc/$p/ns/pid)" "$(readlink /proc/$p/ns/net)" \ |
| 101 | "$(grep -E '^(Seccomp|NoNewPrivs):' /proc/$p/status | tr '\n\t' ' ')" |
| 102 | done |
| 103 | kill "$top" |
| 104 | wait || true |
| 105 | exit 0 |
| 106 | fi |
| 107 | status=0 |
| 108 | "$@" --window-size=1440,900 --screenshot=/out/page.png file:///site/index.html || status=$? |
| 109 | echo "browser exit status $status" |
| 110 | python3 -I - "$dir/out/page.png" <<'PY' |
| 111 | import struct, sys, zlib |
| 112 | try: |
| 113 | png = open(sys.argv[1], "rb").read() |
| 114 | except OSError: |
| 115 | sys.exit("FAIL: no PNG") |
| 116 | w, h = struct.unpack(">II", png[16:24]) |
| 117 | idat, i = b"", 8 |
| 118 | while i < len(png): |
| 119 | n, kind = struct.unpack(">I4s", png[i:i + 8]) |
| 120 | if kind == b"IDAT": |
| 121 | idat += png[i + 8:i + 8 + n] |
| 122 | i += 12 + n |
| 123 | # Whatever row 0's filter, its first pixel's bytes are the pixel itself. |
| 124 | px = tuple(zlib.decompress(idat)[1:4]) |
| 125 | print("PNG %dx%d, pixel (0,0) = #%02x%02x%02x" % ((w, h) + px)) |
| 126 | sys.exit(0 if (w, h, px) == (1440, 900, (0x33, 0x66, 0xcc)) else "FAIL: not the fixture") |
| 127 | PY |
| 128 | |