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.

API: removing a passkey also drops the profile's unused device code, so a code made with a lost passkey adds nothing afterwards (#95)

Duarte Santos7a393f61c506
diff --git a/SECURITY.md b/SECURITY.md
index 343fc00be..67b9595a6 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -213,8 +213,8 @@ Read this before hosting openGym for anyone other than yourself.
reads a code off the screen within its ten minutes can add a passkey to that profile; the owner
sees it in Settings → Passkeys and in the activity log (`auth.link.ok`) and can remove it.
Sessions are `uid:expiry:version` and are not tied to the passkey that opened them, so removing
- a passkey stops it signing in but leaves any session it opened running; "sign out everywhere"
- ends those.
+ a passkey stops it signing in and drops an unused device code, but leaves any session it
+ opened running; "sign out everywhere" ends those.
- **A password is weaker than a passkey, and the throttle is per process.** It can be phished,
reused elsewhere or guessed; a stolen `db.json` allows offline guessing against the scrypt
hashes. The throttle's counters live in memory: a restart clears them, and several API replicas
diff --git a/api/openapi.yaml b/api/openapi.yaml
index aa2918f4f..fa905f42a 100644
--- a/api/openapi.yaml
+++ b/api/openapi.yaml
@@ -896,7 +896,9 @@ paths:
description: |
Refused while it is the profile's last way in: its only passkey, and no password
that can sign in (a password counts only while `PASSWORD_LOGIN` is on). No proof is
- asked — removing takes a way in away. Audited as `auth.passkey.remove`.
+ asked — removing takes a way in away. Audited as `auth.passkey.remove`. An unused
+ device code of the profile is dropped too, so one made with the removed passkey
+ cannot add another afterwards.
Sessions are not tied to the passkey that opened them, so a session it already
opened carries on; `POST /api/logout/all` ends those.
diff --git a/api/server.js b/api/server.js
index b703aef35..645ec29fa 100644
--- a/api/server.js
+++ b/api/server.js
@@ -1306,13 +1306,16 @@ const passkeyRoutes = {
},
// `?id=` — the credential id from the list. No proof: removing takes a way in away rather than
- // adding one, and the last one is never removed (removePasskeyRecord).
+ // adding one, and the last one is never removed (removePasskeyRecord). An unused device code
+ // goes with it: a passkey is removed because it is lost or not trusted, and a code made with it
+ // just before — by whoever holds it — would otherwise add a fresh one right after.
'DELETE /api/account/passkeys': async (req, res) => {
const user = readSession(req);
if (!user) return notSignedIn(res);
const id = new URL(req.url, 'http://x').searchParams.get('id') || '';
const r = removePasskeyRecord(db, user.id, id, passwordWayIn(user) ? 1 : 0);
if (r.error) return json(res, r.code === 'last-way-in' ? 409 : 404, { error: r.error, code: r.code });
+ dropDeviceLinks(db, user.id);
saveDb();
audit(req, 'auth.passkey.remove', { user, msg: r.row.name || null });
json(res, 200, { ok: true, ...passkeyState(user) });
diff --git a/api/test/server-passkeys.test.js b/api/test/server-passkeys.test.js
index d2247f4c5..e633225ee 100644
--- a/api/test/server-passkeys.test.js
+++ b/api/test/server-passkeys.test.js
@@ -398,6 +398,27 @@ test('an expired link, a replaced link and a link of a disabled profile are all
assert.ok(h.audit().some(e => e.ev === 'auth.link.fail' && e.msg === 'user-unavailable' && e.uid === 'u2'));
});
+test('removing a passkey drops an unused code, so one made with it just before adds nothing afterwards', async t => {
+ const key = softPasskey(), stolen = softPasskey(), thief = softPasskey();
+ const h = await startServer(t, { users: [user('u1', 'Ana')], creds: [key.row('u1'), stolen.row('u1', { name: 'Old phone' })] });
+ const ip = '198.51.100.54';
+ // Whoever holds the lost phone makes a code with its passkey…
+ const { code } = (await makeLink(h, stolen, 'u1', ip)).body;
+ assert.equal(h.db().deviceLinks.length, 1);
+ // …the owner removes that passkey…
+ assert.equal((await h.req('DELETE', '/api/account/passkeys?id=' + encodeURIComponent(stolen.id), { cookie: mintSession('u1'), ip })).status, 200);
+ assert.deepEqual(h.db().deviceLinks, []);
+ // …and the code no longer adds a new one.
+ const r = await redeem(h, code, thief, '203.0.113.54');
+ assert.equal(r.status, 400);
+ assert.equal(r.body.code, 'link-invalid');
+ assert.deepEqual(h.db().creds.map(c => c.id), [key.id]);
+ // A refused removal leaves the owner's own code alone.
+ const mine = (await makeLink(h, key, 'u1', ip)).body.code;
+ assert.equal((await h.req('DELETE', '/api/account/passkeys?id=' + encodeURIComponent(key.id), { cookie: mintSession('u1'), ip })).status, 409);
+ assert.equal((await h.req('POST', '/api/device-link/options', { body: { code: mine }, ip })).status, 200);
+});
+
test('a newer link replaces the older one, and signing out everywhere or a new password drops it', async t => {
const key = softPasskey();
const h = await startServer(t, { users: [withPassword('u1', 'Ana')], creds: [key.row('u1')] });