Skip to content

Commit

Permalink
change issue map indexing algo
Browse files Browse the repository at this point in the history
  • Loading branch information
eshaan-deepsource committed Nov 2, 2023
1 parent 73ba761 commit b9aa2d2
Show file tree
Hide file tree
Showing 97 changed files with 145 additions and 125 deletions.
2 changes: 0 additions & 2 deletions analyzers/slither/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
__all__ = [
"ISSUE_MAP_FILE",
"ISSUE_PREFIX",
"ZEROES_PADDING_LENGTH",
"DEEPSOURCE_SEVERITY_WEIGHT_MAP",
"SLITHER_DETECTOR_CLASSIFICATION_DEEPSOURCE_SEVERITY_MAP",
"SLITHER_DETECTOR_CLASSIFICATION_DEEPSOURCE_CATEGORY_MAP",
Expand All @@ -17,7 +16,6 @@
# path to the `issue_map.json` located in the same directory as this script
ISSUE_MAP_FILE = os.path.join(os.path.dirname(__file__), "issue_map.json")
ISSUE_PREFIX = "SLITHER-W"
ZEROES_PADDING_LENGTH = 4


DEEPSOURCE_SEVERITY_WEIGHT_MAP = {
Expand Down
26 changes: 24 additions & 2 deletions analyzers/slither/utils/detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class DetectorJson(TypedDict):
# ref: https://github.com/crytic/slither/blob/master/slither/utils/command_line.py#L368
rule_id: str # custom param
index: int
check: str
title: str
Expand All @@ -21,6 +22,14 @@ class DetectorJson(TypedDict):
recommendation: str


def _gen_rule_id(detector_class: Type[AbstractDetector]) -> str:
# ref: https://github.com/crytic/slither/blob/master/slither/utils/output.py#L91-L97
impact = detector_class.IMPACT.value
confidence = detector_class.CONFIDENCE.value
check_name = detector_class.ARGUMENT
return f"{impact}-{confidence}-{check_name}"


def get_all_detector_classes() -> List[Type[AbstractDetector]]:
# ref: https://github.com/crytic/slither/blob/master/tests/utils.py#L7
detectors = [getattr(all_detectors, name) for name in dir(all_detectors)]
Expand All @@ -31,5 +40,18 @@ def get_all_detector_classes() -> List[Type[AbstractDetector]]:

def get_all_detector_json() -> List[Dict[str, DetectorJson]]:
# ref: https://github.com/crytic/slither/blob/master/slither/utils/command_line.py#L321
detector_classes = get_all_detector_classes()
return output_detectors_json(detector_classes)
detectors_classes = get_all_detector_classes()
detectors_jsons = output_detectors_json(detectors_classes)
detector_class2json_map = {
next(
(d for d in detectors_classes if d.ARGUMENT == detector_json["check"])
): detector_json
for detector_json in detectors_jsons
}
return [
{
**detector_json,
"rule_id": _gen_rule_id(detector_class),
}
for detector_class, detector_json in detector_class2json_map.items()
]
16 changes: 8 additions & 8 deletions analyzers/slither/utils/issue_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
SLITHER_DETECTOR_CLASSIFICATION_DEEPSOURCE_SEVERITY_MAP,
)
from detectors import get_all_detector_json
from issue_map_gen import _gen_issue_id, generate_mapping, get_issue_map
from issue_map_gen import generate_mapping, get_issue_map


def _get_toml_path(issue_code: str) -> str:
Expand All @@ -34,27 +34,27 @@ def get_toml_content(
recommendation_section = (
f"\n\n## Recommendation\n{recommendation}" if recommendation else ""
)
learn_more_section = f"\n\n## Learn more\n{learn_more} on Slither's wiki."

content = f'''title = "{title}"
severity = "{severity}"
category = "{category}"
weight = {weight}
description = """
{description}
<!--more-->{exploit_scenario_section}{recommendation_section}
## Learn more
{learn_more} on Slither's wiki.
<!--more-->{exploit_scenario_section}{recommendation_section}{learn_more_section}
"""
'''

return dedent(content)


def update_issue_tomls() -> None:
"""
Create issue toml files.
"""
# Generates mapping if doesn't exist
# Generates mapping if doesn't exist and then read it
generate_mapping()
mapping = get_issue_map()

Expand All @@ -63,8 +63,8 @@ def update_issue_tomls() -> None:
# For each Slither detector,
# create a DeepSource equivalent .toml issue file
for detector in detectors:
issue_id = _gen_issue_id(detector)
issue_code = mapping[issue_id]["issue_code"]
rule_id = detector["rule_id"]
issue_code = mapping[rule_id]["issue_code"]

filepath = _get_toml_path(issue_code)

Expand Down
Loading

0 comments on commit b9aa2d2

Please sign in to comment.