From b01e9052fd1327470719c94a136a64eeaedeb555 Mon Sep 17 00:00:00 2001 From: Hardeezah Date: Fri, 16 Oct 2026 22:57:25 +0100 Subject: [PATCH] Reject proposals with outputs exceeding inputs Add AbsoluteFeeDecreased error and enforce that the post-adjustment proposal fee is at least the original fee in fee range calculation. Add regression tests and update changelog. --- payjoin/CHANGELOG.md | 8 +++ payjoin/src/core/receive/common/mod.rs | 67 ++++++++++++++++++++++++++ payjoin/src/core/receive/error.rs | 8 +++ payjoin/src/core/receive/v2/mod.rs | 38 +++++++++++++++ 4 files changed, 121 insertions(+) diff --git a/payjoin/CHANGELOG.md b/payjoin/CHANGELOG.md index a11dc6dd2..a4ebe9821 100644 --- a/payjoin/CHANGELOG.md +++ b/payjoin/CHANGELOG.md @@ -12,6 +12,14 @@ fails: `cancel` yields a `Receiver` whose `fallback_tx` is the transaction to broadcast. +### Bug Fixes + +- Reject proposals whose outputs exceed their inputs. A receiver that raises + the total output value through `replace_receiver_outputs` without + contributing inputs covering the increase could otherwise produce a + proposal with a lower absolute fee than the original transaction, which + the sender would reject (#1939) + ## 1.2.0 This release adds a non-blocking interface to the receiver state machine. diff --git a/payjoin/src/core/receive/common/mod.rs b/payjoin/src/core/receive/common/mod.rs index 095deb888..7a1a78cc9 100644 --- a/payjoin/src/core/receive/common/mod.rs +++ b/payjoin/src/core/receive/common/mod.rs @@ -545,6 +545,20 @@ impl WantsFeeRange { ) })?; } + let original_fee = self + .original + .original_psbt + .fee() + .map_err(|_| InternalPayloadError::FeeCalculationOverflow)?; + let proposal_fee = match payjoin_psbt.fee() { + Ok(fee) => fee, + Err(bitcoin::psbt::Error::NegativeFee) => + return Err(InternalPayloadError::AbsoluteFeeDecreased), + Err(_) => return Err(InternalPayloadError::FeeCalculationOverflow), + }; + if proposal_fee < original_fee { + return Err(InternalPayloadError::AbsoluteFeeDecreased); + } Ok(payjoin_psbt) } @@ -846,6 +860,59 @@ mod tests { assert_eq!(wants_inputs.proposal.receiver_inputs[1], input_pair_2); } + #[test] + fn receiver_rejects_proposal_with_outputs_exceeding_inputs() { + let original = original_from_test_vector(); + let original_output = original.psbt.unsigned_tx.output[1].clone(); + let wants_outputs = WantsOutputs::new(original, vec![1]); + let replacement_output = TxOut { + value: original_output.value + Amount::ONE_SAT, + script_pubkey: original_output.script_pubkey.clone(), + }; + + let wants_fee_range = wants_outputs + .replace_receiver_outputs( + [replacement_output], + original_output.script_pubkey.as_script(), + ) + .expect("Output replacement should succeed") + .commit_outputs() + .commit_inputs(); + + let result = wants_fee_range.calculate_psbt_with_fee_range(None, None); + assert!( + matches!(result, Err(InternalPayloadError::AbsoluteFeeDecreased)), + "A proposal that increases output value without receiver inputs must be rejected" + ); + } + + #[test] + fn receiver_accepts_output_increase_covered_by_receiver_input() { + let original = original_from_test_vector(); + let original_fee = original.psbt.fee().expect("Original PSBT should have a valid fee"); + let original_output = original.psbt.unsigned_tx.output[1].clone(); + let increase = original_fee + Amount::ONE_SAT; + + let wants_outputs = WantsOutputs::new(original, vec![1]); + let replacement_output = TxOut { + value: original_output.value + increase, + script_pubkey: original_output.script_pubkey.clone(), + }; + let wants_inputs = wants_outputs + .replace_receiver_outputs( + [replacement_output], + original_output.script_pubkey.as_script(), + ) + .expect("Output replacement should succeed") + .commit_outputs() + .contribute_inputs([candidate_input_from_test_vector(increase)]) + .expect("An input covering the output increase should be accepted") + .commit_inputs(); + + let result = wants_inputs.calculate_psbt_with_fee_range(None, None); + assert!(result.is_ok(), "An input covering the output increase must be accepted"); + } + #[test] fn sender_specifies_excessive_fee_rate() { let original = original_from_test_vector(); diff --git a/payjoin/src/core/receive/error.rs b/payjoin/src/core/receive/error.rs index f6ec0b3b5..1a4417bf7 100644 --- a/payjoin/src/core/receive/error.rs +++ b/payjoin/src/core/receive/error.rs @@ -218,6 +218,7 @@ pub(crate) enum InternalPayloadError { FeeTooHigh(bitcoin::FeeRate, bitcoin::FeeRate), /// Fee calculation overflowed in arithmetic and was incomputeable FeeCalculationOverflow, + AbsoluteFeeDecreased, } impl From<&PayloadError> for JsonReply { @@ -238,6 +239,8 @@ impl From<&PayloadError> for JsonReply { FeeTooHigh(_, _) => JsonReply::new(NotEnoughMoney, e), + AbsoluteFeeDecreased => JsonReply::new(Unavailable, "Receiver error"), + SenderParams(e) => match e { super::optional_parameters::Error::UnknownVersion { supported_versions } => { let supported_versions_json = @@ -281,6 +284,10 @@ impl fmt::Display for InternalPayloadError { "Effective receiver feerate exceeds maximum allowed feerate: {proposed_fee_rate} > {max_fee_rate}" ), FeeCalculationOverflow => write!(f, "Fee arithmetic overflow."), + AbsoluteFeeDecreased => write!( + f, + "The proposal's absolute fee cannot decrease below the original fee." + ), } } } @@ -297,6 +304,7 @@ impl std::error::Error for PayloadError { PsbtBelowFeeRate(_, _) => None, FeeTooHigh(_, _) => None, FeeCalculationOverflow => None, + AbsoluteFeeDecreased => None, MissingPayment => None, OriginalPsbtNotBroadcastable => None, InputOwned(_) => None, diff --git a/payjoin/src/core/receive/v2/mod.rs b/payjoin/src/core/receive/v2/mod.rs index 66c1fee0d..ff44308ed 100644 --- a/payjoin/src/core/receive/v2/mod.rs +++ b/payjoin/src/core/receive/v2/mod.rs @@ -2717,6 +2717,44 @@ pub mod test { Ok(()) } + #[test] + fn receiver_rejects_proposal_with_outputs_exceeding_inputs() { + let persister = InMemoryPersister::default(); + let original = + OriginalPayload { psbt: PARSED_ORIGINAL_PSBT.clone(), params: Params::default() }; + let original_output = original.psbt.unsigned_tx.output[0].clone(); + let wants_outputs = + receiver(WantsOutputs { inner: common::WantsOutputs::new(original, vec![0]) }); + let replacement_output = bitcoin::TxOut { + value: original_output.value + Amount::ONE_SAT, + script_pubkey: original_output.script_pubkey.clone(), + }; + + let wants_inputs = wants_outputs + .replace_receiver_outputs( + [replacement_output], + original_output.script_pubkey.as_script(), + ) + .expect("Output replacement should succeed") + .commit_outputs() + .save(&persister) + .expect("Outputs should be committed"); + let wants_fee_range = + wants_inputs.commit_inputs().save(&persister).expect("Inputs should be committed"); + let err = wants_fee_range + .apply_fee_range(None, None) + .save(&persister) + .expect_err("A proposal whose outputs exceed its inputs must be rejected"); + + assert!(err.is_transient()); + assert!(!err.is_fatal()); + assert!(matches!( + err.api_error_ref(), + Some(ProtocolError::OriginalPayload(e)) + if matches!(e.0, InternalPayloadError::AbsoluteFeeDecreased) + )); + } + #[test] fn default_max_fee_rate() { let persister = InMemoryPersister::default();