From 49b1d5f0babec9cdcc43a0b6c2a8176b144090b4 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Wed, 18 Nov 2026 19:29:51 +0100 Subject: [PATCH] Monitoring settings (Sentry, Umami) and the CSP origins come from the build environment One build per environment: PROD_* variables for the production artifact, TEST_* for the test build and the CSP check. The build fails when a variable is empty. Tests use the production values from config/monitoringTestEnv.ts. Co-Authored-By: Claude Sonnet 5.5 --- .gitea/workflows/continuous_integration.yaml | 29 ++++- docs/deploy-and-ci.md | 20 ++-- web/config/monitoringTestEnv.ts | 12 ++ web/config/securityHeaders.test.ts | 12 +- web/config/securityHeaders.ts | 35 +++--- web/config/writeServerConfig.ts | 7 ++ .../Analytics/UmamiAnalytics.test.tsx | 35 ++++-- .../components/Analytics/UmamiAnalytics.tsx | 4 +- web/src/monitoring/config.test.ts | 94 +++++++++++++-- web/src/monitoring/config.ts | 113 +++++++++++++----- web/src/monitoring/sentry.test.ts | 2 + web/src/monitoring/sentry.ts | 4 +- web/vite.config.ts | 2 + web/vitest.e2e.config.ts | 2 + 14 files changed, 286 insertions(+), 85 deletions(-) create mode 100644 web/config/monitoringTestEnv.ts diff --git a/.gitea/workflows/continuous_integration.yaml b/.gitea/workflows/continuous_integration.yaml index 74c6cc0..47c7a82 100644 --- a/.gitea/workflows/continuous_integration.yaml +++ b/.gitea/workflows/continuous_integration.yaml @@ -88,9 +88,19 @@ jobs: # pakketlijst (snapshot), zonder testadres en met dicht formulier. De # CMS-host schrijft de actuele gegevens bij elk verzoek in de HTML. - name: Build - run: pnpm build + run: | + # Zonder deze waarden meet de site stilletjes niets: liever een rode build + for name in VITE_SENTRY_DSN VITE_UMAMI_SCRIPT_URL VITE_UMAMI_WEBSITE_ID; do + [ -n "${!name}" ] || { echo "$name ontbreekt: stel de Gitea-variabele PROD_${name#VITE_} in"; exit 1; } + done + pnpm build env: VITE_RELEASE: ${{ github.sha }} + # Monitoring van de productiesite (src/monitoring/config.ts) + VITE_APP_ENV: production + VITE_SENTRY_DSN: ${{ vars.PROD_SENTRY_DSN }} + VITE_UMAMI_SCRIPT_URL: ${{ vars.PROD_UMAMI_SCRIPT_URL }} + VITE_UMAMI_WEBSITE_ID: ${{ vars.PROD_UMAMI_WEBSITE_ID }} - name: Audit (productiepakketten, blokkerend) run: pnpm audit:deps @@ -138,9 +148,19 @@ jobs: run: pnpm install --frozen-lockfile - name: Build (testomgeving) - run: pnpm build + run: | + # Zonder deze waarden meet de site stilletjes niets: liever een rode build + for name in VITE_SENTRY_DSN VITE_UMAMI_SCRIPT_URL VITE_UMAMI_WEBSITE_ID; do + [ -n "${!name}" ] || { echo "$name ontbreekt: stel de Gitea-variabele TEST_${name#VITE_} in"; exit 1; } + done + pnpm build env: VITE_RELEASE: ${{ github.sha }} + # Monitoring van de testsite (src/monitoring/config.ts) + VITE_APP_ENV: test + VITE_SENTRY_DSN: ${{ vars.TEST_SENTRY_DSN }} + VITE_UMAMI_SCRIPT_URL: ${{ vars.TEST_UMAMI_SCRIPT_URL }} + VITE_UMAMI_WEBSITE_ID: ${{ vars.TEST_UMAMI_WEBSITE_ID }} # De testsite staat op zichzelf: canonical, og:url, og:image, # JSON-LD en sitemap wijzen naar het eigen adres, zodat een gedeelde # link (WhatsApp, Discord) ook zijn eigen plaatje toont. Alleen in @@ -223,6 +243,11 @@ jobs: - name: Controle na deploy env: HEALTH_URL: ${{ vars.TEST_HEALTH_URL }} + # De CSP komt uit dezelfde bron als de build (Umami en Sentry) + VITE_APP_ENV: test + VITE_SENTRY_DSN: ${{ vars.TEST_SENTRY_DSN }} + VITE_UMAMI_SCRIPT_URL: ${{ vars.TEST_UMAMI_SCRIPT_URL }} + VITE_UMAMI_WEBSITE_ID: ${{ vars.TEST_UMAMI_WEBSITE_ID }} run: | if [ -z "$HEALTH_URL" ]; then echo "Geen TEST_HEALTH_URL ingesteld; overgeslagen"; exit 0; fi # test.andreaslop.nl wordt door de CMS-host bediend: het script diff --git a/docs/deploy-and-ci.md b/docs/deploy-and-ci.md index c878ce0..4eba814 100644 --- a/docs/deploy-and-ci.md +++ b/docs/deploy-and-ci.md @@ -58,13 +58,17 @@ The production site gets the same only when the ASF production site is served by Format `{ENV}_{GROUP}_{NAME}` with `ENV` = `TEST` or `PROD`, no app part (this repo only deploys the ASF site) and no `VITE` (the pipeline maps a value to `VITE_...` in the build). Decision of Bryan, 2026-11-18; the full list is in the SLS repo (`aidlc-docs/_shared/gitea-naamgeving-variabelen.md`). Repo variables and secrets are created by SLS, not by feature sessions. -| Name | Kind | Used by | -| --------------------------------------------------------------- | -------- | ---------------------------------------------------------------------------------------------------- | -| `TEST_DEPLOY_PATH` | variable | `deploy-test`: target folder on the Pi | -| `TEST_HEALTH_URL` | variable | `deploy-test`: check after the deploy | -| `PROD_DEPLOY_PATH` | variable | `deploy-production`: remote folder (default `wwwroot`) | -| `PROD_HEALTH_URL` | variable | `deploy-production`: check after the deploy | -| `PROD_HOST_ADDRESS`, `PROD_HOST_USERNAME`, `PROD_HOST_PASSWORD` | secrets | `deploy-production`: FTP (plain FTP, no FTPS, at the hosting partner) | -| `PI_MAIN_ADDRESS`, `_PORT`, `_USERNAME`, `_PASSWORD` | secrets | `deploy-test`: still the old names; they become `TEST_HOST_*` with an SSH key (SLS arranges the key) | +| Name | Kind | Used by | +| ------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------- | +| `TEST_DEPLOY_PATH` | variable | `deploy-test`: target folder on the Pi | +| `TEST_HEALTH_URL` | variable | `deploy-test`: check after the deploy | +| `PROD_DEPLOY_PATH` | variable | `deploy-production`: remote folder (default `wwwroot`) | +| `PROD_HEALTH_URL` | variable | `deploy-production`: check after the deploy | +| `TEST_SENTRY_DSN`, `TEST_UMAMI_SCRIPT_URL`, `TEST_UMAMI_WEBSITE_ID` | variable | `build-test` and the CSP check in `deploy-test` (as `VITE_SENTRY_DSN`, `VITE_UMAMI_SCRIPT_URL`, `VITE_UMAMI_WEBSITE_ID`, `VITE_APP_ENV=test`) | +| `PROD_SENTRY_DSN`, `PROD_UMAMI_SCRIPT_URL`, `PROD_UMAMI_WEBSITE_ID` | variable | `check` (the production artifact; `VITE_APP_ENV=production`) | +| `PROD_HOST_ADDRESS`, `PROD_HOST_USERNAME`, `PROD_HOST_PASSWORD` | secrets | `deploy-production`: FTP (plain FTP, no FTPS, at the hosting partner) | +| `PI_MAIN_ADDRESS`, `_PORT`, `_USERNAME`, `_PASSWORD` | secrets | `deploy-test`: still the old names; they become `TEST_HOST_*` with an SSH key (SLS arranges the key) | Removed: `DEPLOY_PATH_TEST`, `HEALTH_CHECK_URL_TEST`, `HEALTH_CHECK_URL_PRODUCTION`, `FTP_REMOTE_DIR`, `FTP_HOST`, `FTP_USERNAME`, `FTP_PASSWORD` (renamed), `ASF_OFFERINGS_URL_TEST` and `ASF_TEST_SERVED_BY_CMS` (no longer needed). The old names in the repo are removed by SLS after a green run. + +**Monitoring per build.** Sentry and Umami settings are no longer in the code (`src/monitoring/config.ts`): every build gets them for its own environment and measures only on that environment's host (production build on `andreaslop.nl`, test build on `test.andreaslop.nl`; any other host measures nothing). The CSP origins for Umami and Sentry (`config/securityHeaders.ts`) come from the same variables, so a build without them has neither a measurement nor those origins; the build steps fail when a variable is empty. Unit and browser tests use the production values from `config/monitoringTestEnv.ts` (public values, the same as the variables); `pnpm server-config` does too, so the committed generated files carry the production CSP. The CMS host has its own copy of the origins in its environment (CMS repo). diff --git a/web/config/monitoringTestEnv.ts b/web/config/monitoringTestEnv.ts new file mode 100644 index 0000000..b74e1ae --- /dev/null +++ b/web/config/monitoringTestEnv.ts @@ -0,0 +1,12 @@ +// De monitoringinstellingen die de tests als productiebuild zien. De echte +// waarden komen in de pipeline uit de Gitea-variabelen PROD_SENTRY_DSN, +// PROD_UMAMI_SCRIPT_URL en PROD_UMAMI_WEBSITE_ID (en TEST_...); deze openbare +// waarden zijn er hetzelfde als, zodat de gegenereerde serverconfiguratie en de +// CSP in de tests vergelijkbaar blijven. Zie src/monitoring/config.ts. +export const monitoringTestEnv = { + VITE_APP_ENV: 'production', + VITE_SENTRY_DSN: + 'https://12ec77dcc3216a98b96d3fb3dac13f66@o4511795618185216.ingest.de.sentry.io/4512282287472720', + VITE_UMAMI_SCRIPT_URL: 'https://analytics.slpsoftware.nl/script.js', + VITE_UMAMI_WEBSITE_ID: '3edbb7d1-4388-4f2d-9cef-11ade88ace96', +}; diff --git a/web/config/securityHeaders.test.ts b/web/config/securityHeaders.test.ts index 0a47e65..5a68e72 100644 --- a/web/config/securityHeaders.test.ts +++ b/web/config/securityHeaders.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest'; +import { afterEach, describe, expect, it, vi } from 'vitest'; import { contentSecurityPolicy, securityHeaders } from './securityHeaders'; const umami = 'https://analytics.slpsoftware.nl'; @@ -12,7 +12,17 @@ const captchaFrames = [ const directive = (csp: string, name: string) => csp.split('; ').find((d) => d.startsWith(name + ' ')) ?? ''; +afterEach(() => vi.unstubAllEnvs()); + describe('securityHeaders', () => { + it('noemt Umami en Sentry niet zonder monitoringinstellingen in de build', () => { + vi.stubEnv('VITE_SENTRY_DSN', ''); + const csp = contentSecurityPolicy('production'); + expect(csp).not.toContain(umami); + expect(csp).not.toContain(sentry); + expect(directive(csp, 'script-src')).toBe("script-src 'self'"); + }); + it('productie-CSP heeft geen onveilige uitzonderingen voor scripts', () => { const csp = contentSecurityPolicy('production'); expect(csp).not.toContain('unsafe-eval'); diff --git a/web/config/securityHeaders.ts b/web/config/securityHeaders.ts index cf7766a..440ce45 100644 --- a/web/config/securityHeaders.ts +++ b/web/config/securityHeaders.ts @@ -1,7 +1,7 @@ // HTTP-securityheaders voor de Vite dev- en preview-server. De productieset is // ook het voorschrift voor de toekomstige host (zie NFR-SEC-01/02). -import { sentryIngestOrigin, umamiOrigin } from '../src/monitoring/config.ts'; +import { monitoringOrigins } from '../src/monitoring/config.ts'; type Mode = 'production' | 'development'; @@ -21,19 +21,24 @@ export const captchaFrameOrigins = [ 'https://global1.frcapi.com', ]; -const productionCsp: Record = { - 'default-src': ["'self'"], - 'script-src': ["'self'", umamiOrigin], - 'style-src': ["'self'", "'unsafe-inline'"], - 'img-src': ["'self'", 'data:'], - 'font-src': ["'self'"], - 'connect-src': ["'self'", umamiOrigin, sentryIngestOrigin], - 'frame-src': captchaFrameOrigins, - 'object-src': ["'none'"], - 'base-uri': ["'self'"], - 'form-action': ["'self'"], - 'frame-ancestors': ["'none'"], -}; +// De herkomsten van Umami en Sentry komen uit de build-omgeving (zie +// src/monitoring/config.ts); zonder die instellingen staan ze er niet in. +function productionCsp(): Record { + const origins = monitoringOrigins(); + return { + 'default-src': ["'self'"], + 'script-src': ["'self'", ...origins.umami], + 'style-src': ["'self'", "'unsafe-inline'"], + 'img-src': ["'self'", 'data:'], + 'font-src': ["'self'"], + 'connect-src': ["'self'", ...origins.umami, ...origins.sentry], + 'frame-src': captchaFrameOrigins, + 'object-src': ["'none'"], + 'base-uri': ["'self'"], + 'form-action': ["'self'"], + 'frame-ancestors': ["'none'"], + }; +} // Alleen wat de Vite dev-server zelf nodig heeft: een inline script voor // React Refresh en een websocket voor hot reload. @@ -43,7 +48,7 @@ const developmentExtras: Record = { }; export function contentSecurityPolicy(mode: Mode): string { - return Object.entries(productionCsp) + return Object.entries(productionCsp()) .map(([directive, sources]) => { const extra = mode === 'development' diff --git a/web/config/writeServerConfig.ts b/web/config/writeServerConfig.ts index 18d3256..5fda5e4 100644 --- a/web/config/writeServerConfig.ts +++ b/web/config/writeServerConfig.ts @@ -2,8 +2,15 @@ // Draait direct met Node (type stripping), zonder extra tooling. import { mkdirSync, writeFileSync } from 'node:fs'; import { dirname } from 'node:path'; +import { monitoringTestEnv } from './monitoringTestEnv.ts'; import { serverConfigFiles } from './serverConfig.ts'; +// The committed files carry the CSP of the production build; the origins of +// Umami and Sentry come from the environment (src/monitoring/config.ts) +for (const [name, value] of Object.entries(monitoringTestEnv)) { + process.env[name] ??= value; +} + for (const [path, generate] of Object.entries(serverConfigFiles)) { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, generate()); diff --git a/web/src/components/Analytics/UmamiAnalytics.test.tsx b/web/src/components/Analytics/UmamiAnalytics.test.tsx index 3f3867c..cf353dd 100644 --- a/web/src/components/Analytics/UmamiAnalytics.test.tsx +++ b/web/src/components/Analytics/UmamiAnalytics.test.tsx @@ -1,5 +1,5 @@ import { render } from '@testing-library/react'; -import { afterEach, describe, expect, it } from 'vitest'; +import { afterEach, describe, expect, it, vi } from 'vitest'; import UmamiAnalytics from './UmamiAnalytics'; const umamiScripts = () => @@ -8,6 +8,7 @@ const umamiScripts = () => ); afterEach(() => { + vi.unstubAllEnvs(); umamiScripts().forEach((script) => script.remove()); }); @@ -18,18 +19,28 @@ describe('UmamiAnalytics', () => { }); it.each([ - ['andreaslop.nl', '3edbb7d1-4388-4f2d-9cef-11ade88ace96'], - ['test.andreaslop.nl', '7fb301ab-6ecc-4aea-beee-71bcb6009bfe'], - ])('telt op %s met de juiste website', (hostname, websiteId) => { - render(); + ['production', 'andreaslop.nl', '3edbb7d1-4388-4f2d-9cef-11ade88ace96'], + ['test', 'test.andreaslop.nl', 'test-website'], + ])( + 'telt in de %s-build op %s met de juiste website', + (environment, hostname, websiteId) => { + vi.stubEnv('VITE_APP_ENV', environment); + vi.stubEnv('VITE_UMAMI_WEBSITE_ID', websiteId); + render(); - const [script] = umamiScripts(); - expect(script.defer).toBe(true); - expect(script.dataset).toMatchObject({ - websiteId, - domains: hostname, - doNotTrack: 'true', - }); + const [script] = umamiScripts(); + expect(script.defer).toBe(true); + expect(script.dataset).toMatchObject({ + websiteId, + domains: hostname, + doNotTrack: 'true', + }); + }, + ); + + it('voegt geen script toe op de host van de andere omgeving', () => { + render(); + expect(umamiScripts()).toHaveLength(0); }); it('voegt het script maar één keer toe', () => { diff --git a/web/src/components/Analytics/UmamiAnalytics.tsx b/web/src/components/Analytics/UmamiAnalytics.tsx index 89ab144..0b02b71 100644 --- a/web/src/components/Analytics/UmamiAnalytics.tsx +++ b/web/src/components/Analytics/UmamiAnalytics.tsx @@ -1,5 +1,5 @@ import { useEffect } from 'react'; -import { monitoringFor, umamiOrigin } from '@/monitoring/config'; +import { monitoringFor } from '@/monitoring/config'; const SCRIPT_ELEMENT_ID = 'umami-analytics-script'; @@ -16,7 +16,7 @@ export default function UmamiAnalytics({ hostname }: { hostname?: string }) { const script = document.createElement('script'); script.id = SCRIPT_ELEMENT_ID; - script.src = `${umamiOrigin}/script.js`; + script.src = monitoring.umamiScriptUrl; script.defer = true; script.dataset.websiteId = monitoring.umamiWebsiteId; script.dataset.domains = host; diff --git a/web/src/monitoring/config.test.ts b/web/src/monitoring/config.test.ts index 40f4434..ce2bec6 100644 --- a/web/src/monitoring/config.test.ts +++ b/web/src/monitoring/config.test.ts @@ -1,11 +1,22 @@ import fc from 'fast-check'; -import { describe, expect, it } from 'vitest'; +import { afterEach, describe, expect, it, vi } from 'vitest'; import { monitoredHostnames, + monitoringConfig, monitoringFor, - sentryIngestOrigin, + monitoringOrigins, } from './config'; +const dsn = 'https://key@o1.ingest.de.sentry.io/2'; +const script = 'https://analytics.example.nl/script.js'; +const raw = (extra: Record = {}) => ({ + environment: 'production', + sentryDsn: dsn, + umamiScriptUrl: script, + umamiWebsiteId: 'site-1', + ...extra, +}); + // Alles wat geen test- of productiehost is const otherHostname = fc .oneof( @@ -25,32 +36,89 @@ const otherHostname = fc ) .filter((host) => !monitoredHostnames.includes(host)); -describe('monitoringFor', () => { - it('productiehost meet als production met de productiewebsite', () => { - expect(monitoringFor('andreaslop.nl')).toEqual({ +afterEach(() => vi.unstubAllEnvs()); + +describe('monitoringConfig', () => { + it('leest de waarden uit de omgeving van de build', () => { + // De testomgeving (vite.config.ts) is de productiebuild + expect(monitoringConfig()).toMatchObject({ environment: 'production', umamiWebsiteId: '3edbb7d1-4388-4f2d-9cef-11ade88ace96', }); + vi.stubEnv('VITE_APP_ENV', 'test'); + vi.stubEnv('VITE_UMAMI_WEBSITE_ID', ' abc '); + expect(monitoringConfig()).toMatchObject({ + environment: 'test', + umamiWebsiteId: 'abc', + }); }); - it('testhost meet als test met de testwebsite', () => { - expect(monitoringFor('test.andreaslop.nl')).toEqual({ - environment: 'test', - umamiWebsiteId: '7fb301ab-6ecc-4aea-beee-71bcb6009bfe', + it('geeft null als er iets ontbreekt of ongeldig is', () => { + expect(monitoringConfig(raw())).not.toBeNull(); + for (const bad of [ + { environment: 'staging' }, + { environment: undefined }, + { sentryDsn: undefined }, + { sentryDsn: 'http://key@host/1' }, + { sentryDsn: 'geen url' }, + { umamiScriptUrl: '' }, + { umamiScriptUrl: 'http://analytics.example.nl/script.js' }, + { umamiWebsiteId: ' ' }, + { umamiWebsiteId: 5 }, + ]) { + expect(monitoringConfig(raw(bad))).toBeNull(); + } + }); +}); + +describe('monitoringFor', () => { + it('productiebuild meet alleen op de productiehost', () => { + const config = monitoringConfig(raw()); + expect(monitoringFor('andreaslop.nl', config)).toEqual({ + environment: 'production', + sentryDsn: dsn, + umamiScriptUrl: script, + umamiWebsiteId: 'site-1', }); + expect(monitoringFor('test.andreaslop.nl', config)).toBeNull(); + }); + + it('testbuild meet alleen op de testhost', () => { + const config = monitoringConfig(raw({ environment: 'test' })); + expect(monitoringFor('test.andreaslop.nl', config)?.environment).toBe( + 'test', + ); + expect(monitoringFor('andreaslop.nl', config)).toBeNull(); + }); + + it('meet niets zonder instellingen', () => { + expect(monitoringFor('andreaslop.nl', null)).toBeNull(); }); it('elke andere host meet niets (M1)', () => { fc.assert( fc.property(otherHostname, (host) => { expect(monitoringFor(host)).toBeNull(); + expect( + monitoringFor( + host, + monitoringConfig(raw({ environment: 'test' })), + ), + ).toBeNull(); }), ); }); }); -it('Sentry-ingest is de EU-host uit de DSN, zonder sleutel', () => { - expect(sentryIngestOrigin).toBe( - 'https://o4511795618185216.ingest.de.sentry.io', - ); +describe('monitoringOrigins', () => { + it('zijn de herkomst van het Umami-script en van de Sentry-ingest, zonder sleutel', () => { + expect(monitoringOrigins(monitoringConfig(raw()))).toEqual({ + umami: ['https://analytics.example.nl'], + sentry: ['https://o1.ingest.de.sentry.io'], + }); + }); + + it('zijn leeg zonder instellingen', () => { + expect(monitoringOrigins(null)).toEqual({ umami: [], sentry: [] }); + }); }); diff --git a/web/src/monitoring/config.ts b/web/src/monitoring/config.ts index 6b58ac2..0d71084 100644 --- a/web/src/monitoring/config.ts +++ b/web/src/monitoring/config.ts @@ -1,43 +1,96 @@ -// Instellingen voor Sentry en Umami (monitoring-plan M1/M2). Deze waarden zijn -// openbaar: ze staan hoe dan ook in de browser van elke bezoeker. +// Instellingen voor Sentry en Umami (monitoring-plan M1/M2). De waarden zijn +// openbaar (ze staan in de browser van elke bezoeker) maar komen niet meer uit +// de code: de pipeline geeft ze per omgeving mee aan de build, uit de Gitea- +// variabelen {TEST|PROD}_SENTRY_DSN, _UMAMI_SCRIPT_URL en _UMAMI_WEBSITE_ID, +// als VITE_SENTRY_DSN, VITE_UMAMI_SCRIPT_URL, VITE_UMAMI_WEBSITE_ID en +// VITE_APP_ENV ('test' of 'production'). Eén build hoort dus bij één omgeving. // Geen imports uit Vite: config/securityHeaders.ts leest dit bestand ook in Node. export type MonitoringEnvironment = 'test' | 'production'; -export interface Monitoring { +export interface MonitoringConfig { environment: MonitoringEnvironment; + sentryDsn: string; + umamiScriptUrl: string; umamiWebsiteId: string; } -export const sentryDsn = - 'https://12ec77dcc3216a98b96d3fb3dac13f66@o4511795618185216.ingest.de.sentry.io/4512282287472720'; +export type Monitoring = MonitoringConfig; -// Waar de browser fouten naartoe stuurt; nodig voor connect-src in de CSP -export const sentryIngestOrigin = new URL(sentryDsn).origin; +// De host waarop de build van een omgeving meet +const hostnameByEnvironment: Record = { + production: 'andreaslop.nl', + test: 'test.andreaslop.nl', +}; -export const umamiOrigin = 'https://analytics.slpsoftware.nl'; +// In de Vite-build staan de waarden in import.meta.env (statisch vervangen, +// daarom elke naam los); de buildscripts onder Node lezen process.env. +const fromProcess = (name: string): unknown => + typeof process === 'undefined' ? undefined : process.env[name]; -// Eén build voor beide omgevingen: de hostname bepaalt de omgeving -const monitoringByHostname = new Map([ - [ - 'andreaslop.nl', - { - environment: 'production', - umamiWebsiteId: '3edbb7d1-4388-4f2d-9cef-11ade88ace96', - }, - ], - [ - 'test.andreaslop.nl', - { - environment: 'test', - umamiWebsiteId: '7fb301ab-6ecc-4aea-beee-71bcb6009bfe', - }, - ], -]); - -// Onbekende host (localhost, Lighthouse CI, previews): niets meten (M1) -export function monitoringFor(hostname: string): Monitoring | null { - return monitoringByHostname.get(hostname) ?? null; +function rawSettings(): Record { + const env = import.meta.env; + return { + environment: env?.VITE_APP_ENV ?? fromProcess('VITE_APP_ENV'), + sentryDsn: env?.VITE_SENTRY_DSN ?? fromProcess('VITE_SENTRY_DSN'), + umamiScriptUrl: + env?.VITE_UMAMI_SCRIPT_URL ?? fromProcess('VITE_UMAMI_SCRIPT_URL'), + umamiWebsiteId: + env?.VITE_UMAMI_WEBSITE_ID ?? fromProcess('VITE_UMAMI_WEBSITE_ID'), + }; } -export const monitoredHostnames = [...monitoringByHostname.keys()]; +const isHttps = (value: string): boolean => { + try { + return new URL(value).protocol === 'https:'; + } catch { + return false; + } +}; + +// De instellingen van deze build, of null als er iets ontbreekt of ongeldig is +// (dan meet de site niets: liever geen meting dan een kapotte) +export function monitoringConfig( + raw: Record = rawSettings(), +): MonitoringConfig | null { + const { environment, sentryDsn, umamiScriptUrl, umamiWebsiteId } = raw; + if (environment !== 'test' && environment !== 'production') return null; + const text = (value: unknown) => + typeof value === 'string' ? value.trim() : ''; + const dsn = text(sentryDsn); + const script = text(umamiScriptUrl); + const website = text(umamiWebsiteId); + if (!isHttps(dsn) || !isHttps(script) || website === '') return null; + return { + environment, + sentryDsn: dsn, + umamiScriptUrl: script, + umamiWebsiteId: website, + }; +} + +// Onbekende host (localhost, Lighthouse CI, previews, de andere omgeving): +// niets meten (M1) +export function monitoringFor( + hostname: string, + config: MonitoringConfig | null = monitoringConfig(), +): Monitoring | null { + if (!config || hostname !== hostnameByEnvironment[config.environment]) { + return null; + } + return { ...config }; +} + +// De herkomsten voor de CSP: Umami voor script-src en connect-src, de Sentry- +// ingest voor connect-src. Leeg zonder meting. +export function monitoringOrigins( + config: MonitoringConfig | null = monitoringConfig(), +): { umami: string[]; sentry: string[] } { + if (!config) return { umami: [], sentry: [] }; + return { + umami: [new URL(config.umamiScriptUrl).origin], + sentry: [new URL(config.sentryDsn).origin], + }; +} + +export const monitoredHostnames = Object.values(hostnameByEnvironment); diff --git a/web/src/monitoring/sentry.test.ts b/web/src/monitoring/sentry.test.ts index 90a85ca..747935a 100644 --- a/web/src/monitoring/sentry.test.ts +++ b/web/src/monitoring/sentry.test.ts @@ -11,6 +11,7 @@ async function freshSentry() { } beforeEach(() => { + vi.unstubAllEnvs(); init.mockReset(); captureException.mockReset(); }); @@ -27,6 +28,7 @@ describe('Sentry', () => { }); it('start op test met omgeving test en zonder persoonsgegevens', async () => { + vi.stubEnv('VITE_APP_ENV', 'test'); const { startSentry } = await freshSentry(); startSentry('test.andreaslop.nl'); diff --git a/web/src/monitoring/sentry.ts b/web/src/monitoring/sentry.ts index 914b354..bebe3a9 100644 --- a/web/src/monitoring/sentry.ts +++ b/web/src/monitoring/sentry.ts @@ -1,4 +1,4 @@ -import { monitoringFor, sentryDsn } from './config'; +import { monitoringFor } from './config'; // Sentry laadt pas na het load-event, buiten de hoofdbundel (M4). Fouten van // daarvoor wachten in een wachtrij en gaan mee zodra Sentry klaar is. @@ -33,7 +33,7 @@ export function startSentry(hostname = window.location.hostname): void { // Alleen fouten: geen tracing, geen replay, geen persoonsgegevens (M5). // Sinds SDK 11 verzamelt Sentry standaard alles; daarom alles expliciet uit. Sentry.init({ - dsn: sentryDsn, + dsn: monitoring.sentryDsn, environment: monitoring.environment, release: import.meta.env.VITE_RELEASE, dataCollection: { diff --git a/web/vite.config.ts b/web/vite.config.ts index 2a9b3be..00c6fb7 100644 --- a/web/vite.config.ts +++ b/web/vite.config.ts @@ -1,3 +1,4 @@ +import { monitoringTestEnv } from './config/monitoringTestEnv.ts'; import { configDefaults, defineConfig } from 'vitest/config'; import react from '@vitejs/plugin-react'; import { tanstackStart } from '@tanstack/react-start/plugin/vite'; @@ -80,6 +81,7 @@ export default defineConfig(({ mode }) => ({ preview: { headers: securityHeaders('production') }, test: { environment: 'jsdom', + env: monitoringTestEnv, // De CI-runner (Raspberry Pi, arm64) is ±5-8x trager dan een pc; de // eerste render met axe duurt daar tot 7 s. 5 s (standaard) is te krap. testTimeout: 20_000, diff --git a/web/vitest.e2e.config.ts b/web/vitest.e2e.config.ts index a5b1d29..d50560e 100644 --- a/web/vitest.e2e.config.ts +++ b/web/vitest.e2e.config.ts @@ -1,4 +1,5 @@ import { defineConfig } from 'vitest/config'; +import { monitoringTestEnv } from './config/monitoringTestEnv.ts'; // Browsertests (BR-TST-05, BR-TST-06) tegen de gebouwde site in dist/client. // Los van vite.config.ts: de Start-plugin en de jsdom-opzet van de @@ -8,6 +9,7 @@ export default defineConfig({ resolve: { tsconfigPaths: true }, test: { environment: 'node', + env: monitoringTestEnv, include: ['e2e/**/*.e2e.ts'], // De CI-runner (Raspberry Pi) is veel trager dan een pc: Chrome starten // en een pagina hydrateren kost daar vele seconden. Tests die alle 11