From ec3dc8ba69fe50887a3db7348b03770113974cc9 Mon Sep 17 00:00:00 2001 From: Josevi Date: Tue, 25 Aug 2026 10:01:07 +0200 Subject: [PATCH] web: cabeceras de proxy no falsificables, /api prioritario y cabeceras de seguridad MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .env.example | 26 ++++++++++++++++++++------ docker-compose.yml | 3 +++ web/Dockerfile | 5 +++++ web/nginx.conf.template | 36 +++++++++++++++++++++++++++++++++--- 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index a2c44dd..6ae8d8b 100644 --- a/.env.example +++ b/.env.example @@ -70,12 +70,26 @@ RP_NAME=openGym # AUDIT_IP=full # the whole address # # The address is read from CF-Connecting-IP, then the first entry of -# X-Forwarded-For, then X-Real-IP. The web container sets the last two, so the -# bundled stack is correct as-is. Cloudflare is first in that list on purpose: a -# Cloudflare tunnel does not put the visitor in X-Forwarded-For at all, so that -# header would otherwise record the tunnel itself and look perfectly plausible. -# Whatever you run in front has to overwrite these headers rather than pass a -# client-supplied one through — they are only as trustworthy as your proxy. +# X-Forwarded-For, then X-Real-IP, and finally the connecting socket itself if +# none of those is present. +# +# A forwarded-for header is only worth anything if whatever sits in front +# OVERWRITES it instead of passing a client-supplied one through — otherwise a +# caller simply states the address it would like logged. The web container now +# overwrites X-Forwarded-For and X-Real-IP with the real peer, and DROPS +# CF-Connecting-IP, so the bundled stack records an address nobody can choose. +# (Before, X-Forwarded-For was appended to rather than replaced, and +# CF-Connecting-IP was passed straight through — both were forgeable.) +# +# Cloudflare is first in that list on purpose: a Cloudflare tunnel does not put +# the visitor in X-Forwarded-For at all, so that header would otherwise record +# the tunnel itself and look perfectly plausible. If — and only if — Cloudflare +# is genuinely in front of you, let its header through again: +# +# CF_CONNECTING_IP=$http_cf_connecting_ip +# +# That is safe there because Cloudflare sets the header itself, replacing +# anything the client sent. Anywhere else, leave it unset. # ── Session length (optional) ────────────────────────────────────────────── # How long a sign-in lasts, in days. Default 90 — long enough that people who diff --git a/docker-compose.yml b/docker-compose.yml index 93c9142..dd3e138 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -69,6 +69,9 @@ services: - NGINX_PORT=${NGINX_PORT:-80} - BACKEND=${BACKEND:-api} - PORT=${PORT:-3000} + # Empty drops the CF-Connecting-IP header, so a direct client cannot forge the address + # the activity log records. Cloudflare users set CF_CONNECTING_IP=$$http_cf_connecting_ip. + - CF_CONNECTING_IP=${CF_CONNECTING_IP:-} volumes: - ./media/img:/usr/share/nginx/html/img:ro - ./media/gif:/usr/share/nginx/html/gif:ro diff --git a/web/Dockerfile b/web/Dockerfile index 601917c..8eabfab 100644 --- a/web/Dockerfile +++ b/web/Dockerfile @@ -38,6 +38,11 @@ COPY --from=build /app/dist /usr/share/nginx/html ENV NGINX_PORT=80 ENV BACKEND=api ENV PORT=3000 +# Empty = the CF-Connecting-IP header is dropped before it reaches the API, so it cannot be +# forged by a direct client. Set to $http_cf_connecting_ip only when Cloudflare is really in +# front. Defined here too, not just in compose, so the template always renders for anyone +# running the prebuilt image from their own compose file. +ENV CF_CONNECTING_IP="" # Probe the port nginx was actually told to listen on (NGINX_PORT is configurable, so a # hardcoded http://localhost/ breaks on any instance that changed it), and address it as diff --git a/web/nginx.conf.template b/web/nginx.conf.template index f8d67e6..b1c6bc4 100644 --- a/web/nginx.conf.template +++ b/web/nginx.conf.template @@ -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;