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.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Web: a real Content-Security-Policy
Duarte Santos03b227ce5eab
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 98779cb7c..35407fe8b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -27,6 +27,9 @@ for the reports, each with a patch and a test. Self-hosters: please update.- **One owner, many admins.** Any admin could delete every other admin and then clear the activitylog. Now one admin is the owner: only they make or remove admins, delete an admin, clear the logor hand the role on (Admin, tap a user). Existing instances: the oldest admin is the owner.+- **The web container sends a real Content-Security-Policy.** Scripts only run from your own+ instance, so an injected one does nothing. If your proxy adds analytics, see+ `CONTENT_SECURITY_POLICY` in docs/SELF_HOSTING.md.## v1.4.1 (2026-10-10)diff --git a/SECURITY.md b/SECURITY.mdindex 15de9a55c..97a6ab9e8 100644--- a/SECURITY.md+++ b/SECURITY.md@@ -74,11 +74,12 @@ in the thread; there's no objection, and no request to sit on it indefinitely.throttle *is* in scope, and so is genuine amplification (one small request causing unboundedwork).- **Missing security headers.** `web/nginx.conf.template` sets `X-Frame-Options: DENY`,- `Content-Security-Policy: frame-ancestors 'none'`, `X-Content-Type-Options: nosniff` and- `Referrer-Policy: same-origin`. It deliberately does **not** set HSTS or a full CSP: TLS is the- reverse proxy's job, and a script/style policy tight enough to be worth having needs testing- against the built app rather than being asserted here. A concrete attack that a header would- have stopped is still worth reporting.+ `X-Content-Type-Options: nosniff`, `Referrer-Policy: same-origin` and a Content-Security-Policy+ (`CONTENT_SECURITY_POLICY`, default in `web/Dockerfile`) that runs scripts from the instance+ only. It deliberately does **not** set HSTS: TLS is the reverse proxy's job, and so is any+ header an operator changes. `connect-src` is open on purpose (the Coach on a device talks to+ the provider its owner chose). A concrete attack that a header would have stopped is still+ worth reporting.- Instances served over plain `http://` on a LAN IP. Passkeys don't work there, and withpassword sign-in the password and the session cookie (not marked `Secure`) cross that networkin the clear. That is the documented trade of running without TLS, not a finding.diff --git a/docs/SELF_HOSTING.md b/docs/SELF_HOSTING.mdindex d15260de0..76b33bc15 100644--- a/docs/SELF_HOSTING.md+++ b/docs/SELF_HOSTING.md@@ -468,6 +468,14 @@ were issued; lowering it never cuts an existing session short. A browser renewssigning in; a paired phone renews its token by itself whenever it starts past half of thattime, so only a phone left unopened for longer than `SESSION_DAYS` has to be paired again.+The web container sends a Content-Security-Policy that lets scripts run only from your instance+(plus the app's one inline script, by its hash), so a script that found its way into a page does+not run. Requests the app makes itself stay open: the Coach on a device talks to whatever provider+you chose, a LAN Ollama included. If your reverse proxy injects a script of its own (analytics,+say), it gets blocked; give the `web` service a policy that allows it in `environment:`, or+`CONTENT_SECURITY_POLICY=frame-ancestors 'none'` for the old, minimal header. The default is in+`web/Dockerfile`.+The web image renders its nginx config from these when the container starts, so they take effecton a **prebuilt image** — no rebuild. `BACKEND` and `PORT` together are what `/api` is proxied to,so they have to name a service the web container can actually reach on your compose network.@@ -550,8 +558,8 @@ timeout the app can only report as "The server refused the file as too large":`entryPoints.<name>.transport.respondingTimeouts.readTimeout` (`600s`, say). nginx's`client_body_timeout` counts the gap between two reads, so its default of 60 s is fine. TheAPI itself gives a request 30 minutes and drops one that sends nothing for 60 s.-- **Content-Security-Policy**: if the proxy adds one, it must allow `blob:` in `img-src` and- `media-src`. The app shows the files from its own verified local copy through `blob:` URLs.+- **Content-Security-Policy**: the web container already sends one that allows this. If the+ proxy adds its own, it must allow `blob:` in `img-src` and `media-src`. The app shows the files from its own verified local copy through `blob:` URLs.**What is removed, and when.** A file goes when its owner's stored state has not used it for`MEDIA_GC_GRACE_DAYS` (checked every hour), after one hour unused when the owner's quota is full,diff --git a/frontend/src/lib/csp.test.js b/frontend/src/lib/csp.test.jsnew file mode 100644index 000000000..72129bea4--- /dev/null+++ b/frontend/src/lib/csp.test.js@@ -0,0 +1,37 @@+// The web image's Content-Security-Policy runs the one inline script in index.html by its hash.+// Change that script and the hash in web/Dockerfile has to change with it, or the app loads+// without its first-paint language and direction. Read off the files themselves.+import { readFileSync } from 'node:fs'+import { createHash } from 'node:crypto'+import { dirname, join } from 'node:path'+import { fileURLToPath } from 'node:url'+import { describe, expect, it } from 'vitest'++const root = join(dirname(fileURLToPath(import.meta.url)), '../../..')+const read = p => readFileSync(join(root, p), 'utf8')+const policy = read('web/Dockerfile').match(/^ENV CONTENT_SECURITY_POLICY="([^"]*)"$/m)?.[1] || ''+const directive = name => policy.split(';').map(d => d.trim().split(/\s+/)).find(d => d[0] === name)?.slice(1) || []++describe('web Content-Security-Policy', () => {+ it('allows every inline script in index.html by its hash, and nothing inline beyond that', () => {+ const html = read('frontend/index.html')+ const inline = [...html.matchAll(/<script(?![^>]*\bsrc=)[^>]*>([\s\S]*?)<\/script>/g)].map(m => m[1])+ expect(inline.length).toBeGreaterThan(0)+ const scripts = directive('script-src')+ for (const body of inline) expect(scripts).toContain(`'sha256-${createHash('sha256').update(body).digest('base64')}'`)+ expect(scripts).not.toContain("'unsafe-inline'")+ expect(scripts).not.toContain("'unsafe-eval'")+ })+ it('keeps what the app needs: blob: media, its own frames refused, no plugins', () => {+ expect(directive('img-src')).toContain('blob:')+ expect(directive('media-src')).toContain('blob:')+ expect(directive('frame-ancestors')).toEqual(["'none'"])+ expect(directive('object-src')).toEqual(["'none'"])+ })+ it('is sent from every block of the template that sets headers of its own', () => {+ const conf = read('web/nginx.conf.template')+ const sets = conf.split('\n').filter(l => /^\s*add_header X-Frame-Options/.test(l)).length+ const csp = conf.split('\n').filter(l => /^\s*add_header Content-Security-Policy "\$\{CONTENT_SECURITY_POLICY\}" always;/.test(l)).length+ expect(csp).toBe(sets)+ })+})diff --git a/web/Dockerfile b/web/Dockerfileindex 464f93d31..919767205 100644--- a/web/Dockerfile+++ b/web/Dockerfile@@ -101,6 +101,13 @@ ENV BASE_PATH=""# Above the API's own video cap (MEDIA_VIDEO_MAX_MB, 40 by default) so the API, which says why,# is the one that refuses a file — raise both together.ENV MEDIA_UPLOAD_MAX=48m+# Sent on every page and file. Scripts run only from this origin, plus the one inline script in+# frontend/index.html by its hash (src/lib/csp.test.js keeps the two in step). connect-src stays+# open to any https: and http: address: the Coach on a device talks to the provider the person+# chose, an Ollama on their LAN included, and the update check asks GitHub. Behind a proxy that+# injects a script of its own (analytics), set this to a policy that allows it, or to+# "frame-ancestors 'none'" for the old header.+ENV CONTENT_SECURITY_POLICY="default-src 'self'; script-src 'self' 'sha256-z/iOLe5i8fD4VNlIXz/4hEayGLTQzoTc52dIY51OjYc='; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; media-src 'self' blob: https:; font-src 'self' data:; connect-src 'self' https: http:; worker-src 'self'; manifest-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'"# 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 asdiff --git a/web/nginx.conf.template b/web/nginx.conf.templateindex f66aee554..a9641d0c7 100644--- a/web/nginx.conf.template+++ b/web/nginx.conf.template@@ -26,13 +26,14 @@ server {# 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).+ # own Cache-Control have to repeat these. The Content-Security-Policy is CONTENT_SECURITY_POLICY+ # (default in web/Dockerfile): scripts only from this origin plus the one inline script in+ # index.html, by its hash, so an injected <script> or handler does not run. TLS and HSTS 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;+ add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;# API → backend container.# `^~` so this prefix beats the regex locations below: without it a request for a path such@@ -141,7 +142,7 @@ server {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;+ add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;}# The exercise stills and animations, licensed from Gym visual for openGym only (NOTICE.md).@@ -156,7 +157,7 @@ server {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;+ add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;location ~* \.(png|jpg|jpeg|gif|webp|avif|mp4)$ {rewrite ^\Q${BASE_PATH}\E/(.*)$ /$1 break;add_header Cache-Control "public, max-age=2592000, immutable";@@ -164,7 +165,7 @@ server {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;+ add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;}}@@ -179,7 +180,7 @@ server {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;+ add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;}gzip on;