From 951fff45e64850830abdc91186b4ad8c6de3af68 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Mon, 9 Mar 2020 15:03:00 +0000 Subject: [PATCH] Skip device verif upgrades when callback not present This skips the upgrade when the upgrade callback is not present (which is expected as no one sets it currently). This adds logging for around the upgrade process. --- src/crypto/index.js | 73 ++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/src/crypto/index.js b/src/crypto/index.js index 2388dab61..37f638c31 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -672,39 +672,46 @@ Crypto.prototype._afterCrossSigningLocalKeyChange = async function() { }, }); - // check all users for signatures - // FIXME: do this in batches - const users = {}; - for (const [userId, crossSigningInfo] - of Object.entries(this._deviceList._crossSigningInfo)) { - const upgradeInfo = await this._checkForDeviceVerificationUpgrade( - userId, CrossSigningInfo.fromStorage(crossSigningInfo, userId), - ); - if (upgradeInfo) { - users[userId] = upgradeInfo; - } - } - const shouldUpgradeCb = ( this._baseApis._cryptoCallbacks.shouldUpgradeDeviceVerifications ); - if (Object.keys(users).length > 0 && shouldUpgradeCb) { - try { - const usersToUpgrade = await shouldUpgradeCb({users: users}); - if (usersToUpgrade) { - for (const userId of usersToUpgrade) { - if (userId in users) { - await this._baseApis.setDeviceVerified( - userId, users[userId].crossSigningInfo.getId(), - ); + if (shouldUpgradeCb) { + logger.info("Starting device verification upgrade"); + + // Check all users for signatures if upgrade callback present + // FIXME: do this in batches + const users = {}; + for (const [userId, crossSigningInfo] + of Object.entries(this._deviceList._crossSigningInfo)) { + const upgradeInfo = await this._checkForDeviceVerificationUpgrade( + userId, CrossSigningInfo.fromStorage(crossSigningInfo, userId), + ); + if (upgradeInfo) { + users[userId] = upgradeInfo; + } + } + + if (Object.keys(users).length > 0) { + logger.info(`Found ${Object.keys(users).length} verif users to upgrade`); + try { + const usersToUpgrade = await shouldUpgradeCb({ users: users }); + if (usersToUpgrade) { + for (const userId of usersToUpgrade) { + if (userId in users) { + await this._baseApis.setDeviceVerified( + userId, users[userId].crossSigningInfo.getId(), + ); + } } } + } catch (e) { + logger.log( + "shouldUpgradeDeviceVerifications threw an error: not upgrading", e, + ); } - } catch (e) { - logger.log( - "shouldUpgradeDeviceVerifications threw an error: not upgrading", e, - ); } + + logger.info("Finished device verification upgrade"); } logger.info("Finished cross-signing key change post-processing"); @@ -984,16 +991,21 @@ Crypto.prototype._storeTrustedSelfKeys = async function(keys) { * @param {string} userId the user ID whose key should be checked */ Crypto.prototype._checkDeviceVerifications = async function(userId) { + const shouldUpgradeCb = ( + this._baseApis._cryptoCallbacks.shouldUpgradeDeviceVerifications + ); + if (!shouldUpgradeCb) { + // Upgrading skipped when callback is not present. + return; + } + logger.info(`Starting device verification upgrade for ${userId}`); if (this._crossSigningInfo.keys.user_signing) { const crossSigningInfo = this._deviceList.getStoredCrossSigningForUser(userId); if (crossSigningInfo) { const upgradeInfo = await this._checkForDeviceVerificationUpgrade( userId, crossSigningInfo, ); - const shouldUpgradeCb = ( - this._baseApis._cryptoCallbacks.shouldUpgradeDeviceVerifications - ); - if (upgradeInfo && shouldUpgradeCb) { + if (upgradeInfo) { const usersToUpgrade = await shouldUpgradeCb({ users: { [userId]: upgradeInfo, @@ -1007,6 +1019,7 @@ Crypto.prototype._checkDeviceVerifications = async function(userId) { } } } + logger.info(`Finished device verification upgrade for ${userId}`); }; /**