Last active
February 1, 2023 16:33
-
-
Save bartubozkurt/8d540c26bc37034ed4f8f16631a708d6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* Bad */ | |
contract Bad { | |
function badDelegate(address _yourContract, bytes calldata _data) payable public returns (bytes memory) { | |
(bool success, bytes memory data) = _yourContract.delegatecall(_data); | |
require(success); | |
return data; | |
} | |
} | |
/* Vulnerability | |
Anyone can destroy the Bad contract using by “selfdestruct” | |
because in the context of delegatecall, | |
msg.sender will be BadContract even the caller is anyone. | |
*/ | |
/* Better */ | |
contract Good { | |
mapping(address => bool) whitelist; //add | |
function goodDelegate(address _yourContract, bytes calldata _data) payable public returns (bytes memory) { | |
require(whitelist[msg.sender]); //add | |
(bool success, bytes memory data) = _yourContract.delegatecall(_data); | |
require(success); | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment