Puppy Raffle

AI First Flight #1
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Severity: high
Valid

Refunded players break `selectWinner` prize pool calculation

When a player calls refund, their slot in the players array is set to address(0) (L122), but the array is not shrunk. The selectWinner function computes the total amount collected using the full array length (L158):

uint256 totalAmountCollected = players.length * entranceFee;

This overstates the actual contract balance because refunded players already withdrew their entrance fees. The computed prizePool (80% of the inflated total) exceeds the available ETH, causing winner.call{value: prizePool} at L180 to revert.

This creates two cascading issues:

  1. Bricked raffle: Any refund makes selectWinner revert, preventing the round from completing. New players entering via enterRaffle further inflate players.length, worsening the imbalance.

  2. address(0) winner: If winnerIndex lands on a refunded slot, winner = address(0). The ETH transfer to address(0) succeeds, but _safeMint(address(0), tokenId) reverts (ERC721 prohibits minting to the zero address), reverting the entire transaction.

Additionally, if two or more players refund, the duplicate check in enterRaffle (L96-102) finds address(0) == address(0) and reverts, blocking all new entries for the rest of the round.

Exploit Scenario

  1. Four players enter the raffle (4 ETH total).

  2. One player calls refund, receiving 1 ETH back. Their slot becomes address(0).

  3. selectWinner computes totalAmountCollected = 4 * 1 ETH = 4 ETH, but the contract only holds 3 ETH.

  4. prizePool = 4 ETH * 80% = 3.2 ETH. The contract has only 3 ETH. The transfer reverts.

  5. The raffle round is stuck. If a second player refunds, enterRaffle is also blocked due to duplicate address(0) entries.

Recommendations

Short term: Track the number of active (non-refunded) players separately. Use the active count for the prize pool calculation:

uint256 totalAmountCollected = activePlayerCount * entranceFee;

Long term: Replace the array-zeroing refund mechanism with a data structure that properly removes entries (e.g., swap-and-pop), or maintain a separate deposit ledger that reconciles refunds.

Updates

Lead Judging Commences

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

[H-04] `PuppyRaffle::refund` replaces an index with address(0) which can cause the function `PuppyRaffle::selectWinner` to always revert

## Description `PuppyRaffle::refund` is supposed to refund a player and remove him from the current players. But instead, it replaces his index value with address(0) which is considered a valid value by solidity. This can cause a lot issues because the players array length is unchanged and address(0) is now considered a player. ## Vulnerability Details ```javascript players[playerIndex] = address(0); @> uint256 totalAmountCollected = players.length * entranceFee; (bool success,) = winner.call{value: prizePool}(""); require(success, "PuppyRaffle: Failed to send prize pool to winner"); _safeMint(winner, tokenId); ``` If a player refunds his position, the function `PuppyRaffle::selectWinner` will always revert. Because more than likely the following call will not work because the `prizePool` is based on a amount calculated by considering that that no player has refunded his position and exit the lottery. And it will try to send more tokens that what the contract has : ```javascript uint256 totalAmountCollected = players.length * entranceFee; uint256 prizePool = (totalAmountCollected * 80) / 100; (bool success,) = winner.call{value: prizePool}(""); require(success, "PuppyRaffle: Failed to send prize pool to winner"); ``` However, even if this calls passes for some reason (maby there are more native tokens that what the players have sent or because of the 80% ...). The call will thankfully still fail because of the following line is minting to the zero address is not allowed. ```javascript _safeMint(winner, tokenId); ``` ## Impact The lottery is stoped, any call to the function `PuppyRaffle::selectWinner`will revert. There is no actual loss of funds for users as they can always refund and get their tokens back. However, the protocol is shut down and will lose all it's customers. A core functionality is exposed. Impact is high ### Proof of concept To execute this test : forge test --mt testWinnerSelectionRevertsAfterExit -vvvv ```javascript function testWinnerSelectionRevertsAfterExit() public playersEntered { vm.warp(block.timestamp + duration + 1); vm.roll(block.number + 1); // There are four winners. Winner is last slot vm.prank(playerFour); puppyRaffle.refund(3); // reverts because out of Funds vm.expectRevert(); puppyRaffle.selectWinner(); vm.deal(address(puppyRaffle), 10 ether); vm.expectRevert("ERC721: mint to the zero address"); puppyRaffle.selectWinner(); } ``` ## Recommendations Delete the player index that has refunded. ```diff - players[playerIndex] = address(0); + players[playerIndex] = players[players.length - 1]; + players.pop() ```

Support

FAQs

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

Give us feedback!