diff --git a/.changeset/fix-recovery-verification-persistence.md b/.changeset/fix-recovery-verification-persistence.md new file mode 100644 index 000000000..1f1cec7e7 --- /dev/null +++ b/.changeset/fix-recovery-verification-persistence.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Fix recovery-key verified devices becoming unverified after an app refresh. diff --git a/src/app/components/ManualVerification.test.tsx b/src/app/components/ManualVerification.test.tsx index d95bbaf9b..272ad7dfd 100644 --- a/src/app/components/ManualVerification.test.tsx +++ b/src/app/components/ManualVerification.test.tsx @@ -11,6 +11,7 @@ const storePrivateKey = vi.hoisted(() => vi.fn<() => void>()); const processDeviceLists = vi.hoisted(() => vi.fn<() => Promise>()); const bootstrapCrossSigning = vi.hoisted(() => vi.fn<() => Promise>()); const bootstrapSecretStorage = vi.hoisted(() => vi.fn<() => Promise>()); +const setDeviceVerified = vi.hoisted(() => vi.fn<() => Promise>()); const loadSessionBackupPrivateKeyFromSecretStorage = vi.hoisted(() => vi.fn<() => Promise>()); 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 () => { diff --git a/src/app/components/ManualVerification.tsx b/src/app/components/ManualVerification.tsx index 3765e4fe1..e9fc5fb6d 100644 --- a/src/app/components/ManualVerification.tsx +++ b/src/app/components/ManualVerification.tsx @@ -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(); diff --git a/src/app/hooks/useDeviceVerificationStatus.test.tsx b/src/app/hooks/useDeviceVerificationStatus.test.tsx index 3460d1442..e627e0dff 100644 --- a/src/app/hooks/useDeviceVerificationStatus.test.tsx +++ b/src/app/hooks/useDeviceVerificationStatus.test.tsx @@ -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( diff --git a/src/app/utils/matrix-crypto.ts b/src/app/utils/matrix-crypto.ts index c22b3ee91..99d38b5a9 100644 --- a/src/app/utils/matrix-crypto.ts +++ b/src/app/utils/matrix-crypto.ts @@ -9,6 +9,6 @@ export const verifiedDevice = async ( if (!status) return null; - const verified = status.crossSigningVerified; + const verified = status.crossSigningVerified || status.localVerified; return verified; };