Mystery Box

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

Potential Reentrancy Vulnerability in `MysteryBox` Contract Due to Missing CEI Pattern

Description
The MysteryBox contract is designed to allow users to purchase and open mystery boxes that contain random rewards, creating an exciting experience for users. However, the claimAllRewards() function in the MysteryBox.sol contract uses the .call method to transfer funds, which can expose the contract to reentrancy attacks. A malicious user could exploit this by recursively calling the claimAllRewards() function before the state is properly updated, potentially draining funds from the contract.

Other similar functions that could be affected by reentrancy vulnerabilities are:

  1. transferReward

  2. claimSingleReward

  3. openBox

Impact
Failure to implement the Checks-Effects-Interactions (CEI) pattern in the claimAllRewards() function makes the contract susceptible to reentrancy attacks. This vulnerability could allow an attacker to repeatedly claim rewards without limit, leading to the depletion of the contract’s balance and resulting in significant financial losses.

Recommendation
To mitigate the risk of reentrancy, apply the CEI pattern by updating the contract state before making external calls (such as sending ETH). Specifically, move the delete rewardsOwned[msg.sender]; line before the .call function to ensure that the state is updated before any external interactions.

# File: src/MysteryBox.sol
function claimAllRewards() public {
---SNIP---
+ delete rewardsOwned[msg.sender]; // Update state before external call
(bool success,) = payable(msg.sender).call{value: totalValue}("");
require(success, "Transfer failed");
- delete rewardsOwned[msg.sender]; // Previous position allowed reentrancy risk
}
Updates

Lead Judging Commences

inallhonesty Lead Judge
about 1 year ago

Appeal created

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

`claimAllRewards` reentrancy

Support

FAQs

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