From d36c1b39fdbce354f9386328d260d9d612a15a15 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Fri, 22 Mar 2024 12:28:13 +0000 Subject: [PATCH] Clean up some references to internal js-sdk classes (#12363) * Fix references to `crypto-api/verification` This is supposed to be an internal module; use the front door instead. * `IRecoveryKey` -> `GeneratedSecretStorageKey` `IRecoveryKey` is just a backwards-compatibility alias for `GeneratedSecretStorageKey` * `ISecretStorageKeyInfo` -> `SecretStorage.SecretStorageKeyDescription` Again, same thing * `IPassphraseInfo` -> `SecretStorage.PassphraseInfo` * Remove unused import restriction exceptions --- .eslintrc.js | 5 +--- playwright/e2e/crypto/utils.ts | 7 +---- src/SecurityManager.ts | 29 +++++++++++++------ .../security/CreateSecretStorageDialog.tsx | 5 ++-- .../structures/auth/SetupEncryptionBody.tsx | 4 +-- .../views/dialogs/IncomingSasDialog.tsx | 2 +- .../security/AccessSecretStorageDialog.tsx | 4 +-- .../security/RestoreKeyBackupDialog.tsx | 5 ++-- .../views/right_panel/VerificationPanel.tsx | 4 ++- .../verification/VerificationShowSas.tsx | 2 +- src/stores/SetupEncryptionStore.ts | 5 ++-- .../views/VerificationShowSas-test.tsx | 2 +- .../AccessSecretStorageDialog-test.tsx | 5 ++-- .../views/dialogs/IncomingSasDialog-test.tsx | 2 +- .../right_panel/VerificationPanel-test.tsx | 2 +- 15 files changed, 42 insertions(+), 41 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 3beb4d2332..fe5c1b5794 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -76,6 +76,7 @@ module.exports = { group: [ "matrix-js-sdk/src/**", "!matrix-js-sdk/src/matrix", + "!matrix-js-sdk/src/crypto-api", "!matrix-js-sdk/src/types", "matrix-js-sdk/lib", "matrix-js-sdk/lib/", @@ -105,13 +106,9 @@ module.exports = { "!matrix-js-sdk/src/extensible_events_v1/PollResponseEvent", "!matrix-js-sdk/src/extensible_events_v1/PollEndEvent", "!matrix-js-sdk/src/extensible_events_v1/InvalidEventError", - "!matrix-js-sdk/src/crypto-api", - "!matrix-js-sdk/src/crypto-api/verification", "!matrix-js-sdk/src/crypto", "!matrix-js-sdk/src/crypto/algorithms", - "!matrix-js-sdk/src/crypto/api", "!matrix-js-sdk/src/crypto/aes", - "!matrix-js-sdk/src/crypto/backup", "!matrix-js-sdk/src/crypto/olmlib", "!matrix-js-sdk/src/crypto/crypto", "!matrix-js-sdk/src/crypto/keybackup", diff --git a/playwright/e2e/crypto/utils.ts b/playwright/e2e/crypto/utils.ts index 0362148d39..51e0709343 100644 --- a/playwright/e2e/crypto/utils.ts +++ b/playwright/e2e/crypto/utils.ts @@ -17,12 +17,7 @@ limitations under the License. import { type Page, expect, JSHandle } from "@playwright/test"; import type { CryptoEvent, ICreateRoomOpts, MatrixClient } from "matrix-js-sdk/src/matrix"; -import type { - VerificationRequest, - Verifier, - EmojiMapping, - VerifierEvent, -} from "matrix-js-sdk/src/crypto-api/verification"; +import type { VerificationRequest, Verifier, EmojiMapping, VerifierEvent } from "matrix-js-sdk/src/crypto-api"; import type { ISasEvent } from "matrix-js-sdk/src/crypto/verification/SAS"; import { Credentials, HomeserverInstance } from "../../plugins/homeserver"; import { Client } from "../../pages/client"; diff --git a/src/SecurityManager.ts b/src/SecurityManager.ts index ff8946614f..bef9589ce4 100644 --- a/src/SecurityManager.ts +++ b/src/SecurityManager.ts @@ -14,8 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { DeviceVerificationStatus, ICryptoCallbacks, MatrixClient, encodeBase64 } from "matrix-js-sdk/src/matrix"; -import { ISecretStorageKeyInfo } from "matrix-js-sdk/src/crypto/api"; +import { + DeviceVerificationStatus, + ICryptoCallbacks, + MatrixClient, + encodeBase64, + SecretStorage, +} from "matrix-js-sdk/src/matrix"; import { deriveKey } from "matrix-js-sdk/src/crypto/key_passphrase"; import { decodeRecoveryKey } from "matrix-js-sdk/src/crypto/recoverykey"; import { logger } from "matrix-js-sdk/src/logger"; @@ -38,14 +43,14 @@ import InteractiveAuthDialog from "./components/views/dialogs/InteractiveAuthDia // single secret storage operation, as it will clear the cached keys once the // operation ends. let secretStorageKeys: Record = {}; -let secretStorageKeyInfo: Record = {}; +let secretStorageKeyInfo: Record = {}; let secretStorageBeingAccessed = false; let nonInteractive = false; let dehydrationCache: { key?: Uint8Array; - keyInfo?: ISecretStorageKeyInfo; + keyInfo?: SecretStorage.SecretStorageKeyDescription; } = {}; function isCachingAllowed(): boolean { @@ -80,7 +85,9 @@ async function confirmToDismiss(): Promise { return !sure; } -function makeInputToKey(keyInfo: ISecretStorageKeyInfo): (keyParams: KeyParams) => Promise { +function makeInputToKey( + keyInfo: SecretStorage.SecretStorageKeyDescription, +): (keyParams: KeyParams) => Promise { return async ({ passphrase, recoveryKey }): Promise => { if (passphrase) { return deriveKey(passphrase, keyInfo.passphrase.salt, keyInfo.passphrase.iterations); @@ -94,11 +101,11 @@ function makeInputToKey(keyInfo: ISecretStorageKeyInfo): (keyParams: KeyParams) async function getSecretStorageKey({ keys: keyInfos, }: { - keys: Record; + keys: Record; }): Promise<[string, Uint8Array]> { const cli = MatrixClientPeg.safeGet(); let keyId = await cli.getDefaultSecretStorageKeyId(); - let keyInfo!: ISecretStorageKeyInfo; + let keyInfo!: SecretStorage.SecretStorageKeyDescription; if (keyId) { // use the default SSSS key if set keyInfo = keyInfos[keyId]; @@ -177,7 +184,7 @@ async function getSecretStorageKey({ } export async function getDehydrationKey( - keyInfo: ISecretStorageKeyInfo, + keyInfo: SecretStorage.SecretStorageKeyDescription, checkFunc: (data: Uint8Array) => void, ): Promise { const keyFromCustomisations = SecurityCustomisations.getSecretStorageKey?.(); @@ -226,7 +233,11 @@ export async function getDehydrationKey( return key; } -function cacheSecretStorageKey(keyId: string, keyInfo: ISecretStorageKeyInfo, key: Uint8Array): void { +function cacheSecretStorageKey( + keyId: string, + keyInfo: SecretStorage.SecretStorageKeyDescription, + key: Uint8Array, +): void { if (isCachingAllowed()) { secretStorageKeys[keyId] = key; secretStorageKeyInfo[keyId] = keyInfo; diff --git a/src/async-components/views/dialogs/security/CreateSecretStorageDialog.tsx b/src/async-components/views/dialogs/security/CreateSecretStorageDialog.tsx index 036fb5038b..3fe64499ca 100644 --- a/src/async-components/views/dialogs/security/CreateSecretStorageDialog.tsx +++ b/src/async-components/views/dialogs/security/CreateSecretStorageDialog.tsx @@ -19,10 +19,9 @@ import React, { createRef } from "react"; import FileSaver from "file-saver"; import { logger } from "matrix-js-sdk/src/logger"; import { AuthDict, CrossSigningKeys, MatrixError, UIAFlow, UIAResponse } from "matrix-js-sdk/src/matrix"; -import { IRecoveryKey } from "matrix-js-sdk/src/crypto/api"; import { CryptoEvent } from "matrix-js-sdk/src/crypto"; import classNames from "classnames"; -import { BackupTrustInfo, KeyBackupInfo } from "matrix-js-sdk/src/crypto-api"; +import { BackupTrustInfo, GeneratedSecretStorageKey, KeyBackupInfo } from "matrix-js-sdk/src/crypto-api"; import { MatrixClientPeg } from "../../../../MatrixClientPeg"; import { _t, _td } from "../../../../languageHandler"; @@ -122,7 +121,7 @@ export default class CreateSecretStorageDialog extends React.PureComponent(); private passphraseField = createRef(); diff --git a/src/components/structures/auth/SetupEncryptionBody.tsx b/src/components/structures/auth/SetupEncryptionBody.tsx index 3ad4638306..684a7b5af4 100644 --- a/src/components/structures/auth/SetupEncryptionBody.tsx +++ b/src/components/structures/auth/SetupEncryptionBody.tsx @@ -15,10 +15,10 @@ limitations under the License. */ import React from "react"; -import { ISecretStorageKeyInfo } from "matrix-js-sdk/src/crypto/api"; import { IKeyBackupInfo } from "matrix-js-sdk/src/crypto/keybackup"; import { VerificationRequest } from "matrix-js-sdk/src/crypto-api"; import { logger } from "matrix-js-sdk/src/logger"; +import { SecretStorageKeyDescription } from "matrix-js-sdk/src/secret-storage"; import { _t } from "../../../languageHandler"; import { MatrixClientPeg } from "../../../MatrixClientPeg"; @@ -29,7 +29,7 @@ import EncryptionPanel from "../../views/right_panel/EncryptionPanel"; import AccessibleButton, { ButtonEvent } from "../../views/elements/AccessibleButton"; import Spinner from "../../views/elements/Spinner"; -function keyHasPassphrase(keyInfo: ISecretStorageKeyInfo): boolean { +function keyHasPassphrase(keyInfo: SecretStorageKeyDescription): boolean { return Boolean(keyInfo.passphrase && keyInfo.passphrase.salt && keyInfo.passphrase.iterations); } diff --git a/src/components/views/dialogs/IncomingSasDialog.tsx b/src/components/views/dialogs/IncomingSasDialog.tsx index a562760b6a..f1f09897f9 100644 --- a/src/components/views/dialogs/IncomingSasDialog.tsx +++ b/src/components/views/dialogs/IncomingSasDialog.tsx @@ -15,7 +15,7 @@ limitations under the License. */ import React, { ReactNode } from "react"; -import { GeneratedSas, ShowSasCallbacks, Verifier, VerifierEvent } from "matrix-js-sdk/src/crypto-api/verification"; +import { GeneratedSas, ShowSasCallbacks, Verifier, VerifierEvent } from "matrix-js-sdk/src/crypto-api"; import { logger } from "matrix-js-sdk/src/logger"; import { MatrixClientPeg } from "../../../MatrixClientPeg"; diff --git a/src/components/views/dialogs/security/AccessSecretStorageDialog.tsx b/src/components/views/dialogs/security/AccessSecretStorageDialog.tsx index 3d8447079a..4b24182f6f 100644 --- a/src/components/views/dialogs/security/AccessSecretStorageDialog.tsx +++ b/src/components/views/dialogs/security/AccessSecretStorageDialog.tsx @@ -17,8 +17,8 @@ limitations under the License. import { debounce } from "lodash"; import classNames from "classnames"; import React, { ChangeEvent, FormEvent } from "react"; -import { ISecretStorageKeyInfo } from "matrix-js-sdk/src/crypto/api"; import { logger } from "matrix-js-sdk/src/logger"; +import { SecretStorage } from "matrix-js-sdk/src/matrix"; import { MatrixClientPeg } from "../../../../MatrixClientPeg"; import Field from "../../elements/Field"; @@ -42,7 +42,7 @@ const VALIDATION_THROTTLE_MS = 200; export type KeyParams = { passphrase?: string; recoveryKey?: string }; interface IProps { - keyInfo: ISecretStorageKeyInfo; + keyInfo: SecretStorage.SecretStorageKeyDescription; checkPrivateKey: (k: KeyParams) => Promise; onFinished(result?: false | KeyParams): void; } diff --git a/src/components/views/dialogs/security/RestoreKeyBackupDialog.tsx b/src/components/views/dialogs/security/RestoreKeyBackupDialog.tsx index edc2befe11..c535245e61 100644 --- a/src/components/views/dialogs/security/RestoreKeyBackupDialog.tsx +++ b/src/components/views/dialogs/security/RestoreKeyBackupDialog.tsx @@ -16,9 +16,8 @@ limitations under the License. */ import React, { ChangeEvent } from "react"; -import { MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix"; +import { MatrixClient, MatrixError, SecretStorage } from "matrix-js-sdk/src/matrix"; import { IKeyBackupInfo, IKeyBackupRestoreResult } from "matrix-js-sdk/src/crypto/keybackup"; -import { ISecretStorageKeyInfo } from "matrix-js-sdk/src/crypto/api"; import { logger } from "matrix-js-sdk/src/logger"; import { MatrixClientPeg } from "../../../../MatrixClientPeg"; @@ -53,7 +52,7 @@ interface IProps { interface IState { backupInfo: IKeyBackupInfo | null; - backupKeyStored: Record | null; + backupKeyStored: Record | null; loading: boolean; loadError: boolean | null; restoreError: unknown | null; diff --git a/src/components/views/right_panel/VerificationPanel.tsx b/src/components/views/right_panel/VerificationPanel.tsx index c0872192ec..2cc93fcb1a 100644 --- a/src/components/views/right_panel/VerificationPanel.tsx +++ b/src/components/views/right_panel/VerificationPanel.tsx @@ -21,10 +21,12 @@ import { VerificationPhase as Phase, VerificationRequest, VerificationRequestEvent, + ShowQrCodeCallbacks, + ShowSasCallbacks, + VerifierEvent, } from "matrix-js-sdk/src/crypto-api"; import { RoomMember, Device, User } from "matrix-js-sdk/src/matrix"; import { logger } from "matrix-js-sdk/src/logger"; -import { ShowQrCodeCallbacks, ShowSasCallbacks, VerifierEvent } from "matrix-js-sdk/src/crypto-api/verification"; import { MatrixClientPeg } from "../../../MatrixClientPeg"; import VerificationQRCode from "../elements/crypto/VerificationQRCode"; diff --git a/src/components/views/verification/VerificationShowSas.tsx b/src/components/views/verification/VerificationShowSas.tsx index 42cb772159..de091091ab 100644 --- a/src/components/views/verification/VerificationShowSas.tsx +++ b/src/components/views/verification/VerificationShowSas.tsx @@ -16,7 +16,7 @@ limitations under the License. import React from "react"; import { Device } from "matrix-js-sdk/src/matrix"; -import { GeneratedSas, EmojiMapping } from "matrix-js-sdk/src/crypto-api/verification"; +import { GeneratedSas, EmojiMapping } from "matrix-js-sdk/src/crypto-api"; import SasEmoji from "@matrix-org/spec/sas-emoji.json"; import { _t, getNormalizedLanguageKeys, getUserLanguage } from "../../../languageHandler"; diff --git a/src/stores/SetupEncryptionStore.ts b/src/stores/SetupEncryptionStore.ts index 72e463a9c7..79621322be 100644 --- a/src/stores/SetupEncryptionStore.ts +++ b/src/stores/SetupEncryptionStore.ts @@ -17,10 +17,9 @@ limitations under the License. import EventEmitter from "events"; import { VerificationPhase, VerificationRequest, VerificationRequestEvent } from "matrix-js-sdk/src/crypto-api"; import { IKeyBackupInfo } from "matrix-js-sdk/src/crypto/keybackup"; -import { ISecretStorageKeyInfo } from "matrix-js-sdk/src/crypto/api"; import { logger } from "matrix-js-sdk/src/logger"; import { CryptoEvent } from "matrix-js-sdk/src/crypto"; -import { Device } from "matrix-js-sdk/src/matrix"; +import { Device, SecretStorage } from "matrix-js-sdk/src/matrix"; import { MatrixClientPeg } from "../MatrixClientPeg"; import { AccessCancelledError, accessSecretStorage } from "../SecurityManager"; @@ -48,7 +47,7 @@ export class SetupEncryptionStore extends EventEmitter { // ID of the key that the secrets we want are encrypted with public keyId: string | null = null; // Descriptor of the key that the secrets we want are encrypted with - public keyInfo: ISecretStorageKeyInfo | null = null; + public keyInfo: SecretStorage.SecretStorageKeyDescription | null = null; public hasDevicesToVerifyAgainst?: boolean; public static sharedInstance(): SetupEncryptionStore { diff --git a/test/components/views/VerificationShowSas-test.tsx b/test/components/views/VerificationShowSas-test.tsx index f140de78a2..fe1c3e5d7d 100644 --- a/test/components/views/VerificationShowSas-test.tsx +++ b/test/components/views/VerificationShowSas-test.tsx @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { EmojiMapping } from "matrix-js-sdk/src/crypto-api/verification"; +import { EmojiMapping } from "matrix-js-sdk/src/crypto-api"; import { tEmoji } from "../../../src/components/views/verification/VerificationShowSas"; diff --git a/test/components/views/dialogs/AccessSecretStorageDialog-test.tsx b/test/components/views/dialogs/AccessSecretStorageDialog-test.tsx index 22bcde2557..00b7242d96 100644 --- a/test/components/views/dialogs/AccessSecretStorageDialog-test.tsx +++ b/test/components/views/dialogs/AccessSecretStorageDialog-test.tsx @@ -15,10 +15,9 @@ limitations under the License. */ import React, { ComponentProps } from "react"; -import { IPassphraseInfo } from "matrix-js-sdk/src/crypto/api"; +import { SecretStorage, MatrixClient } from "matrix-js-sdk/src/matrix"; import { act, fireEvent, render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { MatrixClient } from "matrix-js-sdk/src/matrix"; import { Mocked } from "jest-mock"; import { getMockClientWithEventEmitter, mockPlatformPeg } from "../../../test-utils"; @@ -117,7 +116,7 @@ describe("AccessSecretStorageDialog", () => { passphrase: { // this type is weird in js-sdk // cast 'm.pbkdf2' to itself - algorithm: "m.pbkdf2" as IPassphraseInfo["algorithm"], + algorithm: "m.pbkdf2" as SecretStorage.PassphraseInfo["algorithm"], iterations: 2, salt: "nonempty", }, diff --git a/test/components/views/dialogs/IncomingSasDialog-test.tsx b/test/components/views/dialogs/IncomingSasDialog-test.tsx index ee12dfa02d..e33ad752bb 100644 --- a/test/components/views/dialogs/IncomingSasDialog-test.tsx +++ b/test/components/views/dialogs/IncomingSasDialog-test.tsx @@ -23,7 +23,7 @@ import { Verifier, VerifierEvent, VerifierEventHandlerMap, -} from "matrix-js-sdk/src/crypto-api/verification"; +} from "matrix-js-sdk/src/crypto-api"; import { TypedEventEmitter } from "matrix-js-sdk/src/matrix"; import IncomingSasDialog from "../../../../src/components/views/dialogs/IncomingSasDialog"; diff --git a/test/components/views/right_panel/VerificationPanel-test.tsx b/test/components/views/right_panel/VerificationPanel-test.tsx index 54b46c417b..3fd0f1f114 100644 --- a/test/components/views/right_panel/VerificationPanel-test.tsx +++ b/test/components/views/right_panel/VerificationPanel-test.tsx @@ -27,7 +27,7 @@ import { Verifier, VerifierEvent, VerifierEventHandlerMap, -} from "matrix-js-sdk/src/crypto-api/verification"; +} from "matrix-js-sdk/src/crypto-api"; import VerificationPanel from "../../../../src/components/views/right_panel/VerificationPanel"; import { flushPromises, stubClient } from "../../../test-utils";