From 559fc4851c76a225080cf3fbcc25ee9900031f34 Mon Sep 17 00:00:00 2001 From: Timo <16718859+toger5@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:59:47 +0200 Subject: [PATCH] Fix rust crypto: dont use legacy crypto store anymore. (#2587) The logic for the legacy store was intercepting with the rustCrypto that handles all the cases itself. --- src/ClientContext.tsx | 14 +++----- src/utils/matrix.ts | 76 ++++++------------------------------------- 2 files changed, 15 insertions(+), 75 deletions(-) diff --git a/src/ClientContext.tsx b/src/ClientContext.tsx index 8516a13d..4235dbcd 100644 --- a/src/ClientContext.tsx +++ b/src/ClientContext.tsx @@ -33,13 +33,10 @@ import { import { logger } from "matrix-js-sdk/src/logger"; import { useTranslation } from "react-i18next"; import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync"; +import { MatrixError } from "matrix-js-sdk"; import { ErrorView } from "./FullScreenView"; -import { - CryptoStoreIntegrityError, - fallbackICEServerAllowed, - initClient, -} from "./utils/matrix"; +import { fallbackICEServerAllowed, initClient } from "./utils/matrix"; import { widget } from "./widget"; import { PosthogAnalytics, @@ -380,16 +377,15 @@ async function loadClient(): Promise { passwordlessUser, }; } catch (err) { - if (err instanceof CryptoStoreIntegrityError) { + if (err instanceof MatrixError && err.errcode === "M_UNKNOWN_TOKEN") { // We can't use this session anymore, so let's log it out try { const client = await initClient(initClientParams, false); // Don't need the crypto store just to log out) await client.logout(true); } catch (err) { logger.warn( - "The previous session was lost, and we couldn't log it out, " + - err + - "either", + "The previous session was unable to login, and we couldn't log it out: " + + err, ); } } diff --git a/src/utils/matrix.ts b/src/utils/matrix.ts index 6955072b..c5fbe4d7 100644 --- a/src/utils/matrix.ts +++ b/src/utils/matrix.ts @@ -16,9 +16,6 @@ limitations under the License. import { IndexedDBStore } from "matrix-js-sdk/src/store/indexeddb"; import { MemoryStore } from "matrix-js-sdk/src/store/memory"; -import { IndexedDBCryptoStore } from "matrix-js-sdk/src/crypto/store/indexeddb-crypto-store"; -import { LocalStorageCryptoStore } from "matrix-js-sdk/src/crypto/store/localStorage-crypto-store"; -import { MemoryCryptoStore } from "matrix-js-sdk/src/crypto/store/memory-crypto-store"; import { createClient, ICreateClientOpts, @@ -41,18 +38,7 @@ import { EncryptionSystem, saveKeyForRoom } from "../e2ee/sharedKeyManagement"; export const fallbackICEServerAllowed = import.meta.env.VITE_FALLBACK_STUN_ALLOWED === "true"; -export class CryptoStoreIntegrityError extends Error { - public constructor() { - super("Crypto store data was expected, but none was found"); - } -} - const SYNC_STORE_NAME = "element-call-sync"; -// Note that the crypto store name has changed from previous versions -// deliberately in order to force a logout for all users due to -// https://github.com/vector-im/element-call/issues/464 -// (It's a good opportunity to make the database names consistent.) -const CRYPTO_STORE_NAME = "element-call-crypto"; async function waitForSync(client: MatrixClient): Promise { // If there is a saved sync, the client will fire an additional sync event @@ -85,9 +71,8 @@ async function waitForSync(client: MatrixClient): Promise { /** * Initialises and returns a new standalone Matrix Client. - * If true is passed for the 'restore' parameter, a check will be made - * to ensure that corresponding crypto data is stored and recovered. - * If the check fails, CryptoStoreIntegrityError will be thrown. + * If false is passed for the 'restore' parameter, corresponding crypto + * data is cleared before the client initialization. * @param clientOptions Object of options passed through to the client * @param restore Whether the session is being restored from storage * @returns The MatrixClient instance @@ -124,45 +109,6 @@ export async function initClient( baseOpts.store = new MemoryStore({ localStorage }); } - // Check whether we have crypto data store. If we are restoring a session - // from storage then we will have started the crypto store and therefore - // have generated keys for that device, so if we can't recover those keys, - // we must not continue or we'll generate new keys and anyone who saw our - // previous keys will not accept our new key. - // It's worth mentioning here that if support for indexeddb or localstorage - // appears or disappears between sessions (it happens) then the failure mode - // here will be that we'll try a different store, not find crypto data and - // fail to restore the session. An alternative would be to continue using - // whatever we were using before, but that could be confusing since you could - // enable indexeddb and but the app would still not be using it. - if (restore) { - if (indexedDB) { - const cryptoStoreExists = await IndexedDBCryptoStore.exists( - indexedDB, - CRYPTO_STORE_NAME, - ); - if (!cryptoStoreExists) throw new CryptoStoreIntegrityError(); - } else if (localStorage) { - if (!LocalStorageCryptoStore.exists(localStorage)) - throw new CryptoStoreIntegrityError(); - } else { - // if we get here then we're using the memory store, which cannot - // possibly have remembered a session, so it's an error. - throw new CryptoStoreIntegrityError(); - } - } - - if (indexedDB) { - baseOpts.cryptoStore = new IndexedDBCryptoStore( - indexedDB, - CRYPTO_STORE_NAME, - ); - } else if (localStorage) { - baseOpts.cryptoStore = new LocalStorageCryptoStore(localStorage); - } else { - baseOpts.cryptoStore = new MemoryCryptoStore(); - } - // XXX: we read from the URL params in RoomPage too: // it would be much better to read them in one place and pass // the values around, but we initialise the matrix client in @@ -184,15 +130,13 @@ export async function initClient( fallbackICEServerAllowed: fallbackICEServerAllowed, }); - try { - await client.store.startup(); - } catch (error) { - logger.error( - "Error starting matrix client store. Falling back to memory store.", - error, - ); - client.store = new MemoryStore({ localStorage }); - await client.store.startup(); + // In case of registering a new matrix account caused by broken store state. This is particularly needed for: + // - We lost the auth tokens and cannot restore the client resulting in registering a new user. + // - Need to make sure any possible leftover crypto store gets cleared. + // - A new account is created because of missing LocalStorage: "matrix-auth-store", but the crypto IndexDB is still available. + // This would result in conflicting crypto store userId vs matrixClient userId. Caused by EC 0.6.1 + if (!restore) { + client.clearStores(); } await client.initRustCrypto(); @@ -228,7 +172,7 @@ function fullAliasFromRoomName(roomName: string, client: MatrixClient): string { * @param client A matrix client object */ export function sanitiseRoomNameInput(input: string): string { - // check to see if the user has enetered a fully qualified room + // check to see if the user has entered a fully qualified room // alias. If so, turn it into just the localpart because that's what // we use const parts = input.split(":", 2);