From 662ecc001f088f856a0e3a4718efc1c6e29c5877 Mon Sep 17 00:00:00 2001 From: istos Date: Fri, 31 Jul 2026 15:39:46 +0200 Subject: [PATCH] site: add Fathom analytics, and fix the caching rule it sits beside MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two changes to what the live site sends. Analytics: Fathom's tag goes in every template's , deferred. It is cookieless and collects nothing about a person, so no consent banner — but it is a third party, so the CSP names cdn.usefathom.com for script and connect rather than opening the door generally, and the tests that said 'no script at all' now say 'no script this site depends on, and no origin nobody chose'. Caching: the host concatenates a header two matching rules both set rather than overriding, so /* and /static/* each setting Cache-Control sent 'max-age=0, must-revalidate, max-age=31536000, immutable' on the stylesheet — first max-age wins, and the year-long cache never happened. Measured on the live site, which is where the from-memory assumption in task 32 said to check it. Now /static/* is the only rule that sets it and HTML takes the host's revalidating default; the post-deploy checks in site/README.md verify both ends. --- site/README.md | 39 ++++++++++++++++-------- site/root/_headers | 39 ++++++++++++++++-------- site/templates/article.html | 4 +++ site/templates/home.html | 4 +++ site/templates/notfound.html | 4 +++ tests/test_site_build.py | 17 ++++++----- tests/test_site_deploy.py | 56 ++++++++++++++++++++++------------- tests/test_site_landing.py | 10 ++++++- tests/test_site_responsive.py | 14 +++++---- 9 files changed, 127 insertions(+), 60 deletions(-) diff --git a/site/README.md b/site/README.md index 302c306..9cda3c7 100644 --- a/site/README.md +++ b/site/README.md @@ -77,6 +77,7 @@ widening the page, and that the site still ships no JavaScript. | Worker | `bench-site` | | Route | `bench.12vectors.com`, a **custom domain** — Cloudflare owns the hostname at the zone level and creates the DNS record itself | | Served from | `site/dist/`, uploaded whole on every deploy | +| Analytics | [Fathom](https://usefathom.com), site `ZPKDEHCV` — cookieless, no personal data, no consent banner. The only third party the pages touch | | Config | `site/wrangler.jsonc` (routing) and `site/root/_headers` (caching, security) | Deploys are run by hand, by a person, exactly as releases are @@ -129,7 +130,12 @@ answers rather than files: page **with a 404 status**, not the landing page with a 200. 4. `curl -sI https://bench.12vectors.com/` shows `x-content-type-options`, `referrer-policy`, - `strict-transport-security` and a `cache-control` that revalidates. + `strict-transport-security` and a `cache-control` that revalidates — + the last one coming from the host's default now, not from `_headers`, + which is exactly why it is checked rather than assumed. Then + `curl -sI https://bench.12vectors.com/static/site.css` must show + `max-age=31536000, immutable` **once**, with no second `max-age` + beside it. ## How it is served @@ -141,17 +147,26 @@ answers rather than files: with a 404 status. That page is a normal manifest entry (`/404.html`, layout `notfound`) — the site's own design, its own nav, and a link back to the landing page. -- **Two caching policies, because there are two kinds of file.** HTML - revalidates on every view, so a deploy is visible on the next reload - with nobody clearing anything. Everything under `/static/` is kept for - a year and never re-checked, which is only safe because the stylesheet - and the icon are linked with a `?v=` of their own contents: - change the file and the url changes with it. -- **Baseline headers, no third parties.** `nosniff`, a referrer policy, - a year of HSTS, `X-Frame-Options`, and a Content-Security-Policy of - `default-src 'none'` with `'self'` for styles, fonts and images. The - site collects nothing and loads nothing from anywhere else; the CSP is - that promise in a form the browser enforces. +- **One rule sets `Cache-Control`, and it is `/static/*`.** The host + *concatenates* a header two matching rules both set — it does not + override, whatever the ordering suggests. Measured live: `/*` and + `/static/*` each setting `Cache-Control` produced + `max-age=0, must-revalidate, max-age=31536000, immutable` on the + stylesheet, and the first `max-age` wins in every browser, so the + year-long cache never happened. Now only `/static/*` sets it — safe + because the stylesheet and the icon are linked with a `?v=` of + their own contents — and HTML takes the host's own revalidating + default, which check 4 below confirms after every deploy. +- **Baseline headers, and one third party by choice.** `nosniff`, a + referrer policy, a year of HSTS, `X-Frame-Options`, and a + Content-Security-Policy of `default-src 'none'` with `'self'` for + styles, fonts and images. The one origin named besides this one is + `cdn.usefathom.com`: Fathom serves the analytics script and receives + its pageviews. It sets no cookie and collects nothing about a person, + which is why the site still needs no consent banner — but it is a + third party, and the policy names it rather than opening the door + generally. A second one would fail + `tests/test_site_deploy.py` rather than slip in. ## What is where diff --git a/site/root/_headers b/site/root/_headers index 967d760..96d6c36 100644 --- a/site/root/_headers +++ b/site/root/_headers @@ -5,9 +5,18 @@ # site/build.py, because the host looks for it at the root of the build # and nowhere else. # -# Rules apply in order and a later rule wins on a header it repeats. The -# two blocks below are written so that even a host that merged them -# instead would land on the safe side: HTML would still revalidate. +# Rules do NOT override each other: a header two matching rules both set +# arrives concatenated. Measured on the live site, not assumed — +# /* and /static/* both setting Cache-Control produced +# +# cache-control: public, max-age=0, must-revalidate, public, +# max-age=31536000, immutable +# +# on the stylesheet, and the first max-age wins in every browser, so the +# year-long cache never happened. So exactly one rule may set a given +# header: Cache-Control is set on /static/* alone, and HTML is left to +# the host's own default, which is the revalidating one. Check #4 after a +# deploy is what keeps that honest. # Everything, so that no page can ever forget one of these. # @@ -17,26 +26,30 @@ # `preload`: that is a submission to browser # vendors and a commitment this card did not make # X-Frame-Options nothing here is meant to be framed -# CSP the runtime form of the site's own promise — -# no analytics, no font CDN, no third-party -# anything. `default-src 'none'` means an asset -# must be named below to load at all, and no +# CSP the runtime form of the site's own promise. +# `default-src 'none'` means an asset must be +# named below to load at all, and no # 'unsafe-inline' anywhere means an injected -# diff --git a/site/templates/home.html b/site/templates/home.html index 430e165..aaea0c0 100644 --- a/site/templates/home.html +++ b/site/templates/home.html @@ -35,6 +35,10 @@ + + diff --git a/site/templates/notfound.html b/site/templates/notfound.html index 0e79645..3fcc623 100644 --- a/site/templates/notfound.html +++ b/site/templates/notfound.html @@ -20,6 +20,10 @@ + + diff --git a/tests/test_site_build.py b/tests/test_site_build.py index 5863cea..606ac81 100644 --- a/tests/test_site_build.py +++ b/tests/test_site_build.py @@ -194,12 +194,15 @@ class TheRealSiteBuilds(unittest.TestCase): self.assertIn('href="/concepts/claiming-a-card/"', article) self.assertIn("Concepts", article) - def test_nothing_the_pages_load_is_third_party(self): - """The acceptance criterion, mechanised: every asset a browser - would fetch — stylesheet, icon, script, font — is same-origin, - and nothing preconnects anywhere. A is a - statement about this page, not a fetch, so it is not one of - these; anything that makes the browser open a connection is.""" + def test_the_only_third_party_is_the_one_that_was_chosen(self): + """Every asset a browser would fetch — stylesheet, icon, font — + is same-origin, and nothing preconnects anywhere. The single + exception is the analytics script, named here and in the CSP, so + a second third party has to be a deliberate edit to this list + rather than something that slipped into a template. A is a statement about this page, not a fetch, so it + is not one of these; anything that opens a connection is.""" + chosen = ("https://cdn.usefathom.com/",) fetching = {"stylesheet", "icon", "shortcut icon", "preload", "prefetch", "preconnect", "dns-prefetch", "manifest", "modulepreload"} @@ -217,7 +220,7 @@ class TheRealSiteBuilds(unittest.TestCase): r"""<(?:script|img|iframe)\b[^>]*\bsrc=["']([^"']+)""", html, re.IGNORECASE): self.assertTrue( - url.startswith("/"), + url.startswith("/") or url.startswith(chosen), f'{entry["path"]} loads {url} from somewhere else') css = (self.out / "static" / "site.css").read_text("utf-8") diff --git a/tests/test_site_deploy.py b/tests/test_site_deploy.py index 726f4cf..7a6303c 100644 --- a/tests/test_site_deploy.py +++ b/tests/test_site_deploy.py @@ -242,13 +242,22 @@ class TheHeaderPolicyTravelsWithTheBuild(Built): def test_no_page_needs_what_the_policy_forbids(self): """The CSP carries no 'unsafe-inline', so an inline