From db068a47b968766b67ff0226a68cd5de1f9ee16f Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Thu, 7 Nov 2024 15:11:10 +0100 Subject: [PATCH] Get backup info from a specific version --- spec/integ/crypto/megolm-backup.spec.ts | 4 ++++ src/rust-crypto/backup.ts | 23 ++++++++++++++++++----- src/rust-crypto/rust-crypto.ts | 16 +++++++++++++++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/spec/integ/crypto/megolm-backup.spec.ts b/spec/integ/crypto/megolm-backup.spec.ts index 94d98b4cef..e0a69e94bc 100644 --- a/spec/integ/crypto/megolm-backup.spec.ts +++ b/spec/integ/crypto/megolm-backup.spec.ts @@ -317,6 +317,10 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("megolm-keys backup (%s)", (backe beforeEach(async () => { fetchMock.get("path:/_matrix/client/v3/room_keys/version", testData.SIGNED_BACKUP_DATA); + fetchMock.get( + `path:/_matrix/client/v3/room_keys/version/${testData.SIGNED_BACKUP_DATA.version}`, + testData.SIGNED_BACKUP_DATA, + ); aliceClient = await initTestClient(); aliceCrypto = aliceClient.getCrypto()!; diff --git a/src/rust-crypto/backup.ts b/src/rust-crypto/backup.ts index 35b6d491ab..2781f42551 100644 --- a/src/rust-crypto/backup.ts +++ b/src/rust-crypto/backup.ts @@ -502,12 +502,14 @@ export class RustBackupManager extends TypedEventEmitter { - return await requestKeyBackupVersion(this.http); + public async requestKeyBackupVersion(version?: string): Promise { + return await requestKeyBackupVersion(this.http, version); } /** @@ -798,11 +800,22 @@ export class RustBackupDecryptor implements BackupDecryptor { } } +/** + * Fetch a key backup info from the server. + * - If `version` is provided call GET /room_keys/version/$version and get the backup info for that version. + * https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3room_keysversionversion + * - If not, call GET /room_keys/version and get the latest backup info. + * https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3room_keysversion + * @param http + * @param version - the specific version of the backup info to fetch + */ export async function requestKeyBackupVersion( http: MatrixHttpApi, + version?: string, ): Promise { try { - return await http.authedRequest(Method.Get, "/room_keys/version", undefined, undefined, { + const path = version ? encodeUri("/room_keys/version/$version", { $version: version }) : "/room_keys/version"; + return await http.authedRequest(Method.Get, path, undefined, undefined, { prefix: ClientPrefix.V3, }); } catch (e) { diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index 6725e44feb..aadd7f9397 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -1181,6 +1181,15 @@ export class RustCrypto extends TypedEventEmitter { + const backupKeys: RustSdkCryptoJs.BackupKeys = await this.olmMachine.getBackupKeys(); + return backupKeys.backupVersion || null; + } + /** * Store the backup decryption key. * @@ -1325,10 +1334,15 @@ export class RustCrypto extends TypedEventEmitter { + // Get the decryption key from the cache const privateKeyFromCache = await this.getSessionBackupPrivateKey(); if (!privateKeyFromCache) throw new Error("No decryption key found in cache"); - const backupInfo = await this.backupManager.getServerBackupInfo(); + // Get the backup version from the cache + const backupVersion = await this.getSessionBackupVersion(); + if (!backupVersion) throw new Error("No backup version found in cache"); + + const backupInfo = await this.backupManager.requestKeyBackupVersion(backupVersion); if (!backupInfo?.version) throw new Error("Missing version in backup info"); const backupDecryptor = await this.getBackupDecryptor(backupInfo, privateKeyFromCache);