Skip to content

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.

Pairing: one live code per profile, and a ceiling on open passkey challenges

Duarte Santosf7de845ebb35
diff --git a/api/openapi.yaml b/api/openapi.yaml
index 87191718e..5ca2dbb73 100644
--- a/api/openapi.yaml
+++ b/api/openapi.yaml
@@ -549,7 +549,8 @@ paths:
description: |
Called from an already signed-in browser tab (Settings → "Pair the mobile
app"). Returns an 8-character code (alphabet without 0/O/1/I) the phone
- redeems within 5 minutes via /api/pair/redeem. One-shot.
+ redeems within 5 minutes via /api/pair/redeem. One-shot. One live code per
+ profile: minting a new one retires the previous one.
responses:
'200':
description: A fresh pairing code.
diff --git a/api/server.js b/api/server.js
index 98e33786a..8df301ccf 100644
--- a/api/server.js
+++ b/api/server.js
@@ -603,7 +603,12 @@ function csrfOk(req, key) {
// 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}
+// POST /api/login/options is anonymous and unthrottled, and every call leaves a challenge here
+// for five minutes, so the Map has a ceiling: past it the oldest goes. A ceremony takes seconds,
+// so only a flood far beyond any real sign-in traffic reaches back far enough to cost one.
+const MAX_CHALLENGES = 20000;
function putChallenge(data) {
+ while (challenges.size >= MAX_CHALLENGES) challenges.delete(challenges.keys().next().value);
const cid = crypto.randomBytes(16).toString('base64url');
challenges.set(cid, { ...data, exp: Date.now() + 5 * 60000 });
return cid;
@@ -1993,6 +1998,9 @@ const routes = {
'POST /api/pair/create': async (req, res) => {
const user = readSession(req);
if (!user) return json(res, 401, { error: 'not signed in' });
+ // One live code per profile: the newest is the one on screen. Keeping every code minted
+ // let one session grow this Map without limit for five minutes at a time.
+ for (const [k, v] of pairings) if (v.uid === user.id) pairings.delete(k);
const code = makePairCode();
pairings.set(code, { uid: user.id, exp: Date.now() + 5 * 60000 });
audit(req, 'auth.pair.create', { user });
diff --git a/api/test/server-anon-flood.test.js b/api/test/server-anon-flood.test.js
index 3f246f8ee..ce97fe729 100644
--- a/api/test/server-anon-flood.test.js
+++ b/api/test/server-anon-flood.test.js
@@ -71,3 +71,13 @@ test('junk passkey sign-ins and sign-ups are answered but not each written to th
assert.equal(rows.filter(r => r.ev === 'auth.login.fail').length, 10);
assert.equal(rows.filter(r => r.ev === 'auth.register.fail').length, 10);
});
+
+test('minting a pairing code retires the profile\'s previous one', async t => {
+ const h = await startServer(t);
+ const mint = async () => (await (await h.post('/api/pair/create', {}, { ...cookie, Origin: 'http://localhost:8080' })).json()).code;
+ const first = await mint();
+ const second = await mint();
+ assert.notEqual(first, second);
+ assert.equal((await h.post('/api/pair/redeem', { code: first })).status, 400, 'the older code is gone');
+ assert.equal((await h.post('/api/pair/redeem', { code: second })).status, 200, 'the newest one works');
+});