Puppy Raffle

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

`PuppyRaffle::refund` replaces an index with address(0) which can cause `selectWinner` to always revert (funds locked)

Description

The intended behavior is that selectWinner picks an active player and sends them the prize plus an NFT.

The specific issue is that refund sets a refunded player's slot to address(0) but keeps it in the players array (length never reduced). selectWinner picks winnerIndex with % players.length, so it can land on an address(0) slot; minting to address(0) then reverts, so the whole draw reverts and the raffle cannot conclude.

function refund(uint256 playerIndex) public { ... players[playerIndex] = address(0); } // @> counted address(0) entry
function selectWinner() external {
uint256 winnerIndex = uint256(keccak256(...)) % players.length;
address winner = players[winnerIndex]; // @> can be address(0)
(bool success,) = winner.call{value: prizePool}("");
require(success, "...");
_safeMint(winner, tokenId); // @> _safeMint(address(0)) reverts
}

Whenever the modulo resolves to a refunded (zeroed) index, _safeMint(address(0), tokenId) reverts, reverting selectWinner; the raffle is stuck for that draw and funds stay locked.

Risk

Likelihood: Medium

  • Refunds are normal and leave address(0) holes; the more refunds, the more likely the RNG lands on one.

Impact: High

  • selectWinner reverts and the raffle is blocked / funds locked whenever the winner resolves to a refunded slot.

Proof of Concept

// Players enter; some refund -> address(0) slots remain. winnerIndex lands on one:
// _safeMint(address(0), tokenId) reverts -> selectWinner reverts -> raffle cannot conclude.

Recommended Mitigation

Remove refunded players (swap-and-pop) or forbid a zero-address winner, so selectWinner always resolves to a real participant.

Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge 7 days 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!