#!/usr/bin/env bash # 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] # --test: ook X-Robots-Tag, en gzip op de JavaScript is verplicht # (zonder --test geeft ontbrekende gzip alleen een waarschuwing) set -euo pipefail if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then echo "Gebruik: $0 [--test]" >&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 fi if ! [[ "$base" =~ ^https?://[^[:space:]]+$ ]]; then echo "Ongeldige URL: '$base'" >&2 exit 2 fi while [[ "$base" == */ ]]; do base="${base%/}"; done if [ ! -d "$dist" ]; then echo "Geen map: $dist" >&2 exit 2 fi work="$(mktemp -d)" trap 'rm -rf "$work"' EXIT failures=0 fail() { echo "FOUT: $1" >&2 failures=$((failures + 1)) } # Haalt $base$1 op zonder redirects te volgen en zonder Accept-Encoding # (curl stuurt er zelf geen). Bewaart status, headers en body in $work. # Meer argumenten: extra curl-opties (bijvoorbeeld een header). fetch() { local path="$1" shift : > "$work/headers" : > "$work/body" # Geen verbinding of antwoord telt als fout; het script loopt door tot de # samenvatting in plaats van af te breken if ! status="$(curl -sS --max-time 30 -o "$work/body" -D "$work/headers" \ -w '%{http_code}' "$@" "$base$path")"; then status="000" fail "$path: geen antwoord van de server (curl faalde)" fi } # Waarde van een header uit de laatst opgehaalde respons (hoofdletterongevoelig) header() { tr -d '\r' < "$work/headers" | grep -i "^$1:" | head -n 1 | cut -d: -f2- | sed 's/^[[:space:]]*//' || true } check_security_headers() { local label="$1" name local names=(Content-Security-Policy Strict-Transport-Security X-Content-Type-Options X-Frame-Options Referrer-Policy) if [ "$test_mode" -eq 1 ]; then names+=(X-Robots-Tag); fi for name in "${names[@]}"; do if [ -z "$(header "$name")" ]; then fail "$label: header $name ontbreekt" fi done } # Een 200 met de bytes van het verwachte bestand uit dist expect_file() { local path="$1" file="$2" fetch "$path" if [ "$status" != "200" ]; then fail "$path: status $status, verwacht 200" elif ! cmp -s "$work/body" "$dist/$file"; then fail "$path: antwoord is niet gelijk aan $file uit dist (oude serverconfig of oude build?)" fi check_security_headers "$path" } # Een 301 naar een pad (Location mag absoluut of relatief zijn) expect_redirect() { local path="$1" target="$2" location fetch "$path" if [ "$status" != "301" ]; then fail "$path: status $status, verwacht 301" else location="$(header Location)" location="${location#http://*/}" location="${location#https://*/}" if [[ "$location" != /* ]]; then location="/$location"; fi if [ "$location" != "$target" ]; then fail "$path: Location '$(header Location)', verwacht pad $target" fi fi check_security_headers "$path" } for file in index.html over-mij.html soorten-fotoshoots/newborn.html 404.html; do if [ ! -f "$dist/$file" ]; then echo "Geen $file in $dist" >&2 exit 2 fi done # De server moet bereikbaar zijn; kort herhalen zoals de oude controle if ! curl -sS --retry 5 --retry-delay 3 --retry-connrefused --max-time 30 \ -o /dev/null "$base/"; then echo "Niet bereikbaar: $base/" >&2 exit 1 fi expect_file / index.html expect_file /over-mij over-mij.html expect_file /soorten-fotoshoots/newborn soorten-fotoshoots/newborn.html fetch /onbekend-pad-voor-controle if [ "$status" != "404" ]; then fail "/onbekend-pad-voor-controle: status $status, verwacht 404" elif ! cmp -s "$work/body" "$dist/404.html"; then fail "/onbekend-pad-voor-controle: antwoord is niet gelijk aan 404.html uit dist" fi check_security_headers /onbekend-pad-voor-controle expect_redirect /over-mij/ /over-mij expect_redirect /Over-Mij /over-mij # Compressie van de JavaScript uit index.html script="$(grep -o '/assets/[A-Za-z0-9_./-]*\.js' "$dist/index.html" | head -n 1 || true)" if [ -z "$script" ]; then fail "geen /assets/*.js gevonden in index.html om de compressie te controleren" else fetch "$script" -H 'Accept-Encoding: gzip' if [ "$status" != "200" ]; then fail "$script: status $status, verwacht 200" elif [ "$(header Content-Encoding)" != "gzip" ]; then if [ "$test_mode" -eq 1 ]; then fail "$script: geen Content-Encoding: gzip" else echo "WAARSCHUWING: $script wordt niet gecomprimeerd (de webhost beslist)" >&2 fi fi fi if [ "$failures" -gt 0 ]; then echo "Controle mislukt ($failures fout(en)) voor $base" >&2 exit 1 fi echo "Controle geslaagd voor $base"