fix(core): avoid infinite THP error loop
What changed, and why it matters
This small patch fixes a bug in the Trezor hardware wallet's message-handling code where certain errors could cause the device to loop forever instead of stopping. Before the fix, if a specific kind of error happened while processing a message, the code would just log it and continue running, potentially retrying the same failing work endlessly. The patch makes the handler return after logging, breaking the loop. This could prevent device lock-ups or unresponsiveness, but the change alone does not clearly enable remote theft of funds or private keys.
Review whether the added 'return' statements correctly propagate state cleanup for the THP session, and confirm that UnexpectedMessageException remains the only exception type that should restart the loop. Add regression tests covering malformed or aborted THP messages to ensure the loop exits cleanly. Consider whether the device UI or host needs an explicit error code when the handler returns after a generic exception.
Security signals we found
Infinite loop / denial-of-service condition in session message handler
Missing control-flow termination after error handling
THP (Trezor Host Protocol) session context affected
Patch is partial/minimal and does not include tests or changelog
Evidence from the diff
In core/src/trezor/wire/thp/session_context.py, the GenericSessionContext.handle_message loop catches MessageError, UnexpectedMessageException, and a generic Exception. Previously, after catching MessageError it sent a failure response but did not return, and after the generic Exception it only logged and continued. The patch adds ‘return’ after both the MessageError failure write and the generic Exception log, terminating the message-handling coroutine instead of continuing the while-loop. This stops an infinite retry/loop scenario when errors occur during THP (Trezor Host Protocol) session handling.
Changed components
core/src/trezor/wire/thp/session_context.pyGenericSessionContext.handle_messageTrezor Host Protocol (THP) session handlingInspect captured patch +2 / −1
diff --git a/core/src/trezor/wire/thp/session_context.py b/core/src/trezor/wire/thp/session_context.py
index 0c23bb70a..6253a0117 100644
--- a/core/src/trezor/wire/thp/session_context.py
+++ b/core/src/trezor/wire/thp/session_context.py
@@ -56,14 +56,15 @@ class GenericSessionContext(Context):
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
except Exception as exc:
- # Log and try again.
if __debug__:
log.exception(__name__, exc, iface=self.iface)
+ return
async def _read_next_message(self) -> Message:
while True:
Why this scored 42/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.