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

Ensure contribution router target is a contract #374

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions contracts/crowdfund/ContributionRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ contract ContributionRouter {
event ClaimedFees(address indexed partyDao, address indexed recipient, uint256 amount);

error OnlyOwner();
/// @notice Thrown if the target is not a smart contract.
error InvalidTarget();

/// @notice The address allowed to claim fees from the contract.
address public immutable OWNER;
Expand Down Expand Up @@ -73,8 +75,13 @@ contract ContributionRouter {
uint256 feeAmount = _storage.feePerMint;
_storage.caller = msg.sender;
address target;
uint256 targetCodeSize;
assembly {
target := shr(96, calldataload(sub(calldatasize(), 20)))
targetCodeSize := extcodesize(target)
}
if (targetCodeSize == 0) {
revert InvalidTarget();
}
if (
msg.sig == InitialETHCrowdfund.batchContributeFor.selector ||
Expand Down
16 changes: 16 additions & 0 deletions test/crowdfund/ContributionRouter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ contract ContributionRouterTest is TestUtils {
assertEq(address(router).balance, feeAmount);
}

function test_invalidTarget() external {
MockPayableContract target = new MockPayableContract();
uint256 amount = 1 ether;
vm.deal(address(this), amount);
//vm.expectRevert(ContributionRouter.InvalidTarget.selector);
(bool success, bytes memory res) = address(router).call{ value: amount }(
abi.encodePacked(
abi.encodeWithSelector(MockPayableContract.pay.selector),
target,
hex"1234"
)
);
assertEq(success, false);
assertEq(res, abi.encodePacked(ContributionRouter.InvalidTarget.selector));
}

function test_fallback_insufficientFee() public {
MockPayableContract target = new MockPayableContract();
uint256 amount = feePerMint - 1;
Expand Down
Loading