Skip to content

fix(core/thp): correct Failure_InvalidProtocol constant message#6549

Merged
romanz merged 1 commit into
mainfrom
romanz/thp-invalid
Mar 3, 2026
Merged

fix(core/thp): correct Failure_InvalidProtocol constant message#6549
romanz merged 1 commit into
mainfrom
romanz/thp-invalid

Conversation

@romanz
Copy link
Copy Markdown
Contributor

@romanz romanz commented Mar 3, 2026

Otherwise, the host will receive b'\x08\x11' + b'\x00'*18.
The trailing bytes are skipped by trezorlib, since our Python protobuf parser ignores unknown fields:

if ftag not in msg_type.FIELDS: # unknown field, skip it
if wtype == WIRE_TYPE_INT:
load_uvarint(reader)
elif wtype == WIRE_TYPE_LENGTH:
ivalue = load_uvarint(reader)
if ivalue > MAX_FIELD_SIZE:
raise ValueError(f"Unknown field {ftag} too large ({ivalue} bytes)")
reader.readinto(bytearray(ivalue))
else:
raise ValueError
continue

@romanz romanz self-assigned this Mar 3, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 3, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 53ac49c and 68406c3.

📒 Files selected for processing (2)
  • core/src/trezor/wire/thp/interface_context.py
  • tests/device_tests/thp/test_basic.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • core/src/trezor/wire/thp/interface_context.py

Walkthrough

The PR adjusts the THP codec_v1 error response serialization: the failure payload length/type bytes for an InvalidProtocol Failure are changed from 0x14 to 0x02 in the encoded response. No public API signatures were modified and the handler's control flow (construct buffer, memcpy bytes, write packet) remains the same. Tests were updated to use a local expected Failure instance and to assert encoded output matches exactly, preventing trailing bytes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The description explains the problem (trailing bytes sent to host) and the technical context, but lacks structured sections matching the template (no Development status, PR setup sections, or QA notes despite PR being open). Clarify the PR development status (Draft vs. Final) and add a 'Notes for QA' section if testable, or confirm 'Done (no QA)' if not testable, to complete the template structure.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main change: correcting the Failure_InvalidProtocol constant message size in the THP codec.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch romanz/thp-invalid

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@trezor-bot trezor-bot Bot added this to Firmware Mar 3, 2026
@romanz romanz requested a review from mmilata March 3, 2026 12:47
@github-project-automation github-project-automation Bot moved this to 🔎 Needs review in Firmware Mar 3, 2026
@romanz romanz requested a review from matejcik March 3, 2026 12:48
@romanz romanz marked this pull request as ready for review March 3, 2026 12:48
@romanz romanz requested a review from obrusvit as a code owner March 3, 2026 12:48
@romanz romanz requested a review from szymonlesisz March 3, 2026 12:48
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 3, 2026

en main(all)

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: 22625299399

@mmilata
Copy link
Copy Markdown
Member

mmilata commented Mar 3, 2026

Seems to parse correctly by trezorlib in main. Does this fix interoperability with some other parser?

@romanz
Copy link
Copy Markdown
Contributor Author

romanz commented Mar 3, 2026

Yes - @szymonlesisz has found this issue when using a new parser (which didn't skip the trailing zero bytes).

Copy link
Copy Markdown
Member

@mmilata mmilata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, protoscope seems to agree. Will fix this in rust/trezor-thp.

@romanz romanz force-pushed the romanz/thp-invalid branch from 53ac49c to 68406c3 Compare March 3, 2026 13:32
@romanz
Copy link
Copy Markdown
Contributor Author

romanz commented Mar 3, 2026

Added a check in tests/device_tests/thp/test_basic.py::test_v1:
https://github.com/trezor/trezor-firmware/compare/53ac49c2f79f4bcb1e32c38d1c109032f710c56a..68406c3b4b9e83407db247718576ee8d7f0313f3

@romanz romanz merged commit d4b0ca0 into main Mar 3, 2026
104 checks passed
@romanz romanz deleted the romanz/thp-invalid branch March 3, 2026 14:49
@github-project-automation github-project-automation Bot moved this from 🔎 Needs review to 🤝 Needs QA in Firmware Mar 3, 2026
@romanz romanz moved this from 🤝 Needs QA to ✅ Done (no QA) in Firmware Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants