diff --git a/packages/contracts/src/dollar/Diamond.sol b/packages/contracts/src/dollar/Diamond.sol index e3db88be1..cbb606084 100644 --- a/packages/contracts/src/dollar/Diamond.sol +++ b/packages/contracts/src/dollar/Diamond.sol @@ -50,6 +50,7 @@ contract Diamond { ds.slot := position } address facet = ds.selectorToFacetAndPosition[msg.sig].facetAddress; + require(msg.data.length >= 4, "Diamond: Selector is too short"); require(facet != address(0), "Diamond: Function does not exist"); assembly { calldatacopy(0, 0, calldatasize()) diff --git a/packages/contracts/test/diamond/DiamondTest.t.sol b/packages/contracts/test/diamond/DiamondTest.t.sol index 358dc5f05..f4b530683 100644 --- a/packages/contracts/test/diamond/DiamondTest.t.sol +++ b/packages/contracts/test/diamond/DiamondTest.t.sol @@ -345,4 +345,14 @@ contract TestDiamond is DiamondTestSetup { } } } + + function testFallback_ShouldRevert_IfSelectorIsTooShort() public { + // pass 3 bytes selector while normally it should be 4 bytes length + vm.expectRevert(bytes("Diamond: Selector is too short")); + (bool revertsAsExpected, ) = address(diamond).call(bytes("000")); + // NOTICE: for low level calls (like `address.call()`) the returned result + // is NOT the success of the low level call but the success of the `vm.expectRevert()` + // expression, more info: https://book.getfoundry.sh/cheatcodes/expect-revert + assertTrue(revertsAsExpected); + } }