The setPassword function within the PasswordStore contract displays a critical vulnerability due to the lack of access control. This vulnerability allows a malicious actor to set the stored password.
In the setPassword external function, a string value named newPassword is received as argument, used to set the s_password private string variable and an event is emitted. But, since there is no code checking if msg.sender is equal to s_owner, anyone can execute this function and set the password.
Paste the following code inside the PasswordStoreTest contract located at test/PasswordStore.t.sol file:
And run forge test --match-test test_non_owner_set_password. We can see that the test run successfully as it doesn't revert.
The code block above:
Impersonates the address(1) which is not the contract owner
Creates a new variable newPassword storing the new password "myNewPassword"
Executes the setPassword function using the newPassword variable as argument
Impersonates the owner address of the PasswordStore contract
Gets the password from PasswordStore contract using getPassword function and store it inside the actualPassword variable
Checks if the newPassword value is the same as the actualPassword value
A malicious user can change the stored password and make the owner use a incorrect password, losing access to the account the password is used for.
Visual Studio Code and Foundry.
Make the function setPassword revert with PasswordStore__NotOwner error if the caller address (msg.sender) is not equal to the owner address (s_owner):
Anyone can call `setPassword` and set a new password contrary to the intended purpose.
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.