Skip to content

Commit 943d762

Browse files
committed
fix(electrum): validate coinbase merkle path
This fix validates the coinbase merkle path when checking the merkle proof of a transaction we are inserting within the same block. This check mitigates a known brute force exploit that could insert an invalid transaction. f
1 parent 961a9da commit 943d762

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

crates/electrum/src/bdk_electrum_client.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,29 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
398398
);
399399
}
400400

401+
// Validate that the length of the coinbase merkle path matches the length of the
402+
// transaction's merkle path, and verify the coinbase transaction's merkle proof. This
403+
// prevents a known brute-force exploit from inserting invalid transactions.
404+
if let Ok(txid_from_pos_res) = self
405+
.inner
406+
.txid_from_pos_with_merkle(merkle_res.block_height, 0)
407+
{
408+
// Construct the GetMerkleRes required for validating the merkle proof of the
409+
// coinbase transaction.
410+
let coinbase_merkle_res = &electrum_client::GetMerkleRes {
411+
block_height: merkle_res.block_height,
412+
pos: 0,
413+
merkle: txid_from_pos_res.merkle.clone(),
414+
};
415+
416+
is_confirmed_tx = txid_from_pos_res.merkle.len() == merkle_res.merkle.len()
417+
&& electrum_client::utils::validate_merkle_proof(
418+
&txid_from_pos_res.tx_hash,
419+
&header.merkle_root,
420+
coinbase_merkle_res,
421+
);
422+
}
423+
401424
if is_confirmed_tx {
402425
tx_update.anchors.insert((
403426
ConfirmationBlockTime {

0 commit comments

Comments
 (0)