Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: high
Valid

(L-1) Using on-chain data pieces as only source of randomness leads to exploitable and weak randomness

Summary

selectWinner function uses randomness made out of only on-chain data pieces. Using on-chain data pieces as only source of randomness leads to exploitable and weak randomness

Details

selectWinner function uses randomness made out of only on-chain data pieces.

uint256 winnerIndex =
uint256(keccak256(abi.encodePacked(msg.sender, block.timestamp, block.difficulty))) % players.length;

This is not a good source of randomness because on-chain data can be modified by multiple parties (e.g. the user, the miners, etc.)

Since this code is visible publicly, anyone (that possesses enough knowledge and power) can modify certain aspects of the transaction to get the outcome they want.

Filename

src/PuppyRaffle.sol

Permalinks

https://github.com/Cyfrin/2023-10-Puppy-Raffle/blob/07399f4d02520a2abf6f462c024842e495ca82e4/src/PuppyRaffle.sol#L128-L129

Impact

Blocks and transactions can be modified in such a way as to guarantee outcomes wanted.

Recommendations

It is recommended to not make randomness dependent on on-chain data pieces such as block timestamp or difficulty. Consider generating random numbers by using something like Chainlink VRF instead.

Tools Used

  • Manual Audit

  • Foundry

Updates

Lead Judging Commences

Hamiltonite Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

weak-randomness

Root cause: bad RNG Impact: manipulate winner

Support

FAQs

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