From 74939c517b51a08e88106e10b93aafc9988cdb98 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 2 Dec 2026 13:45:51 -0700 Subject: users: unlink an anon account's pubkey only after the signature checks out Updating your pubkey to one held by an anonymous account deleted that account's user: pointer before the signed challenge was verified, so a request with a bad signature still detached the key from its account. Do the unlink after verification succeeds. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VMan6SSfVopM1ZzXKZmLYU --- routes/users.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/routes/users.ts b/routes/users.ts index cfa41b8b..51eb14be 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -412,10 +412,10 @@ export default { const existingUsername = exists?.username ?.toLowerCase() .replace(/\s/g, ""); + const takeAnon = exists && un !== existingUsername && exists.anon; if (exists && un !== existingUsername) { warn("key in use", pubkey, username, existingUsername); - if (exists.anon) await db.del(`user:${pubkey}`); - else fail("Key in use by another account"); + if (!exists.anon) fail("Key in use by another account"); } const event = JSON.parse(body.event); @@ -433,6 +433,9 @@ export default { pubkey = pubkey.replace(/\s*/g, ""); if (pubkey.length !== 64) fail(`Invalid pubkey ${pubkey}`); + // Only unlink an anon account's key once the caller has proven they + // hold it (signed challenge above). + if (takeAnon) await db.del(`user:${pubkey}`); await db.del(`user:${user.pubkey}`); user.pubkey = pubkey; user.nsec = undefined; -- cgit v1.2.3