From f6c99b1d2593363bebbf7dc08cf8cff77ea00b2a Mon Sep 17 00:00:00 2001 From: Valere Date: Fri, 22 Sep 2023 13:17:47 +0200 Subject: [PATCH 1/6] fix restoreKeyBackupWithSecretStorage for rust --- spec/integ/crypto/crypto.spec.ts | 55 +++++++++++++++++++++++++ spec/integ/crypto/megolm-backup.spec.ts | 5 +++ src/client.ts | 2 +- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/spec/integ/crypto/crypto.spec.ts b/spec/integ/crypto/crypto.spec.ts index c81f3167e..47e0a0c6a 100644 --- a/spec/integ/crypto/crypto.spec.ts +++ b/spec/integ/crypto/crypto.spec.ts @@ -73,6 +73,7 @@ import { AddSecretStorageKeyOpts } from "../../../src/secret-storage"; import { CrossSigningKey, CryptoCallbacks, KeyBackupInfo } from "../../../src/crypto-api"; import { E2EKeyResponder } from "../../test-utils/E2EKeyResponder"; import { DecryptionError } from "../../../src/crypto/algorithms"; +import { IKeyBackup } from "../../../src/crypto/backup"; afterEach(() => { // reset fake-indexeddb after each test, to make sure we don't leak connections @@ -2635,6 +2636,60 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string, }); describe("Manage Key Backup", () => { + beforeEach(async () => { + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + + it("Should be able to restore from 4S after bootstrap", async () => { + const backupVersion = "1"; + await bootstrapSecurity(backupVersion); + + const check = await aliceClient.getCrypto()!.checkKeyBackupAndEnable(); + + // Import a new key that should be uploaded + const newKey = testData.MEGOLM_SESSION_DATA; + + const awaitKeyUploaded = new Promise((resolve) => { + fetchMock.put( + "path:/_matrix/client/v3/room_keys/keys", + (url, request) => { + const uploadPayload: IKeyBackup = JSON.parse(request.body?.toString() ?? "{}"); + resolve(uploadPayload); + return { + status: 200, + body: { + count: 1, + etag: "abcdefg", + }, + }; + }, + { + overwriteRoutes: true, + }, + ); + }); + + await aliceClient.getCrypto()!.importRoomKeys([newKey]); + + // The backup loop is waiting a random amount of time to avoid different clients firing at the same time. + jest.runAllTimers(); + + const keyBackupData = await awaitKeyUploaded; + + fetchMock.get("express:/_matrix/client/v3/room_keys/keys", keyBackupData); + //should be able to restore from 4S + const importReult = await aliceClient.restoreKeyBackupWithSecretStorage( + check!.backupInfo!, + undefined, + undefined, + ); + expect(importReult.imported).toStrictEqual(1); + }); + it("Reset key backup should create a new backup and update 4S", async () => { // First set up 4S and key backup const backupVersion = "1"; diff --git a/spec/integ/crypto/megolm-backup.spec.ts b/spec/integ/crypto/megolm-backup.spec.ts index f473f1360..aad79bf35 100644 --- a/spec/integ/crypto/megolm-backup.spec.ts +++ b/spec/integ/crypto/megolm-backup.spec.ts @@ -237,6 +237,11 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("megolm-keys backup (%s)", (backe expect(result.imported).toStrictEqual(1); await awaitKeyCached; + + // The key should be now cached + const afterCache = await aliceClient.restoreKeyBackupWithCache(undefined, undefined, check!.backupInfo!); + + expect(afterCache.imported).toStrictEqual(1); }); it("recover specific session from backup", async function () { diff --git a/src/client.ts b/src/client.ts index 32522d330..001e37338 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3719,7 +3719,7 @@ export class MatrixClient extends TypedEventEmitter { - if (!this.crypto) { + if (!this.cryptoBackend) { throw new Error("End-to-end encryption disabled"); } const storedKey = await this.secretStorage.get("m.megolm_backup.v1"); From d0a10497bbc9367a399e2629ca79de56b5058937 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 25 Sep 2023 16:34:10 +0200 Subject: [PATCH 2/6] Update spec/integ/crypto/crypto.spec.ts Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --- spec/integ/crypto/crypto.spec.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/integ/crypto/crypto.spec.ts b/spec/integ/crypto/crypto.spec.ts index 47e0a0c6a..1c7708b16 100644 --- a/spec/integ/crypto/crypto.spec.ts +++ b/spec/integ/crypto/crypto.spec.ts @@ -2681,7 +2681,8 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string, const keyBackupData = await awaitKeyUploaded; fetchMock.get("express:/_matrix/client/v3/room_keys/keys", keyBackupData); - //should be able to restore from 4S + + // should be able to restore from 4S const importReult = await aliceClient.restoreKeyBackupWithSecretStorage( check!.backupInfo!, undefined, From 3664f8c3c27b236bba392ab7f5c16cb3094f671f Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 25 Sep 2023 16:34:17 +0200 Subject: [PATCH 3/6] Update spec/integ/crypto/crypto.spec.ts Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --- spec/integ/crypto/crypto.spec.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/spec/integ/crypto/crypto.spec.ts b/spec/integ/crypto/crypto.spec.ts index 1c7708b16..260e7bc87 100644 --- a/spec/integ/crypto/crypto.spec.ts +++ b/spec/integ/crypto/crypto.spec.ts @@ -2683,11 +2683,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string, fetchMock.get("express:/_matrix/client/v3/room_keys/keys", keyBackupData); // should be able to restore from 4S - const importReult = await aliceClient.restoreKeyBackupWithSecretStorage( - check!.backupInfo!, - undefined, - undefined, - ); + const importResult = await aliceClient.restoreKeyBackupWithSecretStorage(check!.backupInfo!); expect(importReult.imported).toStrictEqual(1); }); From 76dbc7500f32e279f322ee266554cd0f7f12a6b4 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 25 Sep 2023 16:34:25 +0200 Subject: [PATCH 4/6] Update spec/integ/crypto/crypto.spec.ts Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --- spec/integ/crypto/crypto.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/integ/crypto/crypto.spec.ts b/spec/integ/crypto/crypto.spec.ts index 260e7bc87..6b9a08cfd 100644 --- a/spec/integ/crypto/crypto.spec.ts +++ b/spec/integ/crypto/crypto.spec.ts @@ -2675,7 +2675,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string, await aliceClient.getCrypto()!.importRoomKeys([newKey]); - // The backup loop is waiting a random amount of time to avoid different clients firing at the same time. + // The backup loop waits a random amount of time to avoid different clients firing at the same time. jest.runAllTimers(); const keyBackupData = await awaitKeyUploaded; From fe67a68c9523de99a85cbad5ab86ab07d6bdf5cd Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 26 Sep 2023 09:13:19 +0200 Subject: [PATCH 5/6] fix typo --- spec/integ/crypto/crypto.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/integ/crypto/crypto.spec.ts b/spec/integ/crypto/crypto.spec.ts index 6b9a08cfd..66de6358b 100644 --- a/spec/integ/crypto/crypto.spec.ts +++ b/spec/integ/crypto/crypto.spec.ts @@ -2684,7 +2684,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string, // should be able to restore from 4S const importResult = await aliceClient.restoreKeyBackupWithSecretStorage(check!.backupInfo!); - expect(importReult.imported).toStrictEqual(1); + expect(importResult.imported).toStrictEqual(1); }); it("Reset key backup should create a new backup and update 4S", async () => { From 9fed45e47c2edbfcb5839b62617f83a15c54f9ad Mon Sep 17 00:00:00 2001 From: Valere Date: Tue, 26 Sep 2023 12:05:27 +0200 Subject: [PATCH 6/6] quick test if no crypto --- spec/unit/crypto/backup.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/unit/crypto/backup.spec.ts b/spec/unit/crypto/backup.spec.ts index 70c8651b4..7b6377bba 100644 --- a/spec/unit/crypto/backup.spec.ts +++ b/spec/unit/crypto/backup.spec.ts @@ -215,6 +215,20 @@ describe("MegolmBackup", function () { jest.spyOn(global, "setTimeout").mockRestore(); }); + test("fail if crypto not enabled", async () => { + const client = makeTestClient(cryptoStore); + const data = { + algorithm: olmlib.MEGOLM_BACKUP_ALGORITHM, + version: "1", + auth_data: { + public_key: "hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmo", + }, + }; + await expect(client.restoreKeyBackupWithSecretStorage(data)).rejects.toThrow( + "End-to-end encryption disabled", + ); + }); + it("automatically calls the key back up", function () { const groupSession = new Olm.OutboundGroupSession(); groupSession.create();