Merge PR 'Verify UTXO output matches previous transaction in coin selection' (#5490)
What changed, and why it matters
This change tightens a safety check when a Lightning wallet picks which Bitcoin coins (UTXOs) to spend. Previously, the code only confirmed that the referenced transaction had an output at the expected position. Now it also confirms that the actual output amount and script exactly match what the wallet claims. This prevents a class of bugs or attacks where a wallet source provides a UTXO whose details do not match the real previous transaction, which could lead to building invalid or unexpected funding/closing transactions.
Treat as a security-hardening fix. Users and downstream wallets should upgrade to ensure their coin-selection path validates UTXO outputs against the actual previous transaction. Review any custom wallet-source implementations to ensure they return consistent prevtx and UTXO data.
Security signals we found
Validation strengthened from presence check to exact output equality
Coin-selection path is security-critical for funding and closing transactions
Error message change indicates intent to detect mismatched UTXO data
Small, targeted patch suggests a specific bug or edge case was being closed
Evidence from the diff
In lightning/src/util/wallet_utils.rs, the coin-selection validation was changed from checking prevtx.output.get(utxo.outpoint.vout as usize).is_none() to prevtx.output.get(utxo.outpoint.vout as usize) != Some(&utxo.output). The error message was updated accordingly. This means the wallet source’s claimed UTXO output must now exactly equal the output in the fetched previous transaction. A mismatch could cause the wallet to sign or broadcast transactions with incorrect scripts or values, potentially leading to invalid transactions, failed channel operations, or loss of funds in edge cases.
Changed components
lightning/src/util/wallet_utils.rsCoin selection / UTXO handlingWallet source interfaceInspect captured patch +2 / −2
### lightning/src/util/wallet_utils.rs
@@ -681,11 +681,11 @@ where
let prevtx = self.source.get_prevtx(utxo.outpoint).await?;
let prevtx_id = prevtx.compute_txid();
if prevtx_id != utxo.outpoint.txid
- || prevtx.output.get(utxo.outpoint.vout as usize).is_none()
+ || prevtx.output.get(utxo.outpoint.vout as usize) != Some(&utxo.output)
{
log_error!(
self.logger,
- "Tx {} from wallet source doesn't contain output referenced by outpoint: {}",
+ "Tx {} from wallet source doesn't match output referenced by outpoint: {}",
prevtx_id,
utxo.outpoint,
);Why this scored 59/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.