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: the API base can no longer leave the origin (GHSA-375x-wfvj-jf7q)
Boris 'B' Kurktchiev0494ff666a25
diff --git a/frontend/src/lib/api-base.test.js b/frontend/src/lib/api-base.test.jsindex c9403b7f8..070575a51 100644--- a/frontend/src/lib/api-base.test.js+++ b/frontend/src/lib/api-base.test.js@@ -3,7 +3,7 @@// already relative; the API call was not, so it went to the proxy's own root where nothing// answers it. The base is read from where the app is being served.import { describe, it, expect, vi, afterEach } from 'vitest'-import { appBase, beacon } from './api.js'+import { api, appBase, beacon } from './api.js'import { readFileSync } from 'node:fs'import { spawnSync } from 'node:child_process'import { dirname, join } from 'node:path'@@ -271,3 +271,68 @@ describe('the web container refuses a BASE_PATH it cannot serve', () => {expect(dockerfile).toMatch(/^COPY web\/nginx\.conf\.template \/etc\/nginx\/templates\/default\.conf\.template$/m)})})++// A path that begins with two slashes is still served the real app on the real origin: nginx+// merges the slashes and resolves an encoded "..%2F" before it looks for a file, so+// https://gym.example//evil.example/..%2F/ is the shell, with its scripts. Taken as-is, that+// path is a protocol-relative URL, and every API call, the passkey sign-in included, went to+// evil.example.+const ORIGIN = 'https://gym.test'+const lands = base => new URL(base.replace(/\/$/, '') + '/api/login/verify', ORIGIN).origin++describe('the base never leaves the page\'s origin', () => {+ it.each([+ ['//evil.example/', '/evil.example/'],+ ['//evil.example//', '/evil.example/'],+ ['///evil.example/', '/evil.example/'],+ ['//evil.example/..%2F/', '/evil.example/..%2F/'],+ ['/gym//evil.example/', '/gym/evil.example/'],+ ['//', '/'],+ ['///', '/'],+ ['//index.html', '/'],+ ['/\\evil.example/', '/evil.example/'],+ ])('%s is the path %s on this origin', (pathname, base) => {+ expect(at(pathname)).toBe(base)+ expect(lands(at(pathname))).toBe(ORIGIN)+ })++ it.each([+ ['/', '/'],+ ['/gym/', '/gym/'],+ ['/gym/index.html', '/gym/'],+ ])('an ordinary %s is unchanged', (pathname, base) => {+ expect(at(pathname)).toBe(base)+ expect(lands(at(pathname))).toBe(ORIGIN)+ })++ it('a base the URL parser would still read as another host is refused, not used', () => {+ // A real location never has a raw tab in its path (the parser drops it on the way in), which+ // is exactly why the last word goes to the parser rather than to the slash rule alone.+ expect(() => at('/\t/evil.example/')).toThrow(/another site/)+ })+})++// The same pages through api() itself. Each is the real shell on the stock web image: "//",+// "///" and "//index.html" because nginx merges the slashes, "//evil.example/..%2F/" because it+// also resolves the encoded "..". Read as-is, the first three sent every call to https://api/...+// and the last one to evil.example.+describe('api() on a page whose path starts with two slashes', () => {+ const home = location.href+ afterEach(() => { vi.unstubAllGlobals(); window.happyDOM.setURL(home) })++ it.each([+ ['//', '/api/login/options'],+ ['///', '/api/login/options'],+ ['//index.html', '/api/login/options'],+ ['//evil.example/..%2F/', '/evil.example/..%2F/api/login/options'],+ ])('a page at %s still calls this origin, never the host its path names', async (path, expected) => {+ window.happyDOM.setURL(ORIGIN + path)+ const fetchMock = vi.fn(async () => new Response('{}', { status: 200, headers: { 'content-type': 'application/json' } }))+ vi.stubGlobal('fetch', fetchMock)+ await api('/api/login/options', { method: 'POST', body: '{}' })+ expect(fetchMock).toHaveBeenCalledTimes(1)+ const url = fetchMock.mock.calls[0][0]+ expect(new URL(url, location.href).origin).toBe(ORIGIN)+ expect(url).toBe(expected)+ })+})diff --git a/frontend/src/lib/app-base.js b/frontend/src/lib/app-base.jsindex 38a622142..b747e0cc1 100644--- a/frontend/src/lib/app-base.js+++ b/frontend/src/lib/app-base.js@@ -11,10 +11,22 @@* (web/nginx.conf.template). Anything after the last slash is therefore index.html or a stale* deep link, and is dropped.*+ * Runs of slashes collapse to one; backslashes go with them, although an http(s) location never+ * has one (the parser has already made it a slash). nginx merges slashes and resolves an encoded+ * "..%2F" before it looks for a file, so it serves the real app at+ * https://gym.example//evil.example/..%2F/, scripts and all. Taken as-is, that path starts with+ * "//", which is a URL to another host, and every API call, the passkey sign-in included, went+ * to evil.example. A base that starts with exactly one slash can only ever name a path on this+ * origin. The URL parser gets the last word all the same: a base it would still read as another+ * host is refused rather than used.+ ** Its own module, not api.js, because the store needs it too (the server a signed-in browser* syncs with) and many tests replace api.js wholesale.*/+const PROBE = 'http://base.invalid'export function appBase(loc = typeof location !== 'undefined' ? location : null) {- const path = (loc && loc.pathname) || '/'- return path.slice(0, path.lastIndexOf('/') + 1) || '/'+ const path = ((loc && loc.pathname) || '/').replace(/[\\/]+/g, '/')+ const base = path.slice(0, path.lastIndexOf('/') + 1) || '/'+ if (new URL(base, PROBE).origin !== PROBE) throw new Error('Refusing an API base that points at another site: ' + JSON.stringify(base))+ return base}