web: cabeceras de proxy no falsificables, /api prioritario y cabeceras de seguridad

X-Forwarded-For se ponia con $proxy_add_x_forwarded_for, que APENDE lo que mande
el cliente, y el API lee la primera entrada — asi que cualquiera elegia la IP que
quedaba registrada contra el en el activity log. CF-Connecting-IP no la tocaba
nadie y es la que el API mira primero. Ahora se sobrescribe con $remote_addr y la
de Cloudflare se descarta salvo que pongas CF_CONNECTING_IP=$http_cf_connecting_ip,
que solo es correcto con Cloudflare de verdad delante. .env.example afirmaba que el
stack venia correcto de fabrica; no era cierto.

location /api/ pasa a ^~ para que gane a los location con regex: sin eso una ruta
como /api/algo.json la atraparia el bloque \.(js|css|json|html)$ y se buscaria en
disco en vez de proxearse. Hoy no rompe nada, manana si.

Y cuatro cabeceras de seguridad, repetidas en los dos location con regex porque
add_header dentro de un location reemplaza los heredados en vez de sumarse. Sin
HSTS ni CSP completa a proposito: eso sigue siendo del reverse proxy.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
JoseviandClaude Opus 5 committed 2026-08-25 10:01:07 +02:00
1 parent e326f1b1cb
commit ec3dc8ba69
4 files changed
+61 -9

No files matched your search

+33 -3
View File
@@ -5,14 +5,36 @@ server {
root /usr/share/nginx/html;
index index.html;
# API → backend container
location /api/ {
# Sent on every response below. NOTE: an `add_header` inside a location replaces the whole
# inherited set rather than adding to it, so the two locations further down that set their
# own Cache-Control have to repeat these three. Deliberately not a full Content-Security-
# Policy: only frame-ancestors, which no part of the app depends on. TLS, HSTS and any
# stricter policy stay the reverse proxy's job (see SECURITY.md).
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "same-origin" always;
add_header Content-Security-Policy "frame-ancestors 'none'" always;
# API → backend container.
# `^~` so this prefix beats the regex locations below: without it a request for a path such
# as /api/anything.json would be matched by the \.(js|css|json|html)$ block instead and be
# looked for on disk rather than proxied.
location ^~ /api/ {
proxy_pass http://${BACKEND}:${PORT};
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# $remote_addr, NOT $proxy_add_x_forwarded_for: the latter APPENDS to whatever the client
# sent, and the API reads the first entry — so a client could put any address it liked in
# front of its own and choose what the activity log recorded about it. Overwriting is what
# makes the header mean anything.
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
# Same reasoning, and the API trusts this one FIRST. Empty by default, which drops the
# header entirely, so a direct client cannot forge it. Cloudflare users set
# CF_CONNECTING_IP=$http_cf_connecting_ip to let the real one through — correct only when
# Cloudflare is genuinely in front, because that is what overwrites a client-supplied one.
proxy_set_header CF-Connecting-IP "${CF_CONNECTING_IP}";
}
# SPA fallback
@@ -23,12 +45,20 @@ server {
# App shell: always revalidate so updates show up immediately
location ~* \.(js|css|json|html)$ {
add_header Cache-Control "no-cache, must-revalidate";
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "same-origin" always;
add_header Content-Security-Policy "frame-ancestors 'none'" always;
}
# Exercise images/GIFs never change → cache hard
location ~* \.(png|jpg|jpeg|gif|ico|svg|woff|woff2|webp|avif)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000, immutable";
add_header X-Frame-Options "DENY" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "same-origin" always;
add_header Content-Security-Policy "frame-ancestors 'none'" always;
}
gzip on;