Merge pull request #2203 from vovikhangcdv/dev

update: improve unhandled initializers in unprotected-upgrade detector
pull/2473/head
alpharush 6 months ago committed by GitHub
commit dde3378e74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      slither/core/declarations/contract.py
  2. 9
      slither/detectors/statements/unprotected_upgradeable.py
  3. 1
      tests/e2e/detectors/snapshots/detectors__detector_UnprotectedUpgradeable_0_4_25_AnyInitializer_sol__0.txt
  4. 1
      tests/e2e/detectors/snapshots/detectors__detector_UnprotectedUpgradeable_0_4_25_Reinitializer_sol__0.txt
  5. 1
      tests/e2e/detectors/snapshots/detectors__detector_UnprotectedUpgradeable_0_5_16_AnyInitializer_sol__0.txt
  6. 1
      tests/e2e/detectors/snapshots/detectors__detector_UnprotectedUpgradeable_0_5_16_Reinitializer_sol__0.txt
  7. 1
      tests/e2e/detectors/snapshots/detectors__detector_UnprotectedUpgradeable_0_6_11_AnyInitializer_sol__0.txt
  8. 1
      tests/e2e/detectors/snapshots/detectors__detector_UnprotectedUpgradeable_0_6_11_Reinitializer_sol__0.txt
  9. 1
      tests/e2e/detectors/snapshots/detectors__detector_UnprotectedUpgradeable_0_7_6_AnyInitializer_sol__0.txt
  10. 1
      tests/e2e/detectors/snapshots/detectors__detector_UnprotectedUpgradeable_0_7_6_Reinitializer_sol__0.txt
  11. 1
      tests/e2e/detectors/snapshots/detectors__detector_UnprotectedUpgradeable_0_8_15_AnyInitializer_sol__0.txt
  12. 1
      tests/e2e/detectors/snapshots/detectors__detector_UnprotectedUpgradeable_0_8_15_Reinitializer_sol__0.txt
  13. 15
      tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/AnyInitializer.sol
  14. BIN
      tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/AnyInitializer.sol-0.4.25.zip
  15. 14
      tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/Initializable.sol
  16. 15
      tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/Reinitializer.sol
  17. BIN
      tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/Reinitializer.sol-0.4.25.zip
  18. 15
      tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/AnyInitializer.sol
  19. BIN
      tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/AnyInitializer.sol-0.5.16.zip
  20. 14
      tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/Initializable.sol
  21. 15
      tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/Reinitializer.sol
  22. BIN
      tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/Reinitializer.sol-0.5.16.zip
  23. 15
      tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/AnyInitializer.sol
  24. BIN
      tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/AnyInitializer.sol-0.6.11.zip
  25. 14
      tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/Initializable.sol
  26. 15
      tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/Reinitializer.sol
  27. BIN
      tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/Reinitializer.sol-0.6.11.zip
  28. 15
      tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/AnyInitializer.sol
  29. BIN
      tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/AnyInitializer.sol-0.7.6.zip
  30. 8
      tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/Initializable.sol
  31. 15
      tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/Reinitializer.sol
  32. BIN
      tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/Reinitializer.sol-0.7.6.zip
  33. 15
      tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/AnyInitializer.sol
  34. BIN
      tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/AnyInitializer.sol-0.8.15.zip
  35. 6
      tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/Initializable.sol
  36. 15
      tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/Reinitializer.sol
  37. BIN
      tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/Reinitializer.sol-0.8.15.zip
  38. 50
      tests/e2e/detectors/test_detectors.py

@ -1372,8 +1372,6 @@ class Contract(SourceMapping): # pylint: disable=too-many-public-methods
def is_upgradeable(self) -> bool:
if self._is_upgradeable is None:
self._is_upgradeable = False
if self.is_upgradeable_proxy:
return False
initializable = self.file_scope.get_contract_from_name("Initializable")
if initializable:
if initializable in self.inheritance:

@ -52,7 +52,14 @@ def _whitelisted_modifiers(f: Function) -> bool:
def _initialize_functions(contract: Contract) -> List[Function]:
return list(
filter(_whitelisted_modifiers, [f for f in contract.functions if f.name == "initialize"])
filter(
_whitelisted_modifiers,
[
f
for f in contract.functions
if any((m.name in ["initializer", "reinitializer"]) for m in f.modifiers)
],
)
)

@ -0,0 +1 @@
AnyInitializer (tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/AnyInitializer.sol#3-15) is an upgradeable contract that does not protect its initialize functions: AnyInitializer.anyName() (tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/AnyInitializer.sol#6-9). Anyone can delete the contract with: AnyInitializer.kill() (tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/AnyInitializer.sol#11-14)

@ -0,0 +1 @@
Reinitializer (tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/Reinitializer.sol#3-15) is an upgradeable contract that does not protect its initialize functions: Reinitializer.initialize() (tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/Reinitializer.sol#6-9). Anyone can delete the contract with: Reinitializer.kill() (tests/e2e/detectors/test_data/unprotected-upgrade/0.4.25/Reinitializer.sol#11-14)

@ -0,0 +1 @@
AnyInitializer (tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/AnyInitializer.sol#3-15) is an upgradeable contract that does not protect its initialize functions: AnyInitializer.anyName() (tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/AnyInitializer.sol#6-9). Anyone can delete the contract with: AnyInitializer.kill() (tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/AnyInitializer.sol#11-14)

@ -0,0 +1 @@
Reinitializer (tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/Reinitializer.sol#3-15) is an upgradeable contract that does not protect its initialize functions: Reinitializer.initialize() (tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/Reinitializer.sol#6-9). Anyone can delete the contract with: Reinitializer.kill() (tests/e2e/detectors/test_data/unprotected-upgrade/0.5.16/Reinitializer.sol#11-14)

@ -0,0 +1 @@
AnyInitializer (tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/AnyInitializer.sol#3-15) is an upgradeable contract that does not protect its initialize functions: AnyInitializer.anyName() (tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/AnyInitializer.sol#6-9). Anyone can delete the contract with: AnyInitializer.kill() (tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/AnyInitializer.sol#11-14)

@ -0,0 +1 @@
Reinitializer (tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/Reinitializer.sol#3-15) is an upgradeable contract that does not protect its initialize functions: Reinitializer.initialize() (tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/Reinitializer.sol#6-9). Anyone can delete the contract with: Reinitializer.kill() (tests/e2e/detectors/test_data/unprotected-upgrade/0.6.11/Reinitializer.sol#11-14)

@ -0,0 +1 @@
AnyInitializer (tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/AnyInitializer.sol#3-15) is an upgradeable contract that does not protect its initialize functions: AnyInitializer.anyName() (tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/AnyInitializer.sol#6-9). Anyone can delete the contract with: AnyInitializer.kill() (tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/AnyInitializer.sol#11-14)

@ -0,0 +1 @@
Reinitializer (tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/Reinitializer.sol#3-15) is an upgradeable contract that does not protect its initialize functions: Reinitializer.initialize() (tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/Reinitializer.sol#6-9). Anyone can delete the contract with: Reinitializer.kill() (tests/e2e/detectors/test_data/unprotected-upgrade/0.7.6/Reinitializer.sol#11-14)

@ -0,0 +1 @@
AnyInitializer (tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/AnyInitializer.sol#3-15) is an upgradeable contract that does not protect its initialize functions: AnyInitializer.anyName() (tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/AnyInitializer.sol#6-9). Anyone can delete the contract with: AnyInitializer.kill() (tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/AnyInitializer.sol#11-14)

@ -0,0 +1 @@
Reinitializer (tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/Reinitializer.sol#3-15) is an upgradeable contract that does not protect its initialize functions: Reinitializer.initialize() (tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/Reinitializer.sol#6-9). Anyone can delete the contract with: Reinitializer.kill() (tests/e2e/detectors/test_data/unprotected-upgrade/0.8.15/Reinitializer.sol#11-14)

@ -0,0 +1,15 @@
import "./Initializable.sol";
contract AnyInitializer is Initializable {
address owner;
function anyName() external initializer {
require(owner == address(0));
owner = msg.sender;
}
function kill() external {
require(msg.sender == owner);
selfdestruct(owner);
}
}

@ -1,5 +1,9 @@
contract Initializable{
modifier initializer() {
_;
}
}
contract Initializable {
modifier initializer() {
_;
}
modifier reinitializer(uint64 version) {
_;
}
}

@ -0,0 +1,15 @@
import "./Initializable.sol";
contract Reinitializer is Initializable {
address owner;
function initialize() external reinitializer(2) {
require(owner == address(0));
owner = msg.sender;
}
function kill() external {
require(msg.sender == owner);
selfdestruct(owner);
}
}

@ -0,0 +1,15 @@
import "./Initializable.sol";
contract AnyInitializer is Initializable {
address payable owner;
function anyName() external initializer {
require(owner == address(0));
owner = msg.sender;
}
function kill() external {
require(msg.sender == owner);
selfdestruct(owner);
}
}

@ -1,5 +1,9 @@
contract Initializable{
modifier initializer() {
_;
}
}
contract Initializable {
modifier initializer() {
_;
}
modifier reinitializer(uint64 version) {
_;
}
}

@ -0,0 +1,15 @@
import "./Initializable.sol";
contract Reinitializer is Initializable {
address payable owner;
function initialize() external reinitializer(2) {
require(owner == address(0));
owner = msg.sender;
}
function kill() external {
require(msg.sender == owner);
selfdestruct(owner);
}
}

@ -0,0 +1,15 @@
import "./Initializable.sol";
contract AnyInitializer is Initializable {
address payable owner;
function anyName() external initializer {
require(owner == address(0));
owner = payable(msg.sender);
}
function kill() external {
require(msg.sender == owner);
selfdestruct(owner);
}
}

@ -1,5 +1,9 @@
contract Initializable{
modifier initializer() {
_;
}
}
contract Initializable {
modifier initializer() {
_;
}
modifier reinitializer(uint64 version) {
_;
}
}

@ -0,0 +1,15 @@
import "./Initializable.sol";
contract Reinitializer is Initializable {
address payable owner;
function initialize() external reinitializer(2) {
require(owner == address(0));
owner = payable(msg.sender);
}
function kill() external {
require(msg.sender == owner);
selfdestruct(owner);
}
}

@ -0,0 +1,15 @@
import "./Initializable.sol";
contract AnyInitializer is Initializable {
address payable owner;
function anyName() external initializer {
require(owner == address(0));
owner = payable(msg.sender);
}
function kill() external {
require(msg.sender == owner);
selfdestruct(owner);
}
}

@ -1,4 +1,4 @@
contract Initializable{
contract Initializable {
uint8 private _initialized;
bool private _initializing;
@ -6,10 +6,14 @@ contract Initializable{
_;
}
modifier reinitializer(uint64 version) {
_;
}
function _disableInitializers() internal virtual {
require(!_initializing, "Initializable: contract is initializing");
if (_initialized < type(uint8).max) {
_initialized = type(uint8).max;
}
}
}
}

@ -0,0 +1,15 @@
import "./Initializable.sol";
contract Reinitializer is Initializable {
address payable owner;
function initialize() external reinitializer(2) {
require(owner == address(0));
owner = payable(msg.sender);
}
function kill() external {
require(msg.sender == owner);
selfdestruct(owner);
}
}

@ -0,0 +1,15 @@
import "./Initializable.sol";
contract AnyInitializer is Initializable {
address payable owner;
function anyName() external initializer {
require(owner == address(0));
owner = payable(msg.sender);
}
function kill() external {
require(msg.sender == owner);
selfdestruct(owner);
}
}

@ -6,10 +6,14 @@ contract Initializable {
_;
}
modifier reinitializer(uint64 version) {
_;
}
function _disableInitializers() internal virtual {
require(!_initializing, "Initializable: contract is initializing");
if (_initialized < type(uint8).max) {
_initialized = type(uint8).max;
}
}
}
}

@ -0,0 +1,15 @@
import "./Initializable.sol";
contract Reinitializer is Initializable {
address payable owner;
function initialize() external reinitializer(2) {
require(owner == address(0));
owner = payable(msg.sender);
}
function kill() external {
require(msg.sender == owner);
selfdestruct(owner);
}
}

@ -938,6 +938,16 @@ ALL_TESTS = [
"whitelisted.sol",
"0.4.25",
),
Test(
all_detectors.UnprotectedUpgradeable,
"Reinitializer.sol",
"0.4.25",
),
Test(
all_detectors.UnprotectedUpgradeable,
"AnyInitializer.sol",
"0.4.25",
),
Test(
all_detectors.UnprotectedUpgradeable,
"Buggy.sol",
@ -953,6 +963,16 @@ ALL_TESTS = [
"whitelisted.sol",
"0.5.16",
),
Test(
all_detectors.UnprotectedUpgradeable,
"Reinitializer.sol",
"0.5.16",
),
Test(
all_detectors.UnprotectedUpgradeable,
"AnyInitializer.sol",
"0.5.16",
),
Test(
all_detectors.UnprotectedUpgradeable,
"Buggy.sol",
@ -968,6 +988,16 @@ ALL_TESTS = [
"whitelisted.sol",
"0.6.11",
),
Test(
all_detectors.UnprotectedUpgradeable,
"Reinitializer.sol",
"0.6.11",
),
Test(
all_detectors.UnprotectedUpgradeable,
"AnyInitializer.sol",
"0.6.11",
),
Test(
all_detectors.UnprotectedUpgradeable,
"Buggy.sol",
@ -978,6 +1008,16 @@ ALL_TESTS = [
"Fixed.sol",
"0.7.6",
),
Test(
all_detectors.UnprotectedUpgradeable,
"Reinitializer.sol",
"0.7.6",
),
Test(
all_detectors.UnprotectedUpgradeable,
"AnyInitializer.sol",
"0.7.6",
),
Test(
all_detectors.UnprotectedUpgradeable,
"whitelisted.sol",
@ -998,6 +1038,16 @@ ALL_TESTS = [
"whitelisted.sol",
"0.8.15",
),
Test(
all_detectors.UnprotectedUpgradeable,
"Reinitializer.sol",
"0.8.15",
),
Test(
all_detectors.UnprotectedUpgradeable,
"AnyInitializer.sol",
"0.8.15",
),
Test(
all_detectors.ABIEncoderV2Array,
"storage_ABIEncoderV2_array.sol",

Loading…
Cancel
Save