Skip to content

feat(signmessage): harden QR decoding and derivation path parsing (fixes #1062) - #1063

Open
muskan-creates352 wants to merge 4 commits into
SeedSigner:devfrom
muskan-creates352:robust-signmessage-pipeline
Open

muskan-creates352 wants to merge 4 commits into
SeedSigner:devfrom
muskan-creates352:robust-signmessage-pipeline

Conversation

@muskan-creates352

Copy link
Copy Markdown

Fixes #1062
(Follow-up to #1059 based on feedback from @wolffgang1729)

Description

This PR addresses the follow-up edge cases identified in #1059 to make the signmessage QR parsing and verification pipeline robust end-to-end:

  1. Short/Partial Derivation Paths Downstream:

    • embit_utils.parse_derivation_path() now defensively checks that the derivation path has sufficient sections (len(sections) >= 3 and starts with "m") before indexing.
    • For short/relative paths like m/84h or 0/0, it safely returns clean_match: False without raising an unhandled IndexError.
    • SeedSignMessageStartView catches short/custom paths and routes them cleanly to NotYetImplementedView.
  2. Multisig Derivation Paths (BIP-48 / m/48h):

    • embit_utils.parse_derivation_path() raises NotImplementedError("Signing messages for multisig paths not supported") when encountering 48h.
    • SeedSignMessageStartView handles NotImplementedError gracefully and routes users to NotYetImplementedView explaining that multisig message signing is not yet supported.
  3. Strict Command Token Enforcement:

    • SignMessageQrDecoder.add() now validates parts[0] == "signmessage" to strictly enforce the command token and reject malformed command variations (e.g., signmessage_v2).
  4. Robust Format Prefix Splitting:

    • Instead of splitting the entire raw payload string with segment.split(format_prefix, 1), the decoder now searches for the format_prefix specifically after the derivation path token. This prevents premature splitting if a derivation path contains the substring ascii:.

Testing

  • tests/test_sign_message_qr_decoder.py: Added tests for strict command matching, path containing ascii:, empty message parsing, and multiple colons in message (10/10 passed).
  • tests/test_embit_utils.py: Added tests for short/invalid paths and BIP-48 multisig paths (8/8 passed).
  • tests/test_flows_seed.py: Updated test_sign_message_unsupported_derivation_flow to verify that short and multisig derivation paths route to NotYetImplementedView (21/21 passed).
  • Full test suite: All 325 tests pass cleanly.

@wolgwang1729 wolgwang1729 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tested this branch against an automated fuzzer (published on my fork).

Adding a try...except block in SeedSignMessageStartView only patches the symptom at the first screen. The core issue is that parse_derivation_path() still marks invalid derivation paths as clean_match=True. Because they are marked clean, they bypass StartView and crash downstream in ConfirmAddressView:

  • m/84h/0h/0h/0/2147483648 (raises HDError: Can't do hardened with public key)
  • m/84h/0h/invalid_acc/0/0 (raises ValueError: invalid literal for int())
  • m/99h/0h/0h/0/0 (raises Exception: Custom derivation not supported)

@muskan-creates352

Copy link
Copy Markdown
Author

Thank you so much for the thorough review and for running the automated fuzzer against this branch, @wolgwang1729!

I've addressed the root causes in embit_utils.parse_derivation_path() in commit d120562:

  1. Restricted clean_match strictly to standard single-sig paths:

    • Custom derivation paths (SettingsConstants.CUSTOM_DERIVATION, such as m/99h/...) and non-standard depths now strictly return clean_match=False. This prevents custom paths from bypassing SeedSignMessageStartView.
    • Also added a defensive check in SeedSignMessageStartView (if not addr_format["clean_match"] or addr_format["script_type"] == SettingsConstants.CUSTOM_DERIVATION:) so custom paths gracefully redirect to NotYetImplementedView.
  2. Strict BIP-32 index validation (< 2^31):

    • Added _is_valid_bip32_int() ensuring address indexes and account components are ASCII-only digits within the valid BIP-32 unhardened range (0 <= index < 2147483648).
    • Prevents hardened indexes (e.g. 2147483648), negative indexes (-1), and non-ASCII unicode digits (such as ²) from raising downstream HDError or ValueError.
  3. Account component & casing validation:

    • Validated that the account component is numeric and hardened (endswith("h")), safely returning wallet_derivation_path=None for malformed accounts like invalid_acc.
    • Normalized uppercase H to h.
  4. Verification:

    • Replayed the fuzzer from your branch across all corpus cases and 100,000+ random iterations: Ran 100065 cases, produced 0 findings (0 unique FAIL, 0 unique WARN). RESULT: PASS.
    • Added unit tests covering all fuzzer test vectors in tests/test_embit_utils.py (all tests passing).

@wolgwang1729

Copy link
Copy Markdown

Commit d120562 just patches against the specific fuzzer README vectors without understanding the architecture. It introduces dead code in embit_utils.py that can never execute, redundant checks in seed_views.py, and still breaks the DecodeQR contract by never setting qr_type = INVALID, causing invalid QRs to silently dump users to the Main Menu with no error. Please actually review your own code and test the UI flow instead of just rushing out patches to silence test cases.

@muskan-creates352

Copy link
Copy Markdown
Author

Thank you for calling this out and for the direct feedback, @wolgwang1729 — you're completely right regarding the DecodeQR contract and the UI flow behavior.

I've taken a step back and addressed the root architectural issues in commit 24726fb:

  1. Fixed DecodeQR contract (src/seedsigner/models/decode_qr.py):

    • Updated DecodeQR.add_data() so that when any decoder returns DecodeQRStatus.INVALID, self.qr_type is properly updated to QRType.INVALID.
    • As a result, self.decoder.is_invalid correctly evaluates to True, allowing ScanView to route users to ScanInvalidQRTypeView instead of silently falling through to MainMenuView.
  2. Cleaned up derivation path parser & removed dead code (src/seedsigner/helpers/embit_utils.py):

    • Removed the unreachable elif len(sections) > 3 branch and simplified parse_derivation_path() to directly validate the 6-component single-sig standard (m / purpose' / coin' / account' / change / index).
    • Standard paths cleanly set wallet_derivation_path and clean_match = True; all non-standard, custom, or malformed paths evaluate directly to clean_match = False with no dead code.
  3. Removed redundant checks in views (src/seedsigner/views/seed_views.py):

    • Stripped out the broad except Exception: and redundant CUSTOM_DERIVATION condition in SeedSignMessageStartView. The view now cleanly relies on the clean_match contract and handles NotImplementedError for multisig paths.
  4. Added UI Flow & Decoder tests:

    • Added test_sign_message_malformed_qr_payload_routes_to_invalid_qr_view in tests/test_flows_seed.py asserting that scanning malformed signmessage QRs properly routes through ScanInvalidQRTypeView.
    • Updated tests/test_sign_message_qr_decoder.py to assert decoder.is_invalid is True on invalid payloads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harden signmessage QR decoding and downstream derivation path parsing against malformed inputs and BIP-48 paths

2 participants