The MysteryBox.sol
contract contains a critical vulnerability in the MysteryBox::transferReward
function. This vulnerability creates gaps in the reward array when transferring rewards, potentially leading to inconsistencies in reward management and unexpected behavior in other functions that rely on the integrity of the rewards array and potential denial-of-service attack by filling the array with empty slots.
Affected code - https://github.com/Cyfrin/2024-09-mystery-box/blob/281a3e35761a171ba134e574473565a1afb56b68/src/MysteryBox.sol#L73-L77
The vulnerability stems from the MysteryBox::transferReward
function's improper handling of array elements:
The delete
operation resets the array element to its default value (empty string for name
and 0 for value
) but does not remove the element from the array or reduce its length. This creates a gap in the array, which can lead to inconsistencies and potential issues in other functions that interact with the rewards array.
The provided PoC demonstrates this vulnerability:
This PoC demonstrates that after transferring a reward:
The length of user1's reward array remains unchanged.
The "deleted" reward is reset to default values (empty string for name and 0 for value) instead of being removed from the array.
The console output shows:
The vulnerability in the MysteryBox::transferReward
function can lead to several issues:
Inconsistent Reward Management: Functions that iterate over or access the rewards array may encounter unexpected behavior due to these "empty" slots.
Potential for Exploitation: Malicious users could potentially exploit this behavior to manipulate the reward system or cause denial-of-service by filling the array with empty slots.
Increased Gas Costs: As the array length doesn't decrease, users may incur unnecessary gas costs when interacting with functions that process the entire rewards array.
Data Integrity Issues: The presence of these "ghost" rewards could lead to incorrect calculations or representations of a user's reward inventory.
User Experience Degradation: Users may see inconsistent or confusing information about their rewards, leading to a poor user experience and potential loss of trust in the platform.
This issue can significantly impact the contract's functionality and reliability over time, especially as users accumulate and transfer more rewards.
Manual Review and Foundry Test
To address this vulnerability, consider implementing the following changes:
Use array manipulation techniques to properly remove the transferred reward:
Alternatively, consider using a different data structure, such as a mapping with a separate array for indices, to manage rewards more efficiently:
Implement additional checks to ensure the integrity of the rewards array:
Add events to log reward transfers for better transparency and easier tracking:
By implementing these recommendations, the MysteryBox::transferReward
function will properly manage the rewards array, preventing gaps and ensuring consistent behavior across the contract. This will significantly improve the reliability and efficiency of the reward system, enhancing both security and user experience.
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.