Skip to content

Commit

Permalink
feat: protect protocol upgradeability loss (#904)
Browse files Browse the repository at this point in the history
* feat: protect protocol upgradeability loss

diamondCut function cannot be removed.

Resolves: sherlock-audit/2023-12-ubiquity-judging#21

* chore: explicit explanation of diamondCut function selector value

As proposed during pull request review.
  • Loading branch information
gitcoindev authored Feb 27, 2024
1 parent b13db91 commit 703f559
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/contracts/src/dollar/libraries/LibDiamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ library LibDiamond {
_facetAddress != address(0),
"LibDiamondCut: Can't remove function that doesn't exist"
);
// precomputed diamondCut function selector to save gas
// bytes4(keccak256(abi.encodeWithSignature("diamondCut((address,uint8,bytes4[])[],address,bytes)"))) == 0x1f931c1c
require(
_selector != bytes4(0x1f931c1c),
"LibDiamondCut: Can't remove diamondCut function"
);
// an immutable function is a function defined directly in a diamond
require(
_facetAddress != address(this),
Expand Down
17 changes: 17 additions & 0 deletions packages/contracts/test/diamond/DiamondTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,23 @@ contract TestDiamond is DiamondTestSetup {
IMockFacet(address(diamondCutFacet)).functionB();
}

function testCutFacetShouldNotRemoveDiamondCutFunction() public {
FacetCut[] memory facetCut = new FacetCut[](1);
bytes4[] memory selectors = new bytes4[](1);
selectors[0] = diamondCutFacet.diamondCut.selector;

facetCut[0] = FacetCut({
facetAddress: address(0),
action: FacetCutAction.Remove,
functionSelectors: selectors
});

// try to remove diamondCut function
vm.prank(owner);
vm.expectRevert("LibDiamondCut: Can't remove diamondCut function");
diamondCutFacet.diamondCut(facetCut, address(0x0), "");
}

function testSelectors_ShouldBeAssociatedWithCorrectFacet() public {
for (uint256 i; i < facetAddressList.length; i++) {
if (compareStrings(facetNames[i], "DiamondCutFacet")) {
Expand Down

0 comments on commit 703f559

Please sign in to comment.