Skip to content

Commit

Permalink
do not provide a default value for DeviceIsolationMode for encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed Sep 30, 2024
1 parent a41b1f0 commit 670fe2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
32 changes: 18 additions & 14 deletions spec/unit/rust-crypto/RoomEncryptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ describe("RoomEncryptor", () => {
);
});

const defaultDevicesIsolationMode = new AllDevicesIsolationMode(false);

it("should ensure that there is only one shareRoomKey at a time", async () => {
const deferredShare = defer<void>();
const insideOlmShareRoom = defer<void>();
Expand All @@ -122,19 +124,19 @@ describe("RoomEncryptor", () => {
await deferredShare.promise;
});

roomEncryptor.prepareForEncryption(false);
roomEncryptor.prepareForEncryption(false, defaultDevicesIsolationMode);
await insideOlmShareRoom.promise;

// call several times more
roomEncryptor.prepareForEncryption(false);
roomEncryptor.encryptEvent(createMockEvent("Hello"), false);
roomEncryptor.prepareForEncryption(false);
roomEncryptor.encryptEvent(createMockEvent("World"), false);
roomEncryptor.prepareForEncryption(false, defaultDevicesIsolationMode);
roomEncryptor.encryptEvent(createMockEvent("Hello"), false, defaultDevicesIsolationMode);
roomEncryptor.prepareForEncryption(false, defaultDevicesIsolationMode);
roomEncryptor.encryptEvent(createMockEvent("World"), false, defaultDevicesIsolationMode);

expect(mockOlmMachine.shareRoomKey).toHaveBeenCalledTimes(1);

deferredShare.resolve();
await roomEncryptor.prepareForEncryption(false);
await roomEncryptor.prepareForEncryption(false, defaultDevicesIsolationMode);

// should have been called again
expect(mockOlmMachine.shareRoomKey).toHaveBeenCalledTimes(6);
Expand All @@ -160,8 +162,16 @@ describe("RoomEncryptor", () => {

let firstMessageFinished: string | null = null;

const firstRequest = roomEncryptor.encryptEvent(createMockEvent("Hello"), false);
const secondRequest = roomEncryptor.encryptEvent(createMockEvent("Edit of Hello"), false);
const firstRequest = roomEncryptor.encryptEvent(
createMockEvent("Hello"),
false,
defaultDevicesIsolationMode,
);
const secondRequest = roomEncryptor.encryptEvent(
createMockEvent("Edit of Hello"),
false,
defaultDevicesIsolationMode,
);

firstRequest.then(() => {
if (firstMessageFinished === null) {
Expand Down Expand Up @@ -254,12 +264,6 @@ describe("RoomEncryptor", () => {
});
});

it("should use Legacy as default mode", async () => {
await roomEncryptor.prepareForEncryption(false);
expect(mockOlmMachine.shareRoomKey).toHaveBeenCalled();
expect(capturedSettings?.eq(CollectStrategy.deviceBasedStrategy(false, false))).toBeTruthy();
});

it.each(testCases)(
"prepareForEncryption should properly set sharing strategy based on crypto mode: %s",
async (_, { mode, expectedStrategy, globalBlacklistUnverifiedDevices }) => {
Expand Down
6 changes: 3 additions & 3 deletions src/rust-crypto/RoomEncryptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { HistoryVisibility } from "../@types/partials.ts";
import { OutgoingRequestsManager } from "./OutgoingRequestsManager.ts";
import { logDuration } from "../utils.ts";
import { KnownMembership } from "../@types/membership.ts";
import { AllDevicesIsolationMode, DeviceIsolationMode, DeviceIsolationModeKind } from "../crypto-api/index.ts";
import { DeviceIsolationMode, DeviceIsolationModeKind } from "../crypto-api/index.ts";

/**
* RoomEncryptor: responsible for encrypting messages to a given room
Expand Down Expand Up @@ -127,7 +127,7 @@ export class RoomEncryptor {
*/
public async prepareForEncryption(
globalBlacklistUnverifiedDevices: boolean,
deviceIsolationMode: DeviceIsolationMode = new AllDevicesIsolationMode(false),
deviceIsolationMode: DeviceIsolationMode,
): Promise<void> {
// We consider a prepareForEncryption as an encryption promise as it will potentially share keys
// even if it doesn't send an event.
Expand All @@ -154,7 +154,7 @@ export class RoomEncryptor {
public encryptEvent(
event: MatrixEvent | null,
globalBlacklistUnverifiedDevices: boolean,
deviceIsolationMode: DeviceIsolationMode = new AllDevicesIsolationMode(false),
deviceIsolationMode: DeviceIsolationMode,
): Promise<void> {
const logger = new LogSpan(this.prefixedLogger, event ? (event.getTxnId() ?? "") : "prepareForEncryption");
// Ensure order of encryption to avoid message ordering issues, as the scheduler only ensures
Expand Down

0 comments on commit 670fe2e

Please sign in to comment.