Add new module API to allow setting User Verification CAs (#34620)

* Add new ModuleApi to set the CaCartsPem

* Wire up the new api

* Update packages/module-api/src/api/client-creation-management.ts

* unit test

* fixup! Add new ModuleApi to set the CaCartsPem

* fixup! Update packages/module-api/src/api/client-creation-management.ts

* fixup! fixup! Update packages/module-api/src/api/client-creation-management.ts

* fixup! fixup! fixup! Update packages/module-api/src/api/client-creation-management.ts
This commit is contained in:
Richard van der Hoff
2026-08-18 12:28:18 +00:00
committed by GitHub
parent e89ef1d1d6
commit a6ec53fcdf
7 changed files with 82 additions and 15 deletions
+6 -1
View File
@@ -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<OnLoggedInPayload>({ 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) {
+14 -14
View File
@@ -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<void> {
private async initClientCrypto(opts: MatrixClientPegAssignOpts): Promise<void> {
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);
+2
View File
@@ -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);
@@ -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;
}
}
@@ -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");
});
});
@@ -96,9 +96,16 @@ export interface ChatExportCustomisations<ExportFormat, ExportType> {
// @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;
+22
View File
@@ -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;
}