From a465cfeaa8c49dff3884e966fa759e1661d9aa8c Mon Sep 17 00:00:00 2001 From: M1nd3r Date: Thu, 1 Aug 2024 14:34:35 +0200 Subject: [PATCH] feat(core): add SilentError and suppres message response on Cancel from Host in pairing phase when waiting for user interaction [no changelog] --- core/src/apps/thp/pairing.py | 21 +++++++++++++++++---- core/src/trezor/wire/errors.py | 6 ++++++ core/src/trezor/wire/thp/pairing_context.py | 6 ++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/core/src/apps/thp/pairing.py b/core/src/apps/thp/pairing.py index e0a6d9eb1..92c73a110 100644 --- a/core/src/apps/thp/pairing.py +++ b/core/src/apps/thp/pairing.py @@ -5,6 +5,7 @@ from trezor import loop, protobuf from trezor.crypto.hashlib import sha256 from trezor.enums import MessageType, ThpPairingMethod from trezor.messages import ( + Cancel, ThpCodeEntryChallenge, ThpCodeEntryCommitment, ThpCodeEntryCpaceHost, @@ -23,7 +24,7 @@ from trezor.messages import ( ThpQrCodeTag, ThpStartPairingRequest, ) -from trezor.wire.errors import ActionCancelled, UnexpectedMessage +from trezor.wire.errors import ActionCancelled, SilentError, UnexpectedMessage from trezor.wire.thp import ChannelState, ThpError, crypto from trezor.wire.thp.pairing_context import PairingContext @@ -97,7 +98,9 @@ async def handle_pairing_request( await _prepare_pairing(ctx) await ctx.write(ThpPairingPreparationsFinished()) ctx.channel_ctx.set_channel_state(ChannelState.TP3) - response = await show_display_data(ctx, _get_possible_pairing_methods(ctx)) + response = await show_display_data( + ctx, _get_possible_pairing_methods_and_cancel(ctx) + ) if __debug__: from trezor.messages import DebugLinkGetState @@ -107,8 +110,12 @@ async def handle_pairing_request( dl_state = await dispatch_DebugLinkGetState(response) assert dl_state is not None await ctx.write(dl_state) - response = await show_display_data(ctx, _get_possible_pairing_methods(ctx)) - + response = await show_display_data( + ctx, _get_possible_pairing_methods_and_cancel(ctx) + ) + if Cancel.is_type_of(response): + ctx.channel_ctx.clear() + raise SilentError("Action was cancelled by the Host") # TODO disable NFC (if enabled) response = await _handle_different_pairing_methods(ctx, response) @@ -369,6 +376,12 @@ def _is_method_included(ctx: PairingContext, method: ThpPairingMethod) -> bool: # Helpers - getters +def _get_possible_pairing_methods_and_cancel(ctx: PairingContext) -> Tuple[int, ...]: + r = _get_possible_pairing_methods(ctx) + mtype = Cancel.MESSAGE_WIRE_TYPE + return r + ((mtype,) if mtype is not None else ()) + + def _get_possible_pairing_methods(ctx: PairingContext) -> Tuple[int, ...]: r = tuple( _get_message_type_for_method(method) diff --git a/core/src/trezor/wire/errors.py b/core/src/trezor/wire/errors.py index 376820b58..e8b2d3feb 100644 --- a/core/src/trezor/wire/errors.py +++ b/core/src/trezor/wire/errors.py @@ -8,6 +8,12 @@ class Error(Exception): self.message = message +class SilentError(Exception): + def __init__(self, message: str) -> None: + super().__init__() + self.message = message + + class UnexpectedMessage(Error): def __init__(self, message: str) -> None: super().__init__(FailureType.UnexpectedMessage, message) diff --git a/core/src/trezor/wire/thp/pairing_context.py b/core/src/trezor/wire/thp/pairing_context.py index c816d9154..513121cc0 100644 --- a/core/src/trezor/wire/thp/pairing_context.py +++ b/core/src/trezor/wire/thp/pairing_context.py @@ -7,7 +7,7 @@ from trezor.crypto import random from trezor.ui.layouts.tt import RustLayout from trezor.wire import context, message_handler, protocol_common from trezor.wire.context import UnexpectedMessageException -from trezor.wire.errors import ActionCancelled +from trezor.wire.errors import ActionCancelled, SilentError from trezor.wire.protocol_common import Context, Message if TYPE_CHECKING: @@ -233,7 +233,9 @@ async def handle_pairing_request_message( # We might handle only the few common cases here, like # Initialize and Cancel. return exc.msg - + except SilentError as exc: + if __debug__: + log.error(__name__, "SilentError: %s", exc.message) except BaseException as exc: # Either: # - the message had a type that has a registered handler, but does not have