Skip to content

Commit 83a8fc7

Browse files
morgmartMorgaritaclaude
committed
Refuse key recovery over a different device key
Native keeps a key already on this device, so creating or importing over it "succeeded" as that other key. On relaunch Welcome back, pasting the account's saved key silently kept the device key, and Start over unlinked the account's key before binding the wrong one. Create and import now refuse first while a key is ready, before any account step runs. Signed-off-by: morgmart <[email protected]> Co-authored-by: Morgarita <6e967af659416160ab4f2fd56d74c8c6f273791ee7bfc9468a1be37c387947be@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bd1d351 commit 83a8fc7

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎src/features/identity/identity.test.tsx‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7973,6 +7973,54 @@ it("a relaunch sign-in whose account holds a different key rests on Welcome back
79737973
identity.dispose();
79747974
});
79757975

7976+
/** Relaunch sign-in to an account linked elsewhere, resting on Welcome back. */
7977+
async function relaunchWelcomeBack(
7978+
user: ReturnType<typeof userEvent.setup>,
7979+
identity: Identity,
7980+
unbind: FirstRunAccount["unbind"] = noUnlink,
7981+
) {
7982+
localStorage.setItem(ROUTE_NOTE, "builderlab");
7983+
render(
7984+
<Welcome
7985+
identity={identity}
7986+
communities={communitiesFor(identity)}
7987+
invites={invites}
7988+
account={(...args) => ({ ...linkedTo(owned.pubkey)(...args), unbind })}
7989+
connect={vi.fn().mockResolvedValue(credential)}
7990+
>
7991+
<p>Signed in</p>
7992+
</Welcome>,
7993+
);
7994+
await user.click(await screen.findByRole("button", { name: SIGN_IN }));
7995+
await screen.findByRole("heading", { name: "Continue with this key" });
7996+
}
7997+
const OCCUPIED = /already has a different Buzz key/;
7998+
7999+
it("a relaunch Welcome back refuses to import the linked key over the device key", async () => {
8000+
keyOnDevice();
8001+
const identity = createIdentity();
8002+
const user = userEvent.setup();
8003+
await relaunchWelcomeBack(user, identity);
8004+
await importLinkedKey(user);
8005+
expect(await screen.findByText(OCCUPIED)).toBeVisible();
8006+
expect(invoke).not.toHaveBeenCalledWith("identity_import", expect.anything());
8007+
expect(screen.queryByText("Signed in")).toBeNull();
8008+
identity.dispose();
8009+
});
8010+
8011+
it("a relaunch Start over refuses before unlinking the account's key or creating one", async () => {
8012+
keyOnDevice();
8013+
const unbind = vi.fn(noUnlink);
8014+
const identity = createIdentity();
8015+
const user = userEvent.setup();
8016+
await relaunchWelcomeBack(user, identity, unbind);
8017+
await startOver(user);
8018+
expect(await screen.findByText(OCCUPIED)).toBeVisible();
8019+
expect(unbind).not.toHaveBeenCalled();
8020+
expect(invoke).not.toHaveBeenCalledWith("identity_create", {});
8021+
identity.dispose();
8022+
});
8023+
79768024
it("a relaunch sign-in whose account has no key links and backs up the device key instead of making a new one", async () => {
79778025
localStorage.setItem(ROUTE_NOTE, "builderlab");
79788026
keyOnDevice();

‎src/features/identity/setup.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ function saved(identity: Identity) {
7373
);
7474
}
7575

76+
/**
77+
* Native leaves a key already on this device in place, so creating or
78+
* importing over it would "succeed" as that other key. Refuse first, before
79+
* any account step (Start over's unlink) acts on the result.
80+
*/
81+
function vacant(identity: Identity) {
82+
if (identity.snapshot().status === "ready")
83+
throw new Error(
84+
"This Mac already has a different Buzz key. Sign out of Buzz on this Mac first, or use a different account.",
85+
);
86+
}
87+
7688
/**
7789
* The key for an account that has none: old Buzz's, when it was used on this
7890
* device, so someone who used Buzz before stays the same person; otherwise a
@@ -83,6 +95,7 @@ export function keyForAccount(identity: Identity): SetupStep {
8395
return {
8496
label: "Preparing your identity…",
8597
run: async () => {
98+
vacant(identity);
8699
if (await identity.legacyAvailable().catch(() => false)) {
87100
await identity.importLegacy();
88101
if (identity.snapshot().status === "ready") return;
@@ -98,6 +111,7 @@ export function createKey(identity: Identity): SetupStep {
98111
return {
99112
label: "Creating your key…",
100113
run: async () => {
114+
vacant(identity);
101115
await identity.create();
102116
saved(identity);
103117
},
@@ -110,6 +124,7 @@ export function createKey(identity: Identity): SetupStep {
110124
* under the import page's field, not on the failed page.
111125
*/
112126
export async function importKey(identity: Identity, nsec: string) {
127+
vacant(identity);
113128
await identity.importKey(nsec);
114129
saved(identity);
115130
}

0 commit comments

Comments
 (0)