The contract's random pricing mechanism in the trickOrTreat
function is susceptible to frontrunning attacks. Since the randomness relies on predictable block variables, attackers can manipulate transaction ordering or timing to increase their chances of receiving favorable pricing, such as consistently obtaining half-price treats or avoiding double-price tricks.
Randomness is generated using the following code:
Issues:
Predictable Variables: block.timestamp
and block.prevrandao
are semi-predictable and can be influenced by miners or validators.
Transaction Ordering: Attackers can monitor the mempool and adjust gas prices to influence when their transactions are mined.
Manipulation by Validators: Validators can alter block.timestamp
within a permissible range and may control block.prevrandao
, affecting the outcome of the randomness.
Economic Exploit: Attackers can game the system to consistently receive treats at half price or avoid paying double price, leading to financial losses for the contract owner.
Unfair Advantage: Honest users are disadvantaged as they cannot manipulate transaction timing or block variables.
Reduced Trust: Perception of unfairness may lead to loss of user confidence in the platform.
Monitoring Mempool:
An attacker observes pending transactions and predicts upcoming block variables.
Adjusting Transaction Timing:
By setting a higher gas price, the attacker ensures their transaction is included in the next block.
Influencing Block Variables:
If the attacker is a validator or colludes with one, they can manipulate block.timestamp
and block.prevrandao
to produce a random number that results in favorable pricing.
Consistent Exploitation:
Repeating this process allows the attacker to consistently benefit from the most advantageous pricing.
Implement Secure Randomness:
Use a reliable source of randomness like Chainlink VRF (Verifiable Random Function) which provides verifiable and tamper-proof randomness.
Commit-Reveal Scheme:
Introduce a two-step process where users commit to a purchase with a hash of a secret value and later reveal the secret. This reduces the chance of manipulation.
Randomness Delayed Usage:
Use randomness from future blocks, making it harder for attackers to predict or influence the outcome.
Limit Transaction Ordering Influence:
Encourage users to include a standard gas price, reducing the ability of attackers to front-run by paying higher gas fees.
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.
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.