The claimAllRewards function in the MysteryBox contract is vulnerable to a reentrancy attack. This vulnerability allows an attacker to repeatedly call the function and drain the contract's funds before the state is updated.
The claimAllRewards function transfers funds to the caller using a low-level call within the same function before updating the contract's state. This allows an attacker to reenter the function and repeatedly claim rewards without the state being updated.
The reentrancy occurs because the contract transfers the rewards via a low-level call before updating the state (i.e., deleting the rewards of the caller). As a result, an attacker can invoke the fallback function and reenter claimAllRewards before the state is updated.
An attacker can drain all the funds from the contract by reentering the claimAllRewards function multiple times. This leads to a total loss of all funds in the contract.
Manual code review
Forge test suite for simulating reentrancy
Implement the Checks-Effects-Interactions (CEI) pattern by updating the contract's state before transferring any funds:
Use OpenZeppelin’s ReentrancyGuard and apply the nonReentrant modifier to the claimAllRewards function to prevent reentrant calls:
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.