Skip to content

Commit 5470581

Browse files
authored
Fix backed-up keys being re-backed-up (#121)
Bump to a version of the rust SDK that includes matrix-org/matrix-rust-sdk#3448 and matrix-org/matrix-rust-sdk#3456, to fix matrix-org/matrix-rust-sdk#3447. Also, pass the backup version into import_backed_up_room_keys to avoid using the deprecated method on BackupMachine.
1 parent 294df87 commit 5470581

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

CHANGELOG.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# UNRELEASED
22

3+
**BREAKING CHANGES**
4+
5+
- `OlmMachine.importBackedUpRoomKeys` now takes a `backupVersion` argument.
6+
7+
**Other changes**
8+
9+
- Update matrix-rust-sdk to `7e44fbca7`, which includes:
10+
11+
- Avoid emitting entries from `identities_stream_raw` and `devices_stream` when
12+
we receive a `/keys/query` response which shows that no devices changed.
13+
([#3442](https://github.com/matrix-org/matrix-rust-sdk/pull/3442)).
14+
15+
- Fix to a bug introduced in matrix-sdk-crypto-wasm v4.10.0 which caused
16+
keys that had been imported from key backup to be backed up again, when
17+
using the in-memory datastore.
18+
319
# matrix-sdk-crypto-wasm v4.10.0
420

521
- Expose new constructor function `OlmMachine.openWithKey()`.
@@ -19,7 +35,7 @@
1935
- Add a constructor for the `Curve25519PublicKey` type. This allows us to
2036
create a `Curve25519PublicKey` from a Base64 string on the Javascript side.
2137

22-
- Update matrix-rust-sdk to `7a887766c`, which includes:
38+
- Update matrix-rust-sdk to `d7a887766c`, which includes:
2339

2440
- Add data types to parse the QR code data for the QR code login defined in
2541
[MSC4108](https://github.com/matrix-org/matrix-spec-proposals/pull/4108)

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/machine.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use matrix_sdk_common::ruma::{
1515
};
1616
use matrix_sdk_crypto::{
1717
backups::MegolmV1BackupKey,
18-
olm::BackedUpRoomKey,
18+
olm::{BackedUpRoomKey, ExportedRoomKey},
1919
store::{DeviceChanges, IdentityChanges},
2020
types::RoomKeyBackupInfo,
2121
CryptoStoreError, EncryptionSyncChanges, GossippedSecret,
@@ -1052,11 +1052,12 @@ impl OlmMachine {
10521052
&self,
10531053
backed_up_room_keys: &Map,
10541054
progress_listener: Option<Function>,
1055+
backup_version: String,
10551056
) -> Result<Promise, JsValue> {
10561057
let me = self.inner.clone();
10571058

10581059
// convert the js-side data into rust data
1059-
let mut keys: BTreeMap<_, BTreeMap<_, _>> = BTreeMap::new();
1060+
let mut keys = Vec::new();
10601061
let mut failures = 0;
10611062
for backed_up_room_keys_entry in backed_up_room_keys.entries() {
10621063
let backed_up_room_keys_entry: Array = backed_up_room_keys_entry?.dyn_into()?;
@@ -1071,7 +1072,11 @@ impl OlmMachine {
10711072
if let Ok(key) =
10721073
serde_wasm_bindgen::from_value::<BackedUpRoomKey>(room_room_keys_entry.get(1))
10731074
{
1074-
keys.entry(room_id.clone()).or_default().insert(session_id.into(), key);
1075+
keys.push(ExportedRoomKey::from_backed_up_room_key(
1076+
room_id.clone(),
1077+
session_id.into(),
1078+
key,
1079+
));
10751080
} else {
10761081
failures += 1;
10771082
}
@@ -1080,8 +1085,8 @@ impl OlmMachine {
10801085

10811086
Ok(future_to_promise(async move {
10821087
let result: RoomKeyImportResult = me
1083-
.backup_machine()
1084-
.import_backed_up_room_keys(keys, |progress, total_valid| {
1088+
.store()
1089+
.import_room_keys(keys, Some(&backup_version), |progress, total_valid| {
10851090
if let Some(callback) = &progress_listener {
10861091
callback
10871092
.call3(

tests/machine.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,7 @@ describe(OlmMachine.name, () => {
12571257
const progressListener = jest.fn();
12581258
const m2 = await machine();
12591259
await m2.saveBackupDecryptionKey(keyBackupKey, "1");
1260-
const result = await m2.importBackedUpRoomKeys(decryptedKeyMap, progressListener);
1260+
const result = await m2.importBackedUpRoomKeys(decryptedKeyMap, progressListener, "1");
12611261
expect(result.importedCount).toStrictEqual(1);
12621262
expect(result.totalCount).toStrictEqual(1);
12631263
expect(result.keys()).toMatchObject(
@@ -1442,27 +1442,32 @@ describe(OlmMachine.name, () => {
14421442
test("Updating devices should call devicesUpdatedCallback", async () => {
14431443
const userId = new UserId("@alice:example.org");
14441444
const deviceId = new DeviceId("ABCDEF");
1445-
const machine = await OlmMachine.initialize(userId, deviceId);
1445+
const firstMachine = await OlmMachine.initialize(userId, deviceId);
14461446

14471447
const callback = jest.fn().mockImplementation(() => Promise.resolve(undefined));
1448-
machine.registerDevicesUpdatedCallback(callback);
1448+
firstMachine.registerDevicesUpdatedCallback(callback);
14491449

1450-
const outgoingRequests = await machine.outgoingRequests();
1450+
const secondDeviceId = new DeviceId("GHIJKL");
1451+
const secondMachine = await OlmMachine.initialize(userId, secondDeviceId);
1452+
1453+
// Fish the KeysUploadRequest out of secondMachine's outgoingRequests.
14511454
let deviceKeys;
1452-
// outgoingRequests will have a KeysUploadRequest before the
1453-
// KeysQueryRequest, so we grab the device upload and put it in the
1454-
// response to the /keys/query
1455-
for (const request of outgoingRequests) {
1455+
for (const request of await secondMachine.outgoingRequests()) {
14561456
if (request instanceof KeysUploadRequest) {
14571457
deviceKeys = JSON.parse(request.body).device_keys;
1458-
} else if (request instanceof KeysQueryRequest) {
1459-
await machine.markRequestAsSent(
1458+
}
1459+
}
1460+
1461+
// ... and feed it into firstMachine's KeysQueryRequest
1462+
for (const request of await firstMachine.outgoingRequests()) {
1463+
if (request instanceof KeysQueryRequest) {
1464+
await firstMachine.markRequestAsSent(
14601465
request.id,
14611466
request.type,
14621467
JSON.stringify({
14631468
device_keys: {
14641469
"@alice:example.org": {
1465-
ABCDEF: deviceKeys,
1470+
GHIJKL: deviceKeys,
14661471
},
14671472
},
14681473
}),

0 commit comments

Comments
 (0)