Add logging around key-storage-out-of-sync handling (#31985)

... because unpicking this was a nightmare
This commit is contained in:
Richard van der Hoff
2026-02-10 16:42:14 +00:00
committed by GitHub
parent 6613c3f87a
commit 2540c8a8af
3 changed files with 42 additions and 16 deletions
@@ -6,7 +6,7 @@
* Please see LICENSE files in the repository root for full details. * Please see LICENSE files in the repository root for full details.
*/ */
import React, { type JSX, type MouseEventHandler, useState } from "react"; import React, { type JSX, type MouseEventHandler, useCallback, useState } from "react";
import { import {
Breadcrumb, Breadcrumb,
Button, Button,
@@ -20,6 +20,7 @@ import {
} from "@vector-im/compound-web"; } from "@vector-im/compound-web";
import CopyIcon from "@vector-im/compound-design-tokens/assets/web/icons/copy"; import CopyIcon from "@vector-im/compound-design-tokens/assets/web/icons/copy";
import KeyIcon from "@vector-im/compound-design-tokens/assets/web/icons/key-solid"; import KeyIcon from "@vector-im/compound-design-tokens/assets/web/icons/key-solid";
import { logger } from "matrix-js-sdk/src/logger";
import { _t } from "../../../../languageHandler"; import { _t } from "../../../../languageHandler";
import { EncryptionCard } from "./EncryptionCard"; import { EncryptionCard } from "./EncryptionCard";
@@ -79,6 +80,11 @@ export function ChangeRecoveryKey({
// "recovery" is about. Otherwise, we jump straight to showing the user the new key. // "recovery" is about. Otherwise, we jump straight to showing the user the new key.
const [state, setState] = useState<State>(userHasRecoveryKey ? "save_key_change_flow" : "inform_user"); const [state, setState] = useState<State>(userHasRecoveryKey ? "save_key_change_flow" : "inform_user");
const onCancelClickWrapper = useCallback(() => {
logger.debug("ChangeRecoveryKey: user cancelled");
onCancelClick();
}, [onCancelClick]);
// We create a new recovery key, the recovery key will be displayed to the user // We create a new recovery key, the recovery key will be displayed to the user
const recoveryKey = useAsyncMemo(() => matrixClient.getCrypto()!.createRecoveryKeyFromPassphrase(), []); const recoveryKey = useAsyncMemo(() => matrixClient.getCrypto()!.createRecoveryKeyFromPassphrase(), []);
// Waiting for the recovery key to be generated // Waiting for the recovery key to be generated
@@ -91,7 +97,7 @@ export function ChangeRecoveryKey({
content = ( content = (
<InformationPanel <InformationPanel
onContinueClick={() => setState("save_key_setup_flow")} onContinueClick={() => setState("save_key_setup_flow")}
onCancelClick={onCancelClick} onCancelClick={onCancelClickWrapper}
/> />
); );
break; break;
@@ -109,7 +115,7 @@ export function ChangeRecoveryKey({
: "confirm_key_setup_flow", : "confirm_key_setup_flow",
) )
} }
onCancelClick={onCancelClick} onCancelClick={onCancelClickWrapper}
/> />
); );
break; break;
@@ -120,7 +126,7 @@ export function ChangeRecoveryKey({
<KeyForm <KeyForm
// encodedPrivateKey is always defined, the optional typing is incorrect // encodedPrivateKey is always defined, the optional typing is incorrect
recoveryKey={recoveryKey.encodedPrivateKey!} recoveryKey={recoveryKey.encodedPrivateKey!}
onCancelClick={onCancelClick} onCancelClick={onCancelClickWrapper}
onSubmit={async () => { onSubmit={async () => {
const crypto = matrixClient.getCrypto(); const crypto = matrixClient.getCrypto();
if (!crypto) return onFinish(); if (!crypto) return onFinish();
@@ -133,6 +139,9 @@ export function ChangeRecoveryKey({
// keyStorageOutOfSyncNeedsBackupReset won't be able to check // keyStorageOutOfSyncNeedsBackupReset won't be able to check
// the backup state. // the backup state.
const needsBackupReset = await deviceListener.keyStorageOutOfSyncNeedsBackupReset(true); const needsBackupReset = await deviceListener.keyStorageOutOfSyncNeedsBackupReset(true);
logger.debug(
`ChangeRecoveryKey: user confirmed recovery key; now doing change. needsBackupReset: ${needsBackupReset}`,
);
await deviceListener.whilePaused(async () => { await deviceListener.whilePaused(async () => {
// We need to enable the cache to avoid to prompt the user to enter the new key // We need to enable the cache to avoid to prompt the user to enter the new key
// when we will try to access the secret storage during the bootstrap // when we will try to access the secret storage during the bootstrap
@@ -178,9 +187,9 @@ export function ChangeRecoveryKey({
<> <>
<Breadcrumb <Breadcrumb
backLabel={_t("action|back")} backLabel={_t("action|back")}
onBackClick={onCancelClick} onBackClick={onCancelClickWrapper}
pages={pages} pages={pages}
onPageClick={onCancelClick} onPageClick={onCancelClickWrapper}
/> />
<EncryptionCard <EncryptionCard
Icon={KeyIcon} Icon={KeyIcon}
@@ -8,6 +8,7 @@
import React, { type JSX } from "react"; import React, { type JSX } from "react";
import { Button } from "@vector-im/compound-web"; import { Button } from "@vector-im/compound-web";
import KeyIcon from "@vector-im/compound-design-tokens/assets/web/icons/key"; import KeyIcon from "@vector-im/compound-design-tokens/assets/web/icons/key";
import { logger } from "matrix-js-sdk/src/logger";
import { SettingsSection } from "../shared/SettingsSection"; import { SettingsSection } from "../shared/SettingsSection";
import { _t } from "../../../../languageHandler"; import { _t } from "../../../../languageHandler";
@@ -78,6 +79,9 @@ export function RecoveryPanelOutOfSync({
// the backup state. // the backup state.
const needsBackupReset = await deviceListener.keyStorageOutOfSyncNeedsBackupReset(false); const needsBackupReset = await deviceListener.keyStorageOutOfSyncNeedsBackupReset(false);
logger.debug(
`RecoveryPanelOutOfSync: user clicked 'Enter recovery key'. needsBackupReset: ${needsBackupReset}`,
);
try { try {
// pause the device listener because we could be making lots // pause the device listener because we could be making lots
// of changes, and don't want toasts to pop up and disappear // of changes, and don't want toasts to pop up and disappear
+23 -10
View File
@@ -11,6 +11,7 @@ import React from "react";
import { KeyIcon, ErrorSolidIcon, SettingsSolidIcon } from "@vector-im/compound-design-tokens/assets/web/icons"; import { KeyIcon, ErrorSolidIcon, SettingsSolidIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
import { type ComponentType } from "react"; import { type ComponentType } from "react";
import { type Interaction as InteractionEvent } from "@matrix-org/analytics-events/types/typescript/Interaction"; import { type Interaction as InteractionEvent } from "@matrix-org/analytics-events/types/typescript/Interaction";
import { logger } from "matrix-js-sdk/src/logger";
import Modal from "../Modal"; import Modal from "../Modal";
import { _t } from "../languageHandler"; import { _t } from "../languageHandler";
@@ -144,6 +145,7 @@ const getDescription = (state: DeviceStateForToast): string => {
* @param state The state of the device * @param state The state of the device
*/ */
export const showToast = (state: DeviceStateForToast): void => { export const showToast = (state: DeviceStateForToast): void => {
const myLogger = logger.getChild(`SetupEncryptionToast[${state}]:`);
if ( if (
ModuleRunner.instance.extensions.cryptoSetup.setupEncryptionNeeded({ ModuleRunner.instance.extensions.cryptoSetup.setupEncryptionNeeded({
kind: state as any, kind: state as any,
@@ -162,6 +164,7 @@ export const showToast = (state: DeviceStateForToast): void => {
interactionType: "Pointer", interactionType: "Pointer",
name: state === "set_up_recovery" ? "ToastSetUpRecoveryClick" : "ToastTurnOnKeyStorageClick", name: state === "set_up_recovery" ? "ToastSetUpRecoveryClick" : "ToastTurnOnKeyStorageClick",
}); });
myLogger.debug("Primary button clicked: opening encryption settings dialog");
// Open the user settings dialog to the encryption tab // Open the user settings dialog to the encryption tab
const payload: OpenToTabPayload = { const payload: OpenToTabPayload = {
action: Action.ViewUserSettings, action: Action.ViewUserSettings,
@@ -171,9 +174,11 @@ export const showToast = (state: DeviceStateForToast): void => {
break; break;
} }
case "verify_this_session": case "verify_this_session":
myLogger.debug("Primary button clicked: opening SetupEncryptionDialog");
Modal.createDialog(SetupEncryptionDialog, {}, undefined, /* priority = */ false, /* static = */ true); Modal.createDialog(SetupEncryptionDialog, {}, undefined, /* priority = */ false, /* static = */ true);
break; break;
case "key_storage_out_of_sync": { case "key_storage_out_of_sync": {
myLogger.debug("Primary button clicked: starting recovery process");
const modal = Modal.createDialog( const modal = Modal.createDialog(
Spinner, Spinner,
undefined, undefined,
@@ -215,6 +220,7 @@ export const showToast = (state: DeviceStateForToast): void => {
break; break;
} }
case "identity_needs_reset": { case "identity_needs_reset": {
myLogger.debug("Primary button clicked: opening encryption settings dialog");
// Open the user settings dialog to reset identity // Open the user settings dialog to reset identity
const payload: OpenToTabPayload = { const payload: OpenToTabPayload = {
action: Action.ViewUserSettings, action: Action.ViewUserSettings,
@@ -237,6 +243,7 @@ export const showToast = (state: DeviceStateForToast): void => {
interactionType: "Pointer", interactionType: "Pointer",
name: "ToastSetUpRecoveryDismiss", name: "ToastSetUpRecoveryDismiss",
}); });
myLogger.debug("Secondary button clicked: disabling recovery");
// Record that the user doesn't want to set up recovery // Record that the user doesn't want to set up recovery
const deviceListener = DeviceListener.sharedInstance(); const deviceListener = DeviceListener.sharedInstance();
await deviceListener.recordRecoveryDisabled(); await deviceListener.recordRecoveryDisabled();
@@ -247,14 +254,14 @@ export const showToast = (state: DeviceStateForToast): void => {
// Open the user settings dialog to the encryption tab and start the flow to reset encryption or change the recovery key // Open the user settings dialog to the encryption tab and start the flow to reset encryption or change the recovery key
const deviceListener = DeviceListener.sharedInstance(); const deviceListener = DeviceListener.sharedInstance();
const needsCrossSigningReset = await deviceListener.keyStorageOutOfSyncNeedsCrossSigningReset(true); const needsCrossSigningReset = await deviceListener.keyStorageOutOfSyncNeedsCrossSigningReset(true);
const props = {
initialEncryptionState: needsCrossSigningReset ? "reset_identity_forgot" : "change_recovery_key",
};
myLogger.debug(`Secondary button clicked: opening encryption settings dialog with props`, props);
const payload: OpenToTabPayload = { const payload: OpenToTabPayload = {
action: Action.ViewUserSettings, action: Action.ViewUserSettings,
initialTabId: UserTab.Encryption, initialTabId: UserTab.Encryption,
props: { props,
initialEncryptionState: needsCrossSigningReset
? "reset_identity_forgot"
: "change_recovery_key",
},
}; };
defaultDispatcher.dispatch(payload); defaultDispatcher.dispatch(payload);
break; break;
@@ -273,6 +280,7 @@ export const showToast = (state: DeviceStateForToast): void => {
); );
const [dismissed] = await modal.finished; const [dismissed] = await modal.finished;
if (dismissed) { if (dismissed) {
myLogger.debug("Secondary button clicked and confirmed: recording key storage disabled");
const deviceListener = DeviceListener.sharedInstance(); const deviceListener = DeviceListener.sharedInstance();
await deviceListener.recordKeyBackupDisabled(); await deviceListener.recordKeyBackupDisabled();
deviceListener.dismissEncryptionSetup(); deviceListener.dismissEncryptionSetup();
@@ -280,6 +288,7 @@ export const showToast = (state: DeviceStateForToast): void => {
break; break;
} }
default: default:
myLogger.debug("Secondary button clicked: dismissing");
DeviceListener.sharedInstance().dismissEncryptionSetup(); DeviceListener.sharedInstance().dismissEncryptionSetup();
} }
}; };
@@ -293,20 +302,24 @@ export const showToast = (state: DeviceStateForToast): void => {
*/ */
const onAccessSecretStorageFailed = async (error: Error): Promise<void> => { const onAccessSecretStorageFailed = async (error: Error): Promise<void> => {
if (error instanceof AccessCancelledError) { if (error instanceof AccessCancelledError) {
myLogger.debug("AccessSecretStorage failed: user cancelled");
// The user cancelled the dialog - just allow it to close // The user cancelled the dialog - just allow it to close
} else { } else {
// A real error happened - jump to the reset identity or change // A real error happened - jump to the reset identity or change
// recovery tab // recovery tab
const needsCrossSigningReset = const needsCrossSigningReset =
await DeviceListener.sharedInstance().keyStorageOutOfSyncNeedsCrossSigningReset(true); await DeviceListener.sharedInstance().keyStorageOutOfSyncNeedsCrossSigningReset(true);
const props = {
initialEncryptionState: needsCrossSigningReset ? "reset_identity_sync_failed" : "change_recovery_key",
};
myLogger.debug(
`AccessSecretStorage failed: ${error}. Opening encryption settings dialog with props: `,
props,
);
const payload: OpenToTabPayload = { const payload: OpenToTabPayload = {
action: Action.ViewUserSettings, action: Action.ViewUserSettings,
initialTabId: UserTab.Encryption, initialTabId: UserTab.Encryption,
props: { props,
initialEncryptionState: needsCrossSigningReset
? "reset_identity_sync_failed"
: "change_recovery_key",
},
}; };
defaultDispatcher.dispatch(payload); defaultDispatcher.dispatch(payload);
} }