Trick or Treat

First Flight #27
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: low
Invalid

Function SpookySwap::trickOrTreat uses unsafe randomness

Summary

It is possible to predict when the the treat will be double-priced or half-priced due to bad randomness used in the SpookySwap::trickOrTreat function.

Vulnerability Details

There is no way to generate a random value on-chain in EVM. The function SpookySwap::trickOrTreat uses the expression below to generate a "random" value:

uint256 random =
uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender, nextTokenId, block.prevrandao))) % 1000 + 1

There are some problems with this code:

  1. Validators can manipulate block.timestamp value in some degree, so a validator can affect the random value generation

  2. Users can calculate the same value in their transaction and based on the calculated value decide to call SpookySwap::trickOrTreat or not

Impact

Price of the treat can be manipulated by validators, or users can just revert the transaction if the price does not satisfy them.

Tools Used

Manual review

Recommendations

Do not rely on any on-chain value to generate a random values. Consider to use Chainlink VRF for that purpose.

Updates

Appeal created

bube Lead Judge 8 months ago
Submission Judgement Published
Invalidated
Reason: Known issue
Assigned finding tags:

[invalid] Weak randomness

It's written in the README: "We're aware of the pseudorandom nature of the current implementation. This will be replaced with Chainlink VRF in later builds." This is a known issue.

Support

FAQs

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