From 458384d65887a6c2e02c82258c8d720dd11dd88a Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 14 Jan 2021 19:55:02 -0500 Subject: [PATCH] queue keys for backup even if backup isn't enabled yet We may not have managed to set up the backup yet when we get keys. So we should unconditionally queue up the keys for backup, so that when the backup is set up, they will be sent instead of dropped. --- src/crypto/algorithms/megolm.js | 36 +++++++++++---------------------- src/crypto/index.js | 14 ++++++------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/crypto/algorithms/megolm.js b/src/crypto/algorithms/megolm.js index 006a11010..f8ce23a3f 100644 --- a/src/crypto/algorithms/megolm.js +++ b/src/crypto/algorithms/megolm.js @@ -369,17 +369,11 @@ MegolmEncryption.prototype._prepareNewSession = async function() { key.key, {ed25519: this._olmDevice.deviceEd25519Key}, ); - if (this._crypto.backupInfo) { - // don't wait for it to complete - this._crypto.backupGroupSession( - this._roomId, this._olmDevice.deviceCurve25519Key, [], - sessionId, key.key, - ).catch((e) => { - // This throws if the upload failed, but this is fine - // since it will have written it to the db and will retry. - logger.log("Failed to back up megolm session", e); - }); - } + // don't wait for it to complete + this._crypto.backupGroupSession( + this._roomId, this._olmDevice.deviceCurve25519Key, [], + sessionId, key.key, + ); return new OutboundSessionInfo(sessionId); }; @@ -1347,18 +1341,12 @@ MegolmDecryption.prototype.onRoomKeyEvent = function(event) { } }); }).then(() => { - if (this._crypto.backupInfo) { - // don't wait for the keys to be backed up for the server - this._crypto.backupGroupSession( - content.room_id, senderKey, forwardingKeyChain, - content.session_id, content.session_key, keysClaimed, - exportFormat, - ).catch((e) => { - // This throws if the upload failed, but this is fine - // since it will have written it to the db and will retry. - logger.log("Failed to back up megolm session", e); - }); - } + // don't wait for the keys to be backed up for the server + this._crypto.backupGroupSession( + content.room_id, senderKey, forwardingKeyChain, + content.session_id, content.session_key, keysClaimed, + exportFormat, + ); }).catch((e) => { logger.error(`Error handling m.room_key_event: ${e}`); }); @@ -1564,7 +1552,7 @@ MegolmDecryption.prototype.importRoomKey = function(session, opts = {}) { true, opts.untrusted ? { untrusted: opts.untrusted } : {}, ).then(() => { - if (this._crypto.backupInfo && opts.source !== "backup") { + if (opts.source !== "backup") { // don't wait for it to complete this._crypto.backupGroupSession( session.room_id, diff --git a/src/crypto/index.js b/src/crypto/index.js index 332ff7019..9b4a5bd6e 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -2884,18 +2884,18 @@ Crypto.prototype.backupGroupSession = async function( sessionId, sessionKey, keysClaimed, exportFormat, ) { - if (!this.backupInfo) { - throw new Error("Key backups are not enabled"); - } - await this._cryptoStore.markSessionsNeedingBackup([{ senderKey: senderKey, sessionId: sessionId, }]); - // don't wait for this to complete: it will delay so - // happens in the background - this.scheduleKeyBackupSend(); + if (this.backupInfo) { + // don't wait for this to complete: it will delay so + // happens in the background + this.scheduleKeyBackupSend(); + } + // if this.backupInfo is not set, then the keys will be backed up when + // client.enableKeyBackup is called }; /**