fix: persist recovery-key device trust (#1902)

<!-- Please read
https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before
submitting your pull request -->

### Description

<!-- Please include a summary of the change. Please also include
relevant motivation and context. List any dependencies that are required
for this change. -->

Fixes #

#### Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings

### AI disclosure:

- [ ] Partially AI assisted (clarify which code was AI assisted and
briefly explain what it does).
- [ ] Fully AI generated (explain what all the generated code does in
moderate detail).

<!-- Write any explanation required here, but do not generate the
explanation using AI!! You must prove you understand what the code in
this PR does. -->
This commit is contained in:
Erwan Leboucher authored and GitHub committed 2026-08-21 13:10:15 +02:00
commit e02f57e385
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;
};