Merge pull request #1041 from Chaitanya-Keyal/psbt-negative-fee
What changed, and why it matters
This update makes SeedSigner refuse Bitcoin transaction files (PSBTs) where the outgoing amounts are larger than the incoming amounts. Before the fix, the device would have accepted such impossible transactions and shown the user a negative 'fee,' which could confuse someone into signing a transaction that the Bitcoin network would never accept. The change adds a clear error, a user-facing warning, and tests to confirm the behavior.
No action needed; this is a defensive hardening patch. Users and downstream projects should ensure they are running a version that includes this commit or later. Reviewers may want to confirm that no other PSBT parsing paths bypass this check.
Security signals we found
Input validation: rejects PSBTs with mathematically impossible fee values
User-facing safeguard: prevents confusing negative fee from reaching transaction review screens
New exception class with clear semantics (PSBTNegativeFeeError)
UI routing to discard malformed PSBT rather than crash or proceed
Test coverage for both negative-fee rejection and zero-fee acceptance boundaries
Evidence from the diff
The commit adds a PSBTNegativeFeeError exception in psbt_parser.py and raises it when self.psbt.fee() returns a negative value (outputs exceed inputs). It routes that error to a new PSBTNegativeFeeView in psbt_views.py, which displays a non-alarming warning and discards the PSBT to the main menu. Tests are added to verify that negative fees are rejected and zero-fee transactions are still accepted. The code comments explicitly note this is a malformed PSBT, not an attack.
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 +105 / −6
### src/seedsigner/models/psbt_parser.py
@@ -136,6 +136,16 @@ class PSBTSeedCannotSignError(PSBTVerificationError):
pass
+class PSBTNegativeFeeError(PSBTVerificationError):
+ """
+ The outputs spend more than the inputs bring in, so the fee is negative.
+
+ This is a malformed psbt rather than a proven deception, so we do not treat it as an
+ attack.
+ """
+ pass
+
+
class PSBTParser():
"""
Reads a psbt on behalf of one seed and works out everything the signing flow shows the
@@ -288,7 +298,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 +743,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 spend more than the inputs hold,
+ # 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,34 @@ 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 64/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.