The trickOrTreat function is vulnerable to a reentrancy attack that allows an attacker to mint an unlimited number of NFTs without paying the required cost per mint. By exploiting reentrancy, an attacker can repeatedly call trickOrTreat within a single transaction, minting multiple NFTs from a single function call. This exploit disrupts token scarcity and severely impacts protocol revenue.
The trickOrTreat function’s reentrancy vulnerability allows attackers to mint multiple instances of the same treat (NFT) repeatedly within a single transaction. By bypassing the intended non-reentrant behavior, attackers can create numerous identical NFTs with the same treat name, cost, and metadata URI. This degrades the NFT’s uniqueness and scarcity, which are essential characteristics of any valuable NFT collection.
Here is what my attack contract looks like:
We set up a test using a malicious contract, MaliciousReentrant, to demonstrate how an attacker can exploit this vulnerability. Here’s the test result, showing that the attacker owns multiple identical NFTs after initiating reentrant calls:
Attack test:
Logs:
The reentrancy vulnerability in the trickOrTreat function allows attackers to mint multiple identical NFTs repeatedly, bypassing the protocol's intended uniqueness and scarcity of each NFT. The key impacts include:
NFT Uniqueness and Scarcity Compromise: Attackers can mint multiple identical NFTs, undermining the uniqueness and collectible nature of each treat. This devalues the NFT collection and can damage the reputation and trust of legitimate users who expect exclusive ownership.
Protocol Integrity Risks: The unchecked reentrancy flaw allows attackers to call trickOrTreat recursively within a single transaction. Although each mint incurs a cost, the ability to bypass scarcity controls poses a substantial risk to the protocol's intended operation and economics.
Foundry Testing Framework: This framework allowed the creation of a test simulating the reentrancy attack, helping confirm the multiple minting exploit. As well as for console logging.
To prevent reentrancy-based duplication of treats, add a restriction to the mintTreat function that enforces a unique treat name. Implement a mintedTreats mapping to track each treat’s name and verify if it has already been minted. This approach ensures that duplicate treats cannot be minted in a single transaction or due to reentrancy.
This solution provides robust protection against reentrancy issues by enforcing treat uniqueness through treat names, maintaining protocol integrity and scarcity.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.