# Misuse of Merkle Leaf Nodes

**Overview of the Vulnerability**

At its core, this class of vulnerability arises when the system does not strictly enforce the difference between **leaf nodes** (the original data) and **intermediate nodes** (hashes of combined child nodes). Merkle trees rely on proof mechanisms, where a user provides a sequence of hashes that prove a particular item (a leaf node) is part of the tree.

However, if the contract or system does not differentiate between a **leaf hash** (a hash of the original data) and an **intermediate hash** (a hash of two child nodes), attackers can submit intermediate nodes in place of leaf nodes. This allows them to pass off invalid or incorrect data as if it were part of the set.

**Visualizing the Vulnerability:**

Imagine a Merkle tree structured as follows:

```scss
Root (hash of hash(Leaf A) and hash(Leaf B))
         /      \
    hash(Leaf A)  hash(Leaf B)
```

In this structure, **Leaf A** and **Leaf B** represent valid data, while the root node and intermediate nodes are hashes that represent the combination of child nodes.

In a secure system, only **Leaf A** and **Leaf B** should be valid data points. However, if the system incorrectly treats an intermediate node, such as `hash(Leaf A)`, as valid, an attacker can exploit this by submitting the intermediate node as if it were a valid piece of data.

**Why This Vulnerability Happens**

1. **Lack of Validation Between Leaf and Intermediate Nodes**: The main cause of this vulnerability is a failure to distinguish between valid leaf nodes and intermediate nodes during the proof verification process. A Merkle proof is supposed to prove the inclusion of a specific leaf node, but if intermediate nodes are not explicitly excluded, attackers can trick the system into accepting them as valid data.
2. **Misuse of Proofs**: The system relies on users to provide Merkle proofs (a sequence of intermediate hashes) to validate that a specific leaf node is part of the tree. If an attacker provides an intermediate hash instead of a leaf node, and the system doesn’t properly validate the proof, the system could be tricked into accepting the intermediate hash as if it were valid data.

***

#### Example: Token ID Validation via Merkle Tree

Let’s take an example where a protocol uses a Merkle tree to validate token IDs that a user can submit in a trade or transaction. In this case:

1. **The Setup**: The protocol allows users to specify several token IDs they are willing to accept. It builds a Merkle tree out of these token IDs, and the root hash is stored as the identifier for the item or trade criteria. When the transaction occurs, the user must provide a valid token ID and a proof that this token ID is part of the Merkle tree.
2. **The Attack**: An attacker submits an intermediate hash from the Merkle tree instead of a valid token ID. For instance, if the Merkle tree was generated from token IDs 1 and 2, the attacker might submit `hash(tokenID 1)` instead of `tokenID 1`. The system incorrectly validates the intermediate hash as a valid token, allowing the attacker to fulfill the transaction with an invalid token.

**Visual Breakdown of the Attack:**

Consider a Merkle tree built from two token IDs, 1 and 2:

```scss
Root (hash of hash(1) and hash(2))
         /      \
    hash(1)    hash(2)
```

The protocol should accept only token ID 1 or 2. However, if the system does not differentiate between leaf nodes and intermediate nodes, an attacker can submit `hash(1)` as a valid token. This allows the attacker to bypass the intended validation and fulfill the trade without submitting a valid token ID.

**Consequences:**

* **Invalid Trade Execution**: The attacker can fulfill a trade with an invalid token, bypassing the intended criteria.
* **Loss of Funds**: The other party in the transaction may receive a token that does not meet the specified criteria, resulting in a financial loss.
* **Protocol Vulnerability**: This weakness can be exploited repeatedly, undermining the trust and integrity of the entire protocol.

***

#### Why This Vulnerability Is a Concern for ERC-721 Tokens

In ERC-721 tokens, token IDs are not required to follow a specific sequence or pattern. This flexibility in token ID assignment increases the risk of this Merkle tree vulnerability because the protocol cannot rely on predictable token ID patterns to prevent attacks. Additionally, some contracts allow users to choose arbitrary token IDs during minting, which can further exacerbate this issue.

If a protocol uses a Merkle tree to validate token IDs, the system must carefully distinguish between valid token IDs (leaf nodes) and intermediate hashes. Failure to do so can allow attackers to fulfill trades with invalid tokens, leading to significant losses for users.

***

#### Mitigation Strategies

**1. Hash Leaves Before Inserting into the Merkle Tree**

A recommended approach is to hash the token IDs themselves before placing them in the Merkle tree. This ensures that intermediate hashes are distinct from leaf node hashes, preventing attackers from submitting intermediate nodes as valid token IDs.

* **Example**: Instead of inserting token ID 1 directly into the Merkle tree, hash it first (`hash(tokenID 1)`), and then insert the hash. This makes the leaf node hash distinct from intermediate nodes and avoids confusion.

**2. Strict Leaf Node Validation**

Implement rigorous validation checks to ensure that the submitted data corresponds to a valid leaf node in the Merkle tree. This can be done by hashing the submitted data (like a token ID) and comparing it against the Merkle proof to ensure it is a leaf node.

**3. Use of Type-Bytes to Differentiate Node Types**

Another approach is to use type-bytes to differentiate between leaf nodes and intermediate nodes when constructing the Merkle tree. By including a type byte in the hashing process, you ensure that leaf nodes and intermediate nodes are not confused with each other.

***

#### Conclusion

Merkle trees are an essential cryptographic tool for efficiently proving data inclusion in blockchain systems. However, improper validation of Merkle proofs can expose protocols to significant vulnerabilities, particularly when there is no distinction between leaf nodes (representing valid data) and intermediate nodes (representing combinations of hashes).

In systems that rely on Merkle trees for validating token IDs or other data, developers must enforce strict validation rules to prevent attackers from exploiting intermediate hashes. By adopting practices like hashing leaves before insertion and using type-bytes to differentiate node types, these vulnerabilities can be mitigated, ensuring the integrity and security of decentralized protocols.\
