mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 17:11:17 -08:00
fix: restore cross-signing with recovery key
This commit is contained in:
3 files changed
+72
-7
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Restore cross-signing device trust when unlocking encryption backup with a recovery key.
|
||||
@@ -1,19 +1,28 @@
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { TypedEventEmitter } from 'matrix-js-sdk/lib/models/typed-event-emitter';
|
||||
import type { CryptoApi, KeyBackupInfo } from '$types/matrix-sdk';
|
||||
import type * as MatrixSdk from '$types/matrix-sdk';
|
||||
import type { CryptoApi, CryptoBackend, KeyBackupInfo } from '$types/matrix-sdk';
|
||||
import type { SecretStorageKeyContent } from '$types/matrix/accountData';
|
||||
import { BackupRestoreTile } from './BackupRestore';
|
||||
|
||||
const decodeRecoveryKey = vi.hoisted(() => vi.fn<(key: string) => Uint8Array>());
|
||||
const emitter = new TypedEventEmitter<string, Record<string, (...args: never[]) => void>>();
|
||||
const mockClient = Object.assign(emitter, {
|
||||
secretStorage: { checkKey: vi.fn<() => Promise<boolean>>().mockResolvedValue(true) },
|
||||
getSafeUserId: () => '@me:example.org',
|
||||
getDeviceId: () => 'DEVICE',
|
||||
});
|
||||
|
||||
vi.mock('$hooks/useMatrixClient', () => ({
|
||||
useMatrixClient: () => mockClient,
|
||||
}));
|
||||
|
||||
vi.mock('$types/matrix-sdk', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof MatrixSdk>()),
|
||||
decodeRecoveryKey,
|
||||
}));
|
||||
|
||||
vi.mock('$client/secretStorageKeys', () => ({
|
||||
storePrivateKey: vi.fn<() => void>(),
|
||||
}));
|
||||
@@ -40,16 +49,31 @@ const createCrypto = ({ backupInfo = BACKUP_INFO, backupKey = null }: CryptoOver
|
||||
.fn<CryptoApi['isKeyBackupTrusted']>()
|
||||
.mockResolvedValue({ trusted: true, matchesDecryptionKey: true }),
|
||||
restoreKeyBackup: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
|
||||
processDeviceLists: vi.fn<CryptoBackend['processDeviceLists']>().mockResolvedValue(undefined),
|
||||
bootstrapCrossSigning: vi.fn<CryptoApi['bootstrapCrossSigning']>().mockResolvedValue(undefined),
|
||||
bootstrapSecretStorage: vi
|
||||
.fn<CryptoApi['bootstrapSecretStorage']>()
|
||||
.mockResolvedValue(undefined),
|
||||
setDeviceVerified: vi.fn<CryptoApi['setDeviceVerified']>().mockResolvedValue(undefined),
|
||||
loadSessionBackupPrivateKeyFromSecretStorage: vi
|
||||
.fn<CryptoApi['loadSessionBackupPrivateKeyFromSecretStorage']>()
|
||||
.mockResolvedValue(undefined),
|
||||
}) as unknown as CryptoApi;
|
||||
}) as unknown as CryptoBackend;
|
||||
|
||||
const recoveryPrompt = () => screen.queryByText(/does not hold the backup decryption key/i);
|
||||
|
||||
const submitRecoveryKey = (value: string) => {
|
||||
const input = document.querySelector('form input') as HTMLInputElement;
|
||||
fireEvent.change(input, { target: { value } });
|
||||
const form = input.closest('form') as HTMLFormElement;
|
||||
Object.defineProperty(form, input.name, { value: input, configurable: true });
|
||||
fireEvent.submit(form);
|
||||
};
|
||||
|
||||
describe('BackupRestoreTile', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
decodeRecoveryKey.mockReturnValue(new Uint8Array([1, 2, 3]));
|
||||
});
|
||||
|
||||
it('offers recovery when a backup exists but its key is not in the crypto store', async () => {
|
||||
@@ -65,6 +89,28 @@ describe('BackupRestoreTile', () => {
|
||||
expect(screen.getByText('Recovery Key')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('restores cross-signing before unlocking the backup', async () => {
|
||||
const crypto = createCrypto({ backupKey: null });
|
||||
render(
|
||||
<BackupRestoreTile
|
||||
crypto={crypto}
|
||||
secretStorageKeyId={KEY_ID}
|
||||
secretStorageKeyContent={KEY_CONTENT}
|
||||
/>
|
||||
);
|
||||
|
||||
await waitFor(() => expect(recoveryPrompt()).not.toBeNull());
|
||||
submitRecoveryKey('valid-key');
|
||||
|
||||
await waitFor(() =>
|
||||
expect(crypto.loadSessionBackupPrivateKeyFromSecretStorage).toHaveBeenCalled()
|
||||
);
|
||||
expect(crypto.processDeviceLists).toHaveBeenCalledWith({ changed: ['@me:example.org'] });
|
||||
expect(crypto.bootstrapCrossSigning).toHaveBeenCalled();
|
||||
expect(crypto.setDeviceVerified).toHaveBeenCalledWith('@me:example.org', 'DEVICE');
|
||||
expect(crypto.loadSessionBackupPrivateKeyFromSecretStorage).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('stays out of the way once the key is cached', async () => {
|
||||
render(
|
||||
<BackupRestoreTile
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { MouseEventHandler } from 'react';
|
||||
import { useCallback, useState } from 'react';
|
||||
import { useAtom, useAtomValue } from 'jotai';
|
||||
import type { CryptoApi, KeyBackupInfo } from '$types/matrix-sdk';
|
||||
import type { CryptoApi, CryptoBackend, KeyBackupInfo } from '$types/matrix-sdk';
|
||||
import type { RectCords } from 'folds';
|
||||
import {
|
||||
Badge,
|
||||
@@ -38,6 +38,7 @@ import {
|
||||
import { SecretStorageKeyMethod, SecretStorageKeyPrompt } from './SecretStorage';
|
||||
import { stopPropagation } from '$utils/keyboard';
|
||||
import { useRestoreBackupOnVerification } from '$hooks/useRestoreBackupOnVerification';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import {
|
||||
Download,
|
||||
DotsThreeOutlineVerticalIcon,
|
||||
@@ -56,6 +57,8 @@ function BackupKeyRecovery({
|
||||
secretStorageKeyId,
|
||||
secretStorageKeyContent,
|
||||
}: BackupKeyRecoveryProps) {
|
||||
const mx = useMatrixClient();
|
||||
const cryptoBackend = crypto as CryptoBackend;
|
||||
const hasPassphrase = !!secretStorageKeyContent.passphrase;
|
||||
const [method, setMethod] = useState(
|
||||
hasPassphrase ? SecretStorageKeyMethod.RecoveryPassphrase : SecretStorageKeyMethod.RecoveryKey
|
||||
@@ -65,10 +68,21 @@ function BackupKeyRecovery({
|
||||
useCallback(
|
||||
async (recoveryKey: Uint8Array) => {
|
||||
storePrivateKey(secretStorageKeyId, recoveryKey);
|
||||
|
||||
await cryptoBackend.processDeviceLists({ changed: [mx.getSafeUserId()] });
|
||||
await cryptoBackend.bootstrapCrossSigning({});
|
||||
await cryptoBackend.bootstrapSecretStorage({});
|
||||
|
||||
const deviceId = mx.getDeviceId();
|
||||
if (!deviceId) {
|
||||
throw new Error('Unexpected Error! Current device ID not found.');
|
||||
}
|
||||
await cryptoBackend.setDeviceVerified(mx.getSafeUserId(), deviceId);
|
||||
|
||||
// Emits KeyBackupDecryptionKeyCached, which drives the restore.
|
||||
await crypto.loadSessionBackupPrivateKeyFromSecretStorage();
|
||||
},
|
||||
[crypto, secretStorageKeyId]
|
||||
[crypto, cryptoBackend, mx, secretStorageKeyId]
|
||||
)
|
||||
);
|
||||
|
||||
@@ -80,8 +94,8 @@ function BackupKeyRecovery({
|
||||
return (
|
||||
<Box direction="Column" gap="200">
|
||||
<Text size="T200">
|
||||
This device does not hold the backup decryption key. Provide your recovery details to unlock
|
||||
the backup and restore your message history.
|
||||
This device does not hold the backup decryption key. Provide your recovery details to
|
||||
restore device verification and unlock the backup.
|
||||
</Text>
|
||||
<SecretStorageKeyPrompt
|
||||
method={method}
|
||||
|
||||
Reference in new issue
Block a user