From 8085a323430327c6ab12739a52cbe41c4b4a95ad Mon Sep 17 00:00:00 2001 From: M1nd3r Date: Mon, 14 Apr 2025 12:20:08 +0200 Subject: [PATCH] fix: properly return Failure.ActionCancelled in Pairing flow, add tests --- core/src/apps/thp/pairing.py | 5 +++- tests/device_tests/thp/test_pairing.py | 34 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/core/src/apps/thp/pairing.py b/core/src/apps/thp/pairing.py index 58394178ae..790d3d1419 100644 --- a/core/src/apps/thp/pairing.py +++ b/core/src/apps/thp/pairing.py @@ -27,7 +27,7 @@ from trezor.messages import ( ) from trezor.wire import message_handler from trezor.wire.context import UnexpectedMessageException -from trezor.wire.errors import SilentError, UnexpectedMessage +from trezor.wire.errors import ActionCancelled, SilentError, UnexpectedMessage from trezor.wire.thp import ChannelState, ThpError, crypto, get_enabled_pairing_methods from trezor.wire.thp.pairing_context import PairingContext @@ -90,6 +90,9 @@ async def handle_pairing_request( ctx: PairingContext, message: protobuf.MessageType ) -> ThpEndResponse: + if Cancel.is_type_of(message): + raise ActionCancelled() + if not ThpPairingRequest.is_type_of(message): raise UnexpectedMessage("Unexpected message") diff --git a/tests/device_tests/thp/test_pairing.py b/tests/device_tests/thp/test_pairing.py index 845253fb71..527da751f8 100644 --- a/tests/device_tests/thp/test_pairing.py +++ b/tests/device_tests/thp/test_pairing.py @@ -1,4 +1,5 @@ import os +import time import typing as t from hashlib import sha256 @@ -12,6 +13,9 @@ from trezorlib.debuglink import TrezorClientDebugLink as Client from trezorlib.messages import ( ButtonAck, ButtonRequest, + Cancel, + Failure, + FailureType, ThpCodeEntryChallenge, ThpCodeEntryCommitment, ThpCodeEntryCpaceHostTag, @@ -25,6 +29,7 @@ from trezorlib.messages import ( ThpNfcTagTrezor, ThpPairingMethod, ThpPairingPreparationsFinished, + ThpPairingRequest, ThpQrCodeSecret, ThpQrCodeTag, ThpSelectMethod, @@ -32,6 +37,7 @@ from trezorlib.messages import ( from trezorlib.transport.thp import curve25519 from trezorlib.transport.thp.cpace import Cpace + from .connect import ( get_encrypted_transport_protocol, handle_pairing_request, @@ -145,6 +151,34 @@ def test_pairing_code_entry(client: Client) -> None: protocol._has_valid_channel = True +def test_pairing_cancel_1(client: Client) -> None: + protocol = prepare_protocol_for_pairing(client) + + protocol._send_message(ThpPairingRequest(host_name="TestTrezor Cancel 1")) + button_req = protocol._read_message(ButtonRequest) + assert button_req.name == "thp_pairing_request" + + protocol._send_message(ButtonAck()) + time.sleep(1) + protocol._send_message(Cancel()) + + resp = protocol._read_message(Failure) + assert resp.code == FailureType.ActionCancelled + + +def test_pairing_cancel_2(client: Client) -> None: + protocol = prepare_protocol_for_pairing(client) + + protocol._send_message(ThpPairingRequest(host_name="TestTrezor Cancel 2")) + button_req = protocol._read_message(ButtonRequest) + assert button_req.name == "thp_pairing_request" + + protocol._send_message(ButtonAck()) + client.debug.press_no() + resp = protocol._read_message(Failure) + assert resp.code == FailureType.ActionCancelled + + def test_pairing_nfc(client: Client) -> None: protocol = prepare_protocol_for_pairing(client)