Skip to content

Instantly share code, notes, and snippets.

@bartubozkurt
Last active February 1, 2023 17:18
Show Gist options
  • Save bartubozkurt/5193a568e4dfcdd81ad43e77a7cd1e16 to your computer and use it in GitHub Desktop.
Save bartubozkurt/5193a568e4dfcdd81ad43e77a7cd1e16 to your computer and use it in GitHub Desktop.
/* 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