mirror of https://github.com/crytic/slither
commit
200c5ce875
@ -1,8 +1,13 @@ |
|||||||
|
from typing import Tuple, List, Type |
||||||
|
|
||||||
from slither_my_plugin.detectors.example import Example |
from slither_my_plugin.detectors.example import Example |
||||||
|
|
||||||
|
from slither.detectors.abstract_detector import AbstractDetector |
||||||
|
from slither.printers.abstract_printer import AbstractPrinter |
||||||
|
|
||||||
|
|
||||||
def make_plugin(): |
def make_plugin() -> Tuple[List[Type[AbstractDetector]], List[Type[AbstractPrinter]]]: |
||||||
plugin_detectors = [Example] |
plugin_detectors = [Example] |
||||||
plugin_printers = [] |
plugin_printers: List[Type[AbstractPrinter]] = [] |
||||||
|
|
||||||
return plugin_detectors, plugin_printers |
return plugin_detectors, plugin_printers |
||||||
|
@ -0,0 +1,15 @@ |
|||||||
|
#!/usr/bin/env bash |
||||||
|
|
||||||
|
### Test path filtering across POSIX and Windows |
||||||
|
|
||||||
|
solc-select use 0.8.0 |
||||||
|
slither "tests/test_path_filtering/test_path_filtering.sol" --config "tests/test_path_filtering/slither.config.json" > "output.txt" 2>&1 |
||||||
|
|
||||||
|
if ! grep -q "0 result(s) found" "output.txt" |
||||||
|
then |
||||||
|
echo "Path filtering across POSIX and Windows failed" |
||||||
|
rm output.txt |
||||||
|
exit 5 |
||||||
|
else |
||||||
|
rm output.txt |
||||||
|
fi |
@ -0,0 +1,72 @@ |
|||||||
|
""" |
||||||
|
Module detecting EIP-2612 domain separator collision |
||||||
|
""" |
||||||
|
from typing import Union, List |
||||||
|
|
||||||
|
from slither.core.declarations import Function |
||||||
|
from slither.core.solidity_types.elementary_type import ElementaryType |
||||||
|
from slither.core.variables.state_variable import StateVariable |
||||||
|
from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification |
||||||
|
from slither.utils.function import get_function_id |
||||||
|
|
||||||
|
|
||||||
|
class DomainSeparatorCollision(AbstractDetector): |
||||||
|
""" |
||||||
|
Domain separator collision |
||||||
|
""" |
||||||
|
|
||||||
|
ARGUMENT = "domain-separator-collision" |
||||||
|
HELP = "Detects ERC20 tokens that have a function whose signature collides with EIP-2612's DOMAIN_SEPARATOR()" |
||||||
|
IMPACT = DetectorClassification.MEDIUM |
||||||
|
CONFIDENCE = DetectorClassification.HIGH |
||||||
|
|
||||||
|
WIKI = ( |
||||||
|
"https://github.com/crytic/slither/wiki/Detector-Documentation#domain-separator-collision" |
||||||
|
) |
||||||
|
|
||||||
|
WIKI_TITLE = "Domain separator collision" |
||||||
|
WIKI_DESCRIPTION = "An ERC20 token has a function whose signature collides with EIP-2612's DOMAIN_SEPARATOR(), causing unanticipated behavior for contracts using `permit` functionality." |
||||||
|
|
||||||
|
# region wiki_exploit_scenario |
||||||
|
WIKI_EXPLOIT_SCENARIO = """ |
||||||
|
```solidity |
||||||
|
contract Contract{ |
||||||
|
function some_collisions() external() {} |
||||||
|
} |
||||||
|
``` |
||||||
|
`some_collision` clashes with EIP-2612's DOMAIN_SEPARATOR() and will interfere with contract's using `permit`.""" |
||||||
|
# endregion wiki_exploit_scenario |
||||||
|
|
||||||
|
WIKI_RECOMMENDATION = "Remove or rename the function that collides with DOMAIN_SEPARATOR()." |
||||||
|
|
||||||
|
def _detect(self): |
||||||
|
domain_sig = get_function_id("DOMAIN_SEPARATOR()") |
||||||
|
for contract in self.compilation_unit.contracts_derived: |
||||||
|
if contract.is_erc20(): |
||||||
|
funcs_and_vars: List[Union[Function, StateVariable]] = contract.functions_entry_points + contract.state_variables_entry_points # type: ignore |
||||||
|
for func_or_var in funcs_and_vars: |
||||||
|
# External/ public function names should not collide with DOMAIN_SEPARATOR() |
||||||
|
hash_collision = ( |
||||||
|
func_or_var.solidity_signature != "DOMAIN_SEPARATOR()" |
||||||
|
and get_function_id(func_or_var.solidity_signature) == domain_sig |
||||||
|
) |
||||||
|
# DOMAIN_SEPARATOR() should return bytes32 |
||||||
|
incorrect_return_type = func_or_var.solidity_signature == "DOMAIN_SEPARATOR()" |
||||||
|
if incorrect_return_type: |
||||||
|
if isinstance(func_or_var, Function): |
||||||
|
incorrect_return_type = ( |
||||||
|
not func_or_var.return_type |
||||||
|
or func_or_var.return_type[0] != ElementaryType("bytes32") |
||||||
|
) |
||||||
|
else: |
||||||
|
assert isinstance(func_or_var, StateVariable) |
||||||
|
incorrect_return_type = func_or_var.type != ElementaryType("bytes32") |
||||||
|
if hash_collision or incorrect_return_type: |
||||||
|
info = [ |
||||||
|
"The function signature of ", |
||||||
|
func_or_var, |
||||||
|
" collides with DOMAIN_SEPARATOR and should be renamed or removed.\n", |
||||||
|
] |
||||||
|
res = self.generate_result(info) |
||||||
|
return [res] |
||||||
|
return [] |
@ -1,21 +1,23 @@ |
|||||||
|
from typing import List |
||||||
|
|
||||||
from slither.slithir.operations.lvalue import OperationWithLValue |
from slither.slithir.operations.lvalue import OperationWithLValue |
||||||
from slither.core.solidity_types.elementary_type import ElementaryType |
from slither.core.solidity_types.elementary_type import ElementaryType |
||||||
|
|
||||||
|
|
||||||
class TmpNewElementaryType(OperationWithLValue): |
class TmpNewElementaryType(OperationWithLValue): |
||||||
def __init__(self, new_type, lvalue): |
def __init__(self, new_type: ElementaryType, lvalue): |
||||||
assert isinstance(new_type, ElementaryType) |
assert isinstance(new_type, ElementaryType) |
||||||
super().__init__() |
super().__init__() |
||||||
self._type = new_type |
self._type: ElementaryType = new_type |
||||||
self._lvalue = lvalue |
self._lvalue = lvalue |
||||||
|
|
||||||
@property |
@property |
||||||
def read(self): |
def read(self) -> List: |
||||||
return [] |
return [] |
||||||
|
|
||||||
@property |
@property |
||||||
def type(self): |
def type(self) -> ElementaryType: |
||||||
return self._type |
return self._type |
||||||
|
|
||||||
def __str__(self): |
def __str__(self) -> str: |
||||||
return f"{self.lvalue} = new {self._type}" |
return f"{self.lvalue} = new {self._type}" |
||||||
|
@ -0,0 +1,3 @@ |
|||||||
|
# Slither doctor |
||||||
|
|
||||||
|
Slither doctor is a tool designed to troubleshoot running Slither on a project. |
@ -0,0 +1,37 @@ |
|||||||
|
import argparse |
||||||
|
|
||||||
|
from crytic_compile import cryticparser |
||||||
|
|
||||||
|
from slither.tools.doctor.utils import report_section |
||||||
|
from slither.tools.doctor.checks import ALL_CHECKS |
||||||
|
|
||||||
|
|
||||||
|
def parse_args() -> argparse.Namespace: |
||||||
|
""" |
||||||
|
Parse the underlying arguments for the program. |
||||||
|
:return: Returns the arguments for the program. |
||||||
|
""" |
||||||
|
parser = argparse.ArgumentParser( |
||||||
|
description="Troubleshoot running Slither on your project", |
||||||
|
usage="slither-doctor project", |
||||||
|
) |
||||||
|
|
||||||
|
parser.add_argument("project", help="The codebase to be tested.") |
||||||
|
|
||||||
|
# Add default arguments from crytic-compile |
||||||
|
cryticparser.init(parser) |
||||||
|
|
||||||
|
return parser.parse_args() |
||||||
|
|
||||||
|
|
||||||
|
def main(): |
||||||
|
args = parse_args() |
||||||
|
kwargs = vars(args) |
||||||
|
|
||||||
|
for check in ALL_CHECKS: |
||||||
|
with report_section(check.title): |
||||||
|
check.function(**kwargs) |
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__": |
||||||
|
main() |
@ -0,0 +1,18 @@ |
|||||||
|
from typing import Callable, List |
||||||
|
from dataclasses import dataclass |
||||||
|
|
||||||
|
from slither.tools.doctor.checks.platform import compile_project, detect_platform |
||||||
|
from slither.tools.doctor.checks.versions import show_versions |
||||||
|
|
||||||
|
|
||||||
|
@dataclass |
||||||
|
class Check: |
||||||
|
title: str |
||||||
|
function: Callable[..., None] |
||||||
|
|
||||||
|
|
||||||
|
ALL_CHECKS: List[Check] = [ |
||||||
|
Check("Software versions", show_versions), |
||||||
|
Check("Project platform", detect_platform), |
||||||
|
Check("Project compilation", compile_project), |
||||||
|
] |
@ -0,0 +1,59 @@ |
|||||||
|
import logging |
||||||
|
from pathlib import Path |
||||||
|
|
||||||
|
from crytic_compile import crytic_compile |
||||||
|
|
||||||
|
from slither.tools.doctor.utils import snip_section |
||||||
|
from slither.utils.colors import red, yellow, green |
||||||
|
|
||||||
|
|
||||||
|
def detect_platform(project: str, **kwargs) -> None: |
||||||
|
path = Path(project) |
||||||
|
if path.is_file(): |
||||||
|
print( |
||||||
|
yellow( |
||||||
|
f"{project!r} is a file. Using it as target will manually compile your code with solc and _not_ use a compilation framework. Is that what you meant to do?" |
||||||
|
) |
||||||
|
) |
||||||
|
return |
||||||
|
|
||||||
|
print(f"Trying to detect project type for {project!r}") |
||||||
|
|
||||||
|
supported_platforms = crytic_compile.get_platforms() |
||||||
|
skip_platforms = {"solc", "solc-json", "archive", "standard", "etherscan"} |
||||||
|
detected_platforms = { |
||||||
|
platform.NAME: platform.is_supported(project, **kwargs) |
||||||
|
for platform in supported_platforms |
||||||
|
if platform.NAME.lower() not in skip_platforms |
||||||
|
} |
||||||
|
platform_qty = len([platform for platform, state in detected_platforms.items() if state]) |
||||||
|
|
||||||
|
print("Is this project using...") |
||||||
|
for platform, state in detected_platforms.items(): |
||||||
|
print(f" => {platform + '?':<15}{state and green('Yes') or red('No')}") |
||||||
|
print() |
||||||
|
|
||||||
|
if platform_qty == 0: |
||||||
|
print(red("No platform was detected! This doesn't sound right.")) |
||||||
|
print( |
||||||
|
yellow( |
||||||
|
"Are you trying to analyze a folder with standalone solidity files, without using a compilation framework? If that's the case, then this is okay." |
||||||
|
) |
||||||
|
) |
||||||
|
elif platform_qty > 1: |
||||||
|
print(red("More than one platform was detected! This doesn't sound right.")) |
||||||
|
print( |
||||||
|
red("Please use `--compile-force-framework` in Slither to force the correct framework.") |
||||||
|
) |
||||||
|
else: |
||||||
|
print(green("A single platform was detected."), yellow("Is it the one you expected?")) |
||||||
|
|
||||||
|
|
||||||
|
def compile_project(project: str, **kwargs): |
||||||
|
print("Invoking crytic-compile on the project, please wait...") |
||||||
|
|
||||||
|
try: |
||||||
|
crytic_compile.CryticCompile(project, **kwargs) |
||||||
|
except Exception as e: # pylint: disable=broad-except |
||||||
|
with snip_section("Project compilation failed :( The following error was generated:"): |
||||||
|
logging.exception(e) |
@ -0,0 +1,59 @@ |
|||||||
|
from importlib import metadata |
||||||
|
import json |
||||||
|
from typing import Optional |
||||||
|
import urllib |
||||||
|
|
||||||
|
from packaging.version import parse, LegacyVersion, Version |
||||||
|
|
||||||
|
from slither.utils.colors import yellow, green |
||||||
|
|
||||||
|
|
||||||
|
def get_installed_version(name: str) -> Optional[LegacyVersion | Version]: |
||||||
|
try: |
||||||
|
return parse(metadata.version(name)) |
||||||
|
except metadata.PackageNotFoundError: |
||||||
|
return None |
||||||
|
|
||||||
|
|
||||||
|
def get_github_version(name: str) -> Optional[LegacyVersion | Version]: |
||||||
|
try: |
||||||
|
with urllib.request.urlopen( |
||||||
|
f"https://api.github.com/repos/crytic/{name}/releases/latest" |
||||||
|
) as response: |
||||||
|
text = response.read() |
||||||
|
data = json.loads(text) |
||||||
|
return parse(data["tag_name"]) |
||||||
|
except: # pylint: disable=bare-except |
||||||
|
return None |
||||||
|
|
||||||
|
|
||||||
|
def show_versions(**_kwargs) -> None: |
||||||
|
versions = { |
||||||
|
"Slither": (get_installed_version("slither-analyzer"), get_github_version("slither")), |
||||||
|
"crytic-compile": ( |
||||||
|
get_installed_version("crytic-compile"), |
||||||
|
get_github_version("crytic-compile"), |
||||||
|
), |
||||||
|
"solc-select": (get_installed_version("solc-select"), get_github_version("solc-select")), |
||||||
|
} |
||||||
|
|
||||||
|
outdated = { |
||||||
|
name |
||||||
|
for name, (installed, latest) in versions.items() |
||||||
|
if not installed or not latest or latest > installed |
||||||
|
} |
||||||
|
|
||||||
|
for name, (installed, latest) in versions.items(): |
||||||
|
color = yellow if name in outdated else green |
||||||
|
print(f"{name + ':':<16}{color(installed or 'N/A'):<16} (latest is {latest or 'Unknown'})") |
||||||
|
|
||||||
|
if len(outdated) > 0: |
||||||
|
print() |
||||||
|
print( |
||||||
|
yellow( |
||||||
|
f"Please update {', '.join(outdated)} to the latest release before creating a bug report." |
||||||
|
) |
||||||
|
) |
||||||
|
else: |
||||||
|
print() |
||||||
|
print(green("Your tools are up to date.")) |
@ -0,0 +1,28 @@ |
|||||||
|
from contextlib import contextmanager |
||||||
|
import logging |
||||||
|
from typing import Optional |
||||||
|
from slither.utils.colors import bold, yellow, red |
||||||
|
|
||||||
|
|
||||||
|
@contextmanager |
||||||
|
def snip_section(message: Optional[str]) -> None: |
||||||
|
if message: |
||||||
|
print(red(message), end="\n\n") |
||||||
|
|
||||||
|
print(yellow("---- snip 8< ----")) |
||||||
|
yield |
||||||
|
print(yellow("---- >8 snip ----")) |
||||||
|
|
||||||
|
|
||||||
|
@contextmanager |
||||||
|
def report_section(title: str) -> None: |
||||||
|
print(bold(f"## {title}"), end="\n\n") |
||||||
|
try: |
||||||
|
yield |
||||||
|
except Exception as e: # pylint: disable=broad-except |
||||||
|
with snip_section( |
||||||
|
"slither-doctor failed unexpectedly! Please report this on the Slither GitHub issue tracker, and include the output below:" |
||||||
|
): |
||||||
|
logging.exception(e) |
||||||
|
finally: |
||||||
|
print(end="\n\n") |
@ -1,28 +1,35 @@ |
|||||||
from decimal import Decimal |
from fractions import Fraction |
||||||
from typing import Union |
from typing import Union |
||||||
|
|
||||||
from slither.exceptions import SlitherError |
from slither.exceptions import SlitherError |
||||||
|
|
||||||
|
|
||||||
def convert_string_to_int(val: Union[str, int]) -> int: |
def convert_string_to_fraction(val: Union[str, int]) -> Fraction: |
||||||
if isinstance(val, int): |
if isinstance(val, int): |
||||||
return val |
return Fraction(val) |
||||||
if val.startswith(("0x", "0X")): |
if val.startswith(("0x", "0X")): |
||||||
return int(val, 16) |
return Fraction(int(val, 16)) |
||||||
|
|
||||||
|
# Fractions do not support underscore separators (on Python <3.11) |
||||||
|
val = val.replace("_", "") |
||||||
|
|
||||||
if "e" in val or "E" in val: |
if "e" in val or "E" in val: |
||||||
base, expo = val.split("e") if "e" in val else val.split("E") |
base, expo = val.split("e") if "e" in val else val.split("E") |
||||||
base, expo = Decimal(base), int(expo) |
base, expo = Fraction(base), int(expo) |
||||||
# The resulting number must be < 2**256-1, otherwise solc |
# The resulting number must be < 2**256-1, otherwise solc |
||||||
# Would not be able to compile it |
# Would not be able to compile it |
||||||
# 10**77 is the largest exponent that fits |
# 10**77 is the largest exponent that fits |
||||||
# See https://github.com/ethereum/solidity/blob/9e61f92bd4d19b430cb8cb26f1c7cf79f1dff380/libsolidity/ast/Types.cpp#L1281-L1290 |
# See https://github.com/ethereum/solidity/blob/9e61f92bd4d19b430cb8cb26f1c7cf79f1dff380/libsolidity/ast/Types.cpp#L1281-L1290 |
||||||
if expo > 77: |
if expo > 77: |
||||||
if base != Decimal(0): |
if base != Fraction(0): |
||||||
raise SlitherError( |
raise SlitherError( |
||||||
f"{base}e{expo} is too large to fit in any Solidity integer size" |
f"{base}e{expo} is too large to fit in any Solidity integer size" |
||||||
) |
) |
||||||
return 0 |
return 0 |
||||||
return int(Decimal(base) * Decimal(10**expo)) |
return Fraction(base) * Fraction(10**expo) |
||||||
|
|
||||||
|
return Fraction(val) |
||||||
|
|
||||||
return int(Decimal(val)) |
|
||||||
|
def convert_string_to_int(val: Union[str, int]) -> int: |
||||||
|
return int(convert_string_to_fraction(val)) |
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue