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
API: a pending reset code keeps its profile's name, and redeeming it checks the name again after hashing (#118)
Duarte Santos24505a67d464
diff --git a/api/openapi.yaml b/api/openapi.yamlindex 45b6f3793..049548559 100644--- a/api/openapi.yaml+++ b/api/openapi.yaml@@ -1531,7 +1531,9 @@ components:schema: { $ref: '#/components/schemas/Error' }example: { error: "this password is too easy to guess", code: "too-common" }NameTaken:- description: Another profile already signs in with this name.+ description: >-+ Another profile already signs in with this name, or holds it with a reset code+ that has not been used or expired yet.content:application/json:schema: { $ref: '#/components/schemas/Error' }diff --git a/api/server.js b/api/server.jsindex 70aaf473d..81fdf8fc2 100644--- a/api/server.js+++ b/api/server.js@@ -800,9 +800,18 @@ function strikeAddress(req, kind) {// Profile names were never unique, so the rule is narrower than that: no two profiles *with a// password* may share a name. Setting one is refused while the name is taken that way, which is// also re-checked after every await, where another request could have taken it meanwhile.+//+// An unused reset code holds its profile's name too. The reset has already removed the old+// password, and a name another profile took in the meantime would make the code impossible to+// redeem — there is no rename — and leave that profile locked out. Sign-in itself only ever+// looks at profiles that have a password (passwordHolder).const hasPassword = u => !!(u && u.pw && typeof u.pw.h === 'string');const passwordHolder = k => db.users.find(u => hasPassword(u) && nameKey(u.name) === k) || null;-const nameTaken = (name, exceptId) => { const u = passwordHolder(nameKey(name)); return !!u && u.id !== exceptId; };+const holdsName = u => hasPassword(u) || !!(u.pwReset && u.pwReset.exp > Date.now());+const nameTaken = (name, exceptId) => {+ const k = nameKey(name);+ return db.users.some(u => u.id !== exceptId && holdsName(u) && nameKey(u.name) === k);+};const passkeyCount = u => db.creds.filter(c => c.userId === u.id).length;const publicUser = u => ({ id: u.id, name: u.name, admin: isAdmin(u) });const POLICY_ERRORS = {@@ -1072,6 +1081,7 @@ const passwordRoutes = {const find = () => db.users.find(u => u.pwReset && nameKey(u.name) === k && resetCodeMatches(code, u.pwReset)) || null;const user = find();const invalid = () => json(res, 400, { error: 'that reset code is wrong or has expired', code: 'reset-invalid' });+ const taken = () => json(res, 409, { error: 'another profile already signs in with this name', code: 'name-taken' });if (!user) {strikeAddress(req, 'password');audit(req, 'auth.password.reset', { ok: false, msg: 'reset-invalid' });@@ -1084,10 +1094,11 @@ const passwordRoutes = {// The code is good and stays good until a password is actually set with it.const problem = passwordProblem(body.next, user.name);if (problem) return policyError(res, problem);- if (nameTaken(user.name, user.id)) return json(res, 409, { error: 'another profile already signs in with this name', code: 'name-taken' });+ if (nameTaken(user.name, user.id)) return taken();const h = await hashPassword(body.next);// Single use: a second request that raced this one through the hash finds the code gone.if (find() !== user) return invalid();+ if (nameTaken(user.name, user.id)) return taken();setPassword(user, h);ACCOUNT_FAILS.clear(k);saveDb();diff --git a/api/test/server-password.test.js b/api/test/server-password.test.jsindex d753451eb..316990452 100644--- a/api/test/server-password.test.js+++ b/api/test/server-password.test.js@@ -485,6 +485,59 @@ test('an admin reset: a one-time code that ends the old password, works once, anassert.ok(h.audit().some(e => e.ev === 'auth.password.reset' && !e.ok));});+// The reset removes the old password at once, so for the day the code is valid the profile has+// none — and a name only counted as taken by a password would be free for anyone to take, leaving+// the code unredeemable (there is no rename) and the profile locked out.+test('a pending reset keeps the name: nobody can take it until the code is used or expires', async t => {+ const key = softPasskey();+ const h = await startServer(t, {+ env: { ADMIN_UIDS: 'adm' },+ users: [+ user('adm', 'Root'), withPassword('u1', 'Ana'), user('u2', 'ANA '), user('u3', 'Cleo'), user('u4', 'Dora'),+ user('u5', 'Cleo', { pwReset: { h: hashResetCode('AAAA-BBBB-CCCC'), exp: Date.now() - 1000, by: 'adm' } })+ ],+ creds: [key.row('u2')]+ });+ const admin = `gymsid=${mintSession('adm')}`;+ const ip = '198.51.100.240';+ const { code } = (await h.req('POST', '/api/admin/user/password-reset', { body: { id: 'u1' }, cookie: admin, ip })).body;++ // Registering the name, and another profile of that name setting a first password, are refused.+ const squat = await h.req('POST', '/api/register/password', { body: { name: 'ana', password: GOOD }, ip });+ assert.equal(squat.status, 409);+ assert.equal(squat.body.code, 'name-taken');+ const other = `gymsid=${mintSession('u2')}`;+ assert.equal((await h.req('GET', '/api/account/password', { cookie: other, ip })).body.nameTaken, true);+ const { cid, options } = (await h.req('POST', '/api/login/options', { body: {}, ip })).body;+ const first = await h.req('POST', '/api/account/password', { body: { next: GOOD, cid, credential: key.assertion(options.challenge) }, cookie: other, ip });+ assert.equal(first.status, 409);+ // Nor can a second reset hand the name to the other profile.+ assert.equal((await h.req('POST', '/api/admin/user/password-reset', { body: { id: 'u2' }, cookie: admin, ip })).status, 409);++ // The code still works, and the name is Ana's again.+ const r = await h.req('POST', '/api/login/password-reset', { body: { name: 'Ana', code, next: 'a brand new passphrase' }, ip });+ assert.equal(r.status, 200);+ assert.equal((await login(h, 'ana', 'a brand new passphrase', ip)).status, 200);+ assert.equal(h.db().users.filter(u => u.pw && u.name.trim().toLowerCase() === 'ana').length, 1);++ // An expired code holds nothing: that name is free.+ assert.equal((await h.req('POST', '/api/register/password', { body: { name: 'cleo', password: GOOD }, ip })).status, 200);+});++test('registering a name while its reset code is redeemed leaves one password holder, the reset profile', async t => {+ const h = await startServer(t, { env: { ADMIN_UIDS: 'adm' }, users: [user('adm', 'Root'), withPassword('u1', 'Bob')] });+ const { code } = (await h.req('POST', '/api/admin/user/password-reset', { body: { id: 'u1' }, cookie: `gymsid=${mintSession('adm')}`, ip: '198.51.100.241' })).body;+ const [reg, redeem] = await Promise.all([+ h.req('POST', '/api/register/password', { body: { name: 'BOB', password: GOOD }, ip: '198.51.100.242' }),+ h.req('POST', '/api/login/password-reset', { body: { name: 'bob', code, next: 'a brand new passphrase' }, ip: '198.51.100.243' })+ ]);+ assert.equal(redeem.status, 200);+ assert.equal(reg.status, 409);+ const holders = h.db().users.filter(u => u.pw && u.name.toLowerCase() === 'bob');+ assert.deepEqual(holders.map(u => u.id), ['u1']);+ assert.equal((await login(h, 'Bob', 'a brand new passphrase', '198.51.100.244')).status, 200);+});+// A 60-bit code that lives a day needs no per-name pause, and one would let anyone who knows the// name keep the real code refused for that whole day. Wrong codes count against the address only.test('wrong reset codes for a name do not lock out its real code, and do not touch the password count', async t => {diff --git a/docs/SELF_HOSTING.md b/docs/SELF_HOSTING.mdindex 5428cb24c..87f708c40 100644--- a/docs/SELF_HOSTING.md+++ b/docs/SELF_HOSTING.md@@ -236,6 +236,8 @@ a LAN-only address, see [SELF_HOSTING_HTTPS.md](./SELF_HOSTING_HTTPS.md).activity log shows it (`auth.password.locked`), and passkeys still work. A reset code is notpaused per name — it is 60 random bits and lives a day — so nobody can keep a real code fromworking by sending wrong ones.+- A pending reset code keeps its profile's name: until it is used or expires, nobody else can+ register that name with a password or set a first password on another profile of that name.- Passwords need 10 to 256 characters and may not be one of a short built-in list of thepasswords guessing scripts try first (`Password123!`, `qwerty…`, the profile's own name withdigits). A long passphrase is the point.