mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 09:31:17 -08:00
fix(crypto): reset per-account crypto state when switching accounts
This commit is contained in:
3 files changed
+68
-3
No files matched your search
@@ -27,6 +27,7 @@ import {
|
||||
startClient,
|
||||
stopClient,
|
||||
} from '$client/initMatrix';
|
||||
import { clearSecretStorageKeys } from '$client/secretStorageKeys';
|
||||
import { SplashScreen } from '$components/splash-screen';
|
||||
import { ServerConfigsLoader } from '$components/ServerConfigsLoader';
|
||||
import { CapabilitiesProvider } from '$hooks/useCapabilities';
|
||||
@@ -309,9 +310,14 @@ export function ClientRoot({ children }: ClientRootProps) {
|
||||
'— reloading client'
|
||||
);
|
||||
void pushSessionToSW(activeSession.baseUrl, activeSession.accessToken, activeSession.userId);
|
||||
if (mx?.clientRunning) {
|
||||
// Unconditional: stopClient is what stops the crypto backend, and a client
|
||||
// that never reached clientRunning still holds an open crypto store.
|
||||
if (mx) {
|
||||
stopClient(mx);
|
||||
}
|
||||
// The cache is keyed by 4S key id only, so the previous account's key
|
||||
// would otherwise stay in memory for the next one.
|
||||
clearSecretStorageKeys();
|
||||
loadedUserIdRef.current = undefined;
|
||||
setLoadState({ status: AsyncStatus.Idle });
|
||||
navigate(getHomePath(), { replace: true });
|
||||
@@ -335,7 +341,7 @@ export function ClientRoot({ children }: ClientRootProps) {
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (mx?.clientRunning) {
|
||||
if (mx) {
|
||||
log.log('ClientRoot unmounting — stopping client', mx.getUserId());
|
||||
stopClient(mx);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ import * as Sentry from '@sentry/react';
|
||||
import { pushSessionToSW } from '../sw-session';
|
||||
import { assertAuthMetadataIssuer, createSessionTokenRefresher } from './oidcTokenRefresher';
|
||||
import { revokeOAuthToken } from './oauthTokenRevocation';
|
||||
import { cryptoCallbacks } from './secretStorageKeys';
|
||||
import { clearSecretStorageKeys, cryptoCallbacks } from './secretStorageKeys';
|
||||
import type { SlidingSyncDiagnostics } from './slidingSync';
|
||||
import { scopeEphemeralExtensions, SlidingSyncManager } from './slidingSync';
|
||||
import { PresenceSyncManager } from './presenceSync';
|
||||
@@ -610,6 +610,7 @@ export const logoutClient = async (mx: MatrixClient, session?: Session) => {
|
||||
SlidingSyncSidebarCache.clear(session.userId);
|
||||
clearCachedVersions(session.baseUrl, session.userId);
|
||||
clearCachedUserProfiles(session.userId);
|
||||
clearSecretStorageKeys();
|
||||
const storeName: SessionStoreName = getSessionStoreName(session);
|
||||
await mx.clearStores({ cryptoDatabasePrefix: storeName.rustCryptoPrefix });
|
||||
await deleteDatabase(storeName.sync);
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { clearSecretStorageKeys, cryptoCallbacks, storePrivateKey } from './secretStorageKeys';
|
||||
|
||||
const KEY_A = 'key-account-a';
|
||||
const KEY_B = 'key-account-b';
|
||||
|
||||
const ask = (...keyIds: string[]) =>
|
||||
cryptoCallbacks.getSecretStorageKey({
|
||||
keys: Object.fromEntries(keyIds.map((id) => [id, {}])),
|
||||
});
|
||||
|
||||
describe('secretStorageKeys', () => {
|
||||
beforeEach(() => {
|
||||
clearSecretStorageKeys();
|
||||
});
|
||||
|
||||
it('returns a stored key for the requested key id', async () => {
|
||||
storePrivateKey(KEY_A, new Uint8Array([1, 2, 3]));
|
||||
|
||||
const result = await ask(KEY_A);
|
||||
|
||||
expect(result?.[0]).toBe(KEY_A);
|
||||
expect(result?.[1]).toEqual(new Uint8Array([1, 2, 3]));
|
||||
});
|
||||
|
||||
it('does not answer for a key id it has never seen', async () => {
|
||||
storePrivateKey(KEY_A, new Uint8Array([1]));
|
||||
|
||||
expect(await ask(KEY_B)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('rejects a non-Uint8Array key', () => {
|
||||
expect(() => storePrivateKey(KEY_A, 'not-a-key' as unknown as Uint8Array)).toThrow(
|
||||
'Unable to store, privateKey is invalid.'
|
||||
);
|
||||
});
|
||||
|
||||
// The cache is module-level and shared by every client in the tab, so an
|
||||
// account switch has to empty it or the previous account's 4S key lingers.
|
||||
it('holds nothing after a switch clears it', async () => {
|
||||
storePrivateKey(KEY_A, new Uint8Array([1]));
|
||||
storePrivateKey(KEY_B, new Uint8Array([2]));
|
||||
|
||||
clearSecretStorageKeys();
|
||||
|
||||
expect(await ask(KEY_A)).toBeUndefined();
|
||||
expect(await ask(KEY_B)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('lets the next account store its own key after a clear', async () => {
|
||||
storePrivateKey(KEY_A, new Uint8Array([1]));
|
||||
clearSecretStorageKeys();
|
||||
storePrivateKey(KEY_B, new Uint8Array([9]));
|
||||
|
||||
expect((await ask(KEY_B))?.[1]).toEqual(new Uint8Array([9]));
|
||||
expect(await ask(KEY_A)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user