Skip to content

Commit

Permalink
Add missing validations (#67)
Browse files Browse the repository at this point in the history
* Add zero address check in Wormhole adapter

* Add destination chain validation
  • Loading branch information
ermyas authored Sep 15, 2023
1 parent 5d1307f commit 2913b7d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/MultiMessageSender.sol
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ contract MultiMessageSender {
revert Error.ZERO_CHAIN_ID();
}

if (_dstChainId == block.chainid) {
revert Error.INVALID_DST_CHAIN();
}

if (_target == address(0)) {
revert Error.INVALID_TARGET();
}
Expand Down
12 changes: 6 additions & 6 deletions src/adapters/axelar/AxelarReceiverAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ contract AxelarReceiverAdapter is IAxelarExecutable, IMessageReceiverAdapter {
revert Error.INVALID_SENDER_CHAIN_ID();
}

/// @dev step-2: validate the contract call
if (!gateway.validateContractCall(commandId, sourceChain, sourceAddress, keccak256(payload))) {
revert Error.NOT_APPROVED_BY_GATEWAY();
}

/// @dev step-3: validate the source address
/// @dev step-2: validate the source address
if (sourceAddress.toAddress() != senderAdapter) {
revert Error.INVALID_SENDER_ADAPTER();
}

/// @dev step-3: validate the contract call
if (!gateway.validateContractCall(commandId, sourceChain, sourceAddress, keccak256(payload))) {
revert Error.NOT_APPROVED_BY_GATEWAY();
}

/// decode the cross-chain payload
AdapterPayload memory decodedPayload = abi.decode(payload, (AdapterPayload));
bytes32 msgId = decodedPayload.msgId;
Expand Down
3 changes: 3 additions & 0 deletions src/adapters/wormhole/WormholeReceiverAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ contract WormholeReceiverAdapter is IMessageReceiverAdapter, IWormholeReceiver {
/// @param _gac is global access controller.
/// note: https://docs.wormhole.com/wormhole/quick-start/cross-chain-dev/automatic-relayer
constructor(address _relayer, address _gac) {
if (_relayer == address(0) || _gac == address(0)) {
revert Error.ZERO_ADDRESS_INPUT();
}
relayer = _relayer;
gac = IGAC(_gac);
}
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/wormhole/WormholeSenderAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ contract WormholeSenderAdapter is BaseSenderAdapter {
uint16 wormChainId = chainIdMap[_toChainId];

if (wormChainId == 0) {
revert Error.ZERO_CHAIN_ID();
revert Error.INVALID_DST_CHAIN();
}

msgId = _getNewMessageId(_toChainId, _to);
Expand Down
8 changes: 8 additions & 0 deletions test/unit-tests/MultiMessageSender.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ contract MultiMessageSenderTest is Setup {
sender.remoteCall(DST_CHAIN_ID, address(42), bytes("42"), 0, invalidExpMax, excludedAdapters);
}

/// @dev dst chain cannot be the the sender chain
function test_remote_call_chain_id_is_sender_chain() public {
vm.startPrank(caller);

vm.expectRevert(Error.INVALID_DST_CHAIN.selector);
sender.remoteCall(block.chainid, address(42), bytes("42"), 0, EXPIRATION_CONSTANT);
}

/// @dev cannot call with dst chain ID of 0
function test_remote_call_zero_chain_id() public {
vm.startPrank(caller);
Expand Down
12 changes: 12 additions & 0 deletions test/unit-tests/adapters/wormhole/WormholeReceiverAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ contract WormholeReceiverAdapterTest is Setup {
assertEq(address(adapter.gac()), contractAddress[DST_CHAIN_ID]["GAC"]);
}

/// @dev constructor cannot be called with zero address relayer
function test_constructor_zero_address_relayer() public {
vm.expectRevert(Error.ZERO_ADDRESS_INPUT.selector);
new WormholeReceiverAdapter(address(0), address(42));
}

/// @dev constructor cannot be called with zero address GAC
function test_constructor_zero_address_gac() public {
vm.expectRevert(Error.ZERO_ADDRESS_INPUT.selector);
new WormholeReceiverAdapter(address(42), address(0));
}

/// @dev gets the name
function test_name() public {
assertEq(adapter.name(), "wormhole");
Expand Down
4 changes: 2 additions & 2 deletions test/unit-tests/adapters/wormhole/WormholeSenderAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ contract WormholeSenderAdapterTest is Setup {
}

/// @dev cannot dispatch message to invalid dst chain
function test_dispatch_message_zero_chain_id() public {
function test_dispatch_message_unknown_chain_id() public {
// clear chain ID map entry first
vm.startPrank(owner);
uint256[] memory origIds = new uint256[](1);
Expand All @@ -79,7 +79,7 @@ contract WormholeSenderAdapterTest is Setup {
vm.startPrank(senderAddr);
vm.deal(senderAddr, 1 ether);

vm.expectRevert(Error.ZERO_CHAIN_ID.selector);
vm.expectRevert(Error.INVALID_DST_CHAIN.selector);
adapter.dispatchMessage{value: 1 ether}(DST_CHAIN_ID, address(42), bytes("42"));
}

Expand Down

0 comments on commit 2913b7d

Please sign in to comment.