The claimSingleReward function in the MysteryBox contract has a vulnerability related to the _index parameter, which can be set to the length of the rewardsOwned array. This oversight may lead to off-by-one errors, potentially allowing users to access invalid memory locations and cause runtime errors.
The claimSingleReward function allows the _index parameter to be equal to the length of the rewardsOwned array, which would result in an out-of-bounds access. Specifically, the condition in the require statement currently uses <=, permitting the possibility of accessing an index that does not exist in the array.
If a user provides a value of _index equal to rewardsOwned[msg.sender].length, the function would attempt to access an invalid index, leading to runtime errors. This implementation flaw could cause the function to revert unexpectedly during execution, negatively affecting user experience.
This vulnerability allows an _index value to equal rewardsOwned[msg.sender].length, which can lead to unexpected reverts due to out-of-bounds access.
Write the following code to TestMysteryBox.t.sol:
Output:
Manual review
Foundry
To mitigate the vulnerability, modify the comparison operator in the require statement from <= to <. This change ensures that the _index parameter cannot equal the length of the rewardsOwned array, thereby preventing out-of-bounds access.
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.