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 device-link or Settings challenge no longer finishes a sign-up, so a code cannot add passkeys over and over without being used up (#95)
Duarte Santos703c5f667d70
diff --git a/api/server.js b/api/server.jsindex cdcfde306..5d9cd3c2c 100644--- a/api/server.js+++ b/api/server.js@@ -542,7 +542,14 @@ function csrfOk(req, key) {}/* ---------- challenge store (in-memory, 5 min TTL) ---------- */-const challenges = new Map(); // cid -> {challenge, name?, uid?, exp}+// Every challenge says which ceremony it was handed out for (`kind`), and every route that takes+// one back accepts only its own kind. Four ceremonies share this store — signing up, signing in,+// adding a passkey in Settings and redeeming a device link (#95) — and the last two carry a `uid`+// exactly like a sign-up's. When /api/register/verify asked only for a uid, the challenge that+// POST /api/device-link/options hands to anyone holding a code finished a *sign-up* instead: a+// passkey on the owner's profile and a session for it, without using the code up, as often as+// wanted, and past "sign out everywhere".+const challenges = new Map(); // cid -> {kind, challenge, name?, uid?, exp}function putChallenge(data) {const cid = crypto.randomBytes(16).toString('base64url');challenges.set(cid, { ...data, exp: Date.now() + 5 * 60000 });@@ -882,7 +889,7 @@ function setPassword(user, h) {// to belong to this account.async function passkeyStepUp(user, body) {const c = takeChallenge(body.cid);- const cred = c && db.creds.find(x => x.id === body.credential?.id && x.userId === user.id);+ const cred = c?.kind === 'login' && db.creds.find(x => x.id === body.credential?.id && x.userId === user.id);if (!cred) return false;try {const v = await verifyAuthenticationResponse({@@ -1458,14 +1465,14 @@ const routes = {authenticatorSelection: { residentKey: 'required', userVerification: 'preferred' },excludeCredentials: []});- const cid = putChallenge({ challenge: options.challenge, name, uid, code });+ const cid = putChallenge({ kind: 'register', challenge: options.challenge, name, uid, code });json(res, 200, { cid, options });},'POST /api/register/verify': async (req, res) => {const body = await readBody(req);const c = takeChallenge(body.cid);- if (!c || !c.uid) {+ if (!c || c.kind !== 'register' || !c.uid) {audit(req, 'auth.register.fail', { ok: false, msg: 'challenge-expired' });return json(res, 400, { error: 'challenge expired — try again' });}@@ -1521,14 +1528,14 @@ const routes = {const options = await generateAuthenticationOptions({rpID: RP_ID, userVerification: 'preferred', allowCredentials: []});- const cid = putChallenge({ challenge: options.challenge });+ const cid = putChallenge({ kind: 'login', challenge: options.challenge });json(res, 200, { cid, options });},'POST /api/login/verify': async (req, res) => {const body = await readBody(req);const c = takeChallenge(body.cid);- if (!c) {+ if (c?.kind !== 'login') {audit(req, 'auth.login.fail', { ok: false, msg: 'challenge-expired' });return json(res, 400, { error: 'challenge expired — try again' });}diff --git a/api/test/server-passkeys.test.js b/api/test/server-passkeys.test.jsindex 7cacec876..198fa0fa1 100644--- a/api/test/server-passkeys.test.js+++ b/api/test/server-passkeys.test.js@@ -512,3 +512,77 @@ test('link redemption keeps the origin check; the account routes do too', async// The refused requests used nothing up.assert.equal((await h.req('POST', '/api/device-link/options', { body: { code }, ip })).status, 200);});++test('a Settings or device-link challenge never finishes a sign-up, a sign-in or a proof — and the code stays good once', async t => {+ const key = softPasskey(), thief = softPasskey(), phone = softPasskey(), newcomer = softPasskey();+ const h = await startServer(t, { users: [user('u1', 'Ana')], creds: [key.row('u1')] });+ const ip = '198.51.100.53', other = '203.0.113.53';+ const { code } = (await makeLink(h, key, 'u1', ip)).body;+ const linkOptions = async () => (await h.req('POST', '/api/device-link/options', { body: { code }, ip: other })).body;+ const unchanged = () => {+ const db = h.db();+ assert.deepEqual(db.users.map(u => u.id), ['u1']);+ assert.deepEqual(db.creds.map(c => c.id), [key.id]);+ assert.equal(db.deviceLinks.length, 1);+ };++ // Someone who read the code sends the link's challenge to sign-up instead of to the link route.+ for (let i = 0; i < 2; i++) {+ const opt = await linkOptions();+ const r = await h.req('POST', '/api/register/verify', { body: { cid: opt.cid, credential: thief.attestation(opt.options.challenge) }, ip: other });+ assert.equal(r.status, 400);+ assert.equal(r.cookie, null);+ unchanged();+ }+ assert.ok(h.audit().some(e => e.ev === 'auth.register.fail' && e.msg === 'challenge-expired'));+ assert.ok(!h.audit().some(e => e.ev === 'auth.register.ok'));++ // A Settings ceremony's challenge is no sign-up either.+ const add = (await h.req('POST', '/api/account/passkeys/options', { body: await h.stepUp(key, ip), cookie: mintSession('u1'), ip })).body;+ const viaAdd = await h.req('POST', '/api/register/verify', { body: { cid: add.cid, credential: thief.attestation(add.options.challenge) }, ip });+ assert.equal(viaAdd.status, 400);+ assert.equal(viaAdd.cookie, null);+ unchanged();++ // Nor is a link's challenge a sign-in, or the proof that makes another code.+ const asLogin = await linkOptions();+ const signIn = await h.req('POST', '/api/login/verify', { body: { cid: asLogin.cid, credential: key.assertion(asLogin.options.challenge) }, ip: other });+ assert.equal(signIn.status, 400);+ assert.equal(signIn.cookie, null);+ const asProof = await linkOptions();+ const proof = await h.req('POST', '/api/account/device-link', { body: { cid: asProof.cid, credential: key.assertion(asProof.options.challenge) }, cookie: mintSession('u1'), ip });+ assert.equal(proof.status, 403);+ assert.equal(proof.body.code, 'passkey');+ // …and a sign-up's challenge proves nothing.+ const reg = (await h.req('POST', '/api/register/options', { body: { name: 'Mallory' }, ip })).body;+ const regProof = await h.req('POST', '/api/account/device-link', { body: { cid: reg.cid, credential: key.assertion(reg.options.challenge) }, cookie: mintSession('u1'), ip });+ assert.equal(regProof.status, 403);+ unchanged();++ // A link challenge fetched before "sign out everywhere" finishes nothing afterwards.+ const early = await linkOptions();+ const early2 = await linkOptions();+ assert.equal((await h.req('POST', '/api/logout/all', { body: {}, cookie: mintSession('u1'), ip })).status, 200);+ const late = await h.req('POST', '/api/register/verify', { body: { cid: early.cid, credential: thief.attestation(early.options.challenge) }, ip: other });+ assert.equal(late.status, 400);+ const late2 = await h.req('POST', '/api/device-link/verify', { body: { code, cid: early2.cid, credential: thief.attestation(early2.options.challenge) }, ip: other });+ assert.equal(late2.status, 400);+ assert.deepEqual(h.db().users.map(u => u.id), ['u1']);+ assert.deepEqual(h.db().creds.map(c => c.id), [key.id]);++ // The owner's next code is redeemed exactly once, by the link route.+ const next = (await h.req('POST', '/api/account/device-link', { body: await h.stepUp(key, ip), cookie: mintSession('u1', 1), ip })).body.code;+ const done = await redeem(h, next, phone, ip, 'Phone');+ assert.equal(done.status, 200);+ assert.equal(done.body.user.id, 'u1');+ assert.equal((await redeem(h, next, softPasskey(), ip)).status, 400);+ assert.deepEqual(h.db().creds.map(c => [c.id, c.userId]), [[key.id, 'u1'], [phone.id, 'u1']]);++ // Sign-up with its own challenge still works, and makes one new profile.+ const signUp = (await h.req('POST', '/api/register/options', { body: { name: 'Cleo' }, ip })).body;+ const made = await h.req('POST', '/api/register/verify', { body: { cid: signUp.cid, credential: newcomer.attestation(signUp.options.challenge) }, ip });+ assert.equal(made.status, 200, JSON.stringify(made.body));+ assert.equal(made.body.user.name, 'Cleo');+ assert.ok(made.cookie);+ assert.equal(h.db().users.length, 2);+});