mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-06 00:32:26 -08:00
fix(crypto): match upstream on backup completion, secret storage writes and gossip errors
This commit is contained in:
3 files changed
+53
-14
No files matched your search
@@ -378,7 +378,10 @@ export class EngineCrypto
|
||||
if (this.#stopped) return;
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const request = (await this.#call('backupRoomKeys')) as OutgoingRequest | null;
|
||||
if (!request) break;
|
||||
if (!request) {
|
||||
this.emit(CryptoEvent.KeyBackupSessionsRemaining, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
@@ -402,8 +405,6 @@ export class EngineCrypto
|
||||
const counts = (await this.#call('roomKeyCounts')) as { total: number; backedUp: number };
|
||||
this.emit(CryptoEvent.KeyBackupSessionsRemaining, counts.total - counts.backedUp);
|
||||
}
|
||||
|
||||
this.emit(CryptoEvent.KeyBackupSessionsRemaining, 0);
|
||||
}
|
||||
|
||||
async #recoverFromBackupUploadError(error: unknown): Promise<boolean> {
|
||||
@@ -2003,7 +2004,16 @@ export class EngineCrypto
|
||||
await this.#call('enableBackupV1', { publicKeyBase64: publicKey, version: created.version });
|
||||
await this.storeSessionBackupPrivateKey(key.privateKey, created.version);
|
||||
await this.#pushSecretToVerifiedDevices('m.megolm_backup.v1');
|
||||
await this.#mx.secretStorage.store('m.megolm_backup.v1', encodeBase64(key.privateKey));
|
||||
if (await this.#secretStorageHasAesKey()) {
|
||||
await this.#mx.secretStorage.store('m.megolm_backup.v1', encodeBase64(key.privateKey));
|
||||
}
|
||||
}
|
||||
|
||||
async #secretStorageHasAesKey(): Promise<boolean> {
|
||||
const stored = await this.#mx.secretStorage.getKey();
|
||||
if (!stored) return false;
|
||||
const [, keyInfo] = stored;
|
||||
return keyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES;
|
||||
}
|
||||
|
||||
async #signatureFor(value: Record<string, unknown>): Promise<Record<string, unknown> | null> {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import * as RustSdkCryptoJs from '@matrix-org/matrix-sdk-crypto-wasm';
|
||||
import { CryptoEvent } from 'matrix-js-sdk/lib/crypto-api';
|
||||
import { SECRET_STORAGE_ALGORITHM_V1_AES } from 'matrix-js-sdk/lib/secret-storage';
|
||||
import type { MatrixClient } from '$types/matrix-sdk';
|
||||
import { engineInvoke } from '../olmMachine/engineInvoke';
|
||||
import { EngineCrypto } from './EngineCrypto';
|
||||
@@ -109,6 +110,20 @@ describe('key backup upload', () => {
|
||||
});
|
||||
});
|
||||
|
||||
function clientWithSecretStorage(key: unknown) {
|
||||
const store = vi.fn<(name: string, value: string) => Promise<void>>(async () => undefined);
|
||||
const getKey = vi.fn<() => Promise<unknown>>(async () => key);
|
||||
const mx = {
|
||||
http: {
|
||||
authedRequest: vi.fn<(...args: never[]) => Promise<unknown>>(async () => ({
|
||||
version: '8',
|
||||
})),
|
||||
},
|
||||
secretStorage: { store, getKey },
|
||||
} as unknown as MatrixClient;
|
||||
return { mx, store };
|
||||
}
|
||||
|
||||
describe('resetKeyBackup', () => {
|
||||
beforeAll(() => RustSdkCryptoJs.initAsync());
|
||||
|
||||
@@ -120,15 +135,10 @@ describe('resetKeyBackup', () => {
|
||||
if (method === 'backupVersion') return null;
|
||||
return null;
|
||||
});
|
||||
const store = vi.fn<(name: string, value: string) => Promise<void>>(async () => undefined);
|
||||
const mx = {
|
||||
http: {
|
||||
authedRequest: vi.fn<(...args: never[]) => Promise<unknown>>(async () => ({
|
||||
version: '8',
|
||||
})),
|
||||
},
|
||||
secretStorage: { store },
|
||||
} as unknown as MatrixClient;
|
||||
const { mx, store } = clientWithSecretStorage([
|
||||
'key-id',
|
||||
{ algorithm: SECRET_STORAGE_ALGORITHM_V1_AES },
|
||||
]);
|
||||
|
||||
await new EngineCrypto(mx, { userId: '@me:e.org', deviceId: 'D' }).resetKeyBackup();
|
||||
|
||||
@@ -142,4 +152,18 @@ describe('resetKeyBackup', () => {
|
||||
expect(invoked('getMissingSessions')[0]?.[2]).toMatchObject({ users: ['@me:e.org'] });
|
||||
expect(store).toHaveBeenCalledWith('m.megolm_backup.v1', expect.any(String));
|
||||
});
|
||||
|
||||
it('does not write the backup key to secret storage when 4S is not set up', async () => {
|
||||
mockInvoke.mockImplementation(async (_identity, method) => {
|
||||
if (method === 'isBackupEnabled') return false;
|
||||
if (method === 'backupVersion') return null;
|
||||
return null;
|
||||
});
|
||||
const { mx, store } = clientWithSecretStorage(null);
|
||||
|
||||
await new EngineCrypto(mx, { userId: '@me:e.org', deviceId: 'D' }).resetKeyBackup();
|
||||
|
||||
expect(invoked('pushSecretToVerifiedDevices')).toHaveLength(1);
|
||||
expect(store).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,10 @@
|
||||
import { listen, type UnlistenFn } from '@tauri-apps/api/event';
|
||||
import { createDebugLogger } from '$utils/debugLogger';
|
||||
import type { EngineIdentity } from '../olmMachine/engineInvoke';
|
||||
import type { EngineCrypto } from './EngineCrypto';
|
||||
|
||||
const eventBridgeLog = createDebugLogger('crypto');
|
||||
|
||||
const ROOM_KEYS_RECEIVED = 'matrix-crypto://room-keys-received';
|
||||
const ROOM_KEYS_WITHHELD = 'matrix-crypto://room-keys-withheld';
|
||||
const IDENTITIES_UPDATED = 'matrix-crypto://identities-updated';
|
||||
@@ -43,7 +46,9 @@ export const startCryptoEventBridge = async (
|
||||
listen<Envelope<{ name: string }>>(
|
||||
SECRET_RECEIVED,
|
||||
forAccount<{ name: string }>(({ name }) => {
|
||||
crypto.checkSecrets(name).catch(() => undefined);
|
||||
crypto.checkSecrets(name).catch((error: unknown) => {
|
||||
eventBridgeLog.warn('general', `Failed to handle gossiped secret ${name}`, error);
|
||||
});
|
||||
})
|
||||
),
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user