The claimSingleReward function in the MysteryBox contract is vulnerable to a reentrancy attack. This vulnerability allows an attacker to claim rewards multiple times before the state is updated, draining the contract's funds.
The claimSingleReward function transfers funds to the caller using a low-level call before updating the contract's state. Specifically, it transfers the reward before deleting it from the rewardsOwned array, allowing an attacker to reenter the function and repeatedly claim the same reward.
The reentrancy occurs because the contract transfers the reward via a low-level call before the state (i.e., deletion of the reward from the array) is updated. This enables an attacker to invoke the fallback function and reenter claimSingleReward before the state is updated, allowing multiple claims of the same reward.
An attacker can drain the contract’s funds by repeatedly calling claimSingleReward through reentrancy, claiming rewards multiple times before the state is updated. This can lead to the 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 claimSingleReward 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.