Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ALC-7] add input validation to _initialize and _transferOwnership #6

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/LightAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ contract LightAccount is BaseAccount, TokenCallbackHandler, UUPSUpgradeable, Cus
error ArrayLengthMismatch();

/**
* @dev The new owner is not a valid owner (e.g., `address(0)` or the
* account itself).
* @dev The new owner is not a valid owner (e.g., `address(0)`, the
* account itself, or the current owner).
*/
error InvalidOwner(address owner);

Expand Down Expand Up @@ -239,6 +239,9 @@ contract LightAccount is BaseAccount, TokenCallbackHandler, UUPSUpgradeable, Cus
}

function _initialize(address anOwner) internal virtual {
if (anOwner == address(0)) {
revert InvalidOwner(address(0));
}
_getStorage().owner = anOwner;
emit LightAccountInitialized(_entryPoint, anOwner);
emit OwnershipTransferred(address(0), anOwner);
Expand All @@ -251,6 +254,9 @@ contract LightAccount is BaseAccount, TokenCallbackHandler, UUPSUpgradeable, Cus
function _transferOwnership(address newOwner) internal virtual {
LightAccountStorage storage _storage = _getStorage();
address oldOwner = _storage.owner;
if (newOwner == oldOwner) {
revert InvalidOwner(newOwner);
}
_storage.owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
Expand Down
12 changes: 12 additions & 0 deletions test/LightAccount.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ contract LightAccountTest is Test {
account = factory.createAccount(eoaAddress, 1);
}

function testCannotInitializeWithZeroOwner() public {
LightAccountFactory factory = new LightAccountFactory(entryPoint);
vm.expectRevert(abi.encodeWithSelector(LightAccount.InvalidOwner.selector, (address(0))));
account = factory.createAccount(address(0), 1);
}

function testAddDeposit() public {
assertEq(account.getDeposit(), 0);
account.addDeposit{value: 10}();
Expand Down Expand Up @@ -193,6 +199,12 @@ contract LightAccountTest is Test {
account.transferOwnership(address(0x100));
}

function testCannotTransferOwnershipToCurrentOwner() public {
vm.prank(eoaAddress);
vm.expectRevert(abi.encodeWithSelector(LightAccount.InvalidOwner.selector, (eoaAddress)));
account.transferOwnership(eoaAddress);
}

function testCannotTransferOwnershipToZero() public {
vm.prank(eoaAddress);
vm.expectRevert(abi.encodeWithSelector(LightAccount.InvalidOwner.selector, (address(0))));
Expand Down