|
| 1 | +/* |
| 2 | + * Copyright 2024 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +import { GeneratedSecretStorageKey } from "matrix-js-sdk/src/crypto-api"; |
| 9 | +import { Page } from "@playwright/test"; |
| 10 | + |
| 11 | +import { test, expect } from "."; |
| 12 | +import { |
| 13 | + checkDeviceIsConnectedKeyBackup, |
| 14 | + checkDeviceIsCrossSigned, |
| 15 | + createBot, |
| 16 | + verifySession, |
| 17 | +} from "../../crypto/utils"; |
| 18 | + |
| 19 | +test.describe("Recovery section in Encryption tab", () => { |
| 20 | + test.use({ |
| 21 | + displayName: "Alice", |
| 22 | + }); |
| 23 | + |
| 24 | + let recoveryKey: GeneratedSecretStorageKey; |
| 25 | + let expectedBackupVersion: string; |
| 26 | + |
| 27 | + test.beforeEach(async ({ page, homeserver, credentials }) => { |
| 28 | + const res = await createBot(page, homeserver, credentials); |
| 29 | + recoveryKey = res.recoveryKey; |
| 30 | + expectedBackupVersion = res.expectedBackupVersion; |
| 31 | + }); |
| 32 | + |
| 33 | + test("should verify the device", { tag: "@screenshot" }, async ({ page, app, util }) => { |
| 34 | + const dialog = await util.openEncryptionTab(); |
| 35 | + |
| 36 | + // The user's device is in an unverified state, therefore the only option available to them here is to verify it |
| 37 | + const verifyButton = dialog.getByRole("button", { name: "Verify this device" }); |
| 38 | + await expect(verifyButton).toBeVisible(); |
| 39 | + await expect(util.getEncryptionTabContent()).toMatchScreenshot("verify-device-encryption-tab.png"); |
| 40 | + await verifyButton.click(); |
| 41 | + |
| 42 | + await util.verifyDevice(recoveryKey); |
| 43 | + await expect(util.getEncryptionTabContent()).toMatchScreenshot("default-recovery.png"); |
| 44 | + |
| 45 | + // Check that our device is now cross-signed |
| 46 | + await checkDeviceIsCrossSigned(app); |
| 47 | + |
| 48 | + // Check that the current device is connected to key backup |
| 49 | + // The backup decryption key should be in cache also, as we got it directly from the 4S |
| 50 | + await app.closeDialog(); |
| 51 | + await checkDeviceIsConnectedKeyBackup(page, expectedBackupVersion, true); |
| 52 | + }); |
| 53 | + |
| 54 | + test( |
| 55 | + "should change the recovery key", |
| 56 | + { tag: "@screenshot" }, |
| 57 | + async ({ page, app, homeserver, credentials, util, context }) => { |
| 58 | + await verifySession(app, "new passphrase"); |
| 59 | + const dialog = await util.openEncryptionTab(); |
| 60 | + |
| 61 | + // The user can only change the recovery key |
| 62 | + const changeButton = dialog.getByRole("button", { name: "Change recovery key" }); |
| 63 | + await expect(changeButton).toBeVisible(); |
| 64 | + await expect(util.getEncryptionTabContent()).toMatchScreenshot("default-recovery.png"); |
| 65 | + await changeButton.click(); |
| 66 | + |
| 67 | + // Display the new recovery key and click on the copy button |
| 68 | + await expect(dialog.getByText("Change recovery key?")).toBeVisible(); |
| 69 | + await expect(util.getEncryptionTabContent()).toMatchScreenshot("change-key-1-encryption-tab.png", { |
| 70 | + mask: [dialog.getByTestId("recoveryKey")], |
| 71 | + }); |
| 72 | + await dialog.getByRole("button", { name: "Copy" }).click(); |
| 73 | + await dialog.getByRole("button", { name: "Continue" }).click(); |
| 74 | + |
| 75 | + // Confirm the recovery key |
| 76 | + await util.confirmRecoveryKey( |
| 77 | + "Enter your new recovery key", |
| 78 | + "Confirm new recovery key", |
| 79 | + "change-key-2-encryption-tab.png", |
| 80 | + ); |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + test("should setup the recovery key", { tag: "@screenshot" }, async ({ page, app, util }) => { |
| 85 | + await verifySession(app, "new passphrase"); |
| 86 | + await util.removeSecretStorageDefaultKeyId(); |
| 87 | + |
| 88 | + // The key backup is deleted and the user needs to set it up |
| 89 | + const dialog = await util.openEncryptionTab(); |
| 90 | + const setupButton = dialog.getByRole("button", { name: "Set up recovery" }); |
| 91 | + await expect(setupButton).toBeVisible(); |
| 92 | + await expect(util.getEncryptionTabContent()).toMatchScreenshot("set-up-recovery.png"); |
| 93 | + await setupButton.click(); |
| 94 | + |
| 95 | + // Display an informative panel about the recovery key |
| 96 | + await expect(dialog.getByRole("heading", { name: "Set up recovery" })).toBeVisible(); |
| 97 | + await expect(util.getEncryptionTabContent()).toMatchScreenshot("set-up-key-1-encryption-tab.png"); |
| 98 | + await dialog.getByRole("button", { name: "Continue" }).click(); |
| 99 | + |
| 100 | + // Display the new recovery key and click on the copy button |
| 101 | + await expect(dialog.getByText("Save your recovery key somewhere safe")).toBeVisible(); |
| 102 | + await expect(util.getEncryptionTabContent()).toMatchScreenshot("set-up-key-2-encryption-tab.png", { |
| 103 | + mask: [dialog.getByTestId("recoveryKey")], |
| 104 | + }); |
| 105 | + await dialog.getByRole("button", { name: "Copy" }).click(); |
| 106 | + await dialog.getByRole("button", { name: "Continue" }).click(); |
| 107 | + |
| 108 | + // Confirm the recovery key |
| 109 | + await util.confirmRecoveryKey( |
| 110 | + "Enter your recovery key to confirm", |
| 111 | + "Finish set up", |
| 112 | + "set-up-key-3-encryption-tab.png", |
| 113 | + ); |
| 114 | + |
| 115 | + // The recovery key is now set up and the user can change it |
| 116 | + await expect(dialog.getByRole("button", { name: "Change recovery key" })).toBeVisible(); |
| 117 | + |
| 118 | + await app.closeDialog(); |
| 119 | + // Check that the current device is connected to key backup and the backup version is the expected one |
| 120 | + await checkDeviceIsConnectedKeyBackup(page, "1", true); |
| 121 | + }); |
| 122 | + |
| 123 | + // Test what happens if the cross-signing secrets are in secret storage but are not cached in the local DB. |
| 124 | + // |
| 125 | + // This can happen if we verified another device and secret-gossiping failed, or the other device itself lacked the secrets. |
| 126 | + // We simulate this case by deleting the cached secrets in the indexedDB. |
| 127 | + test( |
| 128 | + "should enter the recovery key when the secrets are not cached", |
| 129 | + { tag: "@screenshot" }, |
| 130 | + async ({ page, app, util }) => { |
| 131 | + await verifySession(app, "new passphrase"); |
| 132 | + // We need to delete the cached secrets |
| 133 | + await deleteCachedSecrets(page); |
| 134 | + |
| 135 | + await util.openEncryptionTab(); |
| 136 | + // We ask the user to enter the recovery key |
| 137 | + const dialog = util.getEncryptionTabContent(); |
| 138 | + const enterKeyButton = dialog.getByRole("button", { name: "Enter recovery key" }); |
| 139 | + await expect(enterKeyButton).toBeVisible(); |
| 140 | + await expect(dialog).toMatchScreenshot("out-of-sync-recovery.png"); |
| 141 | + await enterKeyButton.click(); |
| 142 | + |
| 143 | + // Fill the recovery key |
| 144 | + await util.enterRecoveryKey(recoveryKey); |
| 145 | + await expect(dialog).toMatchScreenshot("default-recovery.png"); |
| 146 | + |
| 147 | + // Check that our device is now cross-signed |
| 148 | + await checkDeviceIsCrossSigned(app); |
| 149 | + |
| 150 | + // Check that the current device is connected to key backup |
| 151 | + // The backup decryption key should be in cache also, as we got it directly from the 4S |
| 152 | + await app.closeDialog(); |
| 153 | + await checkDeviceIsConnectedKeyBackup(page, expectedBackupVersion, true); |
| 154 | + }, |
| 155 | + ); |
| 156 | +}); |
| 157 | + |
| 158 | +/** |
| 159 | + * Remove the cached secrets from the indexedDB |
| 160 | + * This is a workaround to simulate the case where the secrets are not cached. |
| 161 | + */ |
| 162 | +async function deleteCachedSecrets(page: Page) { |
| 163 | + await page.evaluate(async () => { |
| 164 | + const removeCachedSecrets = new Promise((resolve) => { |
| 165 | + const request = window.indexedDB.open("matrix-js-sdk::matrix-sdk-crypto"); |
| 166 | + request.onsuccess = async (event: Event & { target: { result: IDBDatabase } }) => { |
| 167 | + const db = event.target.result; |
| 168 | + const request = db.transaction("core", "readwrite").objectStore("core").delete("private_identity"); |
| 169 | + request.onsuccess = () => { |
| 170 | + db.close(); |
| 171 | + resolve(undefined); |
| 172 | + }; |
| 173 | + }; |
| 174 | + }); |
| 175 | + await removeCachedSecrets; |
| 176 | + }); |
| 177 | + await page.reload(); |
| 178 | +} |
0 commit comments