Skip to content

Commit 2dd4334

Browse files
authored
Fix RustBackupManager remaining values after current backup removal. #4534 (#4537)
1 parent 2210255 commit 2dd4334

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

spec/integ/crypto/megolm-backup.spec.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { Mocked } from "jest-mock";
2222
import {
2323
createClient,
2424
Crypto,
25-
CryptoEvent,
2625
encodeBase64,
2726
ICreateClientOpts,
2827
IEvent,
@@ -45,7 +44,7 @@ import * as testData from "../../test-utils/test-data";
4544
import { KeyBackupInfo, KeyBackupSession } from "../../../src/crypto-api/keybackup";
4645
import { flushPromises } from "../../test-utils/flushPromises";
4746
import { defer, IDeferred } from "../../../src/utils";
48-
import { decodeRecoveryKey, DecryptionFailureCode } from "../../../src/crypto-api";
47+
import { decodeRecoveryKey, DecryptionFailureCode, CryptoEvent } from "../../../src/crypto-api";
4948
import { KeyBackup } from "../../../src/rust-crypto/backup.ts";
5049

5150
const ROOM_ID = testData.TEST_ROOM_ID;
@@ -969,6 +968,40 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("megolm-keys backup (%s)", (backe
969968
expect(backupStatus).toStrictEqual(testData.SIGNED_BACKUP_DATA.version);
970969
});
971970

971+
newBackendOnly("getKeyBackupInfo() should not return a backup if the active backup has been deleted", async () => {
972+
// 404 means that there is no active backup
973+
fetchMock.get("express:/_matrix/client/v3/room_keys/version", 404);
974+
fetchMock.delete(`express:/_matrix/client/v3/room_keys/version/${testData.SIGNED_BACKUP_DATA.version}`, {});
975+
976+
aliceClient = await initTestClient();
977+
const aliceCrypto = aliceClient.getCrypto()!;
978+
await aliceClient.startClient();
979+
980+
// tell Alice to trust the dummy device that signed the backup
981+
await waitForDeviceList();
982+
await aliceCrypto.setDeviceVerified(testData.TEST_USER_ID, testData.TEST_DEVICE_ID);
983+
await aliceCrypto.checkKeyBackupAndEnable();
984+
985+
// At this point there is no backup
986+
expect(await aliceCrypto.getKeyBackupInfo()).toBeNull();
987+
988+
// Return now the backup
989+
fetchMock.get("express:/_matrix/client/v3/room_keys/version", testData.SIGNED_BACKUP_DATA, {
990+
overwriteRoutes: true,
991+
});
992+
993+
expect(await aliceCrypto.getKeyBackupInfo()).toStrictEqual(testData.SIGNED_BACKUP_DATA);
994+
995+
// Delete the backup and we are expecting the key backup to be disabled
996+
const keyBackupStatus = defer<boolean>();
997+
aliceClient.once(CryptoEvent.KeyBackupStatus, (enabled) => keyBackupStatus.resolve(enabled));
998+
await aliceCrypto.deleteKeyBackupVersion(testData.SIGNED_BACKUP_DATA.version!);
999+
expect(await keyBackupStatus.promise).toBe(false);
1000+
1001+
// The backup info should not be available anymore
1002+
expect(await aliceCrypto.getKeyBackupInfo()).toBeNull();
1003+
});
1004+
9721005
describe("isKeyBackupTrusted", () => {
9731006
it("does not trust a backup signed by an untrusted device", async () => {
9741007
aliceClient = await initTestClient();

src/rust-crypto/backup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,11 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
583583
await this.http.authedRequest<void>(Method.Delete, path, undefined, undefined, {
584584
prefix: ClientPrefix.V3,
585585
});
586+
// If the backup we are deleting is the active one, we need to disable the key backup and to have the local properties reset
587+
if (this.activeBackupVersion === version) {
588+
this.serverBackupInfo = null;
589+
await this.disableKeyBackup();
590+
}
586591
}
587592

588593
/**

0 commit comments

Comments
 (0)