Skip to content

Commit

Permalink
Enforce threshold bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
yorhodes committed Nov 5, 2024
1 parent c3001ca commit 13f0c35
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
7 changes: 5 additions & 2 deletions solidity/contracts/isms/aggregation/StorageAggregationIsm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ contract StorageAggregationIsm is
address[] memory _modules,
uint8 _threshold
) public onlyOwner {
require(_threshold <= _modules.length, "Invalid threshold");
require(
0 < _threshold && _threshold <= _modules.length,
"Invalid threshold"
);
modules = _modules;
threshold = _threshold;
emit ModulesAndThresholdSet(_modules, _threshold);
Expand All @@ -64,7 +67,7 @@ contract StorageAggregationIsmFactory is

constructor() {
implementation = address(
new StorageAggregationIsm(new address[](0), 0)
new StorageAggregationIsm(new address[](1), 1)
);
}

Expand Down
5 changes: 4 additions & 1 deletion solidity/contracts/isms/multisig/StorageMultisigIsm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ abstract contract AbstractStorageMultisigIsm is
address[] memory _validators,
uint8 _threshold
) public onlyOwner {
require(_threshold <= _validators.length, "Invalid threshold");
require(
0 < _threshold && _threshold <= _validators.length,
"Invalid threshold"
);
validators = _validators;
threshold = _threshold;
emit ValidatorsAndThresholdSet(_validators, _threshold);
Expand Down
4 changes: 4 additions & 0 deletions solidity/contracts/libs/StaticAddressSetFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ abstract contract StaticThresholdAddressSetFactory is
address[] calldata _values,
uint8 _threshold
) public returns (address) {
require(
0 < _threshold && _threshold <= _values.length,
"Invalid threshold"
);
(bytes32 _salt, bytes memory _bytecode) = _saltAndBytecode(
_values,
_threshold
Expand Down
2 changes: 2 additions & 0 deletions solidity/test/hooks/AggregationHook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ contract AggregationHookTest is Test {
uint8 n,
uint256 fee
) internal returns (address[] memory) {
vm.assume(n > 0);

address[] memory hooks = new address[](n);
for (uint8 i = 0; i < n; i++) {
TestPostDispatchHook subHook = new TestPostDispatchHook();
Expand Down
18 changes: 13 additions & 5 deletions solidity/test/isms/AggregationIsm.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ contract AggregationIsmTest is Test {
uint8 n,
bytes32 seed
) internal returns (address[] memory) {
vm.assume(m > 0 && m <= n && n < 10);

bytes32 randomness = seed;
address[] memory isms = new address[](n);
for (uint256 i = 0; i < n; i++) {
Expand Down Expand Up @@ -93,7 +95,6 @@ contract AggregationIsmTest is Test {
}

function testVerify(uint8 m, uint8 n, bytes32 seed) public {
vm.assume(0 < m && m <= n && n < 10);
deployIsms(m, n, seed);

bytes memory metadata = getMetadata(m, seed);
Expand All @@ -106,7 +107,7 @@ contract AggregationIsmTest is Test {
uint8 i,
bytes32 seed
) public {
vm.assume(0 < m && m <= n && n < 10 && i < n);
vm.assume(i < n);
deployIsms(m, n, seed);
(address[] memory modules, ) = ism.modulesAndThreshold("");
bytes memory noMetadata;
Expand All @@ -117,7 +118,6 @@ contract AggregationIsmTest is Test {
}

function testVerifyMissingMetadata(uint8 m, uint8 n, bytes32 seed) public {
vm.assume(0 < m && m <= n && n < 10);
deployIsms(m, n, seed);

// Populate metadata for one fewer ISMs than needed.
Expand All @@ -131,7 +131,6 @@ contract AggregationIsmTest is Test {
uint8 n,
bytes32 seed
) public {
vm.assume(0 < m && m <= n && n < 10);
deployIsms(m, n, seed);

bytes memory metadata = getMetadata(m, seed);
Expand All @@ -143,13 +142,22 @@ contract AggregationIsmTest is Test {
}

function testModulesAndThreshold(uint8 m, uint8 n, bytes32 seed) public {
vm.assume(0 < m && m <= n && n < 10);
address[] memory expectedIsms = deployIsms(m, n, seed);
(address[] memory actualIsms, uint8 actualThreshold) = ism
.modulesAndThreshold("");
assertEq(abi.encode(actualIsms), abi.encode(expectedIsms));
assertEq(actualThreshold, m);
}

function testZeroThreshold() public {
vm.expectRevert("Invalid threshold");
factory.deploy(new address[](1), 0);
}

function testThresholdExceedsLength() public {
vm.expectRevert("Invalid threshold");
factory.deploy(new address[](1), 2);
}
}

contract StorageAggregationIsmTest is AggregationIsmTest {
Expand Down
10 changes: 10 additions & 0 deletions solidity/test/isms/MultisigIsm.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ abstract contract AbstractMultisigIsmTest is Test {
vm.expectRevert("!threshold");
ism.verify(duplicateMetadata, message);
}

function testZeroThreshold() public virtual {
vm.expectRevert("Invalid threshold");
factory.deploy(new address[](1), 0);
}

function testThresholdExceedsLength() public virtual {
vm.expectRevert("Invalid threshold");
factory.deploy(new address[](1), 2);
}
}

contract MerkleRootMultisigIsmTest is AbstractMultisigIsmTest {
Expand Down
16 changes: 16 additions & 0 deletions solidity/test/isms/WeightedMultisigIsm.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ contract StaticMerkleRootWeightedMultisigIsmTest is
seed
);
}

function testThresholdExceedsLength() public override {
// no-op
}

function testZeroThreshold() public override {
// no-op
}
}

contract StaticMessageIdWeightedMultisigIsmTest is
Expand Down Expand Up @@ -298,4 +306,12 @@ contract StaticMessageIdWeightedMultisigIsmTest is
seed
);
}

function testThresholdExceedsLength() public override {
// no-op
}

function testZeroThreshold() public override {
// no-op
}
}

0 comments on commit 13f0c35

Please sign in to comment.