From be2c1fcf64143c36c3434ae15a545df65eb4724e Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Mon, 30 Sep 2024 11:29:14 +0100 Subject: [PATCH] Add labs option to exclude unverified devices (#92) Add a labs option which will, when set, switch into the "invisible crypto" mode of refusing to send keys to, or decrypt messages from, devices that have not been signed by their owner. --- src/MatrixClientPeg.ts | 4 ++ src/i18n/strings/en_EN.json | 2 + src/settings/Settings.tsx | 11 ++++++ .../DeviceIsolationModeController.ts | 37 +++++++++++++++++++ .../components/structures/MatrixChat-test.tsx | 1 + .../DeviceIsolationModeController-test.ts | 33 +++++++++++++++++ test/test-utils/test-utils.ts | 1 + 7 files changed, 89 insertions(+) create mode 100644 src/settings/controllers/DeviceIsolationModeController.ts create mode 100644 test/settings/controllers/DeviceIsolationModeController-test.ts diff --git a/src/MatrixClientPeg.ts b/src/MatrixClientPeg.ts index 87ad8ec0cb..0ba2ec65d7 100644 --- a/src/MatrixClientPeg.ts +++ b/src/MatrixClientPeg.ts @@ -42,6 +42,7 @@ import PlatformPeg from "./PlatformPeg"; import { formatList } from "./utils/FormattingUtils"; import SdkConfig from "./SdkConfig"; import { Features } from "./settings/Settings"; +import { setDeviceIsolationMode } from "./settings/controllers/DeviceIsolationModeController.ts"; export interface IMatrixClientCreds { homeserverUrl: string; @@ -343,6 +344,9 @@ class MatrixClientPegClass implements IMatrixClientPeg { }); StorageManager.setCryptoInitialised(true); + + setDeviceIsolationMode(this.matrixClient, SettingsStore.getValue("feature_exclude_insecure_devices")); + // TODO: device dehydration and whathaveyou return; } diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 70089dcf3b..64680a93a3 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -1427,6 +1427,8 @@ "dynamic_room_predecessors": "Dynamic room predecessors", "dynamic_room_predecessors_description": "Enable MSC3946 (to support late-arriving room archives)", "element_call_video_rooms": "Element Call video rooms", + "exclude_insecure_devices": "Exclude insecure devices when sending/receiving messages", + "exclude_insecure_devices_description": "When this mode is enabled, encrypted messages will not be shared with unverified devices, and messages from unverified devices will be shown as an error. Note that if you enable this mode, you may be unable to communicate with users who have not verified their devices.", "experimental_description": "Feeling experimental? Try out our latest ideas in development. These features are not finalised; they may be unstable, may change, or may be dropped altogether. Learn more.", "experimental_section": "Early previews", "extended_profiles_msc_support": "Requires your server to support MSC4133", diff --git a/src/settings/Settings.tsx b/src/settings/Settings.tsx index 2fadb53dde..76bb109cac 100644 --- a/src/settings/Settings.tsx +++ b/src/settings/Settings.tsx @@ -11,6 +11,7 @@ import React, { ReactNode } from "react"; import { UNSTABLE_MSC4133_EXTENDED_PROFILES } from "matrix-js-sdk/src/matrix"; import { _t, _td, TranslationKey } from "../languageHandler"; +import DeviceIsolationModeController from "./controllers/DeviceIsolationModeController.ts"; import { NotificationBodyEnabledController, NotificationsEnabledController, @@ -309,6 +310,16 @@ export const SETTINGS: { [setting: string]: ISetting } = { supportedLevelsAreOrdered: true, default: false, }, + "feature_exclude_insecure_devices": { + isFeature: true, + labsGroup: LabGroup.Encryption, + controller: new DeviceIsolationModeController(), + displayName: _td("labs|exclude_insecure_devices"), + description: _td("labs|exclude_insecure_devices_description"), + supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS_WITH_CONFIG_PRIORITISED, + supportedLevelsAreOrdered: true, + default: false, + }, "useOnlyCurrentProfiles": { supportedLevels: LEVELS_ACCOUNT_SETTINGS, displayName: _td("settings|disable_historical_profile"), diff --git a/src/settings/controllers/DeviceIsolationModeController.ts b/src/settings/controllers/DeviceIsolationModeController.ts new file mode 100644 index 0000000000..03fee77742 --- /dev/null +++ b/src/settings/controllers/DeviceIsolationModeController.ts @@ -0,0 +1,37 @@ +/* +Copyright 2024 New Vector Ltd. +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only +Please see LICENSE files in the repository root for full details. +*/ + +import { AllDevicesIsolationMode, OnlySignedDevicesIsolationMode } from "matrix-js-sdk/src/crypto-api"; +import { MatrixClient } from "matrix-js-sdk/src/matrix"; + +import SettingController from "./SettingController"; +import { MatrixClientPeg } from "../../MatrixClientPeg"; +import { SettingLevel } from "../SettingLevel"; + +/** + * A controller for the "exclude_insecure_devices" setting, which will + * update the crypto stack's device isolation mode on change. + */ +export default class DeviceIsolationModeController extends SettingController { + public onChange(level: SettingLevel, roomId: string, newValue: any): void { + setDeviceIsolationMode(MatrixClientPeg.safeGet(), newValue); + } +} + +/** + * Set the crypto stack's device isolation mode based on the current value of the + * "exclude_insecure_devices" setting. + * + * @param client - MatrixClient to update to the new setting. + * @param settingValue - value of the "exclude_insecure_devices" setting. + */ +export function setDeviceIsolationMode(client: MatrixClient, settingValue: boolean): void { + client + .getCrypto() + ?.setDeviceIsolationMode( + settingValue ? new OnlySignedDevicesIsolationMode() : new AllDevicesIsolationMode(true), + ); +} diff --git a/test/components/structures/MatrixChat-test.tsx b/test/components/structures/MatrixChat-test.tsx index 1003d1d167..83b4feea3a 100644 --- a/test/components/structures/MatrixChat-test.tsx +++ b/test/components/structures/MatrixChat-test.tsx @@ -1002,6 +1002,7 @@ describe("", () => { getUserVerificationStatus: jest .fn() .mockResolvedValue(new UserVerificationStatus(false, false, false)), + setDeviceIsolationMode: jest.fn(), }; loginClient.isCryptoEnabled.mockReturnValue(true); loginClient.getCrypto.mockReturnValue(mockCrypto as any); diff --git a/test/settings/controllers/DeviceIsolationModeController-test.ts b/test/settings/controllers/DeviceIsolationModeController-test.ts new file mode 100644 index 0000000000..089a8ddfc8 --- /dev/null +++ b/test/settings/controllers/DeviceIsolationModeController-test.ts @@ -0,0 +1,33 @@ +/* +Copyright 2024 New Vector Ltd. +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only +Please see LICENSE files in the repository root for full details. +*/ + +import { AllDevicesIsolationMode, OnlySignedDevicesIsolationMode } from "matrix-js-sdk/src/crypto-api"; + +import { stubClient } from "../../test-utils"; +import DeviceIsolationModeController from "../../../src/settings/controllers/DeviceIsolationModeController.ts"; +import { SettingLevel } from "../../../src/settings/SettingLevel"; + +describe("DeviceIsolationModeController", () => { + afterEach(() => { + jest.resetAllMocks(); + }); + + describe("tracks enabling and disabling", () => { + it("on sets signed device isolation mode", () => { + const cli = stubClient(); + const controller = new DeviceIsolationModeController(); + controller.onChange(SettingLevel.DEVICE, "", true); + expect(cli.getCrypto()?.setDeviceIsolationMode).toHaveBeenCalledWith(new OnlySignedDevicesIsolationMode()); + }); + + it("off sets all device isolation mode", () => { + const cli = stubClient(); + const controller = new DeviceIsolationModeController(); + controller.onChange(SettingLevel.DEVICE, "", false); + expect(cli.getCrypto()?.setDeviceIsolationMode).toHaveBeenCalledWith(new AllDevicesIsolationMode(true)); + }); + }); +}); diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index b0c3dda279..ebfc6b221b 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -131,6 +131,7 @@ export function createTestClient(): MatrixClient { resetKeyBackup: jest.fn(), isEncryptionEnabledInRoom: jest.fn(), getVerificationRequestsToDeviceInProgress: jest.fn().mockReturnValue([]), + setDeviceIsolationMode: jest.fn(), }), getPushActionsForEvent: jest.fn(),