-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(world): limit call context of
CoreSystem
to delegatecall [C-02] (…
- Loading branch information
Showing
10 changed files
with
124 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@latticexyz/world": patch | ||
--- | ||
|
||
Systems are expected to be always called via the central World contract. | ||
Depending on whether it is a root or non-root system, the call is performed via `delegatecall` or `call`. | ||
Since Systems are expected to be stateless and only interact with the World state, it is not necessary to prevent direct calls to the systems. | ||
However, since the `CoreSystem` is known to always be registered as a root system in the World, it is always expected to be delegatecalled, | ||
so we made this expectation explicit by reverting if it is not delegatecalled. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.21; | ||
|
||
/** | ||
* @title LimitedCallContext | ||
* @dev Systems are expected to be always called via the central World contract. | ||
* Depending on whether it is a root or non-root system, the call is performed via `delegatecall` or `call`. | ||
* Since Systems are expected to be stateless and only interact with the World state, | ||
* it is normally not necessary to prevent direct calls to the systems. | ||
* However, since the `CoreSystem` is known to always be registered as a root system in the World, | ||
* it is always expected to be delegatecalled, so we made this expectation explicit by reverting if it is not delegatecalled. | ||
* | ||
* @dev Based on OpenZeppelin's UUPSUpgradeable.sol | ||
* https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v5.0.0/contracts/proxy/utils/UUPSUpgradeable.sol#L50 | ||
*/ | ||
contract LimitedCallContext { | ||
address private immutable __self = address(this); | ||
|
||
error UnauthorizedCallContext(); | ||
|
||
modifier onlyDelegatecall() { | ||
_checkDelegatecall(); | ||
_; | ||
} | ||
|
||
/** | ||
* @dev Reverts if the execution is not performed via delegatecall. | ||
*/ | ||
function _checkDelegatecall() internal view virtual { | ||
if ( | ||
address(this) == __self // Must be called through delegatecall | ||
) { | ||
revert UnauthorizedCallContext(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.