Skip to content

[security] Verify PSBT input amounts before displaying the fee - #995

Merged
newtonick merged 25 commits into
SeedSigner:devfrom
newtonick:psbt-verify
Oct 20, 2026
Merged

newtonick merged 25 commits into
SeedSigner:devfrom
newtonick:psbt-verify

Conversation

@newtonick

@newtonick newtonick commented Aug 11, 2026 •

Copy link
Copy Markdown
Collaborator

Description

High-level tldr

SeedSigner displayed the fee without checking the input amounts in the PSBT. Since fee = inputs − outputs, a malicious coordinator could inflate an input amount so the device showed a small fee for a transaction that actually hands the difference to miners. Legacy inputs are the worst case: their signatures don't lock in the amount, so one signing session is enough.

This PR verifies each input's amount against the previous transaction, wherever the PSBT supplies one, before any amount is summed. If an amount can't be proven, the PSBT is discarded instead of signed.

The fix

  • _verify_input_amounts() runs during parse() and is the only place input amounts are summed, as verified_input_amount.
  • A non_witness_utxo is the full previous transaction, so it is hashed and checked against the txid the input claims to spend.
  • Legacy (and unrecognized) inputs must supply one, because their signatures don't commit to the amount.
  • A witness_utxo that disagrees with a proven non_witness_utxo is rejected as well: verify() hashes only the non_witness_utxo, but InputScope.utxo and PSBT.fee() prefer the witness_utxo, so a PSBT could otherwise pass verify() while an unproven value drives the displayed fee.
  • An outpoint index pointing past the end of the non_witness_utxo is rejected before anything dereferences it, so a genuine previous transaction paired with an out-of-range index raises PSBTMissingInputUtxoError, which the view can route on, rather than an IndexError. An input with no utxo data at all raises the same error. Both are treated as a correctness problem rather than an attack.
  • _parse_inputs() no longer sums amounts and only determines the policy. That also removes a latent bug: an input with neither utxo field used to leave script_pubkey unbound, so it either raised UnboundLocalError or silently reused the previous input's value and under-counted the fee.

What's still allowed

Segwit and taproot inputs with only a witness_utxo still pass, because the signature itself commits to the amount. Some coordinators — Sparrow among them — send only one of the two utxo fields, likely to keep QR PSBTs small, so requiring the previous transaction would reject ordinary segwit spends. (Adding the proof to a small 2-input regtest fixture grows it from 710 to 1066 bytes; real multisig previous transactions are far larger.) The accepted residual risk is documented in the _verify_input_amounts() docstring.

That residual risk is real, not theoretical: BIP-143 commits only the amount of the input being signed, so a coordinator that gets the same transaction signed twice can keep one valid signature per session and combine them (CVE-2020-14199). I reproduced this on the fixture wallet: both sessions display the same total, fee, and destination, and the combined signatures produce a transaction paying 2,500x the fee shown. This PR doesn't warn about it, because the data needed to check it usually isn't in the PSBT at all, so a warning would fire on nearly every ordinary multi-input segwit spend and only teach users to click through warnings. Taproot inputs are not exposed to this: BIP-341 commits the amount of every input.

Failure handling

PSBTOverviewView catches two new errors and routes each to a discard-only screen that returns to the main menu:

  • PSBTInputAmountVerificationError goes to PSBTInputAmountVerificationFailedView, a dire warning ("Likely an Attack!").
  • PSBTMissingInputUtxoError goes to PSBTMissingInputUtxoView, a milder "Transaction Problem" warning.

Details

Relationship to #977

#977 found the same issue and proposed recording unverified amounts for a later warning. This PR refuses them outright, adds dedicated exceptions, and adds the failure screens. Treat the two as alternatives; if #977 lands first, this rebases on top as its follow-up.

False positives

The main risk in a change like this is rejecting PSBTs people actually sign, so:

  • Three PSBTs from Sparrow (testnet4) run through the finished parser: p2wpkh 2-input and p2wsh multisig 2-input, both with no non_witness_utxo, parse and are accepted; p2pkh legacy 1-input carries it and verifies.
  • The legacy rule won't fire in normal use either: BIP-174 defines witness_utxo as valid only for inputs spending segwit outputs, so a spec-compliant coordinator has to supply the full previous transaction for a legacy input. Confirmed against a legacy p2pkh PSBT from Sparrow.
  • All seven bundled fixtures still verify cleanly.

Testing

pytest passes at 305, with dev merged in through #722 (so it also includes #1032 and #1027). New tests cover each rejection path, both discard flows, and the simplified input parsing. Tested on hardware (SeedSigner OS on a Pi0/Pi0W board), including the changes from the latest review round.

Tested with my fork of Keith's testqrs.com (kdmukAI-bot/btc-datagen#3): five legacy PSBTs that lie about their input amounts, each caught and discarded by this build. That PR has since merged, so the scenarios are on testqrs.com for anyone to test.

Screenshots

PSBTInputAmountVerificationFailedView PSBTMissingInputUtxoView

No other screens are added or modified.


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

305 passed.


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

  • I understand

@newtonick
newtonick marked this pull request as ready for review August 13, 2026 03:28
Comment thread src/seedsigner/models/psbt_parser.py Resolved
Comment thread src/seedsigner/models/psbt_parser.py Resolved
Comment thread src/seedsigner/models/psbt_parser.py Resolved
Comment thread src/seedsigner/views/psbt_views.py Resolved
@kdmukai

kdmukai commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Note to self: credit for identifying this issue should read something like:

Identified in internal scans; initial fix proposed by 0kPN1 (#977); responsibly disclosed by the Red Team 🟥 and others*.

*(I haven't tried to track the various reports if they're just reiterating previously known issues)

@Chaitanya-Keyal Chaitanya-Keyal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've commented on this resolved thread: #995 (comment), and just one nit here:

Comment thread src/seedsigner/models/psbt_parser.py Resolved
@newtonick
newtonick marked this pull request as draft August 29, 2026 03:51
@newtonick

Copy link
Copy Markdown
Collaborator Author

Moved to draft to wait on some changes Keith is planning to make soon.

3rdIteration added a commit to 3rdIteration/seedsigner that referenced this pull request Sep 18, 2026
…ia descriptor

From a review of open upstream SeedSigner PRs and their discussions:

- Previous-tx proof (upstream SeedSigner#995/SeedSigner#977): a non_witness_utxo must hash to the
  txid its input spends (UTXO_MISMATCH), and a legacy p2sh input may not rely
  on a witness_utxo alone (INVALID_WITNESS_UTXO). Legacy signatures commit to
  no amount, so either lie hides fee in a single signing.
- Multisig change without global xpubs (review thread on upstream SeedSigner#1032): a
  change-shaped m-of-n containing our key is not provably this wallet's. It is
  now a payment unless a loaded descriptor identifies it; the new
  PSBTIdentifyChangeView offers "Load descriptor to identify change", and
  returning with a descriptor re-reads the psbt. verify_multisig_output's
  per-entry check is shared as _descriptor_owns_output.
- OP_RETURN payload read from its push opcode (SeedSigner#965/SeedSigner#1003): direct pushes lost
  their first byte on screen.
- INCONSISTENT_FINGERPRINTS (SeedSigner#1040): a global xpub whose fingerprint disagrees
  with the keys it derives.
- Untrusted-QR crashes (SeedSigner#967/SeedSigner#968/SeedSigner#970/SeedSigner#971): SeedQR checksum and whole-payload
  detection, signmessage bounds, parse_derivation_path on short/48h paths, and
  a UR2 seq_len bound.

New psbt_faker vectors cover each (PREV_TX_TXID_MISMATCH, LEGACY_P2SH_WITNESS_ONLY,
OP_RETURN_DIRECT_PUSH, TX-24.xpub_fingerprint_mismatch, TX-21.ms_foreign_quorum*,
TX-21.ms_honest_change_no_xpubs). TX-20 and XTRAS.MIXED_INPUT_TYPES are
re-vendored after generator fixes; TX-20 is no longer vacuous.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
3rdIteration added a commit to 3rdIteration/seedsigner that referenced this pull request Sep 18, 2026
* test+fix: resync psbt_faker corpus and close the gaps it exposes

Re-vendor the regenerated psbt_faker corpus (97 vectors, fixed BIP-371
taproot encoding) and cover every vector in psbt_suite_util.

Parser fixes for vectors that previously parsed:
- FORGED_OUTPUT_OWNERSHIP also when a genuine key of ours annotates an
  output whose script does not pay it (TX-21.claims_us_pays_other,
  TX-21.ms_repointed_*, TX-22.ms_decoy_*).
- MISLABELED_OUTPUT_OWNERSHIP: an output paying our key but attributed to
  a foreign fingerprint (TX-21.pays_us_claims_other, taproot_claims_other,
  ms_fp_relabeled).
- SURPLUS_DERIVATIONS / MIXED_DERIVATION_MAPS for contradictory claims
  (TX-22.surplus_paths, taproot_surplus_internal_key, mixed_path_maps*).
- SCRIPT_TIMELOCK warning for CLTV/CSV in an output's committed script
  (TX-20.cltv_time).
- verify_multisig_output checks every derivation entry instead of embit's
  first-match Descriptor.owns (decoy-last used to verify as ours).

TX-20.cltv_height/csv_relative are vacuous upstream (scriptPubKey is still
p2wpkh) and marked so.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test: stop flow tests hanging when collected after the refusal-screen tests

tests/test_psbt_refusal_screens.py imported the GUI before tests/base.py
installed its renderer and hardware-button doubles. Any flow test collected
after it then ran against the real, never-configured Renderer (every View and
the error View raise, so the Controller loops forever) or against real buttons
(real-screen flows wait for a key press forever). The full suite only passed
because alphabetical order puts the flow tests first.

The refusal-screen test now imports base first, and base.py also rebinds
seedsigner.gui.Renderer when the package was already imported by some other
module collected earlier.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix: port upstream PSBT/QR hardening; identify multisig change only via descriptor

From a review of open upstream SeedSigner PRs and their discussions:

- Previous-tx proof (upstream SeedSigner#995/SeedSigner#977): a non_witness_utxo must hash to the
  txid its input spends (UTXO_MISMATCH), and a legacy p2sh input may not rely
  on a witness_utxo alone (INVALID_WITNESS_UTXO). Legacy signatures commit to
  no amount, so either lie hides fee in a single signing.
- Multisig change without global xpubs (review thread on upstream SeedSigner#1032): a
  change-shaped m-of-n containing our key is not provably this wallet's. It is
  now a payment unless a loaded descriptor identifies it; the new
  PSBTIdentifyChangeView offers "Load descriptor to identify change", and
  returning with a descriptor re-reads the psbt. verify_multisig_output's
  per-entry check is shared as _descriptor_owns_output.
- OP_RETURN payload read from its push opcode (SeedSigner#965/SeedSigner#1003): direct pushes lost
  their first byte on screen.
- INCONSISTENT_FINGERPRINTS (SeedSigner#1040): a global xpub whose fingerprint disagrees
  with the keys it derives.
- Untrusted-QR crashes (SeedSigner#967/SeedSigner#968/SeedSigner#970/SeedSigner#971): SeedQR checksum and whole-payload
  detection, signmessage bounds, parse_derivation_path on short/48h paths, and
  a UR2 seq_len bound.

New psbt_faker vectors cover each (PREV_TX_TXID_MISMATCH, LEGACY_P2SH_WITNESS_ONLY,
OP_RETURN_DIRECT_PUSH, TX-24.xpub_fingerprint_mismatch, TX-21.ms_foreign_quorum*,
TX-21.ms_honest_change_no_xpubs). TX-20 and XTRAS.MIXED_INPUT_TYPES are
re-vendored after generator fixes; TX-20 is no longer vacuous.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test(screenshots): give the multisig PSBT global xpubs; add PSBTIdentifyChangeView

The screenshot generator's multisig PSBT carries no PSBT_GLOBAL_XPUB, so since
multisig change is only identified through global xpubs or a loaded descriptor,
it had no change output and the PSBTChangeDetailsView_multisig_* screenshots
crashed (get_change_data returned None), failing CI's "Generate screenshots".

- The multisig screenshots now use a copy of that PSBT with global xpubs taken
  from its own wallet descriptor, like a coordinator that includes them.
- The parser is built with the loaded descriptor, and the descriptor is loaded
  before the PSBT, so the "verified" screenshot parses the way the device would.
- New screenshots for PSBTIdentifyChangeView (no descriptor / descriptor loaded),
  from the original xpub-less PSBT.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
PSBTParser summed each input's declared amount without ever checking it,
so the fee shown on the overview and math screens was only a claim from
the coordinator. _verify_input_amounts() now runs in parse() and raises
PSBTInputAmountVerificationError when an input has no utxo data, when its
outpoint index falls outside the non_witness_utxo it claims to spend, when
that non_witness_utxo does not hash to the txid it claims to spend, when a
legacy or unrecognized input supplies no non_witness_utxo, or when a
witness_utxo disagrees with a verified non_witness_utxo. PSBTOverviewView
catches it, clears the parser, and routes to a discard-only
PSBTInputAmountVerificationFailedView (approach and placement from SeedSigner#977).

The check runs after the ownership checks so a psbt this seed cannot sign
is reported as a mismatch rather than as a suspicious transaction, and
before _parse_inputs, where amounts are first summed. Segwit inputs
carrying only a witness_utxo are still accepted, since BIP-143 commits the
amount they sign and many coordinators never sends both utxo fields

Co-Authored-By: 0kPN1 <55192256+0kPN1@users.noreply.github.com>
@newtonick newtonick changed the title [bugfix] Verify PSBT input amounts and discard transactions that fail verification [security] Verify PSBT input amounts before displaying the fee Sep 20, 2026
@newtonick
newtonick marked this pull request as ready for review September 23, 2026 02:21
@newtonick newtonick added this to the 0.8.8 milestone Sep 23, 2026

@Chaitanya-Keyal Chaitanya-Keyal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Both of my earlier points are addressed. I also tested with kdmukAI-bot/btc-datagen#3: the five vectors are refused here and accepted on dev, as expected. One suggestion inline on the warning screen text.

Comment thread src/seedsigner/views/psbt_views.py Resolved
@kdmukai

kdmukai commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

testqrs.com has merged @newtonick's PR to add test scenarios for this PR.

On-device testing yielded the expected results.

Comment thread src/seedsigner/models/psbt_parser.py Resolved
Comment thread src/seedsigner/models/psbt_parser.py Resolved
Comment thread tests/test_psbt_parser.py Resolved
Comment thread tests/test_psbt_parser.py Resolved
Comment thread src/seedsigner/models/psbt_parser.py Resolved
Comment thread src/seedsigner/models/psbt_parser.py Resolved
Comment thread src/seedsigner/models/psbt_parser.py Outdated

# With both non_witness_utxo and witness_utxo present, compare them. verify() only
# proved the non_witness_utxo, so the witness_utxo must match its output at inp.vout.
# Reject the psbt if their amounts or scripts contradict.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trivial nit: if you make edits around here anyway, maybe consider moving this part of the comment to go just above the raise call.

# The amounts or scripts contradict. Reject the psbt.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Moved above the raise in commit 4dcdcfa.

Comment thread src/seedsigner/models/psbt_parser.py Resolved
newtonick and others added 10 commits October 19, 2026 22:09
Co-authored-by: kdmukai <934746+kdmukai@users.noreply.github.com>
Co-authored-by: kdmukai <934746+kdmukai@users.noreply.github.com>
Co-authored-by: kdmukai <934746+kdmukai@users.noreply.github.com>
Co-authored-by: kdmukai <934746+kdmukai@users.noreply.github.com>
Co-authored-by: kdmukai <934746+kdmukai@users.noreply.github.com>
@newtonick

Copy link
Copy Markdown
Collaborator Author

I've completed revisions based on review feedback.

@jdlcdl

jdlcdl commented Nov 5, 2026 •

Copy link
Copy Markdown

Regarding proposed embit pr152

I have tested seedsigner as of Nov 1 (last merge pr1047), using pr152 atop embit 0.8.2, instead of embit 0.8.0.

All tests for this PR at testqrs dot com behave as expected without any seedsigner code changes,
EXCEPT for the 6th test case: "output index does not exist (legacy)" due to embit raising PSBTError during parsing and psbt_views.py-line35 learning "No PSBT currently loaded". ??? fix is to catch PSBTError in qr_decoder, separately than base64/43/58 decoding errors ???

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

Labels

None yet

Projects

Status: 0.8.8 Merged

Development

Successfully merging this pull request may close these issues.

4 participants