diff --git a/apps/web/src/Lifecycle.ts b/apps/web/src/Lifecycle.ts index 0a73cd2bed..e05ab16139 100644 --- a/apps/web/src/Lifecycle.ts +++ b/apps/web/src/Lifecycle.ts @@ -69,6 +69,7 @@ import { type OnLoggedInPayload } from "./dispatcher/payloads/OnLoggedInPayload. import { filterBoolean } from "./utils/arrays.ts"; import { CallStatusListener } from "./CallStatusListener.ts"; import { CallStore } from "./stores/CallStore.ts"; +import { ModuleApi } from "./modules/Api.ts"; const HOMESERVER_URL_KEY = "mx_hs_url"; const ID_SERVER_URL_KEY = "mx_is_url"; @@ -842,7 +843,11 @@ async function doSetLoggedIn( // Dispatch this synchronously so SDKContextClass can set the client for other modules to consume. dis.dispatch({ action: Action.OnLoggedIn, client }, true); - const clientPegOpts: MatrixClientPegAssignOpts = {}; + const clientPegOpts: MatrixClientPegAssignOpts = { + userVerificationCaCertsPem: + ModuleApi.instance.client.creationManagement.userVerificationCaCertsPem ?? undefined, + }; + if (credentials.pickleKey) { // The pickleKey, if provided, is probably a base64-encoded 256-bit key, so can be used for the crypto store. if (credentials.pickleKey.length === 43) { diff --git a/apps/web/src/MatrixClientPeg.ts b/apps/web/src/MatrixClientPeg.ts index 8ba654b8d6..1cebf8015c 100644 --- a/apps/web/src/MatrixClientPeg.ts +++ b/apps/web/src/MatrixClientPeg.ts @@ -46,6 +46,13 @@ export interface MatrixClientPegAssignOpts { * directly where possible. */ rustCryptoStorePassword?: string; + + /** + * Optional PEM-formatted string that provides CA certificates. These will be used to check + * X.509 signatures on user identities. Any user identity that has a valid signature according to the supplied + * CAs will be considered verified, without any manual verification taking place. + */ + userVerificationCaCertsPem?: string; } /** @@ -256,7 +263,7 @@ class MatrixClientPegClass implements IMatrixClientPeg { // try to initialise e2e on the new client if (!SettingsStore.getValue("lowBandwidth")) { - await this.initClientCrypto(assignOpts.rustCryptoStoreKey, assignOpts.rustCryptoStorePassword); + await this.initClientCrypto(assignOpts); } const opts = utils.deepCopy(this.opts); @@ -294,28 +301,21 @@ class MatrixClientPegClass implements IMatrixClientPeg { } /** - * Attempt to initialize the crypto layer on a newly-created MatrixClient - * - * @param rustCryptoStoreKey - A key with which to encrypt the rust crypto indexeddb. - * If provided, it must be exactly 32 bytes of data. If both this and `rustCryptoStorePassword` are - * undefined, the store will be unencrypted. - * - * @param rustCryptoStorePassword - An alternative to `rustCryptoStoreKey`. Ignored if `rustCryptoStoreKey` is set. - * A password which will be used to derive a key to encrypt the store with. Deriving a key from a password is - * (deliberately) a slow operation, so prefer to pass a `rustCryptoStoreKey` directly where possible. + * Attempt to initialize the crypto layer on a newly-created MatrixClient. */ - private async initClientCrypto(rustCryptoStoreKey?: Uint8Array, rustCryptoStorePassword?: string): Promise { + private async initClientCrypto(opts: MatrixClientPegAssignOpts): Promise { if (!this.matrixClient) { throw new Error("createClient must be called first"); } - if (!rustCryptoStoreKey && !rustCryptoStorePassword) { + if (!opts.rustCryptoStoreKey && !opts.rustCryptoStorePassword) { logger.error("Warning! Not using an encryption key for rust crypto store."); } await this.matrixClient.initRustCrypto({ - storageKey: rustCryptoStoreKey, - storagePassword: rustCryptoStorePassword, + storageKey: opts.rustCryptoStoreKey, + storagePassword: opts.rustCryptoStorePassword, + caCertsPem: opts.userVerificationCaCertsPem, }); StorageManager.setCryptoInitialised(true); diff --git a/apps/web/src/modules/ClientApi.ts b/apps/web/src/modules/ClientApi.ts index 7b5ccd2828..c5d6967577 100644 --- a/apps/web/src/modules/ClientApi.ts +++ b/apps/web/src/modules/ClientApi.ts @@ -8,9 +8,11 @@ import type { ClientApi as IClientApi, Room } from "@element-hq/element-web-modu import { Room as ModuleRoom } from "./models/Room"; import { AccountDataApi } from "./AccountDataApi"; import { MatrixClientPeg } from "../MatrixClientPeg"; +import { ClientCreationManagementApi } from "./ClientCreationManagementApi.ts"; export class ClientApi implements IClientApi { public readonly accountData = new AccountDataApi(); + public readonly creationManagement = new ClientCreationManagementApi(); public getRoom(roomId: string): Room | null { const sdkRoom = MatrixClientPeg.safeGet().getRoom(roomId); diff --git a/apps/web/src/modules/ClientCreationManagementApi.ts b/apps/web/src/modules/ClientCreationManagementApi.ts new file mode 100644 index 0000000000..52f153107b --- /dev/null +++ b/apps/web/src/modules/ClientCreationManagementApi.ts @@ -0,0 +1,15 @@ +/* +Copyright 2026 Element Creations Ltd. + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ +import type { ClientCreationManagementApi as IClientCreationManagementApi } from "@element-hq/element-web-module-api"; + +export class ClientCreationManagementApi implements IClientCreationManagementApi { + public userVerificationCaCertsPem: string | null = null; + + public setUserVerificationCaCertsPem(pem: string | null): void { + this.userVerificationCaCertsPem = pem; + } +} diff --git a/apps/web/test/unit-tests/modules/ClientCreationManagementApi-test.ts b/apps/web/test/unit-tests/modules/ClientCreationManagementApi-test.ts new file mode 100644 index 0000000000..35dcb6274b --- /dev/null +++ b/apps/web/test/unit-tests/modules/ClientCreationManagementApi-test.ts @@ -0,0 +1,16 @@ +/* + Copyright 2026 Element Creations Ltd. + + SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial + Please see LICENSE files in the repository root for full details. + */ + +import { ClientCreationManagementApi } from "../../../src/modules/ClientCreationManagementApi.ts"; + +describe("ClientCreationManagementApi", () => { + it("should allow setting the CaCertsPem", () => { + const api = new ClientCreationManagementApi(); + api.setUserVerificationCaCertsPem("test"); + expect(api.userVerificationCaCertsPem).toEqual("test"); + }); +}); diff --git a/packages/module-api/element-web-module-api.api.md b/packages/module-api/element-web-module-api.api.md index d27bdc3315..f72ad2b80c 100644 --- a/packages/module-api/element-web-module-api.api.md +++ b/packages/module-api/element-web-module-api.api.md @@ -96,9 +96,16 @@ export interface ChatExportCustomisations { // @public export interface ClientApi { accountData: AccountDataApi; + // @alpha + readonly creationManagement: ClientCreationManagementApi; getRoom: (id: string) => Room | null; } +// @public +export interface ClientCreationManagementApi { + setUserVerificationCaCertsPem(pem: string | null): void; +} + // @alpha @deprecated (undocumented) export interface ComponentVisibilityCustomisations { shouldShowComponent?(component: "UIComponent.sendInvites" | "UIComponent.roomCreation" | "UIComponent.spaceCreation" | "UIComponent.exploreRooms" | "UIComponent.addIntegrations" | "UIComponent.filterContainer" | "UIComponent.roomOptionsMenu"): boolean; diff --git a/packages/module-api/src/api/client.ts b/packages/module-api/src/api/client.ts index 64e2274e89..01c42650fe 100644 --- a/packages/module-api/src/api/client.ts +++ b/packages/module-api/src/api/client.ts @@ -32,6 +32,12 @@ export interface AccountDataApi { * @public */ export interface ClientApi { + /** + * Allows modules to modify aspects of the way the matrix-js-sdk client is created and configured. + * @alpha Subject to change. + */ + readonly creationManagement: ClientCreationManagementApi; + /** * Use this to modify account data on the homeserver. */ @@ -44,3 +50,19 @@ export interface ClientApi { */ getRoom: (id: string) => Room | null; } + +/** + * Methods which manage aspects of the way the matrix-js-sdk Client is created and configured. + * @public + * @alpha + */ +export interface ClientCreationManagementApi { + /** + * Configure the crypto stack to trust user identities that are signed by particular certificate authorities. + * + * @param pem - Optional PEM-formatted string that provides CA certificates. These will be used to check + * X.509 signatures on user identities. Any user identity that has a valid signature according to the supplied + * CAs will be considered verified, without any manual verification taking place. + */ + setUserVerificationCaCertsPem(pem: string | null): void; +}