Mystery Box

First Flight #25
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: high
Valid

Reentrancy in MysteryBox::claimSingleReward allows to withdraw all funds from the contract

Summary

Function MysteryBox::claimSingleRewarddoes not follow CEI pattern and this allows to re-enter to the function and claim rewards again before the rewards array for the user is deleted.

Vulnerability Details

Function MysteryBox::claimSingleReward deletes a reward from the rewardsOwned array after sending the reward to the user:

function claimSingleReward(uint256 _index) public {
require(_index <= rewardsOwned[msg.sender].length, "Invalid index");
uint256 value = rewardsOwned[msg.sender][_index].value;
require(value > 0, "No reward to claim");
(bool success,) = payable(msg.sender).call{value: value}("");
require(success, "Transfer failed");
delete rewardsOwned[msg.sender][_index];
}

This allows to re-enter to the function and claim the rewards again.

Impact

An attacker can withdraw all funds from the contract.

Tools Used

Manual review

Recommendations

Change the function to follow CEI pattern as below:

function claimSingleReward(uint256 _index) public {
require(_index <= rewardsOwned[msg.sender].length, "Invalid index");
uint256 value = rewardsOwned[msg.sender][_index].value;
require(value > 0, "No reward to claim");
+ delete rewardsOwned[msg.sender][_index];
(bool success,) = payable(msg.sender).call{value: value}("");
require(success, "Transfer failed");
- delete rewardsOwned[msg.sender][_index];
}
Updates

Appeal created

inallhonesty Lead Judge about 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

`claimSingleReward` reentrancy

Support

FAQs

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

Give us feedback!