fix: persist recovery-key device trust

This commit is contained in:
Erwan Leboucher committed 2026-08-21 13:03:51 +02:00
1 parent cbb7dcfa84
commit 83cfda5f27
5 files changed
+61 -9

No files matched your search

@@ -0,0 +1,5 @@
---
default: patch
---
Fix recovery-key verified devices becoming unverified after an app refresh.
@@ -11,6 +11,7 @@ const storePrivateKey = vi.hoisted(() => vi.fn<() => void>());
const processDeviceLists = vi.hoisted(() => vi.fn<() => Promise<void>>());
const bootstrapCrossSigning = vi.hoisted(() => vi.fn<() => Promise<void>>());
const bootstrapSecretStorage = vi.hoisted(() => vi.fn<() => Promise<void>>());
const setDeviceVerified = vi.hoisted(() => vi.fn<() => Promise<void>>());
const loadSessionBackupPrivateKeyFromSecretStorage = vi.hoisted(() => vi.fn<() => Promise<void>>());
vi.mock('$types/matrix-sdk', () => ({ decodeRecoveryKey }));
@@ -18,12 +19,14 @@ vi.mock('$client/secretStorageKeys', () => ({ storePrivateKey }));
vi.mock('$hooks/useMatrixClient', () => ({
useMatrixClient: () => ({
getSafeUserId: () => '@me:example.org',
getDeviceId: () => 'DEVICE',
secretStorage: { checkKey },
getCrypto: () =>
({
processDeviceLists,
bootstrapCrossSigning,
bootstrapSecretStorage,
setDeviceVerified,
loadSessionBackupPrivateKeyFromSecretStorage,
}) as unknown as CryptoApi,
}),
@@ -56,6 +59,7 @@ describe('ManualVerificationTile', () => {
processDeviceLists.mockResolvedValue(undefined);
bootstrapCrossSigning.mockResolvedValue(undefined);
bootstrapSecretStorage.mockResolvedValue(undefined);
setDeviceVerified.mockResolvedValue(undefined);
loadSessionBackupPrivateKeyFromSecretStorage.mockResolvedValue(undefined);
});
@@ -68,6 +72,8 @@ describe('ManualVerificationTile', () => {
expect(storePrivateKey).toHaveBeenCalledWith(KEY_ID, recoveryKey);
expect(processDeviceLists).toHaveBeenCalledWith({ changed: ['@me:example.org'] });
expect(bootstrapCrossSigning).toHaveBeenCalledAfter(processDeviceLists);
expect(setDeviceVerified).toHaveBeenCalledWith('@me:example.org', 'DEVICE');
expect(setDeviceVerified).toHaveBeenCalledAfter(bootstrapCrossSigning);
});
it('invalidates the cached verification status after bootstrapping', async () => {
@@ -133,6 +133,12 @@ export function ManualVerificationTile({
await crypto.bootstrapCrossSigning({});
await crypto.bootstrapSecretStorage({});
const deviceId = mx.getDeviceId();
if (!deviceId) {
throw new Error('Unexpected Error! Current device ID not found.');
}
await crypto.setDeviceVerified(mx.getSafeUserId(), deviceId);
await crypto.loadSessionBackupPrivateKeyFromSecretStorage();
refreshVerificationStatus();
@@ -51,7 +51,12 @@ const USER_ID = '@me:example.org';
const DEVICE_ID = 'DEVICEONE';
const getDeviceVerificationStatus =
vi.fn<(userId: string, deviceId: string) => Promise<{ crossSigningVerified: boolean } | null>>();
vi.fn<
(
userId: string,
deviceId: string
) => Promise<{ crossSigningVerified: boolean; localVerified: boolean } | null>
>();
const crypto = { getDeviceVerificationStatus } as unknown as CryptoApi;
const createWrapper = () => {
@@ -66,7 +71,10 @@ const createWrapper = () => {
describe('useDeviceVerificationStatus', () => {
beforeEach(() => {
getDeviceVerificationStatus.mockReset();
getDeviceVerificationStatus.mockResolvedValue({ crossSigningVerified: true });
getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: true,
localVerified: false,
});
sentry.addBreadcrumb.mockClear();
sentry.metrics.count.mockClear();
});
@@ -80,6 +88,19 @@ describe('useDeviceVerificationStatus', () => {
await waitFor(() => expect(result.current).toBe(VerificationStatus.Verified));
});
it('reports a locally verified device as verified', async () => {
getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: false,
localVerified: true,
});
const { result } = renderHook(() => useDeviceVerificationStatus(crypto, USER_ID, DEVICE_ID), {
wrapper: createWrapper(),
});
await waitFor(() => expect(result.current).toBe(VerificationStatus.Verified));
});
it('reports Unsupported when the device has no verification status', async () => {
getDeviceVerificationStatus.mockResolvedValue(null);
@@ -173,7 +194,10 @@ describe('useDeviceVerificationStatus', () => {
await waitFor(() => expect(result.current).toBe(VerificationStatus.Verified));
expect(getDeviceVerificationStatus).toHaveBeenCalledTimes(1);
getDeviceVerificationStatus.mockResolvedValue({ crossSigningVerified: false });
getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: false,
localVerified: false,
});
await act(async () => {
mockMx.emit(event, ...(args as never[]));
});
@@ -188,7 +212,10 @@ describe('useDeviceVerificationStatus', () => {
});
await waitFor(() => expect(result.current).toBe(VerificationStatus.Verified));
getDeviceVerificationStatus.mockResolvedValue({ crossSigningVerified: false });
getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: false,
localVerified: false,
});
await act(async () => {
mockMx.emit(CryptoEvent.KeysChanged, ...([{}] as never[]));
@@ -218,8 +245,13 @@ describe('useDeviceVerificationStatus', () => {
it('triggers a new verification call when the crypto instance changes for the same user/device', async () => {
const wrapper = createWrapper();
const getDeviceVerificationStatus2 = vi
.fn<(userId: string, deviceId: string) => Promise<{ crossSigningVerified: boolean } | null>>()
.mockResolvedValue({ crossSigningVerified: true });
.fn<
(
userId: string,
deviceId: string
) => Promise<{ crossSigningVerified: boolean; localVerified: boolean } | null>
>()
.mockResolvedValue({ crossSigningVerified: true, localVerified: false });
const crypto2 = {
getDeviceVerificationStatus: getDeviceVerificationStatus2,
} as unknown as CryptoApi;
@@ -258,7 +290,10 @@ describe('useDeviceVerificationStatus', () => {
await waitFor(() => expect(resultB.current).toBe(VerificationStatus.Verified));
expect(getDeviceVerificationStatus).toHaveBeenCalledTimes(2);
getDeviceVerificationStatus.mockResolvedValue({ crossSigningVerified: false });
getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: false,
localVerified: false,
});
await act(async () => {
mockMx.emit(CryptoEvent.UserTrustStatusChanged, ...([USER_B, {}] as never[]));
@@ -300,7 +335,7 @@ describe('useUnverifiedDeviceCount', () => {
it('counts only devices that are not cross-signing verified', async () => {
getDeviceVerificationStatus.mockImplementation((_userId: string, deviceId: string) =>
Promise.resolve({ crossSigningVerified: deviceId === 'VERIFIED' })
Promise.resolve({ crossSigningVerified: deviceId === 'VERIFIED', localVerified: false })
);
const { result } = renderHook(
+1 -1
View File
@@ -9,6 +9,6 @@ export const verifiedDevice = async (
if (!status) return null;
const verified = status.crossSigningVerified;
const verified = status.crossSigningVerified || status.localVerified;
return verified;
};