Skip to content

Commit bf92cb1

Browse files
authored
try to re-fetch devices before giving up on trying to heal a broken olm (matrix-org#1224)
1 parent 0112701 commit bf92cb1

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

spec/unit/crypto/algorithms/megolm.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ describe("MegolmDecryption", function() {
598598
aliceClient.initCrypto(),
599599
bobClient.initCrypto(),
600600
]);
601+
aliceClient._crypto.downloadKeys = async () => {};
601602
const bobDevice = bobClient._crypto._olmDevice;
602603

603604
const roomId = "!someroom";
@@ -649,6 +650,7 @@ describe("MegolmDecryption", function() {
649650
bobClient.initCrypto(),
650651
]);
651652
const bobDevice = bobClient._crypto._olmDevice;
653+
aliceClient._crypto.downloadKeys = async () => {};
652654

653655
const roomId = "!someroom";
654656

src/crypto/algorithms/megolm.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,17 +1260,25 @@ MegolmDecryption.prototype.onRoomKeyWithheldEvent = async function(event) {
12601260
this.retryDecryptionFromSender(senderKey);
12611261
return;
12621262
}
1263-
const device = this._crypto._deviceList.getDeviceByIdentityKey(
1263+
let device = this._crypto._deviceList.getDeviceByIdentityKey(
12641264
content.algorithm, senderKey,
12651265
);
12661266
if (!device) {
1267-
logger.info(
1268-
"Couldn't find device for identity key " + senderKey +
1269-
": not establishing session",
1267+
// if we don't know about the device, fetch the user's devices again
1268+
// and retry before giving up
1269+
await this._crypto.downloadKeys([sender], false);
1270+
device = this._crypto._deviceList.getDeviceByIdentityKey(
1271+
content.algorithm, senderKey,
12701272
);
1271-
await this._olmDevice.recordSessionProblem(senderKey, "no_olm", false);
1272-
this.retryDecryptionFromSender(senderKey);
1273-
return;
1273+
if (!device) {
1274+
logger.info(
1275+
"Couldn't find device for identity key " + senderKey +
1276+
": not establishing session",
1277+
);
1278+
await this._olmDevice.recordSessionProblem(senderKey, "no_olm", false);
1279+
this.retryDecryptionFromSender(senderKey);
1280+
return;
1281+
}
12741282
}
12751283
await olmlib.ensureOlmSessionsForDevices(
12761284
this._olmDevice, this._baseApis, {[sender]: [device]}, false,

src/crypto/index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,15 +2723,21 @@ Crypto.prototype._onToDeviceBadEncrypted = async function(event) {
27232723
// on a current session.
27242724
// Note that an undecryptable message from another device could easily be spoofed -
27252725
// is there anything we can do to mitigate this?
2726-
const device = this._deviceList.getDeviceByIdentityKey(algorithm, deviceKey);
2726+
let device = this._deviceList.getDeviceByIdentityKey(algorithm, deviceKey);
27272727
if (!device) {
2728-
logger.info(
2729-
"Couldn't find device for identity key " + deviceKey +
2730-
": not re-establishing session",
2731-
);
2732-
await this._olmDevice.recordSessionProblem(deviceKey, "wedged", false);
2733-
retryDecryption();
2734-
return;
2728+
// if we don't know about the device, fetch the user's devices again
2729+
// and retry before giving up
2730+
await this.downloadKeys([sender], false);
2731+
device = this._deviceList.getDeviceByIdentityKey(algorithm, deviceKey);
2732+
if (!device) {
2733+
logger.info(
2734+
"Couldn't find device for identity key " + deviceKey +
2735+
": not re-establishing session",
2736+
);
2737+
await this._olmDevice.recordSessionProblem(deviceKey, "wedged", false);
2738+
retryDecryption();
2739+
return;
2740+
}
27352741
}
27362742
const devicesByUser = {};
27372743
devicesByUser[sender] = [device];

0 commit comments

Comments
 (0)