Duplicate NFTs Can Be Minted if payableToken Has a Callback Attached to it
The mintToken function pays the payableToken before minting and updating totalSupply, allowing a reentrant call to bypass the maxSupply check and mint duplicate NFTs when the last token is minted.
Submitted by leastwood
Impact
The mintToken() function is called to mint unique tokens from an ERC721 collection. This function will either require users to provide a merkle proof to claim an airdropped token or pay a fee in the form of a payableToken. However, because the payableToken is paid before a token is minted, it may be possible to reenter the mintToken() function if there is a callback attached before or after the token transfer. Because totalSupply() has not been updated for the new token, a user is able to bypass the totalSupply() + amount <= maxSupply check. As a result, if the user mints the last token, they can reenter and mint duplicate NFTs as the way tokenId is generated will wrap around to the start again.
Proof of Concept
For the sake of this example, let’s say startingIndex = 0 and maxSupply = 100. tokenId is minted according to ((startingIndex + totalSupply()) % maxSupply) + 1. If we see that a user mints a token where totalSupply() = maxSupply - 1 = 99 and they reenter the function, then the next token to mint will actually be of index 1 as totalSupply() % maxSupply = 0. Calculating the first tokenId, we get ((0 + 0) % maxSupply) + 1 = 1 which is a duplicate of our example.
Recommended Mitigation Steps
Consider adding reentrancy protections to prevent users from abusing this behaviour. It may also be useful to follow the checks-effects pattern such that all external/state changing calls are made at the end.
sofianeOuafir (Joyn) confirmed and commented:
This is an issue we intend to investigate and fix if indeed it is an issue
deluca-mike (judge) commented:
This is a valid high risk issue. Also, for reference, the checks-effects-interactions (CEI) pattern suggests you, in this order:
- perform checks that something can be done
- perform the effects (update storage and emit events)
- interact with other functions/contracts (since you may not be sure they will call out and re-enter)
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the contract performs an external call to the payableToken before updating totalSupply, allowing reentrancy that bypasses the maxSupply check and causes tokenId generation to wrap around.
Impact
A user could mint duplicate NFTs, potentially exceeding the intended maximum supply and duplicating token IDs.
What the attacker needed
PayableToken has a callback; user mints the last token
The fix
The contract should be fixed by adding reentrancy protections and following the checks-effects-interactions pattern, updating state before external calls.
How it was found
- Set startingIndex to 0 and maxSupply to 100.
- Mint a token when totalSupply() equals maxSupply - 1 (99).
- Reenter the mintToken function before totalSupply is updated.
- Observe that the next tokenId wraps around to 1, duplicating an existing token.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you mint an NFT, the contract first takes your payment and then creates the token. If the payment token can call back into the minting contract before the token count is updated, an attacker can mint again and get a duplicate token. This happens because the contract checks the supply limit before it updates the count.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.