# Attacker can reenter to mint all the collection supply

NextGenCore::mint increments per-address mint counters after _safeMint, so a malicious receiver reenters via onERC721Received to mint the entire collection supply.

- Payout: Contest reward
- Severity: high
- Program: NextGen
- Credited: btk
- Disclosed: 2024-01-08
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2023-10-nextgen-findings/issues/1517
- Library page: https://bugbountylibrary.com/r/c4%3A2023-10-nextgen%3AH-01
- Tags: domain:web3 impact:funds-loss impact:integrity kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:chained technique:source-review vuln:reentrancy

**Target:** NextGenCore/MinterContract NFT minting contracts
**Root cause:** NextGenCore::mint updates tokensMintedPerAddress/tokensMintedAllowlistAddress after the external _safeMint call, violating checks-effects-interactions, so the maxCollectionPurchases limit is not yet incremented when the receiver hook reenters.
**Steps:**
1. Deploy attacker contract implementing onERC721Received that calls MinterContract::mint again for the same collection.
2. Call `MinterContract::mint{value:1e18}(_collectionID:1,_numberOfTokens:1,_mintTo:attackerContract,...)` with a valid merkleProof.
3. NextGenCore::mint uses _safeMint, invoking attacker's onERC721Received before incrementing tokensMintedPerAddress.
4. Hook checks circulationSupply vs totalSupply and recursively calls mint until supply exhausted.
5. Run `forge test --mt testMintAllTheCollection -vvv`; logs show circulation supply 0 -> 50 with 49e18 spent.
**How found:** Code reading of NextGenCore::mint revealed state updates (tokensMintedPerAddress) placed after the _safeMint external call, indicating a CEI violation exploitable via the ERC721 receiver hook.
**Exploitation:** Attacker funds a contract with enough ETH for per-token price, mints one token to it; the ERC721 receive hook recursively re-mints until circulationSupply equals totalSupply, capturing the whole collection supply while bypassing per-address purchase caps.
**Insight:** When auditing NFT mint functions, check whether per-address/per-phase counters are incremented before or after _safeMint; the ERC721 receiver callback is a ready-made reentrancy vector even without ETH transfers.
