Target:
MysteryBox Smart Contract
Severity:
High
Vulnerability Type:
Array Out-of-Bounds Access
Contract Version:
Solidity 0.8.0
The claimSingleReward() function in the MysteryBox contract is vulnerable to an array out-of-bounds access due to improper validation of the _index parameter. This vulnerability could lead to unintended behavior, including reverts and potential denial-of-service attacks if exploited, and affects users attempting to claim rewards.
the vulnerability exists in the following code from the claimSingleReward() function:
The comparison <= in this require() statement is incorrect because arrays in Solidity are 0-indexed. The valid indices for the array rewardsOwned[msg.sender] range from 0 to rewardsOwned[msg.sender].length - 1. By using <=, the function incorrectly allows access to an index that is out of bounds (i.e., when _index == rewardsOwned[msg.sender].length), which results in a runtime error and reverts the transaction with the following error:
The following exploit script demonstrates how a reentrancy attack combined with out-of-bounds access can lead to vulnerabilities in the claimSingleReward() function
After preparing the script, run the following command to reproduce the issue and observe the error:
When the test reaches the claimSingleReward() function, you will encounter the following error:
This error indicates the array is being accessed beyond its valid bounds, confirming the vulnerability.
Any attempt to claim a reward at an out-of-bounds index will cause the transaction to fail, potentially leading to user frustration and loss of gas fees.
A malicious user can repeatedly invoke this function with an out-of-bounds index, causing constant reverts and a potential denial of service for users attempting to interact with the contract.
Replace the <= comparison with < to prevent 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.