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
fix(web): exercise media answer with Cross-Origin-Resource-Policy: same-origin
Duarte Santos3e60fc1e735b
diff --git a/contrib/nix/opengym.nix b/contrib/nix/opengym.nixindex 4bcdd8829..f2bd7a2a7 100644--- a/contrib/nix/opengym.nix+++ b/contrib/nix/opengym.nix@@ -362,11 +362,13 @@ in};# exercise-media/ is served from root like the rest of the app. The files# are named after the exercise and never change within a build, so let- # browsers keep them.+ # browsers keep them. They are licensed for openGym only (NOTICE.md):+ # Cross-Origin-Resource-Policy keeps other sites from embedding them."/exercise-media/" = {extraConfig = ''expires 30d;add_header Cache-Control "public";+ add_header Cross-Origin-Resource-Policy "same-origin" always;'';};};diff --git a/contrib/nix/tests.nix b/contrib/nix/tests.nixindex 3c4037424..bf73f4355 100644--- a/contrib/nix/tests.nix+++ b/contrib/nix/tests.nix@@ -50,5 +50,8 @@ pkgs.testers.runNixOSTest {# The exercise media ship inside the frontend package (catalogue/media), no download involved.machine.succeed("curl -fsS -o /dev/null http://localhost/exercise-media/NOTICE.md")+ # Licensed for openGym only: another site cannot embed them.+ corp = machine.succeed("curl -fsSI http://localhost/exercise-media/NOTICE.md")+ assert "cross-origin-resource-policy: same-origin" in corp.lower(), f"no CORP header: {corp}"'';}\ No newline at end of filediff --git a/frontend/src/lib/api-base.test.js b/frontend/src/lib/api-base.test.jsindex ea9861055..c9403b7f8 100644--- a/frontend/src/lib/api-base.test.js+++ b/frontend/src/lib/api-base.test.js@@ -76,12 +76,36 @@ function blocksOf(conf) {describe('the cache lifetime nginx gives an image', () => {it('is said once, by the explicit header, with no expires beside it', () => {expect(NGINX.replace(/#.*$/gm, '')).not.toMatch(/\bexpires\b/)+ // the exercise media's own block and every other image'sconst media = blocksOf(NGINX).filter(b => /\bimmutable\b/.test(b))- expect(media.length).toBe(1)- expect(media[0]).toMatch(/\badd_header\s+Cache-Control\s+"public, max-age=2592000, immutable"\s*;/)+ expect(media.length).toBe(2)+ for (const b of media) expect(b).toMatch(/\badd_header\s+Cache-Control\s+"public, max-age=2592000, immutable"\s*;/)})})+// The exercise media are licensed for openGym only: Cross-Origin-Resource-Policy keeps another+// site from embedding them, on every answer under /exercise-media/, at the site root and under a+// BASE_PATH alike. An add_header in a block replaces the inherited ones, so the nested block+// that sets the media's caching has to say it again.+describe('the exercise media', () => {+ const CORP = /\badd_header\s+Cross-Origin-Resource-Policy\s+"same-origin"\s+always\s*;/+ for (const base of ['', '/gym']) {+ it(`are same-origin only${base ? ' under ' + base : ''}, files and anything else in the folder`, () => {+ const conf = render(base)+ const at = conf.indexOf(`location ^~ ${base}/exercise-media/ {`)+ expect(at).toBeGreaterThan(-1)+ const blocks = blocksOf(conf.slice(at))+ const nested = blocks[0], outer = blocks[1]+ for (const b of [outer, nested]) {+ expect(b).toMatch(CORP)+ expect(b).toMatch(/\badd_header\s+X-Content-Type-Options\s+"nosniff"\s+always\s*;/)+ expect(b).toMatch(/\brewrite\s+\^\\Q/)+ }+ expect(nested).toMatch(/immutable/)+ })+ }+})+// The template as nginx gets it for a given BASE_PATH, comments left out. Its PCRE patterns are// valid JavaScript too, apart from \Q…\E (PCRE's quotes for literal text), which `quoted` turns// into the same text escaped. Otherwise they run here the way nginx runs them.@@ -150,13 +174,14 @@ describe('what nginx forwards to the API', () => {// to @root, and @root sent it back to /g+ym/, forever; the assets and images 404'd.describe('the file nginx looks for under BASE_PATH', () => {const REWRITE = /\brewrite\s+(\S+)\s+(\S+)\s+break\s*;/g- // All three rewrites (the app's prefix block and the two cache blocks) give the same answer.+ // All five rewrites (the app's prefix block, the two cache blocks, and the exercise media's+ // block and its nested cache block) give the same answer.function onDisk(base, uri) {const got = [...render(base).matchAll(REWRITE)].map(([, pattern, to]) => {const m = new RegExp(quoted(pattern)).exec(uri)return m ? to.replace(/\$(\d)/g, (_, i) => m[i]) : uri})- expect(got.length).toBe(3)+ expect(got.length).toBe(5)expect(new Set(got).size).toBe(1)return got[0]}@@ -167,6 +192,7 @@ describe('the file nginx looks for under BASE_PATH', () => {expect(onDisk('/g+ym', '/g+ym/icon-180.png')).toBe('/icon-180.png')expect(onDisk('/gym', '/gym/icon-180.png')).toBe('/icon-180.png')expect(onDisk('/a/b', '/a/b/index.html')).toBe('/index.html')+ expect(onDisk('/gym', '/gym/exercise-media/clip/0001.mp4')).toBe('/exercise-media/clip/0001.mp4')})it('reads BASE_PATH as text, not as a pattern', () => {diff --git a/web/nginx.conf.template b/web/nginx.conf.templateindex bb4c487fd..696b9c243 100644--- a/web/nginx.conf.template+++ b/web/nginx.conf.template@@ -142,7 +142,31 @@ server {add_header Content-Security-Policy "frame-ancestors 'none'" always;}- # Exercise stills and animations never change → cache hard.+ # The exercise stills and animations, licensed from Gym visual for openGym only (NOTICE.md).+ # Cross-Origin-Resource-Policy keeps another site from showing them in its own pages: the+ # browser refuses a cross-origin <img> or <video> of them. The app loads them same-origin+ # (the mobile app ships its own copy), so nothing of openGym's is affected. `^~` so this+ # prefix beats the image regex below; that one's caching is repeated in the nested block,+ # and so are the headers, since an add_header replaces the inherited set (see the top).+ location ^~ ${BASE_PATH}/exercise-media/ {+ rewrite ^\Q${BASE_PATH}\E/(.*)$ /$1 break;+ add_header Cross-Origin-Resource-Policy "same-origin" always;+ 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;+ location ~* \.(png|jpg|jpeg|gif|webp|avif|mp4)$ {+ rewrite ^\Q${BASE_PATH}\E/(.*)$ /$1 break;+ add_header Cache-Control "public, max-age=2592000, immutable";+ add_header Cross-Origin-Resource-Policy "same-origin" always;+ 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;+ }+ }++ # Images and fonts never change → cache hard.# `expires` would add a second Cache-Control of its own (plus an Expires header) on top of the# explicit one below, so the header is set once, here, where `immutable` can be part of it.location ~* \.(png|jpg|jpeg|gif|ico|svg|woff|woff2|webp|avif|mp4)$ {