Repository navigation
feat: warn when the account is set up on another device #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Home-screen warning raised by services/key-watch.ts: another device published | ||
| // keys for this account. Shown on this device only, until answered; nothing about | ||
| // it (or the answer) is ever sent anywhere. | ||
| import { useEffect, useState } from "react"; | ||
| import { dismissKeyAlert, hasKeyAlert } from "../services/key-watch"; | ||
| import { onKeyAlertChanged } from "../services/events"; | ||
| import { useAuth } from "../store/auth"; | ||
| import { WarningTriangleIcon } from "./icons"; | ||
|
|
||
| export default function KeyAlert() { | ||
| const userId = useAuth((s) => s.userId); | ||
| const [shown, setShown] = useState(false); | ||
| const [notMe, setNotMe] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| if (!userId) { | ||
| setShown(false); | ||
| return; | ||
| } | ||
| const refresh = () => void hasKeyAlert(userId).then(setShown); | ||
| refresh(); | ||
| return onKeyAlertChanged(refresh); | ||
| }, [userId]); | ||
|
|
||
| if (!shown || !userId) return null; | ||
|
|
||
| const btn = "rounded-lg px-3 py-1.5 text-xs font-medium transition-colors"; | ||
| return ( | ||
| <div role="alert" className="mb-4 rounded-xl border border-danger bg-danger-subtle p-3 text-sm"> | ||
| <p className="flex items-center gap-1.5 font-medium text-danger"> | ||
| <WarningTriangleIcon className="h-4 w-4 shrink-0" /> | ||
| Your account was set up on another device | ||
| </p> | ||
| <p className="mt-1 text-xs text-text-secondary"> | ||
| Another device published new keys for your account. That happens when someone restores it | ||
| with your recovery phrase or password. | ||
| </p> | ||
| {!notMe ? ( | ||
| <> | ||
| <p className="mt-1 text-xs text-text-secondary"> | ||
| If it was you — a new phone or browser, or “Log out everywhere” on another of | ||
| your devices — tap “That was me”. | ||
| </p> | ||
| <div className="mt-2 flex gap-2"> | ||
| <button onClick={() => void dismissKeyAlert(userId)} className={`${btn} bg-raised text-text-secondary hover:bg-border-strong`}> | ||
| That was me | ||
| </button> | ||
| <button onClick={() => setNotMe(true)} className={`${btn} border border-danger text-danger hover:bg-danger-subtle`}> | ||
| It wasn’t me | ||
| </button> | ||
| </div> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <p className="mt-2 text-xs text-text-secondary"> | ||
| Then someone has your recovery phrase or password. They hold your account’s keys: | ||
| they can receive messages sent to you and message people as you, and that can’t be | ||
| undone for this account. | ||
| </p> | ||
| <ul className="mt-1 list-disc space-y-0.5 pl-4 text-xs text-text-secondary"> | ||
| <li>Warn your contacts in person or by call — not in Privex.</li> | ||
| <li>When you’re ready, move to a new account and share its ID with them the same way.</li> | ||
| <li>If chat backup is on, turn it off in Settings → Recovery. That deletes it from our servers.</li> | ||
| </ul> | ||
| <button onClick={() => void dismissKeyAlert(userId)} className={`${btn} mt-2 bg-raised text-text-secondary hover:bg-border-strong`}> | ||
| Hide this warning | ||
| </button> | ||
| </> | ||
| )} | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // "Your account was set up on another device" (docs 8.2 self-check, without any | ||
| // new request). Sign-in and WS-ticket replies carry the account's current signed | ||
| // prekey. Every recovery publishes a new one, and this device saves each of its | ||
| // own before publishing it - so a key this device never made, signed by this | ||
| // account's identity, means another device holding the account's keys published | ||
| // it. It all happens here: nothing is sent, and the server never learns whether | ||
| // an alert was raised or what the user answered. | ||
| // | ||
| // A lying server can hide a real change (by serving our own key back) but can't | ||
| // fake one: the key must carry this identity's hybrid signature, and a replay of | ||
| // an old key of ours is caught by KNOWN - every key this device has held, plus | ||
| // any the user said were theirs. Public keys only, kept on this device. | ||
| import { db } from "../db"; | ||
| import type { AccountSpk } from "../api/client"; | ||
| import { loadBundle } from "../onboarding/store"; | ||
| import { fromHex, toHex, type IdentityBundle } from "../crypto/onboarding-crypto"; | ||
| import { cryptoCall } from "../workers/crypto-client"; | ||
| import { emitKeyAlertChanged } from "./events"; | ||
|
|
||
| // Per account, so an alert or trusted key never carries over to a different | ||
| // account restored on this device. | ||
| const knownKey = (userId: string) => `known_spks:${userId}`; // hex SPK pubs: ours, or ones the user said were theirs | ||
| const alertKey = (userId: string) => `key_alert:${userId}`; // the unanswered foreign key (hex) | ||
|
|
||
| export interface KeyWatchDeps { | ||
| loadBundle(): Promise<IdentityBundle | undefined>; | ||
| verifyHybrid( | ||
| data: Uint8Array, | ||
| sigEd: Uint8Array, | ||
| edPub: Uint8Array, | ||
| sigDil: Uint8Array, | ||
| dilPub: Uint8Array, | ||
| ): Promise<boolean>; | ||
| } | ||
|
|
||
| const workerDeps: KeyWatchDeps = { | ||
| loadBundle: () => loadBundle(), | ||
| verifyHybrid: (...a) => cryptoCall<boolean>("verify_hybrid", a), | ||
| }; | ||
|
|
||
| async function knownKeys(userId: string): Promise<Set<string>> { | ||
| return new Set((await db.settings.get(knownKey(userId)))?.value as string[] | undefined); | ||
| } | ||
|
|
||
| /** Check a reply's account key. Best effort: never throws, and does nothing on a | ||
| * reply from a server that predates the field. Call it only after this device | ||
| * has published its keys (not mid-recovery: the server still has the old ones). */ | ||
| export async function checkAccountKeys( | ||
| reply: AccountSpk | undefined, | ||
| deps: KeyWatchDeps = workerDeps, | ||
| ): Promise<void> { | ||
| if (!reply) return; | ||
| try { | ||
| // Loaded AFTER the reply: our keys are saved before they're published, so this | ||
| // copy is never older than what the server could have from us. | ||
| const me = await deps.loadBundle(); | ||
| if (!me) return; | ||
| const known = await knownKeys(me.userId); | ||
| const mine = [me.spk, ...(me.prevSpks ?? [])].map((k) => toHex(k.pub)); | ||
| if (mine.some((k) => !known.has(k))) { | ||
| mine.forEach((k) => known.add(k)); | ||
| await db.settings.put({ key: knownKey(me.userId), value: [...known] }); | ||
| } | ||
|
|
||
| const spk = reply.spk_x25519.toLowerCase(); | ||
| if (known.has(spk)) return; | ||
| const signedByUs = await deps.verifyHybrid( | ||
| fromHex(spk), | ||
| fromHex(reply.spk_sig_ed), | ||
| me.identity.ed25519_pub, | ||
| fromHex(reply.spk_sig_dil), | ||
| me.identity.dilithium3_pub, | ||
| ); | ||
| // Not signed by this account: a broken or lying server, not another device | ||
| // with our keys - nothing to warn about. | ||
| if (!signedByUs) return; | ||
| if ((await db.settings.get(alertKey(me.userId)))?.value === spk) return; | ||
| await db.settings.put({ key: alertKey(me.userId), value: spk }); | ||
| emitKeyAlertChanged(); | ||
| } catch { | ||
| // Malformed reply / storage hiccup: the next reply checks again. | ||
| } | ||
| } | ||
|
|
||
| export async function hasKeyAlert(userId: string): Promise<boolean> { | ||
| return typeof (await db.settings.get(alertKey(userId)))?.value === "string"; | ||
| } | ||
|
|
||
| /** "That was me" (or read and dismissed): remember that key as the user's and | ||
| * clear the alert. A different new key alerts again. */ | ||
| export async function dismissKeyAlert(userId: string): Promise<void> { | ||
| const spk = (await db.settings.get(alertKey(userId)))?.value; | ||
| if (typeof spk === "string") { | ||
| const known = await knownKeys(userId); | ||
| known.add(spk); | ||
| await db.settings.put({ key: knownKey(userId), value: [...known] }); | ||
| } | ||
| await db.settings.delete(alertKey(userId)); | ||
| emitKeyAlertChanged(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.