Skip to content

Commit 1091e68

Browse files
committed
feat(secret storage): keyId in SecretStorage.setDefaultKeyId can be set at null in order to delete an exising recovery key
1 parent 9134471 commit 1091e68

File tree

3 files changed

+97
-11
lines changed

3 files changed

+97
-11
lines changed

spec/unit/secret-storage.spec.ts

+80-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import {
2323
SecretStorageCallbacks,
2424
SecretStorageKeyDescriptionAesV1,
2525
SecretStorageKeyDescriptionCommon,
26+
ServerSideSecretStorage,
2627
ServerSideSecretStorageImpl,
2728
trimTrailingEquals,
2829
} from "../../src/secret-storage";
2930
import { randomString } from "../../src/randomstring";
3031
import { SecretInfo } from "../../src/secret-storage.ts";
31-
import { AccountDataEvents } from "../../src";
32+
import { AccountDataEvents, ClientEvent, MatrixEvent, TypedEventEmitter } from "../../src";
33+
import { defer, IDeferred } from "../../src/utils";
3234

3335
declare module "../../src/@types/event" {
3436
interface SecretStorageAccountDataEvents {
@@ -273,6 +275,78 @@ describe("ServerSideSecretStorageImpl", function () {
273275
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining("unknown algorithm"));
274276
});
275277
});
278+
279+
describe("setDefaultKeyId", function () {
280+
let secretStorage: ServerSideSecretStorage;
281+
let accountDataAdapter: Mocked<AccountDataClient>;
282+
let accountDataPromise: IDeferred<void>;
283+
beforeEach(() => {
284+
accountDataAdapter = mockAccountDataClient();
285+
accountDataPromise = defer();
286+
accountDataAdapter.setAccountData.mockImplementation(() => {
287+
accountDataPromise.resolve();
288+
return Promise.resolve({});
289+
});
290+
291+
secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
292+
});
293+
294+
it("should set the default key id", async function () {
295+
const setDefaultPromise = secretStorage.setDefaultKeyId("keyId");
296+
await accountDataPromise.promise;
297+
298+
expect(accountDataAdapter.setAccountData).toHaveBeenCalledWith("m.secret_storage.default_key", {
299+
key: "keyId",
300+
});
301+
302+
accountDataAdapter.emit(
303+
ClientEvent.AccountData,
304+
new MatrixEvent({
305+
type: "m.secret_storage.default_key",
306+
content: { key: "keyId" },
307+
}),
308+
);
309+
await setDefaultPromise;
310+
});
311+
312+
it("should set the default key id with a null key id", async function () {
313+
const setDefaultPromise = secretStorage.setDefaultKeyId(null);
314+
await accountDataPromise.promise;
315+
316+
expect(accountDataAdapter.setAccountData).toHaveBeenCalledWith("m.secret_storage.default_key", {});
317+
318+
accountDataAdapter.emit(
319+
ClientEvent.AccountData,
320+
new MatrixEvent({
321+
type: "m.secret_storage.default_key",
322+
content: {},
323+
}),
324+
);
325+
await setDefaultPromise;
326+
});
327+
});
328+
329+
describe("getDefaultKeyId", function () {
330+
it("should return null when there is no key", async function () {
331+
const accountDataAdapter = mockAccountDataClient();
332+
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
333+
expect(await secretStorage.getDefaultKeyId()).toBe(null);
334+
});
335+
336+
it("should return the key id when there is a key", async function () {
337+
const accountDataAdapter = mockAccountDataClient();
338+
accountDataAdapter.getAccountDataFromServer.mockResolvedValue({ key: "keyId" });
339+
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
340+
expect(await secretStorage.getDefaultKeyId()).toBe("keyId");
341+
});
342+
343+
it("should return null when an empty object is in the account data", async function () {
344+
const accountDataAdapter = mockAccountDataClient();
345+
accountDataAdapter.getAccountDataFromServer.mockResolvedValue({});
346+
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
347+
expect(await secretStorage.getDefaultKeyId()).toBe(null);
348+
});
349+
});
276350
});
277351

278352
describe("trimTrailingEquals", () => {
@@ -291,8 +365,13 @@ describe("trimTrailingEquals", () => {
291365
});
292366

293367
function mockAccountDataClient(): Mocked<AccountDataClient> {
368+
const eventEmitter = new TypedEventEmitter();
294369
return {
295370
getAccountDataFromServer: jest.fn().mockResolvedValue(null),
296371
setAccountData: jest.fn().mockResolvedValue({}),
372+
on: eventEmitter.on.bind(eventEmitter),
373+
off: eventEmitter.off.bind(eventEmitter),
374+
removeListener: eventEmitter.removeListener.bind(eventEmitter),
375+
emit: eventEmitter.emit.bind(eventEmitter),
297376
} as unknown as Mocked<AccountDataClient>;
298377
}

src/@types/event.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ export interface AccountDataEvents extends SecretStorageAccountDataEvents {
373373
[EventType.PushRules]: IPushRules;
374374
[EventType.Direct]: { [userId: string]: string[] };
375375
[EventType.IgnoredUserList]: { [userId: string]: {} };
376-
"m.secret_storage.default_key": { key: string };
376+
"m.secret_storage.default_key": { key: string } | {};
377377
"m.identity_server": { base_url: string | null };
378378
[key: `${typeof LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${string}`]: LocalNotificationSettings;
379379
[key: `m.secret_storage.key.${string}`]: SecretStorageKeyDescription;

src/secret-storage.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,12 @@ export interface ServerSideSecretStorage {
315315

316316
/**
317317
* Set the default key ID for encrypting secrets.
318+
* If keyId is `null`, the default key id value in the account data will be set to an empty object.
319+
* This is considered as "disabling" the default key.
318320
*
319321
* @param keyId - The new default key ID
320322
*/
321-
setDefaultKeyId(keyId: string): Promise<void>;
323+
setDefaultKeyId(keyId: string | null): Promise<void>;
322324
}
323325

324326
/**
@@ -352,26 +354,31 @@ export class ServerSideSecretStorageImpl implements ServerSideSecretStorage {
352354
*/
353355
public async getDefaultKeyId(): Promise<string | null> {
354356
const defaultKey = await this.accountDataAdapter.getAccountDataFromServer("m.secret_storage.default_key");
355-
if (!defaultKey) return null;
356-
return defaultKey.key ?? null;
357+
if (!defaultKey || !Object.keys(defaultKey).length) return null;
358+
return (defaultKey as { key: string }).key ?? null;
357359
}
358360

359361
/**
360-
* Set the default key ID for encrypting secrets.
361-
*
362-
* @param keyId - The new default key ID
362+
* Implementation of {@link ServerSideSecretStorage#setDefaultKeyId}.
363363
*/
364-
public setDefaultKeyId(keyId: string): Promise<void> {
364+
public setDefaultKeyId(keyId: string | null): Promise<void> {
365365
return new Promise<void>((resolve, reject) => {
366366
const listener = (ev: MatrixEvent): void => {
367-
if (ev.getType() === "m.secret_storage.default_key" && ev.getContent().key === keyId) {
367+
const content = ev.getContent();
368+
const isSameKey = keyId === null ? !Object.keys(content).length : content.key === keyId;
369+
if (ev.getType() === "m.secret_storage.default_key" && isSameKey) {
368370
this.accountDataAdapter.removeListener(ClientEvent.AccountData, listener);
369371
resolve();
370372
}
371373
};
372374
this.accountDataAdapter.on(ClientEvent.AccountData, listener);
373375

374-
this.accountDataAdapter.setAccountData("m.secret_storage.default_key", { key: keyId }).catch((e) => {
376+
// The spec says that the key should be an object with a `key` property
377+
// https://spec.matrix.org/v1.13/client-server-api/#key-storage
378+
// To delete the default key, we send an empty object like the rust sdk does
379+
// (see https://docs.rs/matrix-sdk/latest/matrix_sdk/encryption/recovery/struct.Recovery.html#method.reset_identity)
380+
const newValue = keyId === null ? {} : { key: keyId };
381+
this.accountDataAdapter.setAccountData("m.secret_storage.default_key", newValue).catch((e) => {
375382
this.accountDataAdapter.removeListener(ClientEvent.AccountData, listener);
376383
reject(e);
377384
});

0 commit comments

Comments
 (0)