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
Audit log: a refused proof of ownership is logged as the change it guarded, not as a failed password sign-in
Duarte Santos18234a9cb909
diff --git a/api/server.js b/api/server.jsindex 2b09f209c..25bda9e65 100644--- a/api/server.js+++ b/api/server.js@@ -745,6 +745,8 @@ function audit(req, ev, f = {}) {}if (f.target) { rec.tgt = f.target.id; rec.tname = String(f.target.name || '').slice(0, 40); }if (f.msg) rec.msg = String(f.msg).slice(0, 120);+ // What a failed proof of ownership was guarding (proveOwner): 'email', 'passkey-add', …+ if (f.act) rec.act = String(f.act).slice(0, 40);const ip = clientIp(req);if (ip) rec.ip = ip;try { fs.appendFileSync(auditFile, JSON.stringify(rec) + '\n'); }@@ -1001,9 +1003,10 @@ function passwordAttempt(req, res, k) {catch (e) { settle(); throw e; }},// The audit line names the account only when there is one — what someone typed into the- // name field is sometimes their password.- failed(user, msg, unknown = 'unknown-name') {- audit(req, 'auth.password.fail', user ? { ok: false, user, msg } : { ok: false, msg: unknown });+ // name field is sometimes their password. `ev`/`act` are for a password that was not a+ // sign-in at all: the current one, proving a change (proveOwner) is logged as that change.+ failed(user, msg, unknown = 'unknown-name', { ev = 'auth.password.fail', act } = {}) {+ audit(req, ev, user ? { ok: false, user, msg, act } : { ok: false, msg: unknown, act });if (byName.lock) audit(req, 'auth.password.locked', user ? { ok: false, user } : { ok: false, msg: unknown });if (byAddr.lock) audit(req, 'auth.throttled', { ok: false, msg: 'password' });},@@ -1029,10 +1032,13 @@ function passwordAttempt(req, res, k) {// passwords off. A profile that only ever had a password is moved onto a passkey while the flag// is still on (docs/SELF_HOSTING.md). Answers the refusal itself and returns null, or says which// proof it was ('passkey' | 'password'). Wrong passwords count toward the sign-in pause.-async function proveOwner(req, res, user, body, failEv) {+// `act` names the change being confirmed ('email', 'password-set', 'passkey-remove', …): a failed+// proof is logged as `auth.proof.fail` for that change, not as a failed sign-in — the owner was+// signed in all along, and "Password sign-in failed" sent an admin looking for the wrong thing.+async function proveOwner(req, res, user, body, act) {if (body.credential) {if (await passkeyStepUp(user, body)) return 'passkey';- audit(req, failEv, { ok: false, user, msg: 'step-up-failed' });+ audit(req, 'auth.proof.fail', { ok: false, user, msg: 'step-up-failed', act });json(res, 403, { error: 'the passkey could not be verified', code: 'passkey' });return null;}@@ -1050,7 +1056,7 @@ async function proveOwner(req, res, user, body, failEv) {if (!check) return null;const rec = user.pw;if (!(await check.verify(current, rec.h))) {- check.failed(user, 'bad-current');+ check.failed(user, 'bad-current', undefined, { ev: 'auth.proof.fail', act });return wrongCurrent();}// Changed or removed while it was being checked: it is no longer the current password.@@ -1065,7 +1071,7 @@ if (PASSWORD_LOGIN) warmUp();// setting one; a profile without an address gets 200 with none asked.async function removeEmail(req, res, user, body) {if (!user.email) return json(res, 200, { ok: true, email: null });- const proof = await proveOwner(req, res, user, body, 'auth.email.fail');+ const proof = await proveOwner(req, res, user, body, 'email-remove');if (!proof) return;if (readSession(req) !== user) return json(res, 401, { error: 'not signed in' });delete user.email;@@ -1192,7 +1198,7 @@ const passwordRoutes = {if (problem) return policyError(res, problem);const taken = () => json(res, 409, { error: 'another profile already signs in with this name', code: 'name-taken' });if (nameTaken(user.name, user.id)) return taken();- const proof = await proveOwner(req, res, user, body, 'auth.password.fail');+ const proof = await proveOwner(req, res, user, body, 'password');if (!proof) return;const h = await hashPassword(body.next);// Everything above awaited: an admin reset, a disable or "sign out everywhere" may have ended@@ -1222,7 +1228,7 @@ const passwordRoutes = {const lastWayIn = () => json(res, 409, { error: 'the password is the only way into this profile', code: 'last-way-in' });if (!hasPassword(user)) return json(res, 200, { ok: true });if (!passkeyCount(user)) return lastWayIn();- const proof = await proveOwner(req, res, user, body, 'auth.password.fail');+ const proof = await proveOwner(req, res, user, body, 'password-remove');if (!proof) return;// Everything above awaited: the session may have ended, and the profile's last passkey may// have been removed by a request that ran alongside this one.@@ -1258,7 +1264,7 @@ const passwordRoutes = {return wait > 0 || addressPaused(req, res, 'email');};if (paused()) return;- const proof = await proveOwner(req, res, user, body, 'auth.email.fail');+ const proof = await proveOwner(req, res, user, body, 'email');if (!proof) return;if (sessionOf(req)?.user !== user) return json(res, 401, { error: 'not signed in' });if (paused()) return;@@ -1434,7 +1440,7 @@ const passkeyRoutes = {if (!user) return notSignedIn(res);const body = await readBody(req);if (passkeyCount(user) >= MAX_PASSKEYS) return json(res, 409, LIMIT);- const proof = await proveOwner(req, res, user, body, 'auth.passkey.fail');+ const proof = await proveOwner(req, res, user, body, 'passkey-add');if (!proof) return;// Awaited: a sign-out everywhere, a disable or an admin reset may have ended this session.if (readSession(req) !== user) return notSignedIn(res);@@ -1491,7 +1497,7 @@ const passkeyRoutes = {const refuse = r => json(res, r.code === 'last-way-in' ? 409 : 404, { error: r.error, code: r.code });const refused = passkeyRemovalRefused(db, user.id, id, passwordWayIn(user) ? 1 : 0);if (refused) return refuse(refused);- const proof = await proveOwner(req, res, user, body, 'auth.passkey.fail');+ const proof = await proveOwner(req, res, user, body, 'passkey-remove');if (!proof) return;if (readSession(req) !== user) return notSignedIn(res);if (proof === 'passkey') saveDb(); // the confirming passkey's counter and last use@@ -1510,7 +1516,7 @@ const passkeyRoutes = {if (!user) return notSignedIn(res);const body = await readBody(req);if (passkeyCount(user) >= MAX_PASSKEYS) return json(res, 409, LIMIT);- const proof = await proveOwner(req, res, user, body, 'auth.link.fail');+ const proof = await proveOwner(req, res, user, body, 'device-link');if (!proof) return;if (readSession(req) !== user) return notSignedIn(res);const { code, link } = createDeviceLink(db, user.id);diff --git a/api/test/server-passkeys.test.js b/api/test/server-passkeys.test.jsindex d6170111a..cee5f1552 100644--- a/api/test/server-passkeys.test.js+++ b/api/test/server-passkeys.test.js@@ -186,7 +186,7 @@ test('another profile’s passkey, or a challenge minted for another profile, adconst wrong = await h.req('POST', '/api/account/passkeys/options', { body: await h.stepUp(ana, ip), cookie: mintSession('u2'), ip });assert.equal(wrong.status, 403);assert.equal(wrong.body.code, 'passkey');- assert.ok(h.audit().some(e => e.ev === 'auth.passkey.fail' && e.uid === 'u2' && e.msg === 'step-up-failed'));+ assert.ok(h.audit().some(e => e.ev === 'auth.proof.fail' && e.act === 'passkey-add' && e.uid === 'u2' && e.msg === 'step-up-failed'));// Ana's ceremony finished under Bea's session.const opt = await h.req('POST', '/api/account/passkeys/options', { body: await h.stepUp(ana, ip), cookie: mintSession('u1'), ip });const hijack = await h.req('POST', '/api/account/passkeys/verify', {@@ -227,7 +227,9 @@ test('the current password proves an addition while the instance offers passwordconst wrong = await h.req('POST', '/api/account/passkeys/options', { body: { current: 'not it at all' }, cookie, ip });assert.equal(wrong.status, 403);assert.equal(wrong.body.code, 'current-wrong');- assert.ok(h.audit().some(e => e.ev === 'auth.password.fail' && e.msg === 'bad-current'));+ // Logged as the change it guarded, not as a failed sign-in.+ assert.ok(h.audit().some(e => e.ev === 'auth.proof.fail' && e.act === 'passkey-add' && e.msg === 'bad-current'));+ assert.ok(!h.audit().some(e => e.ev === 'auth.password.fail'));const opt = await h.req('POST', '/api/account/passkeys/options', { body: { current: GOOD }, cookie, ip });assert.equal(opt.status, 200);assert.deepEqual(opt.body.options.excludeCredentials, []);@@ -253,7 +255,7 @@ test('with PASSWORD_LOGIN off only a passkey proves anything; a stored password}}}- assert.ok(!h.audit().some(e => e.ev === 'auth.password.fail' || e.ev === 'auth.password.locked'));+ assert.ok(!h.audit().some(e => e.ev === 'auth.password.fail' || e.ev === 'auth.proof.fail' || e.ev === 'auth.password.locked'));assert.equal(h.db().creds.length, 2);assert.deepEqual(h.db().deviceLinks || [], []);// A profile with only a password cannot confirm anything at all.@@ -342,7 +344,7 @@ test('removing a passkey needs proof made for it: none, another profile’s passawait refused({ current: GOOD }, 'passkey-required');// Bea's passkey says nothing about Ana.await refused(await h.stepUp(bea, ip), 'passkey');- assert.ok(h.audit().some(e => e.ev === 'auth.passkey.fail' && e.uid === 'u1' && e.msg === 'step-up-failed'));+ assert.ok(h.audit().some(e => e.ev === 'auth.proof.fail' && e.act === 'passkey-remove' && e.uid === 'u1' && e.msg === 'step-up-failed'));// Challenges handed out for another ceremony, signed as if they were a sign-in's: adding a// passkey in Settings, a sign-up, redeeming a device link.const add = (await h.req('POST', '/api/account/passkeys/options', { body: await h.stepUp(b, ip), cookie, ip })).body;@@ -392,7 +394,7 @@ test('wrong current passwords given to remove a passkey count toward the sign-infor (let i = 0; i < 7; i++) answers.push(await h.req('DELETE', url, { body: { current: 'wrong password ' + i }, cookie, ip }));assert.deepEqual(answers.map(r => r.status), [403, 403, 403, 403, 403, 403, 429]);assert.equal(answers[0].body.code, 'current-wrong');- assert.ok(h.audit().some(e => e.ev === 'auth.password.fail' && e.uid === 'u1' && e.msg === 'bad-current'));+ assert.ok(h.audit().some(e => e.ev === 'auth.proof.fail' && e.act === 'passkey-remove' && e.uid === 'u1' && e.msg === 'bad-current'));assert.ok(h.audit().some(e => e.ev === 'auth.password.locked' && e.uid === 'u1'));// The name is paused: the right password waits too, from anywhere, and so does a sign-in.assert.equal((await h.req('DELETE', url, { body: { current: GOOD }, cookie, ip: '198.51.100.33' })).status, 429);diff --git a/api/test/server-password.test.js b/api/test/server-password.test.jsindex b08270d9c..23e7c6962 100644--- a/api/test/server-password.test.js+++ b/api/test/server-password.test.js@@ -458,10 +458,10 @@ test('removing the password needs proof made for it: the password itself or a paawait refused(undefined, 'current-required');await refused({}, 'current-required');await refused({ current: 'not the password at all' }, 'current-wrong');- assert.ok(h.audit().some(e => e.ev === 'auth.password.fail' && e.uid === 'u1' && e.msg === 'bad-current'));+ assert.ok(h.audit().some(e => e.ev === 'auth.proof.fail' && e.act === 'password-remove' && e.uid === 'u1' && e.msg === 'bad-current'));// Bea's passkey, and an assertion over some other challenge than the one handed out.await refused(await stepUp(other), 'passkey');- assert.ok(h.audit().some(e => e.ev === 'auth.password.fail' && e.uid === 'u1' && e.msg === 'step-up-failed'));+ assert.ok(h.audit().some(e => e.ev === 'auth.proof.fail' && e.act === 'password-remove' && e.uid === 'u1' && e.msg === 'step-up-failed'));await refused(await stepUp(key, b64u(crypto.randomBytes(32))), 'passkey');// A sign-up's challenge, signed as if it were a sign-in's.const reg = (await h.req('POST', '/api/register/options', { body: { name: 'Mallory' }, ip })).body;diff --git a/frontend/src/lib/audit.js b/frontend/src/lib/audit.jsindex 5f8432b30..1d5bbeec9 100644--- a/frontend/src/lib/audit.js+++ b/frontend/src/lib/audit.js@@ -49,6 +49,8 @@ const LABELS = {'auth.link.create': 'Made a one-time code for another device','auth.link.ok': 'Added a device with a one-time code','auth.link.fail': 'Adding a device with a code failed',+ // The owner's proof (current password or a passkey) for one of the changes below was refused.+ 'auth.proof.fail': 'Confirming a change failed',// The password throttle paused an address; `msg` says for what (password, signup).'auth.throttled': 'Too many failed attempts from one address','admin.user.disable': 'Disabled an account',@@ -81,7 +83,7 @@ const REASONS = {'code-invalid': 'wrong or expired pairing code','user-unavailable': 'the profile behind the code is disabled or gone','bad-password': 'wrong password',- 'bad-current': 'wrong current password while changing it',+ 'bad-current': 'wrong current password','unknown-name': 'no profile with a password has that name','unknown-email': 'no profile with a password has that e-mail','email-taken': 'another profile already uses that e-mail',@@ -100,6 +102,18 @@ const REASONS = {}export const auditReason = msg => REASONS[msg] || (msg ? String(msg) : '')+// What an `auth.proof.fail` was confirming (the server's `act`).+const ACTS = {+ 'email': 'changing the sign-in e-mail',+ 'email-remove': 'removing the sign-in e-mail',+ 'password': 'setting or changing the password',+ 'password-remove': 'removing the password',+ 'passkey-add': 'adding a passkey',+ 'passkey-remove': 'removing a passkey',+ 'device-link': 'making a one-time code for another device'+}+export const auditAct = act => ACTS[act] || (act ? String(act) : '')+// → { title, sub }. `sub` is the house "a · b · c" metadata line used by every list row.export function auditLine(e) {if (!e) return { title: '', sub: '' }@@ -108,6 +122,7 @@ export function auditLine(e) {else if (e.uid) parts.push(e.uid)else if (!e.ok) parts.push('unknown caller')if (e.tname) parts.push('→ ' + e.tname)+ if (e.act) parts.push(auditAct(e.act))// The reason codes and the invite codes share the msg field; only failures read as a reason.if (e.msg) parts.push(e.ok ? e.msg : auditReason(e.msg))if (e.ip) parts.push(e.ip)diff --git a/frontend/src/lib/audit.test.js b/frontend/src/lib/audit.test.jsindex e3923e4e4..2b5f602fe 100644--- a/frontend/src/lib/audit.test.js+++ b/frontend/src/lib/audit.test.js@@ -1,6 +1,6 @@import { describe, it, expect } from 'vitest'import { readFileSync } from 'node:fs'-import { auditCat, auditLabel, auditReason, auditLine, fmtWhen } from './audit.js'+import { auditCat, auditLabel, auditReason, auditLine, auditAct, fmtWhen } from './audit.js'// Every event name and reason code the server can emit, read off api/server.js itself rather// than kept as a second list here: the hand-kept copy silently missed the three pairing events@@ -164,3 +164,20 @@ describe('fmtWhen', () => {expect(fmtWhen(undefined)).toBe('')})})++// QA 1.3.9: a wrong current password while changing the e-mail or adding a passkey was logged as+// "Password sign-in failed · wrong current password while changing it". A failed proof is its+// own event now and names the change it was guarding.+describe('a failed proof of ownership', () => {+ const ACTS = [...new Set([...SERVER.matchAll(/proveOwner\(req, res, user, body, '([a-z-]+)'\)/g)].map(m => m[1]))]+ it('names every change the server asks a proof for', () => {+ expect(ACTS.length).toBeGreaterThanOrEqual(7)+ for (const act of ACTS) expect(auditAct(act), act).not.toBe(act)+ })+ it('reads as the change, not as a sign-in', () => {+ expect(auditLine({ ev: 'auth.proof.fail', ok: false, name: 'Ana', act: 'email', msg: 'bad-current' }))+ .toEqual({ title: 'Confirming a change failed', sub: 'Ana · changing the sign-in e-mail · wrong current password' })+ expect(auditLine({ ev: 'auth.proof.fail', ok: false, name: 'Ana', act: 'passkey-remove', msg: 'step-up-failed' }).sub)+ .toBe('Ana · removing a passkey · the passkey confirming the change was rejected')+ })+})