refactor(core): simplify THP session exception handling
What changed, and why it matters
This is a small internal code cleanup in Trezor firmware's message handling loop. It removes an optional 'no message' path and restructures exception handling so the loop continues cleanly after unexpected messages. There is no indication this fixes or introduces a security vulnerability.
No security action required. Treat as normal refactoring code review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors GenericSessionContext.handle() in core/src/trezor/wire/thp/session_context.py. The signature changes from handle(message: Message | None = None) to handle(message: Message), removing the branch that called _read_next_message() when no message was supplied. Exception handling is simplified: WireError and generic Exception cases no longer return inside the except block; instead the loop naturally falls through to a trailing return. The UnexpectedMessageException case now explicitly continues the loop with the new message. The functional behavior appears equivalent for the caller-supplied-message path.
Changed components
core/src/trezor/wire/thp/session_context.pyInspect captured patch +3 / −6
diff --git a/core/src/trezor/wire/thp/session_context.py b/core/src/trezor/wire/thp/session_context.py
index 13a131777..1dcef8a73 100644
--- a/core/src/trezor/wire/thp/session_context.py
+++ b/core/src/trezor/wire/thp/session_context.py
@@ -35,7 +35,7 @@ class GenericSessionContext(Context):
self.channel: Channel = channel
self.session_id: int = session_id
- async def handle(self, message: Message | None = None) -> None:
+ async def handle(self, message: Message) -> None:
if __debug__:
log.debug(
__name__,
@@ -47,25 +47,22 @@ class GenericSessionContext(Context):
while True:
try:
- if message is None:
- message = await self._read_next_message()
await handle_single_message(self, message)
if __debug__:
self.channel._log("session loop is over")
- return
except protocol_common.WireError as e:
if __debug__:
log.exception(__name__, e, iface=self.iface)
await self.write(failure(e))
- return
except UnexpectedMessageException as unexpected:
# The workflow was interrupted by an unexpected message. We need to
# process it as if it was a new message...
message = unexpected.msg
+ continue
except Exception as exc:
if __debug__:
log.exception(__name__, exc, iface=self.iface)
- return
+ return
async def _read_next_message(self) -> Message:
while True:
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.