Merge PR 'Verify 64-byte transaction inclusion via coinbase-anchored Merkle depth' (#5559)
What changed, and why it matters
This change removes a safety check that previously threw away Bitcoin transactions whose stripped-down form is exactly 64 bytes. That check was meant to guard against a rare Merkle-tree trick, but it also accidentally discarded legitimate Lightning HTLC claim transactions of the same size, which could stop a node from learning a payment secret (preimage) needed to settle a forwarded payment. The patch now verifies those 64-byte transactions normally using the block's Merkle root, so real transactions are accepted while fake ones are still rejected.
Review the Merkle-proof validation paths in Electrum and Esplora clients to confirm they reject invalid 64-byte proofs, and ensure no downstream code still silently drops 64-byte transactions. Monitor for any follow-up patches that harden proof verification specifically for coinbase-anchored Merkle depths.
Security signals we found
Removal of a heuristic transaction-size filter (64-byte base size)
Reliance on Merkle-root verification against block header for inclusion proof
Pending changelog labels the change under '# Security' and describes a preimage-loss / HTLC settlement failure scenario
References the known Bitcoin Merkle-tree leaf-node weakness (bitslog 2018)
Evidence from the diff
The commit deletes is_potentially_unsafe_merkle_leaf and all call sites in lightning-transaction-sync’s Electrum and Esplora sync clients. Previously, transactions with base_size() == 64 were skipped before confirmation verification because a 64-byte serialized transaction can collide with an internal Merkle node hash (the leaf-node weakness in Bitcoin’s Merkle tree). The new behavior relies on standard Merkle proof verification against the block header’s committed Merkle root, which is sufficient to distinguish a genuine leaf from a crafted internal node. A pending changelog entry explicitly frames this as a security fix: legitimate 64-byte HTLC claims were being dropped, preventing preimage extraction for upstream HTLC settlement.
Changed components
lightning-transaction-sync/src/common.rslightning-transaction-sync/src/electrum.rslightning-transaction-sync/src/esplora.rsInspect captured patch +11 / −42
### lightning-transaction-sync/src/common.rs
@@ -152,10 +152,6 @@ impl FilterQueue {
}
}
-pub(crate) fn is_potentially_unsafe_merkle_leaf(tx: &Transaction) -> bool {
- tx.base_size() == 64
-}
-
#[derive(Debug)]
pub(crate) struct ConfirmedTx {
pub tx: Transaction,
### lightning-transaction-sync/src/electrum.rs
@@ -5,7 +5,7 @@
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.
-use crate::common::{is_potentially_unsafe_merkle_leaf, ConfirmedTx, FilterQueue, SyncState};
+use crate::common::{ConfirmedTx, FilterQueue, SyncState};
use crate::error::{InternalError, TxSyncError};
use electrum_client::utils::validate_merkle_proof;
@@ -282,13 +282,6 @@ impl<L: Logger> ElectrumSyncClient<L> {
return Err(InternalError::Failed);
}
- // Skip before potentially using an arbitrary returned output to look up the
- // transaction's script history.
- if is_potentially_unsafe_merkle_leaf(&tx) {
- log_error!(self.logger, "Skipping transaction {} due to retrieving potentially invalid tx data.", txid);
- continue;
- }
-
watched_txs.push((txid, tx.clone()));
// We watch an output's script_pubkey of the transaction of interest in order
// to retrieve the associated script history, before narrowing down our search
@@ -478,19 +471,6 @@ impl<L: Logger> ElectrumSyncClient<L> {
&self, tx: &Transaction, prob_conf_height: u32,
) -> Result<Option<ConfirmedTx>, InternalError> {
let txid = tx.compute_txid();
- // Bitcoin Core's Merkle tree implementation has no way to discern between internal and
- // leaf node entries. As a consequence it is susceptible to an attacker injecting
- // additional transactions by crafting 64-byte transactions matching an inner Merkle
- // node's hash (see https://web.archive.org/web/20240329003521/https://bitslog.com/2018/06/09/leaf-node-weakness-in-bitcoin-merkle-tree-design/).
- if is_potentially_unsafe_merkle_leaf(tx) {
- log_error!(
- self.logger,
- "Skipping transaction {} due to retrieving potentially invalid tx data.",
- txid
- );
- return Ok(None);
- }
-
match self.client.transaction_get_merkle(&txid, prob_conf_height as usize) {
Ok(merkle_res) => {
debug_assert_eq!(prob_conf_height, merkle_res.block_height as u32);
### lightning-transaction-sync/src/esplora.rs
@@ -5,7 +5,7 @@
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.
-use crate::common::{is_potentially_unsafe_merkle_leaf, ConfirmedTx, FilterQueue, SyncState};
+use crate::common::{ConfirmedTx, FilterQueue, SyncState};
use crate::error::{InternalError, TxSyncError};
use lightning::chain::WatchedOutput;
@@ -449,22 +449,6 @@ impl<L: Logger> EsploraSyncClient<L> {
return Err(InternalError::Failed);
}
- // Bitcoin Core's Merkle tree implementation has no way to discern between
- // internal and leaf node entries. As a consequence it is susceptible to an
- // attacker injecting additional transactions by crafting 64-byte
- // transactions matching an inner Merkle node's hash (see
- // https://web.archive.org/web/20240329003521/https://bitslog.com/2018/06/09/leaf-node-weakness-in-bitcoin-merkle-tree-design/).
- // To protect against this (highly unlikely) attack vector, we check that the
- // transaction is at least 65 bytes in length.
- if is_potentially_unsafe_merkle_leaf(&tx) {
- log_error!(
- self.logger,
- "Skipping transaction {} due to retrieving potentially invalid tx data.",
- txid
- );
- return Ok(None);
- }
-
if let Some(block_height) = known_block_height {
// We can take a shortcut here if a previous call already gave us the height.
return Ok(Some(ConfirmedTx { tx, txid, block_header, pos, block_height }));
### pending_changelog/verify-64-byte-merkle-leaves.txt
@@ -0,0 +1,9 @@
+# Security
+
+ * `lightning-transaction-sync`'s Electrum and Esplora clients no longer
+ discard transactions whose witness-stripped serialization is exactly 64
+ bytes when syncing confirmed transactions. Genuine mined HTLC claims can
+ have exactly this size, and dropping them prevented us from learning the
+ preimage needed to settle a forwarded HTLC upstream. Their inclusion is
+ verified like that of any other transaction, against the Merkle root
+ committed to by the block header.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.