Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Settings panels: avoid exceptions with rust crypto (#10962)
Browse files Browse the repository at this point in the history
* Settings panels: avoid exceptions with rust crypto

If we are using rust crypto, `client.crypto` is undefined. We'll need to fix
these up better in future, but for now, just return early.

* Update tests
  • Loading branch information
richvdh committed May 23, 2023
1 parent 7c34d66 commit 161e19d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/components/views/settings/CrossSigningPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ export default class CrossSigningPanel extends React.PureComponent<{}, IState> {

private async getUpdatedStatus(): Promise<void> {
const cli = MatrixClientPeg.get();
const crypto = cli.crypto;
if (!crypto) return;

const pkCache = cli.getCrossSigningCacheCallbacks();
const crossSigning = cli.crypto!.crossSigningInfo;
const secretStorage = cli.crypto!.secretStorage;
const crossSigning = crypto.crossSigningInfo;
const secretStorage = cli.secretStorage;
const crossSigningPublicKeysOnDevice = Boolean(crossSigning.getId());
const crossSigningPrivateKeysInStorage = Boolean(await crossSigning.isStoredInSecretStorage(secretStorage));
const masterPrivateKeyCached = !!(await pkCache?.getCrossSigningKeyCache?.("master"));
Expand Down
9 changes: 6 additions & 3 deletions src/components/views/settings/SecureBackupPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,17 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {

private async getUpdatedDiagnostics(): Promise<void> {
const cli = MatrixClientPeg.get();
const secretStorage = cli.crypto!.secretStorage;
const crypto = cli.crypto;
if (!crypto) return;

const secretStorage = cli.secretStorage;

const backupKeyStored = !!(await cli.isKeyBackupKeyStored());
const backupKeyFromCache = await cli.crypto!.getSessionBackupPrivateKey();
const backupKeyFromCache = await crypto.getSessionBackupPrivateKey();
const backupKeyCached = !!backupKeyFromCache;
const backupKeyWellFormed = backupKeyFromCache instanceof Uint8Array;
const secretStorageKeyInAccount = await secretStorage.hasKey();
const secretStorageReady = await cli.isSecretStorageReady();
const secretStorageReady = await crypto.isSecretStorageReady();

if (this.unmounted) return;
this.setState({
Expand Down
4 changes: 2 additions & 2 deletions test/components/views/settings/CrossSigningPanel-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe("<CrossSigningPanel />", () => {
expect(screen.getByTestId("summarised-status").innerHTML).toEqual("✅ Cross-signing is ready for use.");
expect(screen.getByText("Cross-signing private keys:").parentElement!).toMatchSnapshot();
expect(mockClient.crypto!.crossSigningInfo.isStoredInSecretStorage).toHaveBeenCalledWith(
mockClient.crypto!.secretStorage,
mockClient.secretStorage,
);
});
});
Expand All @@ -106,7 +106,7 @@ describe("<CrossSigningPanel />", () => {
);
expect(screen.getByText("Cross-signing private keys:").parentElement!).toMatchSnapshot();
expect(mockClient.crypto!.crossSigningInfo.isStoredInSecretStorage).toHaveBeenCalledWith(
mockClient.crypto!.secretStorage,
mockClient.secretStorage,
);
});
});
Expand Down
8 changes: 4 additions & 4 deletions test/components/views/settings/SecureBackupPanel-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ describe("<SecureBackupPanel />", () => {
...mockClientMethodsUser(userId),
checkKeyBackup: jest.fn(),
isKeyBackupKeyStored: jest.fn(),
isSecretStorageReady: jest.fn(),
getKeyBackupEnabled: jest.fn(),
getKeyBackupVersion: jest.fn().mockReturnValue("1"),
isKeyBackupTrusted: jest.fn().mockResolvedValue(true),
getClientWellKnown: jest.fn(),
deleteKeyBackupVersion: jest.fn(),
secretStorage: { hasKey: jest.fn() },
});
// @ts-ignore allow it
client.crypto = {
secretStorage: { hasKey: jest.fn() },
getSessionBackupPrivateKey: jest.fn(),
isSecretStorageReady: jest.fn(),
} as unknown as Crypto;

const getComponent = () => render(<SecureBackupPanel />);
Expand All @@ -62,7 +62,7 @@ describe("<SecureBackupPanel />", () => {
},
});

mocked(client.crypto!.secretStorage.hasKey).mockClear().mockResolvedValue(false);
mocked(client.secretStorage.hasKey).mockClear().mockResolvedValue(false);
client.deleteKeyBackupVersion.mockClear().mockResolvedValue();
client.getKeyBackupVersion.mockClear();
client.isKeyBackupTrusted.mockClear();
Expand Down Expand Up @@ -166,7 +166,7 @@ describe("<SecureBackupPanel />", () => {
});

it("resets secret storage", async () => {
mocked(client.crypto!.secretStorage.hasKey).mockClear().mockResolvedValue(true);
mocked(client.secretStorage.hasKey).mockClear().mockResolvedValue(true);
getComponent();
// flush checkKeyBackup promise
await flushPromises();
Expand Down
6 changes: 3 additions & 3 deletions test/test-utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class MockClientWithEventEmitter extends EventEmitter {
* ```
*/
export const getMockClientWithEventEmitter = (
mockProperties: Partial<Record<MethodLikeKeys<MatrixClient>, unknown>>,
mockProperties: Partial<Record<keyof MatrixClient, unknown>>,
): MockedObject<MatrixClient> => {
const mock = mocked(new MockClientWithEventEmitter(mockProperties) as unknown as MatrixClient);

Expand Down Expand Up @@ -143,15 +143,15 @@ export const mockClientMethodsCrypto = (): Partial<
Record<MethodLikeKeys<MatrixClient> & PropertyLikeKeys<MatrixClient>, unknown>
> => ({
isCryptoEnabled: jest.fn(),
isSecretStorageReady: jest.fn(),
isCrossSigningReady: jest.fn(),
isKeyBackupKeyStored: jest.fn(),
getCrossSigningCacheCallbacks: jest.fn().mockReturnValue({ getCrossSigningKeyCache: jest.fn() }),
getStoredCrossSigningForUser: jest.fn(),
checkKeyBackup: jest.fn().mockReturnValue({}),
secretStorage: { hasKey: jest.fn() },
crypto: {
isSecretStorageReady: jest.fn(),
getSessionBackupPrivateKey: jest.fn(),
secretStorage: { hasKey: jest.fn() },
crossSigningInfo: {
getId: jest.fn(),
isStoredInSecretStorage: jest.fn(),
Expand Down

0 comments on commit 161e19d

Please sign in to comment.