Skip to content

Commit ee4fc17

Browse files
fix: cache the deployer to mitigate front-running on initialization (#148)
1 parent b1fc720 commit ee4fc17

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

evm/src/Manager.sol

+8
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ abstract contract Manager is
3636

3737
error RefundFailed(uint256 refundAmount);
3838
error CannotRenounceManagerOwnership(address owner);
39+
error UnexpectedOwner(address expectedOwner, address owner);
3940

4041
address public immutable token;
42+
address public immutable deployer;
4143
Mode public immutable mode;
4244
uint16 public immutable chainId;
4345
uint256 public immutable evmChainId;
@@ -104,9 +106,15 @@ abstract contract Manager is
104106
mode = _mode;
105107
chainId = _chainId;
106108
evmChainId = block.chainid;
109+
// save the deployer (check this on iniitialization)
110+
deployer = msg.sender;
107111
}
108112

109113
function __Manager_init() internal onlyInitializing {
114+
// check if the owner is the deployer of this contract
115+
if (msg.sender != deployer) {
116+
revert UnexpectedOwner(deployer, msg.sender);
117+
}
110118
// TODO: msg.sender may not be the right address for both
111119
__PausedOwnable_init(msg.sender, msg.sender);
112120
// TODO: check if it's safe to not initialise reentrancy guard

evm/test/Upgrades.t.sol

+15-16
Original file line numberDiff line numberDiff line change
@@ -672,23 +672,22 @@ contract TestInitialize is Test {
672672
managerChain1.initialize();
673673
}
674674

675-
// TODO - Keep or remove depending on frontrunning discussion for deployment
676-
// function test_frontrunInitialize() public{
677-
// string memory url = "https://ethereum-goerli.publicnode.com";
678-
// vm.createSelectFork(url);
679-
680-
// vm.chainId(chainId1);
681-
// DummyToken t1 = new DummyToken();
682-
// ManagerStandalone implementation =
683-
// new ManagerStandalone(address(t1), Manager.Mode.LOCKING, chainId1, 1 days);
684-
685-
// managerChain1 = ManagerStandalone(address(new ERC1967Proxy(address(implementation), "")));
675+
function test_cannotFrontrunInitialize() public {
676+
string memory url = "https://ethereum-goerli.publicnode.com";
677+
vm.createSelectFork(url);
686678

687-
// vm.prank(userA);
679+
vm.chainId(chainId1);
680+
DummyToken t1 = new DummyToken();
681+
ManagerStandalone implementation =
682+
new ManagerStandalone(address(t1), Manager.Mode.LOCKING, chainId1, 1 days);
688683

689-
// // Frontrun of initialization occurs HERE
690-
// managerChain1.initialize();
684+
managerChain1 = ManagerStandalone(address(new ERC1967Proxy(address(implementation), "")));
691685

692-
// require(address(this) == managerChain1.owner(), "Unexpected owner");
693-
// }
686+
// Attempt to initialize the contract from a non-deployer account.
687+
vm.prank(userA);
688+
vm.expectRevert(
689+
abi.encodeWithSignature("UnexpectedOwner(address,address)", address(this), userA)
690+
);
691+
managerChain1.initialize();
692+
}
694693
}

0 commit comments

Comments
 (0)