Support MSC4287 m.key_backup stable prefix as well as the unstable prefix (#33034)

* Support MSC4287 m.key_backup stable prefix as well as the unstable prefix

* Update comments to avoid talking about EX since this now applies more widely

* Make comments about m.key_backup more helpful

* Improve comment on recheckBackupDisabled

* Explicitly say backup is disabled only if enabled is actually set to false
This commit is contained in:
Andy Balaam
2026-06-02 10:25:03 +00:00
committed by GitHub
parent 1c04814c2b
commit 65b0ac8c28
6 changed files with 123 additions and 63 deletions
@@ -10,7 +10,11 @@ import { CryptoEvent } from "matrix-js-sdk/src/crypto-api";
import { logger } from "matrix-js-sdk/src/logger";
import { useMatrixClientContext } from "../../../../contexts/MatrixClientContext";
import { DeviceListener, BACKUP_DISABLED_ACCOUNT_DATA_KEY } from "../../../../device-listener";
import {
DeviceListener,
ACCOUNT_DATA_KEY_M_KEY_BACKUP,
ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE,
} from "../../../../device-listener";
import { useEventEmitterAsyncState } from "../../../../hooks/useEventEmitter";
import { resetKeyBackupAndWait } from "../../../../utils/crypto/resetKeyBackup";
@@ -112,18 +116,22 @@ export function useKeyStoragePanelViewModel(): KeyStoragePanelState {
await resetKeyBackupAndWait(crypto);
}
// Set the flag so that EX no longer thinks the user wants backup disabled
await matrixClient.setAccountData(BACKUP_DISABLED_ACCOUNT_DATA_KEY, { disabled: false });
// Set the flag to say that the user wants backup enabled
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP, { enabled: true });
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE, {
disabled: false,
});
} else {
logger.info("User requested disabling key backup");
// This method will delete the key backup as well as server side recovery keys and other
// server-side crypto data.
await crypto.disableKeyStorage();
// Set a flag to say that the user doesn't want key backup.
// Element X uses this to determine whether to set up automatically,
// so this will stop EX turning it back on spontaneously.
await matrixClient.setAccountData(BACKUP_DISABLED_ACCOUNT_DATA_KEY, { disabled: true });
// Set the flag to say that the user wants backup disabled
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP, { enabled: false });
await matrixClient.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE, {
disabled: true,
});
}
});
} finally {
@@ -149,7 +149,8 @@ export class DeviceListener {
}
/**
* Set the account data "m.org.matrix.custom.backup_disabled" to { "disabled": true }.
* Set the account data indicate that the user has chosen to disable key
* backup.
*/
public async recordKeyBackupDisabled(): Promise<void> {
await this.currentDevice?.recordKeyBackupDisabled();
@@ -29,12 +29,11 @@ import { isSecretStorageBeingAccessed } from "../SecurityManager";
const KEY_BACKUP_POLL_INTERVAL = 5 * 60 * 1000;
/**
* Unfortunately-named account data key used by Element X to indicate that the user
* has chosen to disable server side key backups.
*
* We need to set and honour this to prevent Element X from automatically turning key backup back on.
* Account data key used to indicate that the user has chosen to enable or
* disable server side key backups.
*/
export const BACKUP_DISABLED_ACCOUNT_DATA_KEY = "m.org.matrix.custom.backup_disabled";
export const ACCOUNT_DATA_KEY_M_KEY_BACKUP = "m.key_backup";
export const ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE = "m.org.matrix.custom.backup_disabled";
/**
* Account data key to indicate whether the user has chosen to enable or disable recovery.
@@ -148,10 +147,12 @@ export class DeviceListenerCurrentDevice {
}
/**
* Set the account data "m.org.matrix.custom.backup_disabled" to `{ "disabled": true }`.
* Set the account data indicate that the user has chosen to disable key
* backup.
*/
public async recordKeyBackupDisabled(): Promise<void> {
await this.client.setAccountData(BACKUP_DISABLED_ACCOUNT_DATA_KEY, { disabled: true });
await this.client.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP, { enabled: false });
await this.client.setAccountData(ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE, { disabled: true });
}
/**
@@ -295,13 +296,22 @@ export class DeviceListenerCurrentDevice {
}
/**
* Fetch the account data for `backup_disabled`. If this is the first time,
* Fetch the account data for `m.key_backup`. If this is the first time,
* fetch it from the server (in case the initial sync has not finished).
* Otherwise, fetch it from the store as normal.
*
* Returns true if `m.key_backup` has `enabled: false`.
*/
public async recheckBackupDisabled(): Promise<boolean> {
const backupDisabled = await this.client.getAccountDataFromServer(BACKUP_DISABLED_ACCOUNT_DATA_KEY);
return !!backupDisabled?.disabled;
const keyBackup = await this.client.getAccountDataFromServer(ACCOUNT_DATA_KEY_M_KEY_BACKUP);
if (keyBackup) {
return keyBackup.enabled === false;
}
const keyBackupDisabledUnstable = await this.client.getAccountDataFromServer(
ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE,
);
return !!keyBackupDisabledUnstable?.disabled;
}
/**
@@ -350,7 +360,8 @@ export class DeviceListenerCurrentDevice {
ev.getType().startsWith("m.secret_storage.") ||
ev.getType().startsWith("m.cross_signing.") ||
ev.getType() === "m.megolm_backup.v1" ||
ev.getType() === BACKUP_DISABLED_ACCOUNT_DATA_KEY ||
ev.getType() === ACCOUNT_DATA_KEY_M_KEY_BACKUP ||
ev.getType() === ACCOUNT_DATA_KEY_M_KEY_BACKUP_DISABLED_UNSTABLE ||
ev.getType() === RECOVERY_ACCOUNT_DATA_KEY
) {
this.deviceListener.recheck();