Skip to content

Commit

Permalink
feat(legacy crypto!): remove {get,set}GlobalErrorOnUnknownDevices
Browse files Browse the repository at this point in the history
`globalErrorOnUnknownDevices` is not used in the rust-crypto. The API is marked as unstable, we can remove it.
  • Loading branch information
florianduros committed Jan 28, 2025
1 parent 8fc29ae commit c5d6e35
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 125 deletions.
89 changes: 1 addition & 88 deletions spec/integ/crypto/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ import {
establishOlmSession,
getTestOlmAccountKeys,
} from "./olm-utils";
import { ToDevicePayload } from "../../../src/models/ToDeviceMessage";
import { AccountDataAccumulator } from "../../test-utils/AccountDataAccumulator";
import { UNSIGNED_MEMBERSHIP_FIELD } from "../../../src/@types/event";
import { KnownMembership } from "../../../src/@types/membership";
Expand Down Expand Up @@ -995,7 +994,6 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
keyResponder.addDeviceKeys(testDeviceKeys);

await startClientAndAwaitFirstSync();
aliceClient.setGlobalErrorOnUnknownDevices(false);

// tell alice she is sharing a room with bob
syncResponder.sendOrQueueSyncResponse(getSyncResponse(["@bob:xyz"]));
Expand All @@ -1013,8 +1011,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
await expectSendRoomKey("@bob:xyz", testOlmAccount);
});

it("Alice sends a megolm message with GlobalErrorOnUnknownDevices=false", async () => {
aliceClient.setGlobalErrorOnUnknownDevices(false);
it("Alice sends a megolm message", async () => {
const homeserverUrl = aliceClient.getHomeserverUrl();
const keyResponder = new E2EKeyResponder(homeserverUrl);
keyResponder.addKeyReceiver("@alice:localhost", keyReceiver);
Expand Down Expand Up @@ -1042,7 +1039,6 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
});

it("We should start a new megolm session after forceDiscardSession", async () => {
aliceClient.setGlobalErrorOnUnknownDevices(false);
const homeserverUrl = aliceClient.getHomeserverUrl();
const keyResponder = new E2EKeyResponder(homeserverUrl);
keyResponder.addKeyReceiver("@alice:localhost", keyReceiver);
Expand Down Expand Up @@ -1077,87 +1073,6 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
await Promise.all([aliceClient.sendTextMessage(ROOM_ID, "test2"), p2]);
});

describe("get|setGlobalErrorOnUnknownDevices", () => {
it("should raise an error if crypto is disabled", () => {
aliceClient["cryptoBackend"] = undefined;
expect(() => aliceClient.setGlobalErrorOnUnknownDevices(true)).toThrow("encryption disabled");
expect(() => aliceClient.getGlobalErrorOnUnknownDevices()).toThrow("encryption disabled");
});

oldBackendOnly("should permit sending to unknown devices", async () => {
expect(aliceClient.getGlobalErrorOnUnknownDevices()).toBeTruthy();

expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
await startClientAndAwaitFirstSync();
const p2pSession = await establishOlmSession(aliceClient, keyReceiver, syncResponder, testOlmAccount);

syncResponder.sendOrQueueSyncResponse(getSyncResponse(["@bob:xyz"]));
await syncPromise(aliceClient);

// start out with the device unknown - the send should be rejected.
expectAliceKeyQuery(getTestKeysQueryResponse("@bob:xyz"));
expectAliceKeyQuery(getTestKeysQueryResponse("@bob:xyz"));

await aliceClient.sendTextMessage(ROOM_ID, "test").then(
() => {
throw new Error("sendTextMessage failed on an unknown device");
},
(e) => {
expect(e.name).toEqual("UnknownDeviceError");
},
);

// enable sending to unknown devices, and resend
aliceClient.setGlobalErrorOnUnknownDevices(false);
expect(aliceClient.getGlobalErrorOnUnknownDevices()).toBeFalsy();

const room = aliceClient.getRoom(ROOM_ID)!;
const pendingMsg = room.getPendingEvents()[0];

const inboundGroupSessionPromise = expectSendRoomKey("@bob:xyz", testOlmAccount, p2pSession);

await Promise.all([
aliceClient.resendEvent(pendingMsg, room),
expectSendMegolmMessage(inboundGroupSessionPromise),
]);
});
});

describe("get|setGlobalBlacklistUnverifiedDevices", () => {
it("should send a m.unverified code in toDevice messages to an unverified device when globalBlacklistUnverifiedDevices=true", async () => {
aliceClient.getCrypto()!.globalBlacklistUnverifiedDevices = true;

expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
await startClientAndAwaitFirstSync();
await establishOlmSession(aliceClient, keyReceiver, syncResponder, testOlmAccount);

// Tell alice we share a room with bob
syncResponder.sendOrQueueSyncResponse(getSyncResponse(["@bob:xyz"]));
await syncPromise(aliceClient);

// Force alice to download bob keys
expectAliceKeyQuery(getTestKeysQueryResponse("@bob:xyz"));

// Wait to receive the toDevice message and return bob device content
const toDevicePromise = new Promise<ToDevicePayload>((resolve) => {
fetchMock.putOnce(new RegExp("/sendToDevice/m.room_key.withheld/"), (url, request) => {
const content = JSON.parse(request.body as string);
resolve(content.messages["@bob:xyz"]["DEVICE_ID"]);
return {};
});
});

// Mock endpoint of message sending
fetchMock.put(new RegExp("/send/"), { event_id: "$event_id" });

await aliceClient.sendTextMessage(ROOM_ID, "test");

// Finally, check that the toDevice message has the m.unverified code
const toDeviceContent = await toDevicePromise;
expect(toDeviceContent.code).toBe("m.unverified");
});
});

describe("Session should rotate according to encryption settings", () => {
/**
* Send a message to bob and get the encrypted message
Expand Down Expand Up @@ -1475,7 +1390,6 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,

describe("getEncryptionInfoForEvent", () => {
it("handles outgoing events", async () => {
aliceClient.setGlobalErrorOnUnknownDevices(false);
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
await startClientAndAwaitFirstSync();

Expand Down Expand Up @@ -1579,7 +1493,6 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
// set up the aliceTestClient so that it is a room with no known members
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
await startClientAndAwaitFirstSync({ lazyLoadMembers: true });
aliceClient.setGlobalErrorOnUnknownDevices(false);

syncResponder.sendOrQueueSyncResponse(getSyncResponse([]));
await syncPromise(aliceClient);
Expand Down
2 changes: 0 additions & 2 deletions spec/integ/crypto/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,6 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
describe("Send verification request in DM", () => {
beforeEach(async () => {
aliceClient = await startTestClient();
aliceClient.setGlobalErrorOnUnknownDevices(false);

e2eKeyResponder.addCrossSigningData(BOB_SIGNED_CROSS_SIGNING_KEYS_DATA);
e2eKeyResponder.addDeviceKeys(BOB_SIGNED_TEST_DEVICE_DATA);
Expand Down Expand Up @@ -989,7 +988,6 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
testOlmAccount.create();

aliceClient = await startTestClient();
aliceClient.setGlobalErrorOnUnknownDevices(false);
syncResponder.sendOrQueueSyncResponse(getSyncResponse([BOB_TEST_USER_ID]));
await syncPromise(aliceClient);

Expand Down
35 changes: 0 additions & 35 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1970,41 +1970,6 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return this.cryptoBackend;
}

/**
* Set whether sendMessage in a room with unknown and unverified devices
* should throw an error and not send them message. This has 'Global' for
* symmetry with setGlobalBlacklistUnverifiedDevices but there is currently
* no room-level equivalent for this setting.
*
* This API is currently UNSTABLE and may change or be removed without notice.
*
* It has no effect with the Rust crypto implementation.
*
* @param value - whether error on unknown devices
*
* ```ts
* client.getCrypto().globalErrorOnUnknownDevices = value;
* ```
*/
public setGlobalErrorOnUnknownDevices(value: boolean): void {
if (!this.cryptoBackend) {
throw new Error("End-to-end encryption disabled");
}
this.cryptoBackend.globalErrorOnUnknownDevices = value;
}

/**
* @returns whether to error on unknown devices
*
* This API is currently UNSTABLE and may change or be removed without notice.
*/
public getGlobalErrorOnUnknownDevices(): boolean {
if (!this.cryptoBackend) {
throw new Error("End-to-end encryption disabled");
}
return this.cryptoBackend.globalErrorOnUnknownDevices;
}

/**
* Whether encryption is enabled for a room.
* @param roomId - the room id to query.
Expand Down

0 comments on commit c5d6e35

Please sign in to comment.