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
Password signup: whether an e-mail is in use is answered only after the hash and the second invite check
Duarte Santos71461302584c
diff --git a/SECURITY.md b/SECURITY.mdindex ecb13db45..277d6cdca 100644--- a/SECURITY.md+++ b/SECURITY.md@@ -145,9 +145,13 @@ Read this before hosting openGym for anyone other than yourself.the same proof as a password (`proveOwner`). It is folded like a name (NFKC, trimmed,lower-cased; at most 254 characters), unique across profiles and never another passwordholder's name, so an identifier never points at two accounts. Taking one in use answers `409- email-taken` — only after the proof in Settings, and each refusal (Settings or signup) counts- against the caller's address and account like a wrong invite code, so probing which addresses- are registered costs a passkey prompt or a password check per try and runs out after about 20.+ email-taken`. In Settings that answer comes only after the owner's proof (a passkey prompt or+ the current password per try). Password signup needs no session, so there it is the cheaper+ question: it comes after the new password has been hashed and, with `INVITE_ONLY=1`, only to+ someone holding a valid unused invite code (which a refusal does not use up); on an open+ instance anyone can ask. Each refusal, in Settings or on signup, counts against the caller's+ address (and in Settings the account) like a wrong invite code, so probing which addresses are+ registered runs into a pause after about 20 tries.The full address is never logged or audited (masked to `a…@e…`) and is returned only to itsowner (`GET /api/account/password`) and to admins (`GET /api/admin/users`, `/api/admin/user`);it is not in `/api/me`, Coach payloads, the MCP bridge or plan sharing.diff --git a/api/openapi.yaml b/api/openapi.yamlindex b2061af97..643010c39 100644--- a/api/openapi.yaml+++ b/api/openapi.yaml@@ -675,7 +675,9 @@ paths:again after hashing, and burned. The password must be 10–256 characters and not oneof the passwords guessing scripts try first; no two profiles with a password mayshare a name. An optional `email` is stored as the profile's sign-in e-mail (see- `POST /api/account/email`); one that is in use is refused with `409 email-taken`,+ `POST /api/account/email`); one that is not an address is refused with `400+ email-invalid`, one that is in use with `409 email-taken` — asked only after the+ hash and the second invite check, so without a valid code it is never answered —which counts against the caller's address like a wrong invite code. **Not**CSRF-exempt.security: []@@ -705,7 +707,17 @@ paths:type: objectproperties:user: { $ref: '#/components/schemas/SessionUser' }- '400': { $ref: '#/components/responses/PasswordPolicy' }+ '400':+ description: >-+ The password breaks the policy — `too-short` (under 10 characters), `too-long`+ (over 256) or `too-common` — a field is `missing`, or `email-invalid`: the+ `email` given is not an e-mail address.+ content:+ application/json:+ schema: { $ref: '#/components/schemas/Error' }+ examples:+ policy: { value: { error: "this password is too easy to guess", code: "too-common" } }+ email: { value: { error: "that is not an e-mail address", code: "email-invalid" } }'403':description: Invite-only and the code is missing, used or revoked.content:diff --git a/api/server.js b/api/server.jsindex 8ec25003d..2528a2988 100644--- a/api/server.js+++ b/api/server.js@@ -890,10 +890,13 @@ const nameTaken = (name, exceptId) => {is in use answers 409. The route could be made to hide it — accept the address silently andleave it pointing nowhere — but then someone who typed their own address on a second profilewould be told it was saved and find it did not sign in, which is worse. So it answers, and the- answer is made expensive instead: only a signed-in owner can ask (session + proveOwner, a- passkey prompt or a checked password per try), an address already in use counts against the- caller's address and account (20 free, then 30 s doubling to 15 min), and profile creation with- an address counts the same way. What that leaves is what the name-taken answer already gives+ answer is made expensive instead: in Settings only a signed-in owner can ask (session ++ proveOwner, a passkey prompt or a checked password per try), an address already in use counts+ against the caller's address and account (20 free, then 30 s doubling to 15 min), and profile+ creation with a password and an address counts the same way against the caller's address.+ Signup needs no session, so it is the cheaper place to ask: it answers only after hashing the+ new password and, on an invite-only instance, only to someone holding a valid invite code+ (a refusal does not use it up); an open instance answers anyone. What that leaves is what the name-taken answer already givesaway for names — "some profile here uses this", never which one — at a few tries an hour.Sign-in takes it in the same field as the name (loginTarget): an identifier with an "@" is@@ -1122,7 +1125,9 @@ const passwordRoutes = {const taken = () => json(res, 409, { error: 'another profile already signs in with this name', code: 'name-taken' });if (nameTaken(name)) return taken();// An e-mail is optional here. One already in use counts against the address, the way it- // does in Settings: this route needs no session, so it is the cheaper place to ask.+ // does in Settings: this route needs no session, so it is the cheaper place to ask. Whether+ // it is in use is only answered after the hash and the invite check that follows it, so on+ // an invite-only instance nobody without a valid code learns anything about addresses.const hasEmail = typeof body.email === 'string' && body.email.trim() !== '';const email = hasEmail ? normalizeEmail(body.email) : null;if (hasEmail && !email) return json(res, 400, EMAIL_ERRORS.invalid);@@ -1132,7 +1137,6 @@ const passwordRoutes = {return json(res, 409, EMAIL_ERRORS.taken);};if (email && addressPaused(req, res, 'email')) return;- if (email && emailTaken(email)) return emailRefused();const h = await hashPassword(body.password);let inv = null;if (INVITE_ONLY) {@@ -1143,6 +1147,7 @@ const passwordRoutes = {}}if (nameTaken(name)) return taken();+ // No await from here to the push: nothing can take the address between this check and it.if (email && emailTaken(email)) return emailRefused();const created = new Date().toISOString();const user = { id: crypto.randomBytes(12).toString('base64url'), name, created, pw: { h, set: created }, ...(email ? { email } : {}) };diff --git a/api/test/openapi-routes.test.js b/api/test/openapi-routes.test.jsindex 4182e07dd..250c83399 100644--- a/api/test/openapi-routes.test.js+++ b/api/test/openapi-routes.test.js@@ -64,3 +64,45 @@ test('every tag the spec uses is one the docs generator renders', () => {const known = [.../const TAGS = \{([\s\S]*?)\n\}/.exec(docs)[1].matchAll(/^\s{2}(\w+):/gm)].map(m => m[1]);for (const tag of used) assert.ok(known.includes(tag), `tag "${tag}" renders nowhere — add it to TAGS in scripts/build-api-docs.mjs`);});++/* The e-mail routes answer with codes a client switches on. /api/register/password answered+ `400 email-invalid` for a while with the spec listing only the password policy under 400. */+test('every error code the e-mail and password signup routes return is in their spec entry', () => {+ const server = read(path.join(API, 'server.js'));+ const handler = key => {+ const start = server.indexOf(` '${key}': async`);+ assert.ok(start >= 0, key);+ const next = server.slice(start + 1).search(/\n {2}'(GET|POST|PUT|DELETE) \//);+ return server.slice(start, next < 0 ? undefined : start + 1 + next);+ };+ const emailCodes = Object.fromEntries([...server.matchAll(/^\s{2}(\w+): \{ error: '[^']*', code: '([^']+)' \}/gm)].map(m => [m[1], m[2]]));+ const operation = (p, method) => {+ const from = spec.indexOf(`\n ${p}:\n`);+ assert.ok(from >= 0, p);+ const rest = spec.slice(from + 1);+ const block = rest.slice(0, rest.slice(1).search(/\n {2}\/|\n[a-zA-Z]/) + 1);+ const op = block.slice(block.indexOf(`\n ${method}:`));+ const end = op.slice(1).search(/\n {4}\w+:/);+ const text = end < 0 ? op : op.slice(0, end + 1);+ // Follow the shared responses it points at.+ const refs = [...text.matchAll(/#\/components\/responses\/(\w+)/g)].map(m => {+ const at = spec.indexOf(`\n ${m[1]}:\n`);+ const tail = spec.slice(at + 1);+ return tail.slice(0, tail.slice(1).search(/\n {4}\w+:/) + 1);+ });+ return [text, ...refs].join('\n');+ };+ for (const [key, p, method] of [+ ['POST /api/register/password', '/api/register/password', 'post'],+ ['POST /api/account/email', '/api/account/email', 'post']+ ]) {+ const src = handler(key);+ const codes = new Set([+ ...[...src.matchAll(/code: '([^']+)'/g)].map(m => m[1]),+ ...[...src.matchAll(/EMAIL_ERRORS\.(\w+)/g)].map(m => emailCodes[m[1]])+ ]);+ assert.ok(codes.size > 1, key);+ const doc = operation(p, method);+ for (const c of codes) assert.ok(doc.includes(c), `${key} returns "${c}" but its openapi entry never mentions it`);+ }+});diff --git a/api/test/server-email.test.js b/api/test/server-email.test.jsindex 04efabb33..55e17b2eb 100644--- a/api/test/server-email.test.js+++ b/api/test/server-email.test.js@@ -299,6 +299,32 @@ test('an address is unique across profiles, case-insensitively, and never anotheassert.equal(leaks(h, 'example.com'), false);});+test('on an invite-only instance signup says nothing about addresses without a valid invite, and a refusal keeps the invite', async t => {+ const now = new Date().toISOString();+ const h = await startServer(t, {+ env: { INVITE_ONLY: '1' },+ users: [withPassword('u1', 'Ana', { email: '[email protected]' })],+ invites: [{ code: 'GOODCODE', created: now }, { code: 'USEDCODE', created: now, usedBy: 'u1' }, { code: 'GONECODE', created: now, revoked: true }]+ });+ // No code, a wrong one, a used one and a revoked one: the answer is about the invite, the+ // same for an address in use as for a free one, and nothing counts against "email".+ for (const [i, code] of ['', 'NOPE', 'USEDCODE', 'GONECODE'].entries()) {+ for (const email of ['[email protected]', '[email protected]']) {+ const r = await h.req('POST', '/api/register/password', { body: { name: 'Dee', password: GOOD, email, code }, ip: `198.51.100.${170 + i}` });+ assert.equal(r.status, 403, `${code} ${email}`);+ assert.equal(r.body.code, 'invite');+ }+ }+ assert.equal(h.audit().some(e => e.msg === 'email-taken'), false);+ // With a valid code the address in use is refused, and the code is still there to use.+ let r = await h.req('POST', '/api/register/password', { body: { name: 'Dee', password: GOOD, email: '[email protected]', code: 'goodcode' }, ip: '198.51.100.180' });+ assert.equal(r.status, 409); assert.equal(r.body.code, 'email-taken');+ assert.equal(h.db().invites.find(i => i.code === 'GOODCODE').usedBy, undefined);+ r = await h.req('POST', '/api/register/password', { body: { name: 'Dee', password: GOOD, email: '[email protected]', code: 'GOODCODE' }, ip: '198.51.100.181' });+ assert.equal(r.status, 200);+ assert.equal(h.db().invites.find(i => i.code === 'GOODCODE').usedBy, h.db().users.find(u => u.name === 'Dee').id);+});+test('asking over and over whether an address is in use runs into a pause', async t => {const h = await startServer(t, { users: [withPassword('u1', 'Ana', { email: '[email protected]' }), withPassword('u2', 'Bea')] });const bea = `gymsid=${mintSession('u2')}`;diff --git a/docs/SELF_HOSTING.md b/docs/SELF_HOSTING.mdindex cbe669a64..53566db54 100644--- a/docs/SELF_HOSTING.md+++ b/docs/SELF_HOSTING.md@@ -243,9 +243,12 @@ to anyone; it is only a second name that points at the account, and the passwordwhich says "some profile on this instance uses this address" — never which one. The alternative,accepting it silently, would leave someone who typed their address on a second profile believingit worked. So the answer is made expensive instead: in Settings it comes only after the proof (a-passkey prompt or a password check per try), and every such refusal — in Settings or on signup —-counts against the visitor's address (and in Settings the account): 20 are free, then a pause of-30 seconds doubling to 15 minutes. That leaves a few tries an hour, comparable to what the+passkey prompt or a password check per try). Signing up with a password needs no proof, so there+the answer costs less: it comes after the new password is hashed and, with `INVITE_ONLY=1`, only+to someone with a valid invite code (a refusal leaves the code unused); on an open instance+anyone can ask. Every such refusal — in Settings or on signup — counts against the visitor's+address (and in Settings the account): 20 are free, then a pause of 30 seconds doubling to 15+minutes. That leaves a few tries an hour, comparable to what thename-taken answer already says about names. The other thing to know: once someone has paused anaccount by wrong passwords under its name, trying a guessed address shows the same pause, whichties that address to that name — the price of not letting a switch to the e-mail skip the pause.