Skip to content

Commit ce7b7bf

Browse files
authored
Element-R: Wire up globalBlacklistUnverifiedDevices field to rust crypto encryption settings (#3790)
* Wire up `globalBlacklistUnverifiedDevices` rust crypto encrypted settings * Improve test comment * Update comments * Review changes * Fix lint due to merge
1 parent 07a9eb3 commit ce7b7bf

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

spec/integ/crypto/crypto.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import {
9595
establishOlmSession,
9696
getTestOlmAccountKeys,
9797
} from "./olm-utils";
98+
import { ToDevicePayload } from "../../../src/models/ToDeviceMessage";
9899

99100
afterEach(() => {
100101
// reset fake-indexeddb after each test, to make sure we don't leak connections
@@ -943,6 +944,39 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
943944
aliceClient.sendTextMessage(ROOM_ID, "test"),
944945
]);
945946
});
947+
948+
it("should send a m.unverified code in toDevice messages to an unverified device when globalBlacklistUnverifiedDevices=true", async () => {
949+
aliceClient.getCrypto()!.globalBlacklistUnverifiedDevices = true;
950+
951+
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
952+
await startClientAndAwaitFirstSync();
953+
await establishOlmSession(aliceClient, keyReceiver, syncResponder, testOlmAccount);
954+
955+
// Tell alice we share a room with bob
956+
syncResponder.sendOrQueueSyncResponse(getSyncResponse(["@bob:xyz"]));
957+
await syncPromise(aliceClient);
958+
959+
// Force alice to download bob keys
960+
expectAliceKeyQuery(getTestKeysQueryResponse("@bob:xyz"));
961+
962+
// Wait to receive the toDevice message and return bob device content
963+
const toDevicePromise = new Promise<ToDevicePayload>((resolve) => {
964+
fetchMock.putOnce(new RegExp("/sendToDevice/m.room_key.withheld/"), (url, request) => {
965+
const content = JSON.parse(request.body as string);
966+
resolve(content.messages["@bob:xyz"]["DEVICE_ID"]);
967+
return {};
968+
});
969+
});
970+
971+
// Mock endpoint of message sending
972+
fetchMock.put(new RegExp("/send/"), { event_id: "$event_id" });
973+
974+
await aliceClient.sendTextMessage(ROOM_ID, "test");
975+
976+
// Finally, check that the toDevice message has the m.unverified code
977+
const toDeviceContent = await toDevicePromise;
978+
expect(toDeviceContent.code).toBe("m.unverified");
979+
});
946980
});
947981

948982
describe("Session should rotate according to encryption settings", () => {

src/rust-crypto/RoomEncryptor.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
RoomId,
2222
UserId,
2323
HistoryVisibility as RustHistoryVisibility,
24+
ToDeviceRequest,
2425
} from "@matrix-org/matrix-sdk-crypto-wasm";
2526

2627
import { EventType } from "../@types/event";
@@ -43,6 +44,7 @@ export class RoomEncryptor {
4344
/**
4445
* @param olmMachine - The rust-sdk's OlmMachine
4546
* @param keyClaimManager - Our KeyClaimManager, which manages the queue of one-time-key claim requests
47+
* @param outgoingRequestProcessor - The OutgoingRequestProcessor, which sends outgoing requests
4648
* @param room - The room we want to encrypt for
4749
* @param encryptionSettings - body of the m.room.encryption event currently in force in this room
4850
*/
@@ -91,8 +93,10 @@ export class RoomEncryptor {
9193
*
9294
* This ensures that we have a megolm session ready to use and that we have shared its key with all the devices
9395
* in the room.
96+
*
97+
* @param globalBlacklistUnverifiedDevices - When `true`, it will not send encrypted messages to unverified devices
9498
*/
95-
public async ensureEncryptionSession(): Promise<void> {
99+
public async ensureEncryptionSession(globalBlacklistUnverifiedDevices: boolean): Promise<void> {
96100
if (this.encryptionSettings.algorithm !== "m.megolm.v1.aes-sha2") {
97101
throw new Error(
98102
`Cannot encrypt in ${this.room.roomId} for unsupported algorithm '${this.encryptionSettings.algorithm}'`,
@@ -127,7 +131,12 @@ export class RoomEncryptor {
127131
rustEncryptionSettings.rotationPeriodMessages = BigInt(this.encryptionSettings.rotation_period_msgs);
128132
}
129133

130-
const shareMessages = await this.olmMachine.shareRoomKey(
134+
// When this.room.getBlacklistUnverifiedDevices() === null, the global settings should be used
135+
// See Room#getBlacklistUnverifiedDevices
136+
rustEncryptionSettings.onlyAllowTrustedDevices =
137+
this.room.getBlacklistUnverifiedDevices() ?? globalBlacklistUnverifiedDevices;
138+
139+
const shareMessages: ToDeviceRequest[] = await this.olmMachine.shareRoomKey(
131140
new RoomId(this.room.roomId),
132141
userList,
133142
rustEncryptionSettings,
@@ -156,9 +165,10 @@ export class RoomEncryptor {
156165
* then encrypt the event using the session.
157166
*
158167
* @param event - Event to be encrypted.
168+
* @param globalBlacklistUnverifiedDevices - When `true`, it will not send encrypted messages to unverified devices
159169
*/
160-
public async encryptEvent(event: MatrixEvent): Promise<void> {
161-
await this.ensureEncryptionSession();
170+
public async encryptEvent(event: MatrixEvent, globalBlacklistUnverifiedDevices: boolean): Promise<void> {
171+
await this.ensureEncryptionSession(globalBlacklistUnverifiedDevices);
162172

163173
const encryptedContent = await this.olmMachine.encryptRoomEvent(
164174
new RoomId(this.room.roomId),

src/rust-crypto/rust-crypto.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
282282
throw new Error(`Cannot encrypt event in unconfigured room ${roomId}`);
283283
}
284284

285-
await encryptor.encryptEvent(event);
285+
await encryptor.encryptEvent(event, this.globalBlacklistUnverifiedDevices);
286286
}
287287

288288
public async decryptEvent(event: MatrixEvent): Promise<IEventDecryptionResult> {
@@ -376,7 +376,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
376376
const encryptor = this.roomEncryptors[room.roomId];
377377

378378
if (encryptor) {
379-
encryptor.ensureEncryptionSession();
379+
encryptor.ensureEncryptionSession(this.globalBlacklistUnverifiedDevices);
380380
}
381381
}
382382

0 commit comments

Comments
 (0)