From 6f1484005bd883b7f4a32def52d553b6bb9deb11 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 27 Jan 2022 15:14:29 +0100 Subject: [PATCH] stop key backup when on the wrong version users can then enter the new key in the settings to start backing up again --- src/matrix/Session.js | 7 ++++++- src/matrix/e2ee/megolm/keybackup/KeyBackup.ts | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/matrix/Session.js b/src/matrix/Session.js index 5e68829b..9b9d013e 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -646,7 +646,12 @@ export class Session { const operation = this._keyBackup.flush(log); this._keyBackupOperation.set(operation); try { - await operation.result; + const success = await operation.result; + // stop key backup if the version was changed + if (!success) { + this._keyBackup = this._keyBackup.dispose(); + this.needsKeyBackup.set(true); + } } catch (err) { log.catch(err); } diff --git a/src/matrix/e2ee/megolm/keybackup/KeyBackup.ts b/src/matrix/e2ee/megolm/keybackup/KeyBackup.ts index c5fc7210..3792ea96 100644 --- a/src/matrix/e2ee/megolm/keybackup/KeyBackup.ts +++ b/src/matrix/e2ee/megolm/keybackup/KeyBackup.ts @@ -69,7 +69,7 @@ export class KeyBackup { } // TODO: protect against having multiple concurrent flushes - flush(log: ILogItem): AbortableOperation, Progress> { + flush(log: ILogItem): AbortableOperation, Progress> { return new AbortableOperation(async (setAbortable, setProgress) => { let total = 0; let amountFinished = 0; @@ -87,7 +87,7 @@ export class KeyBackup { setProgress(new Progress(total, amountFinished)); const keysNeedingBackup = await txn.sessionsNeedingBackup.getFirstEntries(20); if (keysNeedingBackup.length === 0) { - return; + return true; } const roomKeysOrNotFound = await Promise.all(keysNeedingBackup.map(k => keyFromStorage(k.roomId, k.senderKey, k.sessionId, txn))); const roomKeys = roomKeysOrNotFound.filter(k => !!k) as RoomKey[]; @@ -95,7 +95,16 @@ export class KeyBackup { const payload = await this.encodeKeysForBackup(roomKeys); const uploadRequest = this.hsApi.uploadRoomKeysToBackup(this.backupInfo.version, payload, {log}); setAbortable(uploadRequest); - await uploadRequest.response(); + try { + await uploadRequest.response(); + } catch (err) { + if (err.name === "HomeServerError" && err.errcode === "M_WRONG_ROOM_KEYS_VERSION") { + log.set("wrong_version", true); + return false; + } else { + throw err; + } + } } this.removeBackedUpKeys(keysNeedingBackup, setAbortable); amountFinished += keysNeedingBackup.length;