Summary
The functionality to restrict the use of setPassword
is not implemented.
Vulnerability Details
Any one can change the password. If the password is used somehow on chain, it could lead to a denial of service. If the password is used off chain it can be recovered since the information is public.
POC
pragma solidity 0.8.18;
import {Test, console} from "forge-std/Test.sol";
import {PasswordStore} from "../src/PasswordStore.sol";
import {DeployPasswordStore} from "../script/DeployPasswordStore.s.sol";
contract PasswordStoreTest is Test {
PasswordStore public passwordStore;
DeployPasswordStore public deployer;
address public owner;
function setUp() public {
deployer = new DeployPasswordStore();
passwordStore = deployer.run();
owner = msg.sender;
}
function test_owner_can_set_password() public {
vm.startPrank(owner);
string memory expectedPassword = "myNewPassword";
passwordStore.setPassword(expectedPassword);
string memory actualPassword = passwordStore.getPassword();
assertEq(actualPassword, expectedPassword);
}
function test_anyone_can_set_password() public {
vm.startPrank(address(2));
string memory expectedPassword = "myNewPassword";
passwordStore.setPassword(expectedPassword);
string memory actualPassword = passwordStore.getPassword();
assertEq(actualPassword, expectedPassword);
}
}
Impact
Likehood: High
Impact: Depends on the use case of the password.
Tools Used
Foundry
Recommendations
Use Solady´s Ownable.sol
contract to effectible restrict the usage of the contract.
pragma solidity 0.8.18;
import {Ownable} from "solady/auth/Ownable.sol";
* @author not-so-secure-dev
* @title PasswordStore
* @notice This contract allows you to store a private password that others won't be able to see.
* You can update your password at any time.
*/
contract PasswordStore is Ownable{
error PasswordStore__NotOwner();
address private s_owner;
string private s_password;
event SetNetPassword();
constructor() {
s_owner = msg.sender;
}
* @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 onlyOwner{
s_password = newPassword;
emit SetNetPassword();
}
* @notice This allows only the owner to retrieve the password.
* @param newPassword The new password to set.
*/
function getPassword() external view onlyOwner returns (string memory) {
return s_password;
}
}