diff --git a/apps/web/src/screens/Recovery.tsx b/apps/web/src/screens/Recovery.tsx index b3a1644..6287257 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 { + // 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( + "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); 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(); + }); +});