From e26245b0fc01ce2170348acc2e2ced42e4a04078 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Wed, 9 Oct 2024 18:16:45 +0200 Subject: [PATCH 01/13] Remove `initCrypto` mocking --- test/MatrixClientPeg-test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/MatrixClientPeg-test.ts b/test/MatrixClientPeg-test.ts index 9634a6a54f..3db1ff243d 100644 --- a/test/MatrixClientPeg-test.ts +++ b/test/MatrixClientPeg-test.ts @@ -83,12 +83,10 @@ describe("MatrixClientPeg", () => { it("should initialise the rust crypto library by default", async () => { const mockSetValue = jest.spyOn(SettingsStore, "setValue").mockResolvedValue(undefined); - const mockInitCrypto = jest.spyOn(testPeg.safeGet(), "initCrypto").mockResolvedValue(undefined); const mockInitRustCrypto = jest.spyOn(testPeg.safeGet(), "initRustCrypto").mockResolvedValue(undefined); const cryptoStoreKey = new Uint8Array([1, 2, 3, 4]); await testPeg.start({ rustCryptoStoreKey: cryptoStoreKey }); - expect(mockInitCrypto).not.toHaveBeenCalled(); expect(mockInitRustCrypto).toHaveBeenCalledWith({ storageKey: cryptoStoreKey }); // we should have stashed the setting in the settings store From 5660b2adf64840b65b9c2c10a887db01351643d1 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Thu, 10 Oct 2024 17:31:47 +0200 Subject: [PATCH 02/13] Remove `MatrixClient.downloadKeys` mocking --- test/components/views/right_panel/UserInfo-test.tsx | 1 - test/test-utils/test-utils.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/test/components/views/right_panel/UserInfo-test.tsx b/test/components/views/right_panel/UserInfo-test.tsx index 32b50c99e7..c53eaef2cf 100644 --- a/test/components/views/right_panel/UserInfo-test.tsx +++ b/test/components/views/right_panel/UserInfo-test.tsx @@ -160,7 +160,6 @@ beforeEach(() => { getRoom: jest.fn(), credentials: {}, setPowerLevel: jest.fn(), - downloadKeys: jest.fn(), getCrypto: jest.fn().mockReturnValue(mockCrypto), } as unknown as MatrixClient); diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index acf2a9d6e0..7648c20f81 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -214,7 +214,6 @@ export function createTestClient(): MatrixClient { }), hasLazyLoadMembersEnabled: jest.fn().mockReturnValue(false), isInitialSyncComplete: jest.fn().mockReturnValue(true), - downloadKeys: jest.fn(), fetchRoomEvent: jest.fn().mockRejectedValue({}), makeTxnId: jest.fn().mockImplementation(() => `t${txnId++}`), sendToDevice: jest.fn().mockResolvedValue(undefined), From 18ae8116fc1582d8fa803b85c96ebdeb91d24221 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Thu, 10 Oct 2024 17:48:09 +0200 Subject: [PATCH 03/13] Remove `MatrixClient.getStoredDevice` mocking --- .../settings/tabs/user/SessionManagerTab-test.tsx | 14 -------------- test/test-utils/test-utils.ts | 2 -- test/toasts/UnverifiedSessionToast-test.tsx | 9 --------- 3 files changed, 25 deletions(-) diff --git a/test/components/views/settings/tabs/user/SessionManagerTab-test.tsx b/test/components/views/settings/tabs/user/SessionManagerTab-test.tsx index 37186d33b5..d9d3d99cfe 100644 --- a/test/components/views/settings/tabs/user/SessionManagerTab-test.tsx +++ b/test/components/views/settings/tabs/user/SessionManagerTab-test.tsx @@ -17,7 +17,6 @@ import { waitForElementToBeRemoved, within, } from "jest-matrix-react"; -import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo"; import { logger } from "matrix-js-sdk/src/logger"; import { CryptoApi, DeviceVerificationStatus, VerificationRequest } from "matrix-js-sdk/src/crypto-api"; import { defer, sleep } from "matrix-js-sdk/src/utils"; @@ -202,7 +201,6 @@ describe("", () => { ...mockClientMethodsServer(), getCrypto: jest.fn().mockReturnValue(mockCrypto), getDevices: jest.fn(), - getStoredDevice: jest.fn(), getDeviceId: jest.fn().mockReturnValue(deviceId), deleteMultipleDevices: jest.fn(), generateClientSecret: jest.fn(), @@ -217,10 +215,6 @@ describe("", () => { }); jest.clearAllMocks(); jest.spyOn(logger, "error").mockRestore(); - mockClient.getStoredDevice.mockImplementation((_userId, id) => { - const device = [alicesDevice, alicesMobileDevice].find((device) => device.device_id === id); - return device ? new DeviceInfo(device.device_id) : null; - }); mockCrypto.getDeviceVerificationStatus.mockReset().mockResolvedValue(new DeviceVerificationStatus({})); mockClient.getDevices.mockReset().mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice] }); @@ -289,7 +283,6 @@ describe("", () => { mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice, alicesOlderMobileDevice], }); - mockClient.getStoredDevice.mockImplementation((_userId, deviceId) => new DeviceInfo(deviceId)); mockCrypto.getDeviceVerificationStatus.mockImplementation(async (_userId, deviceId) => { // alices device is trusted if (deviceId === alicesDevice.device_id) { @@ -461,7 +454,6 @@ describe("", () => { mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice], }); - mockClient.getStoredDevice.mockImplementation(() => new DeviceInfo(alicesDevice.device_id)); mockCrypto.getDeviceVerificationStatus.mockResolvedValue( new DeviceVerificationStatus({ crossSigningVerified: true, localVerified: true }), ); @@ -565,7 +557,6 @@ describe("", () => { mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice], }); - mockClient.getStoredDevice.mockImplementation((_userId, deviceId) => new DeviceInfo(deviceId)); mockCrypto.getDeviceVerificationStatus.mockImplementation(async (_userId, deviceId) => { if (deviceId === alicesDevice.device_id) { return new DeviceVerificationStatus({ crossSigningVerified: true, localVerified: true }); @@ -592,7 +583,6 @@ describe("", () => { mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice], }); - mockClient.getStoredDevice.mockImplementation((_userId, deviceId) => new DeviceInfo(deviceId)); mockCrypto.getDeviceVerificationStatus.mockImplementation(async (_userId, deviceId) => { // current session verified = able to verify other sessions if (deviceId === alicesDevice.device_id) { @@ -626,7 +616,6 @@ describe("", () => { mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice], }); - mockClient.getStoredDevice.mockImplementation((_userId, deviceId) => new DeviceInfo(deviceId)); mockCrypto.getDeviceVerificationStatus.mockImplementation(async (_userId, deviceId) => { if (deviceId === alicesDevice.device_id) { return new DeviceVerificationStatus({ crossSigningVerified: true, localVerified: true }); @@ -664,7 +653,6 @@ describe("", () => { mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice, alicesDehydratedDevice], }); - mockClient.getStoredDevice.mockImplementation((_userId, deviceId) => new DeviceInfo(deviceId)); const devicesMap = new Map([ [alicesDeviceObj.deviceId, alicesDeviceObj], @@ -705,7 +693,6 @@ describe("", () => { mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice, alicesDehydratedDevice], }); - mockClient.getStoredDevice.mockImplementation((_userId, deviceId) => new DeviceInfo(deviceId)); const devicesMap = new Map([ [alicesDeviceObj.deviceId, alicesDeviceObj], @@ -746,7 +733,6 @@ describe("", () => { mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice, alicesDehydratedDevice, alicesOtherDehydratedDevice], }); - mockClient.getStoredDevice.mockImplementation((_userId, deviceId) => new DeviceInfo(deviceId)); const devicesMap = new Map([ [alicesDeviceObj.deviceId, alicesDeviceObj], diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index 7648c20f81..b1f46b31bc 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -95,8 +95,6 @@ export function createTestClient(): MatrixClient { getUser: jest.fn().mockReturnValue({ on: jest.fn(), off: jest.fn() }), getDevice: jest.fn(), getDeviceId: jest.fn().mockReturnValue("ABCDEFGHI"), - getStoredCrossSigningForUser: jest.fn(), - getStoredDevice: jest.fn(), requestVerification: jest.fn(), deviceId: "ABCDEFGHI", getDevices: jest.fn().mockResolvedValue({ devices: [{ device_id: "ABCDEFGHI" }] }), diff --git a/test/toasts/UnverifiedSessionToast-test.tsx b/test/toasts/UnverifiedSessionToast-test.tsx index 18f69aa9bc..2f009911c4 100644 --- a/test/toasts/UnverifiedSessionToast-test.tsx +++ b/test/toasts/UnverifiedSessionToast-test.tsx @@ -11,7 +11,6 @@ import { render, RenderResult, screen } from "jest-matrix-react"; import userEvent from "@testing-library/user-event"; import { mocked, Mocked } from "jest-mock"; import { IMyDevice, MatrixClient } from "matrix-js-sdk/src/matrix"; -import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo"; import { CryptoApi, DeviceVerificationStatus } from "matrix-js-sdk/src/crypto-api"; import dis from "../../src/dispatcher/dispatcher"; @@ -25,7 +24,6 @@ describe("UnverifiedSessionToast", () => { const otherDevice: IMyDevice = { device_id: "ABC123", }; - const otherDeviceInfo = new DeviceInfo(otherDevice.device_id); let client: Mocked; let renderResult: RenderResult; @@ -40,13 +38,6 @@ describe("UnverifiedSessionToast", () => { throw new Error(`Unknown device ${deviceId}`); }); - client.getStoredDevice.mockImplementation((userId: string, deviceId: string) => { - if (deviceId === otherDevice.device_id) { - return otherDeviceInfo; - } - - return null; - }); client.getCrypto.mockReturnValue({ getDeviceVerificationStatus: jest .fn() From fab2a3be51092086cd896221a6947a6fe22428d9 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Thu, 10 Oct 2024 18:01:23 +0200 Subject: [PATCH 04/13] Remove `MatrixClient.requestVerification` call --- src/verification.ts | 2 -- test/test-utils/test-utils.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/src/verification.ts b/src/verification.ts index 5b6d011ba1..a34f023341 100644 --- a/src/verification.ts +++ b/src/verification.ts @@ -77,8 +77,6 @@ export async function legacyVerifyUser(matrixClient: MatrixClient, user: User): return; } } - const verificationRequestPromise = matrixClient.requestVerification(user.userId); - setRightPanel({ member: user, verificationRequestPromise }); } export async function verifyUser(matrixClient: MatrixClient, user: User): Promise { diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index b1f46b31bc..25b54cc128 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -95,7 +95,6 @@ export function createTestClient(): MatrixClient { getUser: jest.fn().mockReturnValue({ on: jest.fn(), off: jest.fn() }), getDevice: jest.fn(), getDeviceId: jest.fn().mockReturnValue("ABCDEFGHI"), - requestVerification: jest.fn(), deviceId: "ABCDEFGHI", getDevices: jest.fn().mockResolvedValue({ devices: [{ device_id: "ABCDEFGHI" }] }), getSessionId: jest.fn().mockReturnValue("iaszphgvfku"), From dfa9eda10e92749d62b8d174d8052c7c6d09569b Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Thu, 10 Oct 2024 18:50:49 +0200 Subject: [PATCH 05/13] Replace `MatrixClient.setGlobalBlacklistUnverifiedDevices` by `MatrixClient.CryptoApi.globalBlacklistUnverifiedDevices` --- src/components/structures/MatrixChat.tsx | 5 +++-- src/components/views/settings/CryptographyPanel.tsx | 3 ++- test/components/structures/MatrixChat-test.tsx | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx index 32d3bc7bd6..bbb1b77c9d 100644 --- a/src/components/structures/MatrixChat.tsx +++ b/src/components/structures/MatrixChat.tsx @@ -1707,9 +1707,10 @@ export default class MatrixChat extends React.PureComponent { } } - if (cli.getCrypto()) { + const crypto = cli.getCrypto(); + if (crypto) { const blacklistEnabled = SettingsStore.getValueAt(SettingLevel.DEVICE, "blacklistUnverifiedDevices"); - cli.setGlobalBlacklistUnverifiedDevices(blacklistEnabled); + crypto.globalBlacklistUnverifiedDevices = blacklistEnabled; // With cross-signing enabled, we send to unknown devices // without prompting. Any bad-device status the user should diff --git a/src/components/views/settings/CryptographyPanel.tsx b/src/components/views/settings/CryptographyPanel.tsx index 853a75e6b1..08917d215c 100644 --- a/src/components/views/settings/CryptographyPanel.tsx +++ b/src/components/views/settings/CryptographyPanel.tsx @@ -140,6 +140,7 @@ export default class CryptographyPanel extends React.Component { }; private updateBlacklistDevicesFlag = (checked: boolean): void => { - MatrixClientPeg.safeGet().setGlobalBlacklistUnverifiedDevices(checked); + const crypto = MatrixClientPeg.safeGet().getCrypto(); + if (crypto) crypto.globalBlacklistUnverifiedDevices = checked; }; } diff --git a/test/components/structures/MatrixChat-test.tsx b/test/components/structures/MatrixChat-test.tsx index 4d1f78af97..432494b0e3 100644 --- a/test/components/structures/MatrixChat-test.tsx +++ b/test/components/structures/MatrixChat-test.tsx @@ -125,7 +125,6 @@ describe("", () => { }), getVisibleRooms: jest.fn().mockReturnValue([]), getRooms: jest.fn().mockReturnValue([]), - setGlobalBlacklistUnverifiedDevices: jest.fn(), setGlobalErrorOnUnknownDevices: jest.fn(), getCrypto: jest.fn().mockReturnValue({ getVerificationRequestsToDeviceInProgress: jest.fn().mockReturnValue([]), @@ -135,6 +134,7 @@ describe("", () => { getVersion: jest.fn().mockReturnValue("1"), setDeviceIsolationMode: jest.fn(), userHasCrossSigningKeys: jest.fn(), + globalBlacklistUnverifiedDevices: false, }), // This needs to not finish immediately because we need to test the screen appears bootstrapCrossSigning: jest.fn().mockImplementation(() => bootstrapDeferred.promise), From 6fe7322229b03edcb23ee77179710c13ad5f6bed Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Thu, 10 Oct 2024 18:59:15 +0200 Subject: [PATCH 06/13] Remove `MatrixClient.getStoredCrossSigningForUser` mocking --- test/test-utils/client.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test-utils/client.ts b/test/test-utils/client.ts index 7b0e22e70e..3170bcf220 100644 --- a/test/test-utils/client.ts +++ b/test/test-utils/client.ts @@ -144,7 +144,6 @@ export const mockClientMethodsCrypto = (): Partial< isCrossSigningReady: jest.fn(), isKeyBackupKeyStored: jest.fn(), getCrossSigningCacheCallbacks: jest.fn().mockReturnValue({ getCrossSigningKeyCache: jest.fn() }), - getStoredCrossSigningForUser: jest.fn(), getKeyBackupVersion: jest.fn().mockResolvedValue(null), secretStorage: { hasKey: jest.fn() }, getCrypto: jest.fn().mockReturnValue({ From 3640841f4e0b11fdd6cfef940bb7798efb9874cc Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Thu, 10 Oct 2024 19:05:36 +0200 Subject: [PATCH 07/13] Replace `MatrixClient.legacyDeviceVerification` by `MatrixClient.CryptoApi.requestDeviceVerification` --- src/verification.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/verification.ts b/src/verification.ts index a34f023341..952ac7e59f 100644 --- a/src/verification.ts +++ b/src/verification.ts @@ -7,7 +7,6 @@ Please see LICENSE files in the repository root for full details. */ import { User, MatrixClient, RoomMember } from "matrix-js-sdk/src/matrix"; -import { VerificationMethod } from "matrix-js-sdk/src/types"; import { CrossSigningKey, VerificationRequest } from "matrix-js-sdk/src/crypto-api"; import dis from "./dispatcher/dispatcher"; @@ -50,11 +49,9 @@ export async function verifyDevice(matrixClient: MatrixClient, user: User, devic device, onFinished: async (action): Promise => { if (action === "sas") { - const verificationRequestPromise = matrixClient.legacyDeviceVerification( - user.userId, - device.deviceId, - VerificationMethod.Sas, - ); + const verificationRequestPromise = matrixClient + .getCrypto() + ?.requestDeviceVerification(user.userId, device.deviceId); setRightPanel({ member: user, verificationRequestPromise }); } else if (action === "legacy") { Modal.createDialog(ManualDeviceKeyVerificationDialog, { From a076c6769740e57f91585cd364c87c517ccc198f Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Mon, 14 Oct 2024 14:45:57 +0200 Subject: [PATCH 08/13] Remove `MatrixClient.isCrossSigningReady` mock --- test/components/views/settings/CrossSigningPanel-test.tsx | 1 - test/test-utils/client.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/test/components/views/settings/CrossSigningPanel-test.tsx b/test/components/views/settings/CrossSigningPanel-test.tsx index 3252ee84a7..cb0dbde8b3 100644 --- a/test/components/views/settings/CrossSigningPanel-test.tsx +++ b/test/components/views/settings/CrossSigningPanel-test.tsx @@ -34,7 +34,6 @@ describe("", () => { }); mockClient.doesServerSupportUnstableFeature.mockResolvedValue(true); - mockClient.isCrossSigningReady.mockResolvedValue(false); }); afterEach(() => { diff --git a/test/test-utils/client.ts b/test/test-utils/client.ts index 3170bcf220..0a5798d8a1 100644 --- a/test/test-utils/client.ts +++ b/test/test-utils/client.ts @@ -141,7 +141,6 @@ export const mockClientMethodsDevice = ( export const mockClientMethodsCrypto = (): Partial< Record & PropertyLikeKeys, unknown> > => ({ - isCrossSigningReady: jest.fn(), isKeyBackupKeyStored: jest.fn(), getCrossSigningCacheCallbacks: jest.fn().mockReturnValue({ getCrossSigningKeyCache: jest.fn() }), getKeyBackupVersion: jest.fn().mockResolvedValue(null), From 400ddab10f8268593b5c05e1116af974bcc386ab Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Mon, 14 Oct 2024 14:48:35 +0200 Subject: [PATCH 09/13] Replace `MatrixClient.bootstrapCrossSigning` by `MatrixClient.getCrypto.bootstrapCrossSigning` --- .../views/dialogs/security/AccessSecretStorageDialog.tsx | 2 +- .../views/dialogs/security/CreateCrossSigningDialog.tsx | 2 +- test/components/structures/MatrixChat-test.tsx | 6 ++++-- test/test-utils/test-utils.ts | 1 - 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/components/views/dialogs/security/AccessSecretStorageDialog.tsx b/src/components/views/dialogs/security/AccessSecretStorageDialog.tsx index 412d88c704..6c094e7374 100644 --- a/src/components/views/dialogs/security/AccessSecretStorageDialog.tsx +++ b/src/components/views/dialogs/security/AccessSecretStorageDialog.tsx @@ -229,7 +229,7 @@ export default class AccessSecretStorageDialog extends React.PureComponent => { // Now reset cross-signing so everything Just Works™ again. const cli = MatrixClientPeg.safeGet(); - await cli.bootstrapCrossSigning({ + await cli.getCrypto()?.bootstrapCrossSigning({ authUploadDeviceSigningKeys: async (makeRequest): Promise => { const { finished } = Modal.createDialog(InteractiveAuthDialog, { title: _t("encryption|bootstrap_title"), diff --git a/src/components/views/dialogs/security/CreateCrossSigningDialog.tsx b/src/components/views/dialogs/security/CreateCrossSigningDialog.tsx index bc5bc6b21e..69b13a93c9 100644 --- a/src/components/views/dialogs/security/CreateCrossSigningDialog.tsx +++ b/src/components/views/dialogs/security/CreateCrossSigningDialog.tsx @@ -137,7 +137,7 @@ export default class CreateCrossSigningDialog extends React.PureComponent", () => { setDeviceIsolationMode: jest.fn(), userHasCrossSigningKeys: jest.fn(), globalBlacklistUnverifiedDevices: false, + // This needs to not finish immediately because we need to test the screen appears + bootstrapCrossSigning: jest.fn().mockImplementation(() => bootstrapDeferred.promise), }), - // This needs to not finish immediately because we need to test the screen appears - bootstrapCrossSigning: jest.fn().mockImplementation(() => bootstrapDeferred.promise), secretStorage: { isStored: jest.fn().mockReturnValue(null), }, @@ -1010,6 +1010,8 @@ describe("", () => { .mockResolvedValue(new UserVerificationStatus(false, false, false)), setDeviceIsolationMode: jest.fn(), userHasCrossSigningKeys: jest.fn().mockResolvedValue(false), + // This needs to not finish immediately because we need to test the screen appears + bootstrapCrossSigning: jest.fn().mockImplementation(() => bootstrapDeferred.promise), }; loginClient.getCrypto.mockReturnValue(mockCrypto as any); }); diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index 25b54cc128..b6668f4529 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -99,7 +99,6 @@ export function createTestClient(): MatrixClient { getDevices: jest.fn().mockResolvedValue({ devices: [{ device_id: "ABCDEFGHI" }] }), getSessionId: jest.fn().mockReturnValue("iaszphgvfku"), credentials: { userId: "@userId:matrix.org" }, - bootstrapCrossSigning: jest.fn(), hasSecretStorageKey: jest.fn(), getKeyBackupVersion: jest.fn(), From 6892035f41699c56d83c909cd566800116e165a8 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Mon, 14 Oct 2024 14:55:55 +0200 Subject: [PATCH 10/13] Replace `MatrixClient.getCryptoTrustCrossSignedDevices` by `MatrixClient.getCrypto.getTrustCrossSignedDevices` --- src/verification.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/verification.ts b/src/verification.ts index 952ac7e59f..24ee2469f7 100644 --- a/src/verification.ts +++ b/src/verification.ts @@ -38,7 +38,7 @@ export async function verifyDevice(matrixClient: MatrixClient, user: User, devic return; } // if cross-signing is not explicitly disabled, check if it should be enabled first. - if (matrixClient.getCryptoTrustCrossSignedDevices()) { + if (matrixClient.getCrypto()?.getTrustCrossSignedDevices()) { if (!(await enable4SIfNeeded(matrixClient))) { return; } @@ -69,7 +69,7 @@ export async function legacyVerifyUser(matrixClient: MatrixClient, user: User): return; } // if cross-signing is not explicitly disabled, check if it should be enabled first. - if (matrixClient.getCryptoTrustCrossSignedDevices()) { + if (matrixClient.getCrypto()?.getTrustCrossSignedDevices()) { if (!(await enable4SIfNeeded(matrixClient))) { return; } From bdfc4a4cd28f14ad8574fd4b021e66b9302a8902 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Mon, 14 Oct 2024 18:22:22 +0200 Subject: [PATCH 11/13] Replace `MatrixClient.hasSecretStorageKey` by `MatrixClient.SecretStorage.hasKey` --- .../views/dialogs/security/CreateKeyBackupDialog.tsx | 2 +- .../views/dialogs/security/RestoreKeyBackupDialog.tsx | 2 +- .../views/dialogs/security/CreateKeyBackupDialog-test.tsx | 4 ++-- test/test-utils/test-utils.ts | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/async-components/views/dialogs/security/CreateKeyBackupDialog.tsx b/src/async-components/views/dialogs/security/CreateKeyBackupDialog.tsx index 3ec62c3df5..9608abc4bc 100644 --- a/src/async-components/views/dialogs/security/CreateKeyBackupDialog.tsx +++ b/src/async-components/views/dialogs/security/CreateKeyBackupDialog.tsx @@ -69,7 +69,7 @@ export default class CreateKeyBackupDialog extends React.PureComponent { it("should display an error message when backup creation failed", async () => { const matrixClient = createTestClient(); - mocked(matrixClient.hasSecretStorageKey).mockResolvedValue(true); + jest.spyOn(matrixClient.secretStorage, "hasKey").mockResolvedValue(true); mocked(matrixClient.getCrypto()!.resetKeyBackup).mockImplementation(() => { throw new Error("failed"); }); @@ -49,7 +49,7 @@ describe("CreateKeyBackupDialog", () => { it("should display an error message when there is no Crypto available", async () => { const matrixClient = createTestClient(); - mocked(matrixClient.hasSecretStorageKey).mockResolvedValue(true); + jest.spyOn(matrixClient.secretStorage, "hasKey").mockResolvedValue(true); mocked(matrixClient.getCrypto).mockReturnValue(undefined); MatrixClientPeg.safeGet = MatrixClientPeg.get = () => matrixClient; diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index b6668f4529..467c8b7909 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -99,13 +99,13 @@ export function createTestClient(): MatrixClient { getDevices: jest.fn().mockResolvedValue({ devices: [{ device_id: "ABCDEFGHI" }] }), getSessionId: jest.fn().mockReturnValue("iaszphgvfku"), credentials: { userId: "@userId:matrix.org" }, - hasSecretStorageKey: jest.fn(), getKeyBackupVersion: jest.fn(), secretStorage: { get: jest.fn(), isStored: jest.fn().mockReturnValue(false), checkKey: jest.fn().mockResolvedValue(false), + hasKey: jest.fn().mockReturnValue(false), }, store: { From 4fbbee56de7aa4261273396cea2e024056a5655f Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Mon, 14 Oct 2024 18:24:17 +0200 Subject: [PATCH 12/13] Replace `MatrixClient.getDefaultSecretStorageKeyId` by `MatrixClient.SecretStorage.getDefaultKeyId` --- src/SecurityManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SecurityManager.ts b/src/SecurityManager.ts index 2d103cf25e..4717404222 100644 --- a/src/SecurityManager.ts +++ b/src/SecurityManager.ts @@ -76,7 +76,7 @@ async function getSecretStorageKey({ keys: Record; }): Promise<[string, Uint8Array]> { const cli = MatrixClientPeg.safeGet(); - let keyId = await cli.getDefaultSecretStorageKeyId(); + let keyId = await cli.secretStorage.getDefaultKeyId(); let keyInfo!: SecretStorage.SecretStorageKeyDescription; if (keyId) { // use the default SSSS key if set From b565895b32c0ed9bc5aacd3b8f68f1d7fb72aa4e Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Tue, 15 Oct 2024 17:59:16 +0200 Subject: [PATCH 13/13] Remove `MatrixClient.encryptAndSendToDevices` call --- src/stores/widgets/StopGapWidgetDriver.ts | 49 ----------- .../widgets/StopGapWidgetDriver-test.ts | 49 ----------- .../StopGapWidgetDriver-test.ts.snap | 82 ------------------- test/test-utils/test-utils.ts | 1 - 4 files changed, 181 deletions(-) delete mode 100644 test/stores/widgets/__snapshots__/StopGapWidgetDriver-test.ts.snap diff --git a/src/stores/widgets/StopGapWidgetDriver.ts b/src/stores/widgets/StopGapWidgetDriver.ts index 88e79d97f2..bf4ee16b5d 100644 --- a/src/stores/widgets/StopGapWidgetDriver.ts +++ b/src/stores/widgets/StopGapWidgetDriver.ts @@ -414,55 +414,6 @@ export class StopGapWidgetDriver extends WidgetDriver { await client._unstable_updateDelayedEvent(delayId, action); } - public async sendToDevice( - eventType: string, - encrypted: boolean, - contentMap: { [userId: string]: { [deviceId: string]: object } }, - ): Promise { - const client = MatrixClientPeg.safeGet(); - - if (encrypted) { - const deviceInfoMap = await client.crypto!.deviceList.downloadKeys(Object.keys(contentMap), false); - - await Promise.all( - Object.entries(contentMap).flatMap(([userId, userContentMap]) => - Object.entries(userContentMap).map(async ([deviceId, content]): Promise => { - const devices = deviceInfoMap.get(userId); - if (!devices) return; - - if (deviceId === "*") { - // Send the message to all devices we have keys for - await client.encryptAndSendToDevices( - Array.from(devices.values()).map((deviceInfo) => ({ - userId, - deviceInfo, - })), - content, - ); - } else if (devices.has(deviceId)) { - // Send the message to a specific device - await client.encryptAndSendToDevices( - [{ userId, deviceInfo: devices.get(deviceId)! }], - content, - ); - } - }), - ), - ); - } else { - await client.queueToDevice({ - eventType, - batch: Object.entries(contentMap).flatMap(([userId, userContentMap]) => - Object.entries(userContentMap).map(([deviceId, content]) => ({ - userId, - deviceId, - payload: content, - })), - ), - }); - } - } - private pickRooms(roomIds?: (string | Symbols.AnyRoom)[]): Room[] { const client = MatrixClientPeg.get(); if (!client) throw new Error("Not attached to a client"); diff --git a/test/stores/widgets/StopGapWidgetDriver-test.ts b/test/stores/widgets/StopGapWidgetDriver-test.ts index 6e3387e216..4c2b86d211 100644 --- a/test/stores/widgets/StopGapWidgetDriver-test.ts +++ b/test/stores/widgets/StopGapWidgetDriver-test.ts @@ -18,7 +18,6 @@ import { MsgType, RelationType, } from "matrix-js-sdk/src/matrix"; -import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo"; import { Widget, MatrixWidgetType, @@ -171,54 +170,6 @@ describe("StopGapWidgetDriver", () => { expect(listener).toHaveBeenCalledWith(openIdUpdate); }); - describe("sendToDevice", () => { - const contentMap = { - "@alice:example.org": { - "*": { - hello: "alice", - }, - }, - "@bob:example.org": { - bobDesktop: { - hello: "bob", - }, - }, - }; - - let driver: WidgetDriver; - - beforeEach(() => { - driver = mkDefaultDriver(); - }); - - it("sends unencrypted messages", async () => { - await driver.sendToDevice("org.example.foo", false, contentMap); - expect(client.queueToDevice.mock.calls).toMatchSnapshot(); - }); - - it("sends encrypted messages", async () => { - const aliceWeb = new DeviceInfo("aliceWeb"); - const aliceMobile = new DeviceInfo("aliceMobile"); - const bobDesktop = new DeviceInfo("bobDesktop"); - - mocked(client.crypto!.deviceList).downloadKeys.mockResolvedValue( - new Map([ - [ - "@alice:example.org", - new Map([ - ["aliceWeb", aliceWeb], - ["aliceMobile", aliceMobile], - ]), - ], - ["@bob:example.org", new Map([["bobDesktop", bobDesktop]])], - ]), - ); - - await driver.sendToDevice("org.example.foo", true, contentMap); - expect(client.encryptAndSendToDevices.mock.calls).toMatchSnapshot(); - }); - }); - describe("getTurnServers", () => { let driver: WidgetDriver; diff --git a/test/stores/widgets/__snapshots__/StopGapWidgetDriver-test.ts.snap b/test/stores/widgets/__snapshots__/StopGapWidgetDriver-test.ts.snap deleted file mode 100644 index da3c19d567..0000000000 --- a/test/stores/widgets/__snapshots__/StopGapWidgetDriver-test.ts.snap +++ /dev/null @@ -1,82 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`StopGapWidgetDriver sendToDevice sends encrypted messages 1`] = ` -[ - [ - [ - { - "deviceInfo": DeviceInfo { - "algorithms": [], - "deviceId": "aliceWeb", - "keys": {}, - "known": false, - "signatures": {}, - "unsigned": {}, - "verified": 0, - }, - "userId": "@alice:example.org", - }, - { - "deviceInfo": DeviceInfo { - "algorithms": [], - "deviceId": "aliceMobile", - "keys": {}, - "known": false, - "signatures": {}, - "unsigned": {}, - "verified": 0, - }, - "userId": "@alice:example.org", - }, - ], - { - "hello": "alice", - }, - ], - [ - [ - { - "deviceInfo": DeviceInfo { - "algorithms": [], - "deviceId": "bobDesktop", - "keys": {}, - "known": false, - "signatures": {}, - "unsigned": {}, - "verified": 0, - }, - "userId": "@bob:example.org", - }, - ], - { - "hello": "bob", - }, - ], -] -`; - -exports[`StopGapWidgetDriver sendToDevice sends unencrypted messages 1`] = ` -[ - [ - { - "batch": [ - { - "deviceId": "*", - "payload": { - "hello": "alice", - }, - "userId": "@alice:example.org", - }, - { - "deviceId": "bobDesktop", - "payload": { - "hello": "bob", - }, - "userId": "@bob:example.org", - }, - ], - "eventType": "org.example.foo", - }, - ], -] -`; diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index 467c8b7909..205f80130d 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -214,7 +214,6 @@ export function createTestClient(): MatrixClient { makeTxnId: jest.fn().mockImplementation(() => `t${txnId++}`), sendToDevice: jest.fn().mockResolvedValue(undefined), queueToDevice: jest.fn().mockResolvedValue(undefined), - encryptAndSendToDevices: jest.fn().mockResolvedValue(undefined), cancelPendingEvent: jest.fn(), getMediaHandler: jest.fn().mockReturnValue({