diff --git a/spec/integ/megolm-integ.spec.js b/spec/integ/megolm-integ.spec.js index 73fcfa81d8e..4c60f26fcc5 100644 --- a/spec/integ/megolm-integ.spec.js +++ b/spec/integ/megolm-integ.spec.js @@ -618,6 +618,9 @@ describe("megolm", function() { aliceTestClient.httpBackend.when( 'PUT', '/sendToDevice/org.matrix.room_key.withheld/', ).respond(200, {}); + aliceTestClient.httpBackend.when( + 'PUT', '/sendToDevice/m.room_key.withheld/', + ).respond(200, {}); return Promise.all([ aliceTestClient.client.sendTextMessage(ROOM_ID, 'test'), @@ -718,6 +721,9 @@ describe("megolm", function() { aliceTestClient.httpBackend.when( 'PUT', '/sendToDevice/org.matrix.room_key.withheld/', ).respond(200, {}); + aliceTestClient.httpBackend.when( + 'PUT', '/sendToDevice/m.room_key.withheld/', + ).respond(200, {}); return Promise.all([ aliceTestClient.client.sendTextMessage(ROOM_ID, 'test2'), diff --git a/spec/unit/crypto/algorithms/megolm.spec.js b/spec/unit/crypto/algorithms/megolm.spec.js index d949b1bed58..c63031f65a4 100644 --- a/spec/unit/crypto/algorithms/megolm.spec.js +++ b/spec/unit/crypto/algorithms/megolm.spec.js @@ -462,7 +462,7 @@ describe("MegolmDecryption", function() { let run = false; aliceClient.sendToDevice = async (msgtype, contentMap) => { run = true; - expect(msgtype).toBe("org.matrix.room_key.withheld"); + expect(msgtype).toMatch(/^(org.matrix|m).room_key.withheld$/); delete contentMap["@bob:example.com"].bobdevice1.session_id; delete contentMap["@bob:example.com"].bobdevice2.session_id; expect(contentMap).toStrictEqual({ @@ -572,7 +572,7 @@ describe("MegolmDecryption", function() { const sendPromise = new Promise((resolve, reject) => { aliceClient.sendToDevice = async (msgtype, contentMap) => { - expect(msgtype).toBe("org.matrix.room_key.withheld"); + expect(msgtype).toMatch(/^(org.matrix|m).room_key.withheld$/); expect(contentMap).toStrictEqual({ '@bob:example.com': { bobdevice: { @@ -619,7 +619,7 @@ describe("MegolmDecryption", function() { content: { algorithm: "m.megolm.v1.aes-sha2", room_id: roomId, - session_id: "session_id", + session_id: "session_id1", sender_key: bobDevice.deviceCurve25519Key, code: "m.blacklisted", reason: "You have been blocked", @@ -636,7 +636,34 @@ describe("MegolmDecryption", function() { ciphertext: "blablabla", device_id: "bobdevice", sender_key: bobDevice.deviceCurve25519Key, - session_id: "session_id", + session_id: "session_id1", + }, + }))).rejects.toThrow("The sender has blocked you."); + + aliceClient.crypto.onToDeviceEvent(new MatrixEvent({ + type: "m.room_key.withheld", + sender: "@bob:example.com", + content: { + algorithm: "m.megolm.v1.aes-sha2", + room_id: roomId, + session_id: "session_id2", + sender_key: bobDevice.deviceCurve25519Key, + code: "m.blacklisted", + reason: "You have been blocked", + }, + })); + + await expect(aliceClient.crypto.decryptEvent(new MatrixEvent({ + type: "m.room.encrypted", + sender: "@bob:example.com", + event_id: "$event", + room_id: roomId, + content: { + algorithm: "m.megolm.v1.aes-sha2", + ciphertext: "blablabla", + device_id: "bobdevice", + sender_key: bobDevice.deviceCurve25519Key, + session_id: "session_id2", }, }))).rejects.toThrow("The sender has blocked you."); }); @@ -665,7 +692,7 @@ describe("MegolmDecryption", function() { content: { algorithm: "m.megolm.v1.aes-sha2", room_id: roomId, - session_id: "session_id", + session_id: "session_id1", sender_key: bobDevice.deviceCurve25519Key, code: "m.no_olm", reason: "Unable to establish a secure channel.", @@ -686,7 +713,39 @@ describe("MegolmDecryption", function() { ciphertext: "blablabla", device_id: "bobdevice", sender_key: bobDevice.deviceCurve25519Key, - session_id: "session_id", + session_id: "session_id1", + }, + origin_server_ts: now, + }))).rejects.toThrow("The sender was unable to establish a secure channel."); + + aliceClient.crypto.onToDeviceEvent(new MatrixEvent({ + type: "m.room_key.withheld", + sender: "@bob:example.com", + content: { + algorithm: "m.megolm.v1.aes-sha2", + room_id: roomId, + session_id: "session_id2", + sender_key: bobDevice.deviceCurve25519Key, + code: "m.no_olm", + reason: "Unable to establish a secure channel.", + }, + })); + + await new Promise((resolve) => { + setTimeout(resolve, 100); + }); + + await expect(aliceClient.crypto.decryptEvent(new MatrixEvent({ + type: "m.room.encrypted", + sender: "@bob:example.com", + event_id: "$event", + room_id: roomId, + content: { + algorithm: "m.megolm.v1.aes-sha2", + ciphertext: "blablabla", + device_id: "bobdevice", + sender_key: bobDevice.deviceCurve25519Key, + session_id: "session_id2", }, origin_server_ts: now, }))).rejects.toThrow("The sender was unable to establish a secure channel."); diff --git a/src/crypto/algorithms/megolm.ts b/src/crypto/algorithms/megolm.ts index 31beb64ef2c..f960dd4f15e 100644 --- a/src/crypto/algorithms/megolm.ts +++ b/src/crypto/algorithms/megolm.ts @@ -709,6 +709,7 @@ class MegolmEncryption extends EncryptionAlgorithm { } await this.baseApis.sendToDevice("org.matrix.room_key.withheld", contentMap); + await this.baseApis.sendToDevice("m.room_key.withheld", contentMap); // record the fact that we notified these blocked devices for (const userId of Object.keys(contentMap)) { diff --git a/src/crypto/index.ts b/src/crypto/index.ts index 14771da4312..f5676f37e65 100644 --- a/src/crypto/index.ts +++ b/src/crypto/index.ts @@ -3115,7 +3115,8 @@ export class Crypto extends TypedEventEmitter