Skip to content

fix(psbt): harden change branch detection and path validation (fixes #1076) - #1077

Closed
muskan-creates352 wants to merge 1 commit into
SeedSigner:devfrom
muskan-creates352:fix-psbt-change-derivation-handling
Closed

muskan-creates352 wants to merge 1 commit into
SeedSigner:devfrom
muskan-creates352:fix-psbt-change-derivation-handling

Conversation

@muskan-creates352

Copy link
Copy Markdown

Description

Problem or Issue being addressed

  1. In PSBTParser.is_change_branch(), passing None as derivation_path resulted in an unhandled TypeError: object of type 'NoneType' has no len().
  2. In PSBTChangeDetailsView.run(), verified_derivation_path[-1] directly indexed the derivation path without validating its presence or length, leading to potential IndexError / TypeError crashes when encountering change outputs with malformed, empty, or truncated paths (leaving an unaddressed # TODO: Refuse a path too short to carry a branch and an address index).

Fixes #1076

Solution

  • PSBTParser.is_change_branch (src/seedsigner/models/psbt_parser.py):
    • Defensively validates that derivation_path is a non-empty sequence (list or tuple) of length >= 2 before inspecting derivation_path[-2] == 1. Returns False safely for None, empty, or truncated inputs.
  • PSBTChangeDetailsView (src/seedsigner/views/psbt_views.py):
    • Safely extracts verified_derivation_path and checks whether it contains at least 2 elements.
    • If missing, invalid, or too short to carry a branch and address index, gracefully returns Destination(PSBTUnsupportedDerivationPathView, clear_history=True) instead of raising an uncaught exception.
  • Tests:
    • Added test__is_change_branch__handles_invalid_and_edge_case_paths in tests/test_psbt_parser.py testing None, [], [0], invalid types, and valid change/receive paths.
    • Added test_change_details_with_invalid_derivation_path_redirects_to_unsupported_derivation in tests/test_flows_psbt.py verifying graceful redirection to PSBTUnsupportedDerivationPathView.

Additional Information

Zero breaking changes. All valid PSBT change outputs and self-transfers continue to parse and display as expected.

Screenshots

N/A (Redirection to existing PSBTUnsupportedDerivationPathView).


This pull request is categorized as a:

  • New feature
  • Bug fix
  • Code refactor
  • Documentation
  • Other

Checklist

  • I ran pytest locally

  • All tests passed before submitting the PR

  • I couldn't run the tests

  • N/A

  • I included screenshots of any new or modified screens

  • Yes

  • No

  • N/A

  • I added or updated tests

  • Yes

  • No, I’m a fool

  • N/A

  • I tested this PR hands-on on the following platform(s):

  • I have reviewed these notes:

    • Keep your changes limited in scope.
    • If you uncover other issues or improvements along the way, ideally submit those as a separate PR.
    • The more complicated the PR, the harder it is to review, test, and merge.
    • We appreciate your efforts, but we're a small team of volunteers so PR review can be a very slow process.
    • Please only "@" mention a contributor if their input is truly needed to enable further progress.
  • I understand

@kdmukai

kdmukai commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

NACK.

#1073 closes the gap behind this TODO at parse time, before any screen is shown.

Recommend closing this PR and Issue #1076.

Analysis and reply by Claude:


What dev does today, measured on 995ce5c with a single sig p2wpkh change output paying this seed:

  • None: unreachable. All three callers of is_change_branch pass verified_derivation_path from the parser's change_data, which is always the .derivation list of a verified DerivationPath. The TypeError only appears when is_change_branch(None) is called directly.
  • Empty path (m, the master key): the parse refuses the psbt with PSBTUnsupportedDerivationPathError ([security] Check input claims against the input's committed script #1060), so PSBTChangeDetailsView is never reached.
  • One-level path (m/5): the parse succeeds and the output is shown as a "Self-Transfer" at m/5 with address index 5. The funds still belong to this seed. But standard wallets do not scan m/5, so the user's wallet software is unlikely to find them. This is the case the TODO describes.

How #1073 handles it: its change path rules require every change derivation path to hold both a branch and an address index, come back to a base path the inputs spend from, use the receive (0) or change (1) branch, and use a non-hardened address index. A path that fails any of these raises PSBTChangeDerivationPathError during the parse and routes to a Dire Warning ("Unsafe Derivation Path"): change sent to a path the wallet is unlikely to scan is treated as an attack. #1073 also removes the TODO, and test__parse__refuses_change_at_a_derivation_path_too_short_for_a_wallet covers the m/5 case.

Notes on this PR's approach:

  • The check runs in PSBTChangeDetailsView, after the overview screen has already counted the output as a self-transfer.
  • It routes to PSBTUnsupportedDerivationPathView, a Warning-level screen whose text ("This transaction uses a key of yours that this device will not sign with") describes an input claim rather than a change output.
  • The isinstance checks in is_change_branch guard against argument types the callers never produce, and the flow test reaches the new branch only through a MagicMock parser returning data the real parser cannot produce.
  • It conflicts with [security] Check change derivation paths against the inputs #1073 in psbt_views.py and test_flows_psbt.py.

@muskan-creates352

Copy link
Copy Markdown
Author

Thank you so much for the detailed explanation and analysis, @kdmukai!

Handling this validation comprehensively at parse-time in #1073 (raising PSBTChangeDerivationPathError before the transaction overview renders) makes complete architectural sense and is definitely much cleaner.

Closing this PR and Issue #1076 in favor of #1073.

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.

[Bug] PSBTParser.is_change_branch raises TypeError on None derivation path and PSBTChangeDetailsView crashes

2 participants