From 00508367f6c90a8b9cd931aad70ae8d886941beb Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 15 May 2019 13:47:48 +0100 Subject: [PATCH 1/2] Fix Single Sign-on https://github.com/matrix-org/matrix-react-sdk/pull/2826 checked that we had data in the crypto store if the had credentials in localStorage. However, SSO stores creds in localStorage and then redirects the browser to remove the loginToken parameter from the URL without starting crypto, so after the redirect, we see creds in localStorage but no crypto data, and error. Fix by marking when we've successfully initialised crypto and only erroring if that flag is set. Fixes https://github.com/vector-im/riot-web/issues/9695 --- src/Lifecycle.js | 2 +- src/MatrixClientPeg.js | 1 + src/utils/StorageManager.js | 22 ++++++++++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index 0e2389fd1c..a7f90f847d 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -369,7 +369,7 @@ async function _doSetLoggedIn(credentials, clearStorage) { // If there's an inconsistency between account data in local storage and the // crypto store, we'll be generally confused when handling encrypted data. // Show a modal recommending a full reset of storage. - if (results.dataInLocalStorage && !results.dataInCryptoStore) { + if (results.dataInLocalStorage && results.cryptoInited && !results.dataInCryptoStore) { const signOut = await _showStorageEvictedDialog(); if (signOut) { await _clearStorage(); diff --git a/src/MatrixClientPeg.js b/src/MatrixClientPeg.js index 763eddbd5d..d3cf19ef48 100644 --- a/src/MatrixClientPeg.js +++ b/src/MatrixClientPeg.js @@ -121,6 +121,7 @@ class MatrixClientPeg { // check that we have a version of the js-sdk which includes initCrypto if (this.matrixClient.initCrypto) { await this.matrixClient.initCrypto(); + StorageManager.setCryptoInitialised(true); } } catch (e) { if (e && e.name === 'InvalidCryptoStoreError') { diff --git a/src/utils/StorageManager.js b/src/utils/StorageManager.js index 1c0931273b..d0fcd58106 100644 --- a/src/utils/StorageManager.js +++ b/src/utils/StorageManager.js @@ -50,11 +50,15 @@ export async function checkConsistency() { let dataInLocalStorage = false; let dataInCryptoStore = false; + let cryptoInited = false; let healthy = true; if (localStorage) { dataInLocalStorage = localStorage.length > 0; log(`Local storage contains data? ${dataInLocalStorage}`); + + cryptoInited = localStorage.getItem("mx_crypto_initialised"); + log(`Crypto initialised? ${cryptoInited}`); } else { healthy = false; error("Local storage cannot be used on this browser"); @@ -84,10 +88,11 @@ export async function checkConsistency() { track("Crypto store disabled"); } - if (dataInLocalStorage && !dataInCryptoStore) { + if (dataInLocalStorage && cryptoInited && !dataInCryptoStore) { healthy = false; error( - "Data exists in local storage but not in crypto store. " + + "Data exists in local storage and crypto is marked as initialised " + + " but no data found in crypto store. " + "IndexedDB storage has likely been evicted by the browser!", ); track("Crypto store evicted"); @@ -104,6 +109,7 @@ export async function checkConsistency() { return { dataInLocalStorage, dataInCryptoStore, + cryptoInited, healthy, }; } @@ -155,3 +161,15 @@ export function trackStores(client) { }); } } + +/** + * Sets whether crypto has ever been successfully + * initialised on this client. + * StorageManager uses this to determine whether indexeddb + * has been wiped by the browser: this flag is saved to localStorage + * and if it is true and not crypto data is found, an error is + * presented to the user. + */ +export function setCryptoInitialised(cryptoInited) { + localStorage.setItem("mx_crypto_initialised", cryptoInited); +} From 0cde4bf2cd7f50ec8c06a3a8a07884b570721c3f Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 15 May 2019 14:00:37 +0100 Subject: [PATCH 2/2] lint --- src/utils/StorageManager.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils/StorageManager.js b/src/utils/StorageManager.js index d0fcd58106..49a120a470 100644 --- a/src/utils/StorageManager.js +++ b/src/utils/StorageManager.js @@ -169,6 +169,8 @@ export function trackStores(client) { * has been wiped by the browser: this flag is saved to localStorage * and if it is true and not crypto data is found, an error is * presented to the user. + * + * @param {bool} cryptoInited True if crypto has been set up */ export function setCryptoInitialised(cryptoInited) { localStorage.setItem("mx_crypto_initialised", cryptoInited);