From acc74c5b840eea3d7d08cead9706070e0777d71c Mon Sep 17 00:00:00 2001 From: L Date: Mon, 9 Nov 2026 18:09:33 +1000 Subject: [PATCH] Analytics: allow every host gtag sends GA4 hits to in the CSP (#166) A `*.` wildcard needs a subdomain, so `https://*.analytics.google.com` never matched the bare `analytics.google.com` that gtag posts page_view hits to; with Google signals on, hits also go to www.google.com and stats.g.doubleclick.net. All were blocked by connect-src. One GA_HOSTS list now feeds connect-src and img-src (Google's CSP guide for GA4 with signals). The country-specific ga-audiences pixel stays blocked. An e2e test checks the header carries the new hosts. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- e2e/pages.spec.ts | 22 ++++++++++++++++++++++ vite.config.ts | 25 ++++++++++++++++--------- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 106bec7..da2a181 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -208,7 +208,7 @@ REAL_DATA_DIR=data pnpm vitest run src/lib/server/import/realData.test.ts # op ## What not to do - Don't edit, rename, re-save or "clean" the spreadsheets. Don't commit them, fixtures derived from them, or SQLite caches. -- Don't add a chart library or third-party scripts/fonts without updating the CSP in `vite.config.ts` and this file. Allowed third parties: GA4 gtag (`www.googletagmanager.com` / `*.google-analytics.com` / `*.analytics.google.com`; Measurement ID `G-VZF5DR37YH`, loaded only on `data.physioshark.org` / `physioshark.org` via `GoogleAnalytics.svelte`) and map tiles (Esri World Imagery, OpenStreetMap — the only external images besides GA beacons; a new tile host needs the CSP, the e2e tile stub in `e2e/pages.spec.ts` and the attribution). +- Don't add a chart library or third-party scripts/fonts without updating the CSP in `vite.config.ts` and this file. Allowed third parties: GA4 gtag (`GA_HOSTS` in `vite.config.ts`: `*.googletagmanager.com`, `*.google-analytics.com`, `analytics.google.com` and `*.analytics.google.com` (a `*.` wildcard doesn't match the bare host), plus `www.google.com` and `*.g.doubleclick.net` for Google signals, #166; Measurement ID `G-VZF5DR37YH`, loaded only on `data.physioshark.org` / `physioshark.org` via `GoogleAnalytics.svelte`) and map tiles (Esri World Imagery, OpenStreetMap — the only external images besides GA beacons; a new tile host needs the CSP, the e2e tile stub in `e2e/pages.spec.ts` and the attribution). - Don't hard-code numbers in page copy; compute them in the load function. - Don't claim causation from the correlations; don't overstate the human-pressure results (the source paper found no significant effect). - Don't reintroduce dual-axis charts or per-point value labels. diff --git a/e2e/pages.spec.ts b/e2e/pages.spec.ts index 16cf2bf..a3d92d3 100644 --- a/e2e/pages.spec.ts +++ b/e2e/pages.spec.ts @@ -95,3 +95,25 @@ test('pages are compressed and revalidate with a 304 (#76)', async ({ request }) const again = await request.get('/morphometrics', { headers: { 'if-none-match': etag } }); expect(again.status()).toBe(304); }); + +test('the content security policy lets Google Analytics hits through (#166)', async ({ + request +}) => { + const res = await request.get('/'); + const csp = res.headers()['content-security-policy'] ?? ''; + const directive = (name: string) => + csp + .split(';') + .map((d) => d.trim().split(/\s+/)) + .find(([n]) => n === name) + ?.slice(1) ?? []; + for (const host of [ + 'https://analytics.google.com', + 'https://*.google-analytics.com', + 'https://www.google.com', + 'https://*.g.doubleclick.net' + ]) { + expect(directive('connect-src')).toContain(host); + expect(directive('img-src')).toContain(host); + } +}); diff --git a/vite.config.ts b/vite.config.ts index 82518b1..7278e76 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -7,6 +7,18 @@ import { defineConfig } from 'vite'; // Release version shown in the footer. Release workflows bump package.json before tagging. const { version } = JSON.parse(readFileSync('package.json', 'utf8')) as { version: string }; +// Hosts gtag.js sends GA4 hits to (#166). A `*.` wildcard needs a subdomain, so the bare +// analytics.google.com is listed on its own; with Google signals on, hits also go to +// www.google.com and stats.g.doubleclick.net. +const GA_HOSTS = [ + 'https://*.google-analytics.com', + 'https://*.analytics.google.com', + 'https://analytics.google.com', + 'https://*.googletagmanager.com', + 'https://www.google.com', + 'https://*.g.doubleclick.net' +] as const; + export default defineConfig({ define: { __APP_VERSION__: JSON.stringify(version) @@ -29,22 +41,17 @@ export default defineConfig({ // Svelte `style:` directives use inline styles. 'style-src': ['self', 'unsafe-inline'], // Map tiles for the interactive maps (#29): Esri imagery and OpenStreetMap. - // GA4 beacons may use image pings. + // GA4 beacons may use image pings (Google's CSP guide for GA4 with signals). + // The country-specific ga-audiences pixel (www.google.com.au…) stays blocked. 'img-src': [ 'self', 'data:', 'https://server.arcgisonline.com', 'https://tile.openstreetmap.org', - 'https://www.google-analytics.com', - 'https://www.googletagmanager.com' + ...GA_HOSTS ], 'font-src': ['self', 'data:'], - 'connect-src': [ - 'self', - 'https://*.google-analytics.com', - 'https://*.analytics.google.com', - 'https://*.googletagmanager.com' - ], + 'connect-src': ['self', ...GA_HOSTS], 'object-src': ['none'], 'base-uri': ['self'], 'form-action': ['self'],