The PuppyRaffle::selectWinner winnerIndex is predictable in advance to know exactly which index will win. An player can know at each moment who the winner of the lottery is. Plus any miner that has control over block.timestamp and/or block.difficulty can manipulate it to his benefit.
The rarity calculation is also impacted but has less impact.
The comments suggest that the winner is randomly chosen from the array of players. But that is not true because :
The value of uint256(keccak256(abi.encodePacked(msg.sender, block.timestamp, block.difficulty))) % players.length can be known in advance by anyone. Consequently, a player can add multiple wallet addresses of their own to the players array. Then he will calculate the winning index for each addition to see if any of their addresses would be chosen as the winner. Finally, he will execute the PuppyRaffle::selectWinner function when they confirm one of their addresses would be chosen.
The value uint256(keccak256(abi.encodePacked(msg.sender, block.timestamp, block.difficulty))) % players.length can be manipulated by miners in favor of a particular winner. Since they have control over block parameters like block.timestamp and block.difficulty, a miner can iterate through different block hashes until the outcome matches a desired winner.
The rarity calculation can also be manipulated:
The lottery can be rigged to favor a particular winner. The core functionality is thus compromised. Users will not use the protocol because of lack of trust.
There are many ways to rig this protocol. Here is one of them, the Miner can just change block.difficulty to favor a certain player right before selecting a winner. To execute this test : forge test --mt testMinerChangesBlockDiffiultyToFavorPlayerOne -vv
foundry
Use a real fair randomizer to chose the winner. For example, the Chainlink VRF is a well proven fair and audited randomizer. Miners will not be able to manipulate it.
Add an onlyOwner modifier to the function PuppyRaffle::selectWinner. Since Users will not know the exact time of the winner selection. They will be discouraged from adding many of their own address as players right before the execution of the function, to increase their chances.
Root cause: bad RNG Impact: manipulate winner
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.