From 6d3a1566c2c5de5786307ebf8364a9f4df7cca11 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Mon, 19 Oct 2026 12:08:47 +0200 Subject: [PATCH] Adds Sentry and Umami monitoring to the site Sentry loads after the page and reports errors only, without personal data; Umami counts visits per environment. Both only run on andreaslop.nl and test.andreaslop.nl. The CSP allows just these two hosts, and builds carry the commit SHA as release. Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/continuous_integration.yaml | 3 + .../features/frontend-shell/aidlc-state.md | 4 +- aidlc-docs/features/frontend-shell/audit.md | 36 +++++++ .../operations/monitoring/monitoring-setup.md | 16 +-- web/config/securityHeaders.test.ts | 27 ++++- web/config/securityHeaders.ts | 9 +- web/deploy/iis/web.config | 2 +- web/deploy/nginx/andreaslop-test.conf | 8 +- web/package.json | 1 + web/pnpm-lock.yaml | 88 +++++++++++++++++ .../Analytics/UmamiAnalytics.test.tsx | 47 +++++++++ .../components/Analytics/UmamiAnalytics.tsx | 31 ++++++ web/src/components/AppShell.tsx | 2 + web/src/components/StatusPage/ErrorPage.tsx | 4 +- web/src/main.tsx | 4 + web/src/monitoring/config.test.ts | 56 +++++++++++ web/src/monitoring/config.ts | 43 ++++++++ web/src/monitoring/sentry.test.ts | 99 +++++++++++++++++++ web/src/monitoring/sentry.ts | 55 +++++++++++ web/src/monitoring/sentryClient.ts | 4 + 20 files changed, 518 insertions(+), 21 deletions(-) create mode 100644 web/src/components/Analytics/UmamiAnalytics.test.tsx create mode 100644 web/src/components/Analytics/UmamiAnalytics.tsx create mode 100644 web/src/monitoring/config.test.ts create mode 100644 web/src/monitoring/config.ts create mode 100644 web/src/monitoring/sentry.test.ts create mode 100644 web/src/monitoring/sentry.ts create mode 100644 web/src/monitoring/sentryClient.ts diff --git a/.gitea/workflows/continuous_integration.yaml b/.gitea/workflows/continuous_integration.yaml index 84ea453..327e1fb 100644 --- a/.gitea/workflows/continuous_integration.yaml +++ b/.gitea/workflows/continuous_integration.yaml @@ -77,8 +77,11 @@ jobs: - name: Tests run: pnpm test + # Sentry toont bij elke fout uit welke commit de build komt - name: Build run: pnpm build + env: + VITE_RELEASE: ${{ github.sha }} - name: Audit (productiepakketten, blokkerend) run: pnpm audit:deps diff --git a/aidlc-docs/features/frontend-shell/aidlc-state.md b/aidlc-docs/features/frontend-shell/aidlc-state.md index 25cf27f..d4bded1 100644 --- a/aidlc-docs/features/frontend-shell/aidlc-state.md +++ b/aidlc-docs/features/frontend-shell/aidlc-state.md @@ -5,7 +5,7 @@ - **Feature Slug**: frontend-shell - **Project Type**: Brownfield - **Start Date**: 2026-10-14T09:46:02Z -- **Current Stage**: OPERATIONS - Monitoring Setup (awaiting approval) +- **Current Stage**: OPERATIONS - Monitoring Setup (code done, awaiting approval) - **Branch**: feature/frontend-shell ## Workspace State @@ -74,7 +74,7 @@ ### 🟡 OPERATIONS PHASE - [x] Deployment Setup - EXECUTE (approved; pipeline verified, first test deploy green) -- [~] Monitoring Setup - EXECUTE (artifacts generated, awaiting approval) +- [~] Monitoring Setup - EXECUTE (setup done in Sentry/Umami, code implemented and green; awaiting approval) - [ ] Production Readiness Validation - EXECUTE ## Current Status diff --git a/aidlc-docs/features/frontend-shell/audit.md b/aidlc-docs/features/frontend-shell/audit.md index 485d893..0217b7c 100644 --- a/aidlc-docs/features/frontend-shell/audit.md +++ b/aidlc-docs/features/frontend-shell/audit.md @@ -858,3 +858,39 @@ Verder heb ik de vraag ook beantwoord" **Context**: OPERATIONS - Monitoring Setup; awaiting approval / Sentry setup in a new session --- + +## Monitoring Setup - Sentry Project via MCP + +**Timestamp**: 2026-10-19T09:55:00Z + +**User Input**: "ik wil verder met de openstaande feature. we waren bezig met monitoring en ik was bezig met het opzetten van sentry. Die geeft mij de volgende prompt zodat jij mij kan helpen opzetten met behulp van de mcp server die ik heb toegevoegd, prompt van sentry: Help me create and set up Sentry projects for this application. org slug: slp-software run code: 8fc9ce6c75" / "kan je het nog eens proberen? members kunnen nu een project aanmaken" + +**AI Response**: "Org slp-software (EU, de.sentry.io), team slp-software. First create_project failed with 403: the MCP OAuth token acts as a member and 'Let Members Create Projects' was off. Owner enabled it; created project andreaslop-fotografie (javascript-react), DSN https://12ec77dcc3216a98b96d3fb3dac13f66@o4511795618185216.ingest.de.sentry.io/4512282287472720. Changed the default alert 1390900 from 'high priority issues' to 'first seen event' (a new issue is created) → e-mail, renamed 'Send a notification for new issues', to match Q7 = A. 'Prevent Storing of IP Addresses' has no MCP tool: left for the owner in the UI. Seer PR-ready rule left untouched." + +**Context**: OPERATIONS - Monitoring Setup; one-time Sentry setup + +--- + +## Monitoring Setup - Umami IDs and Code Changes + +**Timestamp**: 2026-10-19T10:07:53Z + +**User Input**: "de umami Website ID's zijn: - Andrea Slop Fotografie (Test): 7fb301ab-6ecc-4aea-beee-71bcb6009bfe - Andrea Slop Fotografie: 3edbb7d1-4388-4f2d-9cef-11ade88ace96" + +**AI Response**: "Took the owner's completed one-time setup and supplied values as approval of the monitoring setup and implemented the code changes from monitoring-setup.md: @sentry/react 11.8.0; src/monitoring/config.ts (DSN, ingest origin derived from DSN, Umami origin, monitoringFor via Map); src/monitoring/sentry.ts (startSentry after load event, queue incl. early unhandled errors/rejections, reportError); src/monitoring/sentryClient.ts (named re-exports, lazy chunk 31 kB gzip instead of 150 kB); UmamiAnalytics in AppShell (data-website-id, data-domains, data-do-not-track); ErrorPage calls reportError; CSP script-src + Umami, connect-src + Umami + Sentry ingest, regenerated web.config and nginx; workflow Build step sets VITE_RELEASE=github.sha. Found: SDK 11 removed sendDefaultPii and collects all data by default, so every dataCollection category is set off explicitly (M5). Tests: monitoringFor examples + PBT (domains, IPs, strings, lookalikes → null), sentry queue/start/once/load-event/unhandled, UmamiAnalytics per host/unknown host/once, CSP exactly two external hosts in the right directives. lint, format, 109 tests and build green. Main bundle unchanged (572 kB vs 579 kB before). Not committed." + +**Context**: OPERATIONS - Monitoring Setup; code changes done, awaiting approval + +--- + +## Monitoring Setup - Commit/Push and Watch Run + +**Timestamp**: 2026-10-19T10:08:46Z + +**User Input**: "Zodra je klaar bent mag je comiiten en pushen. Daarna wil ik dat je de run van de PR in de gaten houdt om te zien of die slaagt. Je hebt toestemming om zelf de problemen die je tegenkomt op te lossen, te committen en te pushen totdat alles weer slaagt en groen is. Ik wil achteraf wel een samenvatting van wat je gedaan hebt. ga nog NIET automatisch door naar de volgende stage van aidlc. dat geef ik aan wanneer dat mag" + +**AI Response**: "Committing and pushing the monitoring changes to PR #4; watching the PR run and fixing/committing/pushing until green (permission scoped to this task). Monitoring Setup stays awaiting approval; not proceeding to Production Readiness until the owner says so." + +**Context**: OPERATIONS - Monitoring Setup; task-scoped commit/push permission + +--- diff --git a/aidlc-docs/features/frontend-shell/operations/monitoring/monitoring-setup.md b/aidlc-docs/features/frontend-shell/operations/monitoring/monitoring-setup.md index a2776b1..7862028 100644 --- a/aidlc-docs/features/frontend-shell/operations/monitoring/monitoring-setup.md +++ b/aidlc-docs/features/frontend-shell/operations/monitoring/monitoring-setup.md @@ -15,7 +15,8 @@ What to do once in each service, and what changes in the code. The code changes |---|---| | `package.json` | Add `@sentry/react` (same package as SlpSoftware) | | `src/monitoring/config.ts` (new) | `sentryDsn` (from step 2) and `monitoringFor(hostname)` → `{ environment, umamiWebsiteId }` or `null` for unknown hosts (M1) | -| `src/monitoring/sentry.ts` (new) | `startSentry()`: loads `@sentry/react` dynamically after the `load` event (M4); `init` with `environment`, `release` (commit SHA from the build), `sendDefaultPii: false`, no tracing or replay. `reportError(error)`: queues errors until Sentry is ready. | +| `src/monitoring/sentry.ts` (new) | `startSentry()`: loads `@sentry/react` dynamically after the `load` event (M4); `init` with `environment`, `release` (commit SHA from the build), all `dataCollection` categories off (SDK 11 replaced `sendDefaultPii` and collects everything by default), no tracing or replay. `reportError(error)`: queues errors, including unhandled ones, until Sentry is ready. | +| `src/monitoring/sentryClient.ts` (new) | Re-exports only `init` and `captureException`, so the lazy chunk is tree-shaken (31 kB gzip instead of 150 kB). | | `src/main.tsx` | Calls `startSentry()` | | `src/components/StatusPage/ErrorPage.tsx` | Besides `console.error`, also `reportError(error)` (BR-ERR-03 extended) | | `config/securityHeaders.ts` | `connect-src` + Sentry ingest host (M7); regenerated into `web.config` and nginx | @@ -68,11 +69,12 @@ Optional: type **Keyword** with the keyword `Andrea Slop Fotografie`. That catch - **`securityHeaders`**: the production CSP contains exactly these two external hosts, in the right directives only, and no wildcards. - **`serverConfig`**: drift test; `web.config` and nginx contain the new CSP. -## 5. Values needed from you -Fill these in after steps 1 and 2. Placeholders are fine to start with; monitoring stays off until they are set. +## 5. Values (filled in 2026-10-19, in `src/monitoring/config.ts`) -| Value | From | +| Value | Setting | |---|---| -| Sentry DSN | Sentry, step 1.2 | -| Umami website ID, production | Umami, step 2.3 | -| Umami website ID, test | Umami, step 2.3 | +| Sentry DSN | `https://12ec77dcc3216a98b96d3fb3dac13f66@o4511795618185216.ingest.de.sentry.io/4512282287472720` (project `andreaslop-fotografie`) | +| Umami website ID, production | `3edbb7d1-4388-4f2d-9cef-11ade88ace96` | +| Umami website ID, test | `7fb301ab-6ecc-4aea-beee-71bcb6009bfe` | + +Sentry project created via the Sentry MCP. The default alert ("high priority issues") was changed to "a new issue is created" → e-mail, to match Q7 = A: handled errors from `reportError` often get a lower priority and would not have sent an e-mail. diff --git a/web/config/securityHeaders.test.ts b/web/config/securityHeaders.test.ts index 69c0024..b51c328 100644 --- a/web/config/securityHeaders.test.ts +++ b/web/config/securityHeaders.test.ts @@ -1,6 +1,9 @@ import { describe, expect, it } from 'vitest'; import { contentSecurityPolicy, securityHeaders } from './securityHeaders'; +const umami = 'https://analytics.slpsoftware.nl'; +const sentry = 'https://o4511795618185216.ingest.de.sentry.io'; + const directive = (csp: string, name: string) => csp.split('; ').find((d) => d.startsWith(name + ' ')) ?? ''; @@ -8,19 +11,37 @@ describe('securityHeaders', () => { it('productie-CSP heeft geen onveilige uitzonderingen voor scripts', () => { const csp = contentSecurityPolicy('production'); expect(csp).not.toContain('unsafe-eval'); - expect(directive(csp, 'script-src')).toBe("script-src 'self'"); + expect(directive(csp, 'script-src')).toBe(`script-src 'self' ${umami}`); expect(csp).not.toContain('ws:'); expect(directive(csp, 'frame-ancestors')).toBe( "frame-ancestors 'none'", ); }); + it('productie-CSP opent alleen Umami en Sentry, op de juiste plekken (M7)', () => { + const csp = contentSecurityPolicy('production'); + const externalHosts = new Set(csp.match(/https:\/\/[^\s;]+/g)); + expect([...externalHosts].sort()).toEqual([sentry, umami].sort()); + expect(directive(csp, 'connect-src')).toBe( + `connect-src 'self' ${umami} ${sentry}`, + ); + // Alleen script-src en connect-src noemen een externe host + for (const d of csp.split('; ')) { + if (!/^(script|connect)-src /.test(d)) { + expect(d).not.toContain('https://'); + } + } + expect(csp).not.toContain('*'); + }); + it('dev-CSP voegt alleen toe wat Vite nodig heeft', () => { const csp = contentSecurityPolicy('development'); expect(directive(csp, 'script-src')).toBe( - "script-src 'self' 'unsafe-inline'", + `script-src 'self' ${umami} 'unsafe-inline'`, + ); + expect(directive(csp, 'connect-src')).toBe( + `connect-src 'self' ${umami} ${sentry} ws:`, ); - expect(directive(csp, 'connect-src')).toBe("connect-src 'self' ws:"); expect(csp).not.toContain('unsafe-eval'); }); diff --git a/web/config/securityHeaders.ts b/web/config/securityHeaders.ts index 3979130..8575f7b 100644 --- a/web/config/securityHeaders.ts +++ b/web/config/securityHeaders.ts @@ -1,17 +1,20 @@ // 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'; + type Mode = 'production' | 'development'; // Chakra/Emotion zetten tijdens runtime