From 0b76b36bbed87b2a028469c87dfb0eeda36cf0e7 Mon Sep 17 00:00:00 2001 From: IliesAllali Date: Wed, 30 Sep 2026 17:38:11 +0200 Subject: [PATCH] =?UTF-8?q?nginx=20:=20en-t=C3=AAtes=20de=20s=C3=A9curit?= =?UTF-8?q?=C3=A9=20dans=20chaque=20location,=20CSP,=20HSTS,=20TLS=201.2+?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Frame-Options ALLOWALL (invalide) et X-XSS-Protection retirés. Les location qui posent leur propre add_header répètent la liste complète, sinon nginx n'hérite de rien. CSP de l'app et de la landing construites depuis ce que chargent les builds (PostHog, YouTube sans cookie, OpenStreetMap, blob:, iframe de démo). Prod seulement : HSTS avec includeSubDomains, ssl_protocols TLSv1.2 TLSv1.3. Co-Authored-By: Claude Opus 5.5 --- deploy/nginx.conf | 81 ++++++++++++++++++++++++++++++++++------ deploy/nginx.docker.conf | 20 +++++++++- 2 files changed, 89 insertions(+), 12 deletions(-) diff --git a/deploy/nginx.conf b/deploy/nginx.conf index 0af8c3b..b9880e1 100644 --- a/deploy/nginx.conf +++ b/deploy/nginx.conf @@ -1,3 +1,42 @@ +# ─── En-têtes de sécurité ───────────────────────────────────────────────────── +# +# nginx n'hérite des add_header du niveau server que dans les location qui n'en déclarent aucun. +# Toute location qui pose son propre add_header (Cache-Control par exemple) répète donc la liste +# complète. Les politiques longues sont rangées dans des variables (map) pour ne pas les recopier. +# +# CSP de l'application (app.parenthese.io), sources autorisées : +# script-src, connect-src https://*.posthog.com, analytics (VITE_POSTHOG_HOST = us.i.posthog.com, +# extensions chargées depuis us-assets.i.posthog.com ; forme recommandée par PostHog) +# connect-src 'self' appels /api, /auth, /trees +# style-src 'unsafe-inline' styles posés en ligne par React et par posthog-js +# img-src data: blob: aperçu d'une photo avant envoi (URL.createObjectURL), images en data: +# font-src 'self' data: polices servies par le site (Vite peut en inliner une petite en data:) +# media-src blob: lecture locale d'un fichier avant envoi +# worker-src blob: compression des enregistrements posthog-js +# frame-src 'self' pour les PDF servis par /api, YouTube sans cookie, carte OpenStreetMap +# frame-ancestors la landing parenthese.io intègre l'app en iframe +map $host $parenthese_app_csp { + default "default-src 'self'; script-src 'self' https://*.posthog.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self' https://*.posthog.com; media-src 'self' blob:; worker-src 'self' blob:; frame-src 'self' https://www.youtube-nocookie.com https://www.openstreetmap.org; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self' https://parenthese.io https://www.parenthese.io"; +} + +# CSP de la landing (parenthese.io), sources autorisées : +# script-src 'sha256-...' script en ligne du (animation du logo, dans index.html et en/index.html). +# S'il change, recalculer l'empreinte : base64(sha256(contenu exact entre +# )), sinon l'animation ne se joue plus (sans autre effet). +# posthog comme pour l'app +# style-src 'unsafe-inline' attributs style du rendu serveur React +# frame-src démonstration de l'app en iframe +# frame-ancestors 'none' équivalent de X-Frame-Options DENY +map $host $parenthese_landing_csp { + default "default-src 'self'; script-src 'self' 'sha256-Av5a1/yoW/tQ2ZQWqH5FnaL896Bubcs9ivdFvsKZBrU=' https://*.posthog.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self' https://*.posthog.com; worker-src 'self' blob:; frame-src https://app.parenthese.io; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'"; +} + +# Réponses de l'API relayées (JSON, médias des familles) : intégrables seulement par l'app (PDF en iframe). +map $host $parenthese_api_csp { + default "frame-ancestors 'self' https://parenthese.io https://www.parenthese.io"; +} + + # ─── app.parenthese.io ──────────────────────────────────────────────────────── server { @@ -7,18 +46,22 @@ server { } server { + # « listen ... http2 » gardé : la directive « http2 on; » demande nginx 1.25.1 ou plus, et la version + # installée sur le VPS n'est pas écrite dans le dépôt (Ubuntu 24.04 livre la 1.24). À passer en + # « listen 443 ssl; » + « http2 on; » une fois « nginx -v » vérifié. listen 443 ssl http2; server_name app.parenthese.io; ssl_certificate /etc/letsencrypt/live/app.parenthese.io/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/app.parenthese.io/privkey.pem; + ssl_protocols TLSv1.2 TLSv1.3; - # Security headers - # frame-ancestors allows the landing page (parenthese.io) to embed the app in an iframe - add_header Content-Security-Policy "frame-ancestors 'self' https://parenthese.io https://www.parenthese.io" always; + # Hérités par les location sans add_header, c'est-à-dire les routes relayées vers l'API + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; + add_header Content-Security-Policy $parenthese_api_csp always; add_header X-Content-Type-Options "nosniff" always; - add_header X-XSS-Protection "1; mode=block" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; + add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=()" always; gzip on; gzip_vary on; @@ -70,20 +113,21 @@ server { try_files $uri $uri/ /index.html; add_header Cache-Control "no-cache, no-store, must-revalidate" always; add_header Pragma "no-cache" always; - add_header Content-Security-Policy "frame-ancestors 'self' https://parenthese.io https://www.parenthese.io" always; - # Keep embedding control in CSP; override any inherited SAMEORIGIN frame header. - add_header X-Frame-Options "ALLOWALL" always; + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; + add_header Content-Security-Policy $parenthese_app_csp always; add_header X-Content-Type-Options "nosniff" always; - add_header X-XSS-Protection "1; mode=block" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; + add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=()" always; } location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { expires 30d; add_header Cache-Control "public, immutable"; - add_header Content-Security-Policy "frame-ancestors 'self' https://parenthese.io https://www.parenthese.io" always; - add_header X-Frame-Options "ALLOWALL" always; + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; + add_header Content-Security-Policy $parenthese_app_csp always; add_header X-Content-Type-Options "nosniff" always; + add_header Referrer-Policy "strict-origin-when-cross-origin" always; + add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=()" always; } } @@ -101,6 +145,8 @@ server { server_name www.parenthese.io; ssl_certificate /etc/letsencrypt/live/parenthese.io/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/parenthese.io/privkey.pem; + ssl_protocols TLSv1.2 TLSv1.3; + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; return 301 https://parenthese.io$request_uri; } @@ -110,10 +156,13 @@ server { ssl_certificate /etc/letsencrypt/live/parenthese.io/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/parenthese.io/privkey.pem; + ssl_protocols TLSv1.2 TLSv1.3; + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; + add_header Content-Security-Policy $parenthese_landing_csp always; add_header X-Content-Type-Options "nosniff" always; - add_header X-XSS-Protection "1; mode=block" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; + add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=()" always; add_header X-Frame-Options "DENY" always; gzip on; @@ -129,7 +178,11 @@ server { try_files $uri $uri/ /index.html; add_header Cache-Control "no-cache, no-store, must-revalidate" always; add_header Pragma "no-cache" always; + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; + add_header Content-Security-Policy $parenthese_landing_csp always; add_header X-Content-Type-Options "nosniff" always; + add_header Referrer-Policy "strict-origin-when-cross-origin" always; + add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=()" always; add_header X-Frame-Options "DENY" always; } @@ -137,5 +190,11 @@ server { location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { expires 30d; add_header Cache-Control "public, immutable"; + add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; + add_header Content-Security-Policy $parenthese_landing_csp always; + add_header X-Content-Type-Options "nosniff" always; + add_header Referrer-Policy "strict-origin-when-cross-origin" always; + add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=()" always; + add_header X-Frame-Options "DENY" always; } } diff --git a/deploy/nginx.docker.conf b/deploy/nginx.docker.conf index ae6eea2..1d42a65 100644 --- a/deploy/nginx.docker.conf +++ b/deploy/nginx.docker.conf @@ -1,6 +1,16 @@ # nginx du service `web` (docker compose). Dérivé de deploy/nginx.conf (prod app.parenthese.io) : # même origine, /api/* réécrit en /* vers l'API, /auth, /trees et /health relayés tels quels, -# fallback SPA. Pas de TLS ici : placez un reverse proxy HTTPS devant si vous exposez sur Internet. +# fallback SPA. Pas de TLS ici : placez un reverse proxy HTTPS devant si vous exposez sur Internet +# (c'est lui qui posera Strict-Transport-Security). +# +# nginx n'hérite des add_header du niveau server que dans les location qui n'en déclarent aucun : +# les location qui posent leur propre add_header répètent la liste complète. +# +# CSP de l'application : même politique que la production (deploy/nginx.conf, sources expliquées là-bas), +# sans PostHog (l'image ne reçoit pas de clé d'analytics) et sans intégration par un autre site. +map $host $parenthese_app_csp { + default "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self'; media-src 'self' blob:; worker-src 'self' blob:; frame-src 'self' https://www.youtube-nocookie.com https://www.openstreetmap.org; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'"; +} server { listen 80; @@ -14,9 +24,12 @@ server { gzip_min_length 1024; gzip_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript image/svg+xml; + # Hérités par les location sans add_header, c'est-à-dire les routes relayées vers l'API + add_header Content-Security-Policy "frame-ancestors 'self'" always; add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; + add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=()" always; location /api/ { rewrite ^/api(/.*)$ $1 break; @@ -60,15 +73,20 @@ server { try_files $uri $uri/ /index.html; add_header Cache-Control "no-cache, no-store, must-revalidate" always; add_header Pragma "no-cache" always; + add_header Content-Security-Policy $parenthese_app_csp always; add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; + add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=()" always; } location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { expires 30d; add_header Cache-Control "public, immutable"; + add_header Content-Security-Policy $parenthese_app_csp always; add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "SAMEORIGIN" always; + add_header Referrer-Policy "strict-origin-when-cross-origin" always; + add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=()" always; } }