From 41a6e7be5a74bf053f6d8d6cc9c78681f35815f2 Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Sun, 8 Nov 2026 02:05:59 +0100 Subject: [PATCH] Prepares the test site for the CMS host The test route on the proxy Pi now points at the CMS host on port 5200. verify-deployment.sh gets a --cms mode that checks the exact security headers, caching, gzip only, a percent-encoded redirect, the 405 for POST, and the CMS routes. The deploy-test job uses it once the Gitea variable ASF_TEST_SERVED_BY_CMS is true. Co-Authored-By: Claude Sonnet 5.5 --- .gitea/workflows/continuous_integration.yaml | 15 +- web/config/printCsp.ts | 8 + web/config/serverConfig.test.ts | 14 ++ web/config/serverConfig.ts | 21 ++- .../nginx/test.andreaslop.nl.proxy.conf | 13 +- web/deploy/verify-deployment.sh | 138 ++++++++++++++++-- web/e2e/deployScripts.e2e.ts | 31 ++++ 7 files changed, 216 insertions(+), 24 deletions(-) create mode 100644 web/config/printCsp.ts diff --git a/.gitea/workflows/continuous_integration.yaml b/.gitea/workflows/continuous_integration.yaml index db05187..aa81e10 100644 --- a/.gitea/workflows/continuous_integration.yaml +++ b/.gitea/workflows/continuous_integration.yaml @@ -113,6 +113,11 @@ jobs: steps: - uses: actions/checkout@v4 + # Voor config/printCsp.ts (de exacte CSP in de controle na deploy) + - uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + - name: Download build artifact uses: actions/download-artifact@v3 with: @@ -161,9 +166,17 @@ jobs: - name: Controle na deploy env: HEALTH_URL: ${{ vars.HEALTH_CHECK_URL_TEST }} + # 'true' zodra test.andreaslop.nl door de CMS-host (Api.AndreaSlopFotografie) + # wordt bediend in plaats van nginx: dan controleert het script ook de + # exacte CSP en de CMS-routes. Zet hem om samen met de proxy-route. + SERVED_BY_CMS: ${{ vars.ASF_TEST_SERVED_BY_CMS }} run: | if [ -z "$HEALTH_URL" ]; then echo "Geen HEALTH_CHECK_URL_TEST ingesteld; overgeslagen"; exit 0; fi - bash deploy/verify-deployment.sh "$HEALTH_URL" dist --test + if [ "$SERVED_BY_CMS" = "true" ]; then + EXPECTED_CSP="$(node config/printCsp.ts)" bash deploy/verify-deployment.sh "$HEALTH_URL" dist --test --cms + else + bash deploy/verify-deployment.sh "$HEALTH_URL" dist --test + fi echo "Test bereikbaar en gelijk aan de build" # Lighthouse tegen de gebouwde site (mobiel en desktop) diff --git a/web/config/printCsp.ts b/web/config/printCsp.ts new file mode 100644 index 0000000..6d8bd6d --- /dev/null +++ b/web/config/printCsp.ts @@ -0,0 +1,8 @@ +// Schrijft de productie-CSP van de site naar stdout, zonder regeleinde. De +// controle na deploy (deploy/verify-deployment.sh --cms) vergelijkt hiermee +// de header van de CMS-host, zodat die niet van config/securityHeaders.ts +// kan afwijken. + +import { contentSecurityPolicy } from './securityHeaders.ts'; + +process.stdout.write(contentSecurityPolicy('production')); diff --git a/web/config/serverConfig.test.ts b/web/config/serverConfig.test.ts index 56b956f..e99978b 100644 --- a/web/config/serverConfig.test.ts +++ b/web/config/serverConfig.test.ts @@ -83,6 +83,20 @@ describe('serverconfiguratie', () => { expect(proxy).not.toContain('add_header'); }); + it('de proxy stuurt naar de CMS-host op poort 5200 met de forwarded headers', () => { + // De CMS-host vertrouwt alleen de proxy-Pi (ForwardedHeaders:KnownProxies) + // en haalt het bezoekersadres uit deze headers voor de rate limiter + const proxy = nginxProxyConf(); + expect(proxy).toMatch( + /^\s*proxy_pass http:\/\/192\.168\.1\.103:5200;/m, + ); + expect(proxy).toContain( + 'proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;', + ); + expect(proxy).toContain('proxy_set_header X-Forwarded-Proto $scheme;'); + expect(proxy).toContain('proxy_set_header X-Real-IP $remote_addr;'); + }); + it('proxy en testblok gebruiken dezelfde hostname', () => { // Alleen echte config-regels, geen commentaar const name = /^\s*server_name ([^;]+);/m; diff --git a/web/config/serverConfig.ts b/web/config/serverConfig.ts index 5a7769c..a3cf789 100644 --- a/web/config/serverConfig.ts +++ b/web/config/serverConfig.ts @@ -292,23 +292,28 @@ ${block(' ', noCache)} // nginx op de reverse-proxy-Pi (test): alleen HTTP. certbot voegt daarna // HTTPS en de redirect toe (certbot --nginx -d test.andreaslop.nl). Geen -// security headers hier: die zet de hosting-Pi al, dubbel geeft dubbele headers. -// Intern adres van de hosting-Pi (waar het testblok en WordPress draaien) +// security headers hier: de CMS-host zet ze al, dubbel geeft dubbele headers. +// Intern adres van de hosting-Pi (waar de CMS-host en WordPress draaien) const hostingPiAddress = '192.168.1.103'; +// Poort van de CMS-host Api.AndreaSlopFotografie (test) op de hosting-Pi. De +// host serveert /admin, /api/v1 en de voorgerenderde site zelf; nginx op de +// hosting-Pi (poort 80, andreaslop-test.conf) blijft als terugvaloptie staan. +const cmsHostPort = 5200; export function nginxProxyConf(): string { return `# ${generatedNotice} -# Reverse proxy voor de testsite naar de hosting-Pi. certbot past dit bestand -# aan voor HTTPS; kopieer het daarna dus niet opnieuw zonder certbot opnieuw te -# draaien. +# Reverse proxy voor de testsite naar de CMS-host op de hosting-Pi. certbot +# past dit bestand aan voor HTTPS; kopieer het daarna dus niet opnieuw zonder +# certbot opnieuw te draaien. Terugvallen op nginx van de hosting-Pi: haal +# :${cmsHostPort} weg achter proxy_pass. server { listen 80; server_name test.andreaslop.nl; location / { - proxy_pass http://${hostingPiAddress}; - # Verplicht: de hosting-Pi kiest op basis van de hostname tussen het - # testblok en WordPress + proxy_pass http://${hostingPiAddress}:${cmsHostPort}; + # De CMS-host gebruikt Host niet om een site te kiezen, maar de + # terugvaloptie (nginx op de hosting-Pi) wel: daarom blijft hij staan proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; diff --git a/web/deploy/nginx/test.andreaslop.nl.proxy.conf b/web/deploy/nginx/test.andreaslop.nl.proxy.conf index 87c5851..381c94b 100644 --- a/web/deploy/nginx/test.andreaslop.nl.proxy.conf +++ b/web/deploy/nginx/test.andreaslop.nl.proxy.conf @@ -1,15 +1,16 @@ # Gegenereerd door web/config/serverConfig.ts (pnpm server-config). Niet handmatig aanpassen. -# Reverse proxy voor de testsite naar de hosting-Pi. certbot past dit bestand -# aan voor HTTPS; kopieer het daarna dus niet opnieuw zonder certbot opnieuw te -# draaien. +# Reverse proxy voor de testsite naar de CMS-host op de hosting-Pi. certbot +# past dit bestand aan voor HTTPS; kopieer het daarna dus niet opnieuw zonder +# certbot opnieuw te draaien. Terugvallen op nginx van de hosting-Pi: haal +# :5200 weg achter proxy_pass. server { listen 80; server_name test.andreaslop.nl; location / { - proxy_pass http://192.168.1.103; - # Verplicht: de hosting-Pi kiest op basis van de hostname tussen het - # testblok en WordPress + proxy_pass http://192.168.1.103:5200; + # De CMS-host gebruikt Host niet om een site te kiezen, maar de + # terugvaloptie (nginx op de hosting-Pi) wel: daarom blijft hij staan proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; diff --git a/web/deploy/verify-deployment.sh b/web/deploy/verify-deployment.sh index a1e535c..74918d9 100644 --- a/web/deploy/verify-deployment.sh +++ b/web/deploy/verify-deployment.sh @@ -2,24 +2,40 @@ # Controle na een deploy (BR-HST-18): wat de server teruggeeft moet gelijk zijn # aan wat in dist staat. Een oude nginx-config die voor elk pad de homepage # geeft (SPA-fallback) valt hier door de mand. -# Gebruik: verify-deployment.sh [--test] +# Gebruik: verify-deployment.sh [--test] [--cms] # --test: ook X-Robots-Tag, en gzip op de JavaScript is verplicht # (zonder --test geeft ontbrekende gzip alleen een waarschuwing) +# --cms: de site wordt geserveerd door de CMS-host (Api.AndreaSlopFotografie) +# in plaats van nginx. Dan geldt ook: de CSP is exact gelijk aan +# $EXPECTED_CSP (verplicht, zie config/printCsp.ts), alle security +# headers staan met de exacte waarde op elk antwoord (pagina, asset, +# 301, 404, robots.txt), cachebeleid, alleen gzip (nooit br), een +# procentgecodeerd pad blijft gecodeerd, en /admin, /health en +# /api/v1/System/capabilities (zonder Offerings) antwoorden. set -euo pipefail -if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then - echo "Gebruik: $0 [--test]" >&2 +if [ "$#" -lt 2 ] || [ "$#" -gt 4 ]; then + echo "Gebruik: $0 [--test] [--cms]" >&2 exit 2 fi base="$1" dist="$2" test_mode=0 -if [ "$#" -eq 3 ]; then - if [ "$3" != "--test" ]; then - echo "Onbekende optie: $3" >&2 - exit 2 - fi - test_mode=1 +cms_mode=0 +shift 2 +for option in "$@"; do + case "$option" in + --test) test_mode=1 ;; + --cms) cms_mode=1 ;; + *) + echo "Onbekende optie: $option" >&2 + exit 2 + ;; + esac +done +if [ "$cms_mode" -eq 1 ] && [ -z "${EXPECTED_CSP:-}" ]; then + echo "--cms vraagt om EXPECTED_CSP (node config/printCsp.ts)" >&2 + exit 2 fi if ! [[ "$base" =~ ^https?://[^[:space:]]+$ ]]; then @@ -72,6 +88,31 @@ check_security_headers() { fail "$label: header $name ontbreekt" fi done + if [ "$cms_mode" -eq 1 ]; then check_exact_headers "$label"; fi +} + +# Alleen met --cms: de CMS-host moet exact de waarden van de site sturen +# (geen drift tussen config/securityHeaders.ts en de host) +check_exact_headers() { + local label="$1" + local -A expected=( + [Content-Security-Policy]="$EXPECTED_CSP" + [Strict-Transport-Security]="max-age=31536000; includeSubDomains" + [X-Content-Type-Options]="nosniff" + [X-Frame-Options]="DENY" + [Referrer-Policy]="strict-origin-when-cross-origin" + ) + local name actual + for name in "${!expected[@]}"; do + actual="$(header "$name")" + # Ontbrekend is hierboven al gemeld + if [ -n "$actual" ] && [ "$actual" != "${expected[$name]}" ]; then + fail "$label: header $name is '$actual', verwacht '${expected[$name]}'" + fi + done + if [ "$test_mode" -eq 1 ] && [ -n "$(header X-Robots-Tag)" ] && [ "$(header X-Robots-Tag)" != "noindex, nofollow" ]; then + fail "$label: header X-Robots-Tag is '$(header X-Robots-Tag)', verwacht 'noindex, nofollow'" + fi } # Een 200 met de bytes van het verwachte bestand uit dist @@ -150,6 +191,85 @@ else fi fi +# Alleen met --cms: gedrag van de CMS-host zelf +if [ "$cms_mode" -eq 1 ]; then + # Cachebeleid: pagina's no-cache, assets onveranderlijk + fetch / + case "$(header Cache-Control)" in + *no-cache*) ;; + *) fail "/: Cache-Control '$(header Cache-Control)', verwacht no-cache" ;; + esac + if [ -n "$script" ]; then + fetch "$script" + check_security_headers "$script" + case "$(header Cache-Control)" in + *immutable*) ;; + *) fail "$script: Cache-Control '$(header Cache-Control)', verwacht immutable" ;; + esac + fi + + # Alleen gzip: nooit brotli, ook niet als de client erom vraagt + fetch / -H 'Accept-Encoding: br, gzip' + if [ "$(header Content-Encoding)" = "br" ]; then + fail "/: Content-Encoding br, de CMS-host spreekt alleen gzip" + fi + + # robots.txt: volledige headerset, en op test alles uitsluiten + fetch /robots.txt + if [ "$status" != "200" ]; then + fail "/robots.txt: status $status, verwacht 200" + elif [ "$test_mode" -eq 1 ] && ! grep -q '^Disallow: /$' "$work/body"; then + fail "/robots.txt: 'Disallow: /' ontbreekt op test" + fi + check_security_headers /robots.txt + + # 404.html zelf geeft ook status 404 en de 404-pagina + fetch /404.html + if [ "$status" != "404" ]; then + fail "/404.html: status $status, verwacht 404" + fi + check_security_headers /404.html + + # Een procentgecodeerd pad met slash blijft gecodeerd (zou anders een + # andere resource worden: /a?b) + fetch '/a%3Fb/' + if [ "$status" != "301" ]; then + fail "/a%3Fb/: status $status, verwacht 301" + elif [ "$(header Location)" != "/a%3Fb" ]; then + fail "/a%3Fb/: Location '$(header Location)', verwacht /a%3Fb" + fi + + # Niet-GET op een websitepad. Met een lege body (-d ''): een POST zonder + # body en zonder Content-Length via de HTTP/1.0-proxy weigert Kestrel met + # 400 voordat de middleware het ziet + fetch / -X POST -d '' + if [ "$status" != "405" ]; then + fail "POST /: status $status, verwacht 405" + fi + + # De CMS-routes: health, admin, API (zonder Offerings), onbekend API-pad + fetch /health + if [ "$status" != "200" ]; then fail "/health: status $status, verwacht 200"; fi + + fetch /admin/ + if [ "$status" != "200" ]; then fail "/admin/: status $status, verwacht 200"; fi + + fetch /api/v1/System/capabilities + if [ "$status" != "200" ]; then + fail "/api/v1/System/capabilities: status $status, verwacht 200" + elif grep -qi 'offerings' "$work/body"; then + fail "/api/v1/System/capabilities noemt Offerings; die module hoort voor ASF uit te staan" + fi + + # Een onbekend API-pad is een API-404, nooit de website + fetch /api/v1/onbekend-pad-voor-controle + if [ "$status" != "404" ]; then + fail "/api/v1/onbekend-pad-voor-controle: status $status, verwacht 404" + elif grep -qi '&2 exit 1 diff --git a/web/e2e/deployScripts.e2e.ts b/web/e2e/deployScripts.e2e.ts index 0a25f47..b18514e 100644 --- a/web/e2e/deployScripts.e2e.ts +++ b/web/e2e/deployScripts.e2e.ts @@ -429,6 +429,37 @@ describe.skipIf(toolsMissing)('verify-deployment.sh', () => { ); }); + it('met --cms: weigert te starten zonder EXPECTED_CSP', async () => { + const run = await runScript( + 'verify-deployment.sh', + ['http://127.0.0.1:9', dist, '--test', '--cms'], + { EXPECTED_CSP: '' }, + ); + expect(run.code).toBe(2); + expect(run.stderr).toContain('EXPECTED_CSP'); + }); + + it('met --cms: meldt een afwijkende CSP en de ontbrekende CMS-routes', async () => { + // De gewone stub is een nginx-achtige server: geen /health, /admin/ + // of API, en een andere CSP dan hier verwacht + await withStub( + { behaviour: 'new', robots: true, gzip: true }, + async (origin) => { + const run = await runScript( + 'verify-deployment.sh', + [origin, dist, '--test', '--cms'], + { EXPECTED_CSP: "default-src 'none'" }, + ); + expect(run.code).toBe(1); + expect(run.stderr).toContain( + "header Content-Security-Policy is '", + ); + expect(run.stderr).toContain('/health: status 404'); + expect(run.stderr).toContain('/admin/: status 301'); + }, + ); + }); + it('telt een verbroken verbinding als fout en eindigt met de samenvatting', async () => { await withStub( { behaviour: 'new', drop: '/over-mij' },