From bf5811326f62d4e2c3764596b1e521258ad6bf41 Mon Sep 17 00:00:00 2001 From: sonix aka privex Date: Sun, 27 Sep 2026 17:06:07 +0530 Subject: [PATCH 1/2] fix(web): recovery saves the identity only after the server accepts it completeRecovery saved the recovered identity (with onboarding progress "keys") BEFORE signing in. A phrase that passes its checksum but belongs to no account - one mistyped word that happens to be another valid word - failed at sign-in with "api 401", but its identity stayed saved. Pressing Back or reloading then took onboarding to its register step, which starts on its own and registered the mistyped phrase as a brand-new, empty account. Now it signs in first and saves only once the server accepts the keys (still before publishing any of them). The seed tab also explains a refused sign-in: no account uses these words - check them, and the device clock (a clock off by over 5 minutes is refused the same way; the server never says which). Co-Authored-By: Claude Opus 5.5 --- apps/web/src/screens/Recovery.tsx | 12 +++++++++- apps/web/src/services/recovery.ts | 18 +++++++++------ apps/web/src/test/recovery-prekeys.test.ts | 27 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/apps/web/src/screens/Recovery.tsx b/apps/web/src/screens/Recovery.tsx index b3a1644..2494eb6 100644 --- a/apps/web/src/screens/Recovery.tsx +++ b/apps/web/src/screens/Recovery.tsx @@ -168,8 +168,18 @@ function PasswordRecovery({ busy, onRun }: { busy: boolean; onRun: RunFn }) { ); } -/** bip39's errors count words from 0 and read like a stack trace. */ +/** bip39's errors count words from 0 and read like a stack trace, and a refused + * sign-in only says "api 401". */ function friendlySeedError(e: unknown): Error { + // A valid phrase that no account uses (e.g. one mistyped word that's another + // real word and still passes the checksum). The server answers every failed + // sign-in the same way - it never says whether an account exists - and a + // device clock off by over 5 minutes fails it too. + if (e instanceof api.ApiError && e.status === 401) { + return new Error( + "No Privex account uses these words. Check each one against your saved copy, and that this device's clock is right.", + ); + } const msg = e instanceof Error ? e.message : String(e); const unknown = /unknown word \(word (\d+)\)/.exec(msg); if (unknown) { diff --git a/apps/web/src/services/recovery.ts b/apps/web/src/services/recovery.ts index 4f35205..33c8349 100644 --- a/apps/web/src/services/recovery.ts +++ b/apps/web/src/services/recovery.ts @@ -79,16 +79,20 @@ async function provisionPrekeys(bundle: IdentityBundle, token: string): Promise< ); } -/** Shared tail: persist the recovered identity, obtain a token (OPAQUE provides - * one; seed recovery signs a challenge), re-provision prekeys, enter the app. */ +/** Shared tail: prove the account (OPAQUE provides a token; seed and contact + * recovery sign a challenge), THEN save the identity, re-provision prekeys and + * enter the app. Nothing is saved until the server accepts these keys: a phrase + * that passes its checksum but belongs to no account (a typo that happens to be + * another valid word) must leave no identity behind - onboarding would pick it up + * and register it as a brand-new, empty account. */ async function completeRecovery(bundle: IdentityBundle, tokenOpt?: string): Promise { - // provisionPrekeys publishes this bundle's signed prekey as brand new, so its - // first rotation is a full period away (unset = "rotate now" to prekey upkeep). - bundle.spkRotateAfter = nextSpkRotation(Math.floor(Date.now() / 1000)); - await persistGeneratedIdentity(bundle); // No key check here: the server still has the account's old signed prekey until // provisionPrekeys below publishes ours. const token = tokenOpt ?? (await authenticateBundle(bundle)).session_token; + // provisionPrekeys publishes this bundle's signed prekey as brand new, so its + // first rotation is a full period away (unset = "rotate now" to prekey upkeep). + bundle.spkRotateAfter = nextSpkRotation(Math.floor(Date.now() / 1000)); + await persistGeneratedIdentity(bundle); // saved before any of its keys are published useAuth.getState().setSession(token, bundle.userId); await provisionPrekeys(bundle, token); await finalizeIdentity(bundle); @@ -374,7 +378,7 @@ export async function pollContactRecovery( const seed = await unsealAndReconstruct(session.rk.priv, blobs, collected, crypto); if (!seed) return { userId: null, posted: blobs.length }; const bundle = await crypto.recoverBundleFromSeed(seed); - await completeRecovery(bundle); // persists identity, auths, re-provisions prekeys + await completeRecovery(bundle); // auths, then saves the identity and re-provisions prekeys return { userId: bundle.userId, posted: blobs.length }; } diff --git a/apps/web/src/test/recovery-prekeys.test.ts b/apps/web/src/test/recovery-prekeys.test.ts index f6646e5..dd61f62 100644 --- a/apps/web/src/test/recovery-prekeys.test.ts +++ b/apps/web/src/test/recovery-prekeys.test.ts @@ -33,6 +33,7 @@ vi.mock("../workers/crypto-client", async () => { import { initCrypto, wasm } from "../crypto/wasm"; import { genIdentityBundle, recoverBundleFromSeed } from "../crypto/onboarding-crypto"; import { recoverWithSeed, type RecoveryCryptoApi } from "../services/recovery"; +import { loadProgress } from "../onboarding/store"; import * as api from "../api/client"; import { db } from "../db"; @@ -70,3 +71,29 @@ describe("seed recovery re-provisions prekeys", () => { expect(opks.length).toBeGreaterThan(0); }); }); + +// A phrase that passes its checksum but belongs to no account (one mistyped word +// that's another valid word) must leave nothing behind: a saved identity with +// onboarding progress would be registered as a brand-new account by onboarding. +describe("seed recovery for a phrase with no account", () => { + it("saves no identity and no progress, and publishes nothing", async () => { + await db.identity.clear(); + await db.settings.clear(); + const stranger = genIdentityBundle(wasm, new Uint8Array(32).fill(0x3d)); // never registered + const seedCrypto = { + seedToMasterSeed: async () => stranger.masterSeed, + recoverBundleFromSeed: async (s: Uint8Array) => recoverBundleFromSeed(wasm, s), + } as unknown as RecoveryCryptoApi; + + vi.spyOn(api, "authChallenge").mockResolvedValue({ challenge: "00".repeat(32), expires_at: 0 }); + vi.spyOn(api, "authVerify").mockRejectedValue(new api.ApiError(401)); // unknown account + const rotate = vi.spyOn(api, "spkRotate"); + const replenish = vi.spyOn(api, "replenishPrekeys"); + + await expect(recoverWithSeed(stranger.mnemonic, seedCrypto)).rejects.toMatchObject({ status: 401 }); + expect(await db.identity.count()).toBe(0); + expect((await loadProgress()).step).toBe("welcome"); + expect(rotate).not.toHaveBeenCalled(); + expect(replenish).not.toHaveBeenCalled(); + }); +}); From 04284000b8308adcea7a1473eeaa1264a9a41e0b Mon Sep 17 00:00:00 2001 From: sonix aka privex Date: Sun, 27 Sep 2026 19:04:00 +0530 Subject: [PATCH 2/2] fix(web): don't claim no account uses the phrase on a refused sign-in A refused sign-in can also mean the device clock is over 5 minutes off, and the server answers both the same way. Name both causes without asserting either. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/screens/Recovery.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/web/src/screens/Recovery.tsx b/apps/web/src/screens/Recovery.tsx index 2494eb6..6287257 100644 --- a/apps/web/src/screens/Recovery.tsx +++ b/apps/web/src/screens/Recovery.tsx @@ -171,13 +171,13 @@ function PasswordRecovery({ busy, onRun }: { busy: boolean; onRun: RunFn }) { /** bip39's errors count words from 0 and read like a stack trace, and a refused * sign-in only says "api 401". */ function friendlySeedError(e: unknown): Error { - // A valid phrase that no account uses (e.g. one mistyped word that's another - // real word and still passes the checksum). The server answers every failed - // sign-in the same way - it never says whether an account exists - and a - // device clock off by over 5 minutes fails it too. + // Either a valid phrase that no account uses (e.g. one mistyped word that's + // another real word and still passes the checksum), or a device clock off by + // over 5 minutes. The server answers every failed sign-in the same way - it + // never says whether an account exists - so name both, assert neither. if (e instanceof api.ApiError && e.status === 401) { return new Error( - "No Privex account uses these words. Check each one against your saved copy, and that this device's clock is right.", + "Couldn't sign in with these words. Either they don't match a Privex account - check each one against your saved copy - or this device's clock is more than 5 minutes off.", ); } const msg = e instanceof Error ? e.message : String(e);