Beatland Festival

AI First Flight #4
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Severity: medium
Valid

Off-by-one in redeemMemorabilia: a collection with maxSupply N can only mint N-1 items (maxSupply 1 is unredeemable)

Root + Impact

Description

createMemorabiliaCollection initialises currentItemId to 1, but redeemMemorabilia gates minting on currentItemId < maxSupply (strict less-than):

// createMemorabiliaCollection
collections[collectionId] = MemorabiliaCollection({
...
@> currentItemId: 1, // starts at 1
maxSupply: maxSupply,
...
});
// redeemMemorabilia
@> require(collection.currentItemId < collection.maxSupply, "Collection sold out");
uint256 itemId = collection.currentItemId++;

Because currentItemId starts at 1 and the check is strict <, a collection can only mint items while currentItemId is 1..maxSupply-1 - i.e. maxSupply - 1 items total, one fewer than configured. In the worst case, a collection created with maxSupply == 1 can never mint anything: 1 < 1 is false, so the single intended item is permanently unredeemable.

Risk

Likelihood: High - affects every memorabilia collection; the last edition is always unmintable, and maxSupply == 1 collections are fully broken.

Impact: Low - reduced availability (one fewer NFT than advertised) and bricked single-item collections; no direct fund loss, but it breaks the documented supply and wastes the organizer's configuration.

Proof of Concept

A collection with maxSupply == 1 cannot be redeemed at all. Runnable Foundry test (add to FestivalPass.t.sol):

function test_PoC_memorabiliaOffByOne() public {
// organizer creates a 1-supply collection priced in BEAT
vm.prank(organizer);
uint256 cid = festivalPass.createMemorabiliaCollection("art", "ipfs://x", 1e18, 1, true);
// give user1 BEAT via a backstage pass welcome bonus (15 BEAT)
vm.prank(user1);
festivalPass.buyPass{value: BACKSTAGE_PRICE}(3);
// maxSupply is 1 but currentItemId starts at 1 and the check is `currentItemId < maxSupply`,
// so `1 < 1` is false -> the only item can never be minted
vm.prank(user1);
vm.expectRevert("Collection sold out");
festivalPass.redeemMemorabilia(cid);
}

Run forge test --mt test_PoC_memorabiliaOffByOne -vv; it passes - the 1-supply collection reverts as "sold out" before minting a single item.

Recommended Mitigation

Use an inclusive bound so exactly maxSupply items can be minted. Either start currentItemId at 1 and check <=, or count minted items from 0:

- require(collection.currentItemId < collection.maxSupply, "Collection sold out");
+ require(collection.currentItemId <= collection.maxSupply, "Collection sold out");

With currentItemId starting at 1, the <= bound allows item IDs 1..maxSupply inclusive, yielding exactly maxSupply items and fixing the maxSupply == 1 case. (Equivalently, keep < but initialise currentItemId to 0 and emit itemId = ++currentItemId.)

Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 4 hours ago
Submission Judgement Published
Validated
Assigned finding tags:

[M-03] Off-by-One in `redeemMemorabilia` Prevents Last NFT From Being Redeemed

# Off-by-One in `redeemMemorabilia` Prevents Last NFT From Being Redeemed ## Description * The `createMemorabiliaCollection` function allows an organizer to create an NFT collection that can be exchanged for the BEAT token via the `redeemMemorabilia` function by users. * The `redeemMemorabilia` function checks if `collection.currentItemId` is less than `collection.maxSupply`. However, the `currentItemId` starts with 1 in the `createMemorabiliaCollection` function. This prevents the final item (where `currentItemId` equals `maxSupply`) from being redeemed. ```Solidity function createMemorabiliaCollection( string memory name, string memory baseUri, uint256 priceInBeat, uint256 maxSupply, bool activateNow ) external onlyOrganizer returns (uint256) { require(priceInBeat > 0, "Price must be greater than 0"); require(maxSupply > 0, "Supply must be at least 1"); require(bytes(name).length > 0, "Name required"); require(bytes(baseUri).length > 0, "URI required"); uint256 collectionId = nextCollectionId++; collections[collectionId] = MemorabiliaCollection({ name: name, baseUri: baseUri, priceInBeat: priceInBeat, maxSupply: maxSupply, @> currentItemId: 1, // Start item IDs at 1 isActive: activateNow }); emit CollectionCreated(collectionId, name, maxSupply); return collectionId; } function redeemMemorabilia(uint256 collectionId) external { MemorabiliaCollection storage collection = collections[collectionId]; require(collection.priceInBeat > 0, "Collection does not exist"); require(collection.isActive, "Collection not active"); @> require(collection.currentItemId < collection.maxSupply, "Collection sold out"); // Burn BEAT tokens BeatToken(beatToken).burnFrom(msg.sender, collection.priceInBeat); // Generate unique token ID uint256 itemId = collection.currentItemId++; uint256 tokenId = encodeTokenId(collectionId, itemId); // Store edition number tokenIdToEdition[tokenId] = itemId; // Mint the unique NFT _mint(msg.sender, tokenId, 1, ""); emit MemorabiliaRedeemed(msg.sender, tokenId, collectionId, itemId); } ``` ## Risk **Likelihood**: * A legitimate user calls `redeemMemorabilia` attempting to redeem the last NFT in a collection. **Impact**: * The user fails to get the NFT, even though the redemption counter has not reached the maximum supply of the collection. ## Proof of Concept The following test shows a user trying to redeem the 10th NFT in one collection. Running `forge test --mt test_Audit_RedeemMaxSupply -vv` shows the output that the 10th redemption is reverted due to the sold out. ```Solidity function test_Audit_RedeemMaxSupply() public { vm.prank(organizer); uint256 maxSupply = 10; // Cap for memorabilia NFT collection uint256 collectionId = festivalPass.createMemorabiliaCollection( "Future Release", "ipfs://QmFuture", 10e18, maxSupply, true ); vm.startPrank(address(festivalPass)); beatToken.mint(user1, 10000e18); // Give enough BEAT for user vm.stopPrank(); vm.startPrank(user1); for (uint256 i = 0; i < maxSupply - 1; i++) { festivalPass.redeemMemorabilia(collectionId); console.log("Redeem sucess:", i + 1); // Redeem success from 1 to 9 } // 10th redeem call reverts with "Collection Sold out" vm.expectRevert("Collection sold out"); festivalPass.redeemMemorabilia(collectionId); console.log("Redeem reverted:", maxSupply); vm.stopPrank(); } ``` ## Recommended Mitigation Modify the supply check in `redeemMemorabilia` to use `<=` (less than or equal to) instead of `<`, ensuring that the final item can be redeemed. This approach is preferable to modifying the `createMemorabiliaCollection` function (which is clearly documented to start `currentItemId` at 1). ```diff // Redeem a memorabilia NFT from a collection function redeemMemorabilia(uint256 collectionId) external { MemorabiliaCollection storage collection = collections[collectionId]; require(collection.priceInBeat > 0, "Collection does not exist"); require(collection.isActive, "Collection not active"); - require(collection.currentItemId < collection.maxSupply, "Collection sold out"); + require(collection.currentItemId <= collection.maxSupply, "Collection sold out"); // allow equals // Burn BEAT tokens BeatToken(beatToken).burnFrom(msg.sender, collection.priceInBeat); // Generate unique token ID uint256 itemId = collection.currentItemId++; uint256 tokenId = encodeTokenId(collectionId, itemId); // Store edition number tokenIdToEdition[tokenId] = itemId; // Mint the unique NFT _mint(msg.sender, tokenId, 1, ""); emit MemorabiliaRedeemed(msg.sender, tokenId, collectionId, itemId); } ```

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!