Skip to content

Commit 1ffcf3f

Browse files
committed
Support MSC4287 m.key_backup stable prefix as well as the unstable prefix
1 parent 7bbd86c commit 1ffcf3f

6 files changed

Lines changed: 106 additions & 39 deletions

File tree

apps/web/playwright/e2e/crypto/toasts.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ test.describe("'Turn on key storage' toast", () => {
132132
test("should show toast if key storage is off but account data is missing", async ({ app, page, toasts }) => {
133133
// Given the backup is disabled but we didn't set account data saying that is expected
134134
await disableKeyBackup(app);
135-
await botClient.setAccountData("m.org.matrix.custom.backup_disabled", { disabled: false });
135+
await botClient.setAccountData("m.org.matrix.custom.backup_disabled", {} as any as { disabled: boolean });
136+
await botClient.setAccountData("m.key_backup", {} as any as { enabled: boolean });
136137

137138
// Wait for the account data setting to stick
138139
await new Promise((resolve) => setTimeout(resolve, 2000));

apps/web/src/components/viewmodels/settings/encryption/KeyStoragePanelViewModel.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { CryptoEvent } from "matrix-js-sdk/src/crypto-api";
1010
import { logger } from "matrix-js-sdk/src/logger";
1111

1212
import { useMatrixClientContext } from "../../../../contexts/MatrixClientContext";
13-
import { DeviceListener, BACKUP_DISABLED_ACCOUNT_DATA_KEY } from "../../../../device-listener";
13+
import {
14+
DeviceListener,
15+
ACCOUNT_DATA_KEY_M_KEY_BACKUP,
16+
ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE,
17+
} from "../../../../device-listener";
1418
import { useEventEmitterAsyncState } from "../../../../hooks/useEventEmitter";
1519
import { resetKeyBackupAndWait } from "../../../../utils/crypto/resetKeyBackup";
1620

@@ -113,7 +117,10 @@ export function useKeyStoragePanelViewModel(): KeyStoragePanelState {
113117
}
114118

115119
// Set the flag so that EX no longer thinks the user wants backup disabled
116-
await matrixClient.setAccountData(BACKUP_DISABLED_ACCOUNT_DATA_KEY, { disabled: false });
120+
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP, { enabled: true });
121+
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE, {
122+
disabled: false,
123+
});
117124
} else {
118125
logger.info("User requested disabling key backup");
119126
// This method will delete the key backup as well as server side recovery keys and other
@@ -123,7 +130,10 @@ export function useKeyStoragePanelViewModel(): KeyStoragePanelState {
123130
// Set a flag to say that the user doesn't want key backup.
124131
// Element X uses this to determine whether to set up automatically,
125132
// so this will stop EX turning it back on spontaneously.
126-
await matrixClient.setAccountData(BACKUP_DISABLED_ACCOUNT_DATA_KEY, { disabled: true });
133+
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP, { enabled: false });
134+
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE, {
135+
disabled: true,
136+
});
127137
}
128138
});
129139
} finally {

apps/web/src/device-listener/DeviceListener.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class DeviceListener {
149149
}
150150

151151
/**
152-
* Set the account data "m.org.matrix.custom.backup_disabled" to { "disabled": true }.
152+
* Set the account data "m.key_backup" to { "enabled": false }.
153153
*/
154154
public async recordKeyBackupDisabled(): Promise<void> {
155155
await this.currentDevice?.recordKeyBackupDisabled();

apps/web/src/device-listener/DeviceListenerCurrentDevice.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ import { asyncSomeParallel } from "../utils/arrays";
2929
const KEY_BACKUP_POLL_INTERVAL = 5 * 60 * 1000;
3030

3131
/**
32-
* Unfortunately-named account data key used by Element X to indicate that the user
33-
* has chosen to disable server side key backups.
34-
*
35-
* We need to set and honour this to prevent Element X from automatically turning key backup back on.
32+
* Account data key used by to indicate that the user has chosen to enable or
33+
* disable server side key backups.
3634
*/
37-
export const BACKUP_DISABLED_ACCOUNT_DATA_KEY = "m.org.matrix.custom.backup_disabled";
35+
export const ACCOUNT_DATA_KEY_M_KEY_BACKUP = "m.key_backup";
36+
export const ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE = "m.org.matrix.custom.backup_disabled";
3837

3938
/**
4039
* Account data key to indicate whether the user has chosen to enable or disable recovery.
@@ -130,10 +129,11 @@ export class DeviceListenerCurrentDevice {
130129
}
131130

132131
/**
133-
* Set the account data "m.org.matrix.custom.backup_disabled" to `{ "disabled": true }`.
132+
* Set the account data "m.key_backup" to `{ "enabled": false }`.
134133
*/
135134
public async recordKeyBackupDisabled(): Promise<void> {
136-
await this.client.setAccountData(BACKUP_DISABLED_ACCOUNT_DATA_KEY, { disabled: true });
135+
await this.client.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP, { enabled: false });
136+
await this.client.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE, { disabled: true });
137137
}
138138

139139
/**
@@ -280,8 +280,15 @@ export class DeviceListenerCurrentDevice {
280280
* Otherwise, fetch it from the store as normal.
281281
*/
282282
public async recheckBackupDisabled(): Promise<boolean> {
283-
const backupDisabled = await this.client.getAccountDataFromServer(BACKUP_DISABLED_ACCOUNT_DATA_KEY);
284-
return !!backupDisabled?.disabled;
283+
const keyBackup = await this.client.getAccountDataFromServer(ACCOUNT_DATA_KEY_M_KEY_BACKUP);
284+
if (keyBackup) {
285+
return !keyBackup.enabled;
286+
}
287+
288+
const keyBackupDisabledUnstable = await this.client.getAccountDataFromServer(
289+
ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE,
290+
);
291+
return !!keyBackupDisabledUnstable?.disabled;
285292
}
286293

287294
/**
@@ -330,7 +337,8 @@ export class DeviceListenerCurrentDevice {
330337
ev.getType().startsWith("m.secret_storage.") ||
331338
ev.getType().startsWith("m.cross_signing.") ||
332339
ev.getType() === "m.megolm_backup.v1" ||
333-
ev.getType() === BACKUP_DISABLED_ACCOUNT_DATA_KEY ||
340+
ev.getType() === ACCOUNT_DATA_KEY_M_KEY_BACKUP ||
341+
ev.getType() === ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE ||
334342
ev.getType() === RECOVERY_ACCOUNT_DATA_KEY
335343
) {
336344
this.deviceListener.recheck();

apps/web/test/unit-tests/DeviceListener-test.ts

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import {
2525
} from "matrix-js-sdk/src/crypto-api";
2626
import { type CryptoSessionStateChange } from "@matrix-org/analytics-events/types/typescript/CryptoSessionStateChange";
2727

28-
import { DeviceListener, BACKUP_DISABLED_ACCOUNT_DATA_KEY } from "../../src/device-listener";
28+
import {
29+
DeviceListener,
30+
ACCOUNT_DATA_KEY_M_KEY_BACKUP,
31+
ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE,
32+
} from "../../src/device-listener";
2933
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
3034
import * as SetupEncryptionToast from "../../src/toasts/SetupEncryptionToast";
3135
import * as UnverifiedSessionToast from "../../src/toasts/UnverifiedSessionToast";
@@ -430,7 +434,12 @@ describe("DeviceListener", () => {
430434
mockCrypto!.getSecretStorageStatus.mockResolvedValue(readySecretStorageStatus);
431435
mockCrypto!.getSessionBackupPrivateKey.mockResolvedValue(null);
432436
mockClient.getAccountDataFromServer.mockImplementation((eventType) =>
433-
eventType === BACKUP_DISABLED_ACCOUNT_DATA_KEY ? ({ disabled: true } as any) : null,
437+
eventType === ACCOUNT_DATA_KEY_M_KEY_BACKUP ? ({ enabled: false } as any) : null,
438+
);
439+
mockClient.getAccountDataFromServer.mockImplementation((eventType) =>
440+
eventType === ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE
441+
? ({ disabled: true } as any)
442+
: null,
434443
);
435444

436445
await createAndStart();
@@ -537,6 +546,10 @@ describe("DeviceListener", () => {
537546
expect(mockClient.setAccountData).toHaveBeenCalledWith("m.org.matrix.custom.backup_disabled", {
538547
disabled: true,
539548
});
549+
550+
expect(mockClient.setAccountData).toHaveBeenCalledWith("m.key_backup", {
551+
enabled: false,
552+
});
540553
});
541554

542555
it("sets the recovery account data when we call recordRecoveryDisabled", async () => {
@@ -568,7 +581,7 @@ describe("DeviceListener", () => {
568581

569582
it("shows the 'Turn on key storage' toast if we never explicitly turned off key storage", async () => {
570583
// Given key backup is off but the account data saying we turned it off is not set
571-
// (m.org.matrix.custom.backup_disabled)
584+
// (m.key_backup or m.org.matrix.custom.backup_disabled)
572585
mockClient.getAccountData.mockReturnValue(undefined);
573586

574587
// When we launch the DeviceListener
@@ -581,11 +594,16 @@ describe("DeviceListener", () => {
581594
it("shows the 'Turn on key storage' toast if we turned on key storage", async () => {
582595
// Given key backup is off but the account data says we turned it on (this should not happen - the
583596
// account data should only be updated if we turn on key storage)
584-
mockClient.getAccountData.mockImplementation((eventType) =>
585-
eventType === BACKUP_DISABLED_ACCOUNT_DATA_KEY
586-
? new MatrixEvent({ content: { disabled: false } })
587-
: undefined,
588-
);
597+
mockClient.getAccountData.mockImplementation((eventType) => {
598+
switch (eventType) {
599+
case ACCOUNT_DATA_KEY_M_KEY_BACKUP:
600+
return new MatrixEvent({ content: { enabled: true } });
601+
case ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE:
602+
return new MatrixEvent({ content: { disabled: false } });
603+
default:
604+
return undefined;
605+
}
606+
});
589607

590608
// When we launch the DeviceListener
591609
await createAndStart();
@@ -596,9 +614,16 @@ describe("DeviceListener", () => {
596614

597615
it("does not show the 'Turn on key storage' toast if we turned off key storage", async () => {
598616
// Given key backup is off but the account data saying we turned it off is set
599-
mockClient.getAccountDataFromServer.mockImplementation((eventType) =>
600-
eventType === BACKUP_DISABLED_ACCOUNT_DATA_KEY ? ({ disabled: true } as any) : null,
601-
);
617+
mockClient.getAccountDataFromServer.mockImplementation((eventType) => {
618+
switch (eventType) {
619+
case ACCOUNT_DATA_KEY_M_KEY_BACKUP:
620+
return new MatrixEvent({ content: { enabled: false } });
621+
case ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE:
622+
return new MatrixEvent({ content: { disabled: true } });
623+
default:
624+
return undefined;
625+
}
626+
});
602627

603628
// When we launch the DeviceListener
604629
await createAndStart();
@@ -627,11 +652,16 @@ describe("DeviceListener", () => {
627652

628653
it("does not show the 'Turn on key storage' toast if we turned on key storage", async () => {
629654
// Given key backup is on and the account data says we turned it on
630-
mockClient.getAccountData.mockImplementation((eventType) =>
631-
eventType === BACKUP_DISABLED_ACCOUNT_DATA_KEY
632-
? new MatrixEvent({ content: { disabled: false } })
633-
: undefined,
634-
);
655+
mockClient.getAccountData.mockImplementation((eventType) => {
656+
switch (eventType) {
657+
case ACCOUNT_DATA_KEY_M_KEY_BACKUP:
658+
return new MatrixEvent({ content: { enabled: true } });
659+
case ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE:
660+
return new MatrixEvent({ content: { disabled: false } });
661+
default:
662+
return undefined;
663+
}
664+
});
635665

636666
// When we launch the DeviceListener
637667
await createAndStart();
@@ -643,11 +673,16 @@ describe("DeviceListener", () => {
643673
it("does not show the 'Turn on key storage' toast if we turned off key storage", async () => {
644674
// Given key backup is on but the account data saying we turned it off is set (this should never
645675
// happen - it should only be set when we turn off key storage or dismiss the toast)
646-
mockClient.getAccountData.mockImplementation((eventType) =>
647-
eventType === BACKUP_DISABLED_ACCOUNT_DATA_KEY
648-
? new MatrixEvent({ content: { disabled: true } })
649-
: undefined,
650-
);
676+
mockClient.getAccountData.mockImplementation((eventType) => {
677+
switch (eventType) {
678+
case ACCOUNT_DATA_KEY_M_KEY_BACKUP:
679+
return new MatrixEvent({ content: { enabled: false } });
680+
case ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE:
681+
return new MatrixEvent({ content: { disabled: true } });
682+
default:
683+
return undefined;
684+
}
685+
});
651686

652687
// When we launch the DeviceListener
653688
await createAndStart();
@@ -1195,10 +1230,16 @@ describe("DeviceListener", () => {
11951230

11961231
it("does not show the 'set up recovery' toast if the user has chosen to disable key storage", async () => {
11971232
mockClient!.getAccountData.mockImplementation((k: string) => {
1198-
if (k === "m.org.matrix.custom.backup_disabled") {
1199-
return new MatrixEvent({ content: { disabled: true } });
1233+
switch (k) {
1234+
case "m.org.matrix.custom.backup_disabled":
1235+
return new MatrixEvent({ content: { disabled: true } });
1236+
1237+
case "m.key_backup":
1238+
return new MatrixEvent({ content: { enabled: false } });
1239+
1240+
default:
1241+
return undefined;
12001242
}
1201-
return undefined;
12021243
});
12031244
await createAndStart();
12041245

apps/web/test/unit-tests/components/viewmodels/settings/encryption/KeyStoragePanelViewModel-test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ describe("KeyStoragePanelViewModel", () => {
128128
expect(mocked(matrixClient.setAccountData)).toHaveBeenCalledWith("m.org.matrix.custom.backup_disabled", {
129129
disabled: false,
130130
});
131+
132+
expect(mocked(matrixClient.setAccountData)).toHaveBeenCalledWith("m.key_backup", {
133+
enabled: true,
134+
});
131135
});
132136

133137
it("should delete key storage when disabling", async () => {
@@ -145,5 +149,8 @@ describe("KeyStoragePanelViewModel", () => {
145149
expect(mocked(matrixClient.setAccountData)).toHaveBeenCalledWith("m.org.matrix.custom.backup_disabled", {
146150
disabled: true,
147151
});
152+
expect(mocked(matrixClient.setAccountData)).toHaveBeenCalledWith("m.key_backup", {
153+
enabled: false,
154+
});
148155
});
149156
});

0 commit comments

Comments
 (0)