Skip to content

Commit ed397d9

Browse files
authored
CryptoApi.resetEncryption should always create a new key backup (#4648)
* fix(crypto api): `resetEncryption` always calls `resetKeyBackup` * test(crypto api): update `resetEncryption` tests * chore(crypto api): add logging in `resetEncryption`
1 parent c0e30ce commit ed397d9

File tree

3 files changed

+13
-29
lines changed

3 files changed

+13
-29
lines changed

spec/unit/rust-crypto/rust-crypto.spec.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,22 +1896,6 @@ describe("RustCrypto", () => {
18961896
});
18971897

18981898
it("reset should reset 4S, backup and cross-signing", async () => {
1899-
// We don't have a key backup
1900-
fetchMock.get("path:/_matrix/client/v3/room_keys/version", {});
1901-
1902-
const rustCrypto = await makeTestRustCrypto(makeMatrixHttpApi(), undefined, undefined, secretStorage);
1903-
1904-
const authUploadDeviceSigningKeys = jest.fn();
1905-
await rustCrypto.resetEncryption(authUploadDeviceSigningKeys);
1906-
1907-
// The default key id should be deleted
1908-
expect(secretStorage.setDefaultKeyId).toHaveBeenCalledWith(null);
1909-
expect(await rustCrypto.getActiveSessionBackupVersion()).toBeNull();
1910-
// The new cross signing keys should be uploaded
1911-
expect(authUploadDeviceSigningKeys).toHaveBeenCalledWith(expect.any(Function));
1912-
});
1913-
1914-
it("key backup should be re-enabled after reset", async () => {
19151899
// When we will delete the key backup
19161900
let backupIsDeleted = false;
19171901
fetchMock.delete("path:/_matrix/client/v3/room_keys/version/1", () => {
@@ -1923,6 +1907,13 @@ describe("RustCrypto", () => {
19231907
return backupIsDeleted ? {} : testData.SIGNED_BACKUP_DATA;
19241908
});
19251909

1910+
// A new key backup should be created after the reset
1911+
let newKeyBackupInfo!: KeyBackupInfo;
1912+
fetchMock.post("path:/_matrix/client/v3/room_keys/version", (res, options) => {
1913+
newKeyBackupInfo = JSON.parse(options.body as string);
1914+
return { version: "2" };
1915+
});
1916+
19261917
// We consider the key backup as trusted
19271918
jest.spyOn(RustBackupManager.prototype, "isKeyBackupTrusted").mockResolvedValue({
19281919
trusted: true,
@@ -1933,13 +1924,6 @@ describe("RustCrypto", () => {
19331924
// We have a key backup
19341925
expect(await rustCrypto.getActiveSessionBackupVersion()).not.toBeNull();
19351926

1936-
// A new key backup should be created after the reset
1937-
let newKeyBackupInfo!: KeyBackupInfo;
1938-
fetchMock.post("path:/_matrix/client/v3/room_keys/version", (res, options) => {
1939-
newKeyBackupInfo = JSON.parse(options.body as string);
1940-
return { version: "2" };
1941-
});
1942-
19431927
const authUploadDeviceSigningKeys = jest.fn();
19441928
await rustCrypto.resetEncryption(authUploadDeviceSigningKeys);
19451929

src/crypto-api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ export interface CryptoApi {
401401
* - Disable backing up room keys and delete any existing backups.
402402
* - Remove the default secret storage key from the account data (ie: the recovery key).
403403
* - Reset the cross-signing keys.
404-
* - Re-enable backing up room keys if enabled before.
404+
* - Create a new key backup.
405405
*
406406
* @param authUploadDeviceSigningKeys - Callback to authenticate the upload of device signing keys.
407407
* Used when resetting the cross signing keys.

src/rust-crypto/rust-crypto.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
14771477
* Implementation of {@link CryptoApi#resetEncryption}.
14781478
*/
14791479
public async resetEncryption(authUploadDeviceSigningKeys: UIAuthCallback<void>): Promise<void> {
1480-
const backupEnabled = (await this.backupManager.getActiveBackupVersion()) !== null;
1480+
this.logger.debug("resetEncryption: resetting encryption");
14811481

14821482
// Disable backup, and delete all the backups from the server
14831483
await this.backupManager.deleteAllKeyBackupVersions();
@@ -1491,10 +1491,10 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
14911491
authUploadDeviceSigningKeys,
14921492
});
14931493

1494-
// If key backup was enabled, we create a new backup
1495-
if (backupEnabled) {
1496-
await this.resetKeyBackup();
1497-
}
1494+
// Create a new key backup
1495+
await this.resetKeyBackup();
1496+
1497+
this.logger.debug("resetEncryption: ended");
14981498
}
14991499

15001500
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)