Reject psbts whose outputs exceed their inputs
What changed, and why it matters
This commit fixes a bug in SeedSigner where a malformed Bitcoin transaction file (PSBT) whose outputs spend more than its inputs could be loaded and shown to the user with a nonsensical negative fee. The fix rejects such PSBTs during parsing and shows the user a plain warning instead. It is treated as bad data, not an attack, because no honest tool creates these and the Bitcoin network would not relay them anyway.
No immediate user action required beyond applying the update. Developers should ensure any downstream PSBT parsing similarly validates fee sign before presenting transaction summaries.
Security signals we found
Input validation gap: signed arithmetic result used without sign check
UI deception risk: negative fee displayed to user during transaction review
Denial-of-service-by-confusion: malformed PSBT could reach review flow
Defensive fix: explicit exception and user-facing warning added
No signing occurs for malformed PSBT
Evidence from the diff
embit’s PSBT.fee() returns inputs minus outputs without checking sign. When outputs exceed inputs, the fee becomes negative and was previously carried into the review UI. The patch adds PSBTNegativeFeeError, raises it immediately after fee computation in PSBTParser._parse_outputs(), and routes it to a new PSBTNegativeFeeView that warns the user and discards the PSBT. Zero-fee PSBTs remain accepted. Tests cover both the negative-fee rejection and the zero-fee boundary.
Changed components
src/seedsigner/models/psbt_parser.pysrc/seedsigner/views/psbt_views.pytests/test_psbt_parser.pytests/test_flows_psbt.pytests/screenshot_generator/generator.pyInspect captured patch +108 / −6
### src/seedsigner/models/psbt_parser.py
@@ -136,6 +136,21 @@ class PSBTSeedCannotSignError(PSBTVerificationError):
pass
+class PSBTNegativeFeeError(PSBTVerificationError):
+ """
+ The outputs spend more than the inputs bring in, so the fee is negative.
+
+ Treated as malformed data rather than an attack. No honest coordinator builds one,
+ and a coordinator lying about its input amounts wants the fee it shows us to look
+ small, not impossible. Note that the amounts on both sides of that subtraction are
+ the coordinator's claims, so a negative result only establishes that the arithmetic
+ cannot be trusted, not which side of it is false. Either way nothing is signed here.
+ It is raised because the alternative is walking the user through a review that quotes
+ a negative fee.
+ """
+ pass
+
+
class PSBTParser():
"""
Reads a psbt on behalf of one seed and works out everything the signing flow shows the
@@ -288,7 +303,8 @@ def parse(self):
- multisig: Match each of the seed's verified keys against the pubkeys in
the script the output commits to.
Every change_data entry after this point will carry a derivation path that
- our seed provably owns.
+ our seed provably owns. Raises PSBTNegativeFeeError if the outputs spend more
+ than the inputs bring in.
Optimization via child_key_derivation_cache:
Parsing traverses a derivation path down to an individual address one level at a
@@ -732,7 +748,14 @@ def _parse_outputs(self, child_key_derivation_cache: dict):
self.destination_amounts.append(vout[i].value)
self.spend_amount += vout[i].value
+ # embit computes the fee as inputs minus outputs and does not look at the sign of
+ # the result. A negative fee means the outputs claim more than the inputs fund,
+ # which no valid transaction can do. Zero is left alone: it is a pointless
+ # transaction, but not a malformed one.
self.fee_amount = self.psbt.fee()
+ if self.fee_amount < 0:
+ raise PSBTNegativeFeeError()
+
return True
### src/seedsigner/views/psbt_views.py
@@ -4,7 +4,7 @@
from seedsigner.models.psbt_parser import (PSBTInputAmountVerificationError,
PSBTInputOwnershipClaimError, PSBTMissingInputUtxoError,
- PSBTMixedDerivationPathTypesError, PSBTOutputOwnershipClaimError,
+ PSBTMixedDerivationPathTypesError, PSBTNegativeFeeError, PSBTOutputOwnershipClaimError,
PSBTOutputOwnershipContradictionError, PSBTParser, PSBTSeedCannotSignError,
PSBTSurplusDerivationPathsError)
from seedsigner.models.settings import SettingsConstants
@@ -132,6 +132,10 @@ def __init__(self):
self.set_redirect(Destination(PSBTOutputOwnershipContradictionView, clear_history=True))
return
+ except PSBTNegativeFeeError:
+ self.set_redirect(Destination(PSBTNegativeFeeView, clear_history=True))
+ return
+
except PSBTSeedCannotSignError:
# Not a suspicious psbt, just the wrong seed for it. Send the user back to
# pick another rather than clearing the flow.
@@ -668,6 +672,32 @@ def run(self):
+class PSBTNegativeFeeView(View):
+ """
+ Reached when a psbt's outputs spend more than its inputs bring in (see
+ PSBTNegativeFeeError). Malformed rather than hostile, so it shows a plain (not dire)
+ warning and discards the psbt to the main menu.
+ """
+ DISCARD = ButtonOption("Discard transaction")
+
+ def run(self):
+ self.run_screen(
+ WarningScreen,
+ title=_("Transaction Problem"),
+ status_headline=None,
+ # TRANSLATOR_NOTE: The transaction's outputs spend more than its inputs hold, which is impossible.
+ text=_("This transaction's outputs send more than its inputs provide."),
+ button_data=[self.DISCARD],
+ show_back_button=False,
+ )
+
+ # We're done with this PSBT. Route back to MainMenuView, which clears all ephemeral
+ # data (except in-memory seeds).
+ # Set clear_history to disable returning via BACK button.
+ return Destination(MainMenuView, clear_history=True)
+
+
+
class PSBTAddressVerificationFailedView(View):
"""
Reached from PSBTChangeDetailsView when a multisig change or self-transfer output
### tests/screenshot_generator/generator.py
@@ -457,6 +457,7 @@ def mock_version_to_most_recent_release():
ScreenshotConfig(psbt_views.PSBTAddressVerificationFailedView, dict(is_change=False), screenshot_name="PSBTAddressVerificationFailedView_multisig_selftransfer"),
ScreenshotConfig(psbt_views.PSBTOutputOwnershipClaimFailedView),
ScreenshotConfig(psbt_views.PSBTInputOwnershipClaimFailedView),
+ ScreenshotConfig(psbt_views.PSBTNegativeFeeView),
ScreenshotConfig(psbt_views.PSBTSeedCannotSignView),
ScreenshotConfig(psbt_views.PSBTInputAmountVerificationFailedView),
ScreenshotConfig(psbt_views.PSBTMissingInputUtxoView),
### tests/test_flows_psbt.py
@@ -208,9 +208,10 @@ def load_seed_into_decoder(view: scan_views.ScanView):
class TestPSBTOwnershipClaimRouting(FlowTest):
"""
- A psbt whose own description of itself does not hold up is refused during parsing,
- before the user is shown anything about the transaction. These cover what the user
- meets when that happens: a warning screen that ends the flow, not a crash.
+ A psbt whose own description of itself does not hold up, or whose arithmetic is
+ impossible, is refused during parsing, before the user is shown anything about the
+ transaction. These cover what the user meets when that happens: a warning screen that
+ ends the flow, not a crash.
"""
def _load_psbt_for_signing(self, psbt: PSBT, seed: Seed = None):
@@ -396,3 +397,21 @@ def test_wrong_seed_routes_back_to_seed_selection_flow(self):
# The psbt itself is kept: the user is choosing a different seed for it, not
# starting over
assert self.controller.psbt is not None
+
+
+ def test_negative_fee_terminates_signing_flow(self):
+ """
+ A psbt whose outputs spend more than its inputs bring in is malformed rather than
+ hostile, but it is still rejected during the parse and routes to a warning that
+ aborts the signing flow.
+ """
+ psbt = self._psbt_with_change()
+ psbt.outputs[0].value = sum(inp.utxo.value for inp in psbt.inputs) + 1
+ self._load_psbt_for_signing(psbt)
+
+ self.run_sequence([
+ FlowStep(psbt_views.PSBTSelectSeedView, screen_return_value=0),
+ FlowStep(psbt_views.PSBTOverviewView, is_redirect=True),
+ FlowStep(psbt_views.PSBTNegativeFeeView, button_data_selection=psbt_views.PSBTNegativeFeeView.DISCARD),
+ FlowStep(MainMenuView),
+ ])
### tests/test_psbt_parser.py
@@ -14,7 +14,7 @@
from seedsigner.models.psbt_parser import (PSBTInputAmountVerificationError,
PSBTInputOwnershipClaimError, PSBTMissingInputUtxoError,
- PSBTMixedDerivationPathTypesError, PSBTOutputOwnershipClaimError,
+ PSBTMixedDerivationPathTypesError, PSBTNegativeFeeError, PSBTOutputOwnershipClaimError,
PSBTOutputOwnershipContradictionError, PSBTParser, PSBTSeedCannotSignError,
PSBTSurplusDerivationPathsError)
from seedsigner.models.seed import Seed
@@ -2673,3 +2673,32 @@ def p2pk(public_key: PublicKey) -> script.Script:
with pytest.raises(RuntimeError, match="Unsupported policy type"):
self._parse(psbt)
+ def test__parse__rejects_outputs_that_exceed_inputs(self):
+ """
+ Outputs that spend more than the inputs bring in produce a negative fee. `parse`
+ should raise PSBTNegativeFeeError rather than carry that impossible figure into
+ the review screens.
+
+ Note that `psbt.tx` is rebuilt from the output scopes on every access, so the
+ amount has to be changed on the scope itself.
+ """
+ psbt = self._psbt_with_change()
+ input_total = sum(inp.utxo.value for inp in psbt.inputs)
+
+ # One satoshi more than the inputs fund, i.e. the smallest negative fee there is
+ psbt.outputs[0].value = input_total + 1
+
+ with pytest.raises(PSBTNegativeFeeError):
+ self._parse(psbt)
+
+
+ def test__parse__accepts_a_zero_fee(self):
+ """
+ A transaction that pays no fee is pointless but not malformed (and can still be
+ relayed as part of a package), so only a strictly negative fee is rejected. This
+ pins the boundary the check is written against.
+ """
+ psbt = self._psbt_with_change()
+ psbt.outputs[0].value = sum(inp.utxo.value for inp in psbt.inputs)
+
+ assert self._parse(psbt).fee_amount == 0Why this scored 50/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.