Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

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

Joseviec3dc8ba69fe
diff --git a/.env.example b/.env.example
index a2c44ddb1..6ae8d8bcb 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 93c914209..dd3e1383f 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 601917cae..8eabfab88 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 f8d67e674..b1c6bc4b9 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;