fix(python): raise ProtocolError on invalid THP continuation#6560
Conversation
WalkthroughThe changes improve error handling in THP packet parsing within the thp_io module. Invalid initial headers now raise a specific Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
ProtocolError on invalid THP continuation
|
| model | device_test | click_test | persistence_test |
|---|---|---|---|
| T2T1 | test(all) main(all) ![]() |
test(all) main(all) ![]() |
test(all) main(all) ![]() |
| T3B1 | test(all) main(all) ![]() |
test(all) main(all) ![]() |
test(all) main(all) ![]() |
| T3T1 | test(all) main(all) ![]() |
test(all) main(all) ![]() |
test(all) main(all) ![]() |
| T3W1 | test(all) main(all) ![]() |
test(all) main(all) ![]() |
test(all) main(all) ![]() |
Latest CI run: 22725403312
There was a problem hiding this comment.
🧹 Nitpick comments (1)
python/src/trezorlib/thp/thp_io.py (1)
97-98: Chain the originalstruct.errorexception when re-raisingProtocolErrorto preserve the parsing failure source.Both handlers currently lose the original exception context. Add
as errand chain withfrom errat lines 97-98 and 123-124.Proposed diff
- except struct.error: - raise exceptions.ProtocolError("Invalid message header") + except struct.error as err: + raise exceptions.ProtocolError("Invalid message header") from err ... - except struct.error: - raise exceptions.ProtocolError("Invalid continuation header") + except struct.error as err: + raise exceptions.ProtocolError("Invalid continuation header") from err🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@python/src/trezorlib/thp/thp_io.py` around lines 97 - 98, The except blocks that catch struct.error and re-raise exceptions.ProtocolError must preserve the original exception context; update both occurrences (the header handler that currently does "except struct.error: raise exceptions.ProtocolError('Invalid message header')" and the later body handler that raises ProtocolError for the message body) to capture the original exception (e.g., "except struct.error as err") and chain it when re-raising the ProtocolError using "from err" so the original struct.error is preserved in the exception chain.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@python/src/trezorlib/thp/thp_io.py`:
- Around line 97-98: The except blocks that catch struct.error and re-raise
exceptions.ProtocolError must preserve the original exception context; update
both occurrences (the header handler that currently does "except struct.error:
raise exceptions.ProtocolError('Invalid message header')" and the later body
handler that raises ProtocolError for the message body) to capture the original
exception (e.g., "except struct.error as err") and chain it when re-raising the
ProtocolError using "from err" so the original struct.error is preserved in the
exception chain.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 2ac7717d-9af9-4bae-ac53-9ccf714dd26a
📒 Files selected for processing (1)
python/src/trezorlib/thp/thp_io.py




































Fixes #6559.