Skip to content

Commit b19817b

Browse files
authored
Bump matrix-sdk-crypto-wasm to 5.0.0 (#4216)
Slightly more involved than normal because it requires us to pass a backup version into OlmMachine.importBackedUpRoomKeys. On the other hand we can now re-enable the test that was disabled in #4214 due to matrix-org/matrix-rust-sdk#3447 Fixes: element-hq/element-web#27165
1 parent 36196ea commit b19817b

File tree

9 files changed

+46
-25
lines changed

9 files changed

+46
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
],
5454
"dependencies": {
5555
"@babel/runtime": "^7.12.5",
56-
"@matrix-org/matrix-sdk-crypto-wasm": "^4.9.0",
56+
"@matrix-org/matrix-sdk-crypto-wasm": "^5.0.0",
5757
"another-json": "^0.2.0",
5858
"bs58": "^5.0.0",
5959
"content-type": "^1.0.4",

spec/unit/rust-crypto/rust-crypto.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,19 +1415,19 @@ describe("RustCrypto", () => {
14151415
expect(await keyBackupStatusPromise).toBe(true);
14161416
});
14171417

1418-
// XXX: disabled until https://github.com/matrix-org/matrix-rust-sdk/issues/3447 is fixed
1419-
it.skip("does not back up keys that came from backup", async () => {
1418+
it("does not back up keys that came from backup", async () => {
14201419
const rustCrypto = await makeTestRustCrypto();
14211420
const olmMachine: OlmMachine = rustCrypto["olmMachine"];
14221421

1422+
const backupVersion = testData.SIGNED_BACKUP_DATA.version!;
14231423
await olmMachine.enableBackupV1(
14241424
(testData.SIGNED_BACKUP_DATA.auth_data as Curve25519AuthData).public_key,
1425-
testData.SIGNED_BACKUP_DATA.version!,
1425+
backupVersion,
14261426
);
14271427

14281428
// we import two keys: one "from backup", and one "from export"
14291429
const [backedUpRoomKey, exportedRoomKey] = testData.MEGOLM_SESSION_DATA_ARRAY;
1430-
await rustCrypto.importBackedUpRoomKeys([backedUpRoomKey]);
1430+
await rustCrypto.importBackedUpRoomKeys([backedUpRoomKey], backupVersion);
14311431
await rustCrypto.importRoomKeys([exportedRoomKey]);
14321432

14331433
// we ask for the keys that should be backed up
@@ -1462,16 +1462,17 @@ describe("RustCrypto", () => {
14621462
const rustCrypto = await makeTestRustCrypto();
14631463
const olmMachine: OlmMachine = rustCrypto["olmMachine"];
14641464

1465+
const backupVersion = testData.SIGNED_BACKUP_DATA.version!;
14651466
await olmMachine.enableBackupV1(
14661467
(testData.SIGNED_BACKUP_DATA.auth_data as Curve25519AuthData).public_key,
1467-
testData.SIGNED_BACKUP_DATA.version!,
1468+
backupVersion,
14681469
);
14691470

14701471
const backup = Array.from(testData.MEGOLM_SESSION_DATA_ARRAY);
14711472
// in addition to correct keys, we restore an invalid key
14721473
backup.push({ room_id: "!roomid", session_id: "sessionid" } as IMegolmSessionData);
14731474
const progressCallback = jest.fn();
1474-
await rustCrypto.importBackedUpRoomKeys(backup, { progressCallback });
1475+
await rustCrypto.importBackedUpRoomKeys(backup, backupVersion, { progressCallback });
14751476
expect(progressCallback).toHaveBeenCalledWith({
14761477
total: 3,
14771478
successes: 0,

src/client.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3863,12 +3863,13 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
38633863
if (!backupInfo.version) {
38643864
throw new Error("Backup version must be defined");
38653865
}
3866+
const backupVersion = backupInfo.version!;
38663867

38673868
let totalKeyCount = 0;
38683869
let totalFailures = 0;
38693870
let totalImported = 0;
38703871

3871-
const path = this.makeKeyBackupPath(targetRoomId, targetSessionId, backupInfo.version);
3872+
const path = this.makeKeyBackupPath(targetRoomId, targetSessionId, backupVersion);
38723873

38733874
const backupDecryptor = await this.cryptoBackend.getBackupDecryptor(backupInfo, privKey);
38743875

@@ -3882,7 +3883,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
38823883
// Cache the key, if possible.
38833884
// This is async.
38843885
this.cryptoBackend
3885-
.storeSessionBackupPrivateKey(privKey, backupInfo.version)
3886+
.storeSessionBackupPrivateKey(privKey, backupVersion)
38863887
.catch((e) => {
38873888
this.logger.warn("Error caching session backup key:", e);
38883889
})
@@ -3922,7 +3923,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
39223923
async (chunk) => {
39233924
// We have a chunk of decrypted keys: import them
39243925
try {
3925-
await this.cryptoBackend!.importBackedUpRoomKeys(chunk, {
3926+
const backupVersion = backupInfo.version!;
3927+
await this.cryptoBackend!.importBackedUpRoomKeys(chunk, backupVersion, {
39263928
untrusted,
39273929
});
39283930
totalImported += chunk.length;
@@ -3952,7 +3954,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
39523954
for (const k of keys) {
39533955
k.room_id = targetRoomId!;
39543956
}
3955-
await this.cryptoBackend.importBackedUpRoomKeys(keys, {
3957+
await this.cryptoBackend.importBackedUpRoomKeys(keys, backupVersion, {
39563958
progressCallback,
39573959
untrusted,
39583960
});
@@ -3966,7 +3968,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
39663968
key.room_id = targetRoomId!;
39673969
key.session_id = targetSessionId!;
39683970

3969-
await this.cryptoBackend.importBackedUpRoomKeys([key], {
3971+
await this.cryptoBackend.importBackedUpRoomKeys([key], backupVersion, {
39703972
progressCallback,
39713973
untrusted,
39723974
});

src/common-crypto/CryptoBackend.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ export interface CryptoBackend extends SyncCryptoCallbacks, CryptoApi {
113113
* Import a list of room keys restored from backup
114114
*
115115
* @param keys - a list of session export objects
116+
* @param backupVersion - the version of the backup these keys came from.
116117
* @param opts - options object
117118
* @returns a promise which resolves once the keys have been imported
118119
*/
119-
importBackedUpRoomKeys(keys: IMegolmSessionData[], opts?: ImportRoomKeysOpts): Promise<void>;
120+
importBackedUpRoomKeys(keys: IMegolmSessionData[], backupVersion: string, opts?: ImportRoomKeysOpts): Promise<void>;
120121
}
121122

122123
/** The methods which crypto implementations should expose to the Sync api

src/crypto/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,11 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
18881888
/**
18891889
* Implementation of {@link CryptoBackend#importBackedUpRoomKeys}.
18901890
*/
1891-
public importBackedUpRoomKeys(keys: IMegolmSessionData[], opts: ImportRoomKeysOpts = {}): Promise<void> {
1891+
public importBackedUpRoomKeys(
1892+
keys: IMegolmSessionData[],
1893+
backupVersion: string,
1894+
opts: ImportRoomKeysOpts = {},
1895+
): Promise<void> {
18921896
opts.source = "backup";
18931897
return this.importRoomKeys(keys, opts);
18941898
}

src/rust-crypto/PerSessionKeyBackupDownloader.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ class KeyDownloadRateLimitError extends Error {
5757
/** Details of a megolm session whose key we are trying to fetch. */
5858
type SessionInfo = { roomId: string; megolmSessionId: string };
5959

60-
/** Holds the current backup decryptor and version that should be used. */
60+
/** Holds the current backup decryptor and version that should be used.
61+
*
62+
* This is intended to be used as an immutable object (a new instance should be created if the configuration changes),
63+
* and some of the logic relies on that, so the properties are marked as `readonly`.
64+
*/
6165
type Configuration = {
62-
backupVersion: string;
63-
decryptor: BackupDecryptor;
66+
readonly backupVersion: string;
67+
readonly decryptor: BackupDecryptor;
6468
};
6569

6670
/**
@@ -392,7 +396,7 @@ export class PerSessionKeyBackupDownloader {
392396
for (const k of keys) {
393397
k.room_id = sessionInfo.roomId;
394398
}
395-
await this.backupManager.importBackedUpRoomKeys(keys);
399+
await this.backupManager.importBackedUpRoomKeys(keys, configuration.backupVersion);
396400
}
397401

398402
/**

src/rust-crypto/backup.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,11 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
239239
/**
240240
* Implementation of {@link CryptoBackend#importBackedUpRoomKeys}.
241241
*/
242-
public async importBackedUpRoomKeys(keys: IMegolmSessionData[], opts?: ImportRoomKeysOpts): Promise<void> {
242+
public async importBackedUpRoomKeys(
243+
keys: IMegolmSessionData[],
244+
backupVersion: string,
245+
opts?: ImportRoomKeysOpts,
246+
): Promise<void> {
243247
const keysByRoom: Map<RustSdkCryptoJs.RoomId, Map<string, IMegolmSessionData>> = new Map();
244248
for (const key of keys) {
245249
const roomId = new RustSdkCryptoJs.RoomId(key.room_id);
@@ -259,6 +263,7 @@ export class RustBackupManager extends TypedEventEmitter<RustBackupCryptoEvents,
259263
};
260264
opts?.progressCallback?.(importOpt);
261265
},
266+
backupVersion,
262267
);
263268
}
264269

src/rust-crypto/rust-crypto.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,12 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
12171217
/**
12181218
* Implementation of {@link CryptoBackend#importBackedUpRoomKeys}.
12191219
*/
1220-
public async importBackedUpRoomKeys(keys: IMegolmSessionData[], opts?: ImportRoomKeysOpts): Promise<void> {
1221-
return await this.backupManager.importBackedUpRoomKeys(keys, opts);
1220+
public async importBackedUpRoomKeys(
1221+
keys: IMegolmSessionData[],
1222+
backupVersion: string,
1223+
opts?: ImportRoomKeysOpts,
1224+
): Promise<void> {
1225+
return await this.backupManager.importBackedUpRoomKeys(keys, backupVersion, opts);
12221226
}
12231227

12241228
/**

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,10 +1772,10 @@
17721772
"@jridgewell/resolve-uri" "^3.1.0"
17731773
"@jridgewell/sourcemap-codec" "^1.4.14"
17741774

1775-
"@matrix-org/matrix-sdk-crypto-wasm@^4.9.0":
1776-
version "4.10.0"
1777-
resolved "https://registry.yarnpkg.com/@matrix-org/matrix-sdk-crypto-wasm/-/matrix-sdk-crypto-wasm-4.10.0.tgz#324211b9bad3d2aa640800f62ba9478ce2845097"
1778-
integrity sha512-zOqKVAYPfzs6Hav/Km9F5xWwoQ0bxDuoUU0/121m03Fg2VnfcHk43TjKImZolFc7IlgXwVGoda9Pp9Z/eTVKJA==
1775+
"@matrix-org/matrix-sdk-crypto-wasm@^5.0.0":
1776+
version "5.0.0"
1777+
resolved "https://registry.yarnpkg.com/@matrix-org/matrix-sdk-crypto-wasm/-/matrix-sdk-crypto-wasm-5.0.0.tgz#f45a7bccaad218c05bcf9e7c8ca783c9d9a07af4"
1778+
integrity sha512-37ASjCKSTU5ycGfkP+LUXG4Ok6OAf6vE+1qU6uwWhe6FwadCS3vVWzJYd/3d9BQFwsx4GhFTIAXrW4iLG85rmQ==
17791779

17801780
"@matrix-org/[email protected]":
17811781
version "3.2.15"

0 commit comments

Comments
 (0)