diff --git a/slither/__main__.py b/slither/__main__.py index 26aec783a0..eafa5a4c03 100644 --- a/slither/__main__.py +++ b/slither/__main__.py @@ -39,6 +39,7 @@ read_config_file, JSON_OUTPUT_TYPES, DEFAULT_JSON_OUTPUT_TYPES, + check_and_sanitize_markdown_root, ) from slither.exceptions import SlitherException @@ -429,6 +430,7 @@ def parse_args(detector_classes, printer_classes): # pylint: disable=too-many-s group_misc.add_argument( "--markdown-root", + type=check_and_sanitize_markdown_root, help="URL for markdown generation", action="store", default="", diff --git a/slither/core/compilation_unit.py b/slither/core/compilation_unit.py index 693eff821a..b1789ff1c5 100644 --- a/slither/core/compilation_unit.py +++ b/slither/core/compilation_unit.py @@ -100,12 +100,12 @@ def crytic_compile(self) -> CryticCompile: @property def pragma_directives(self) -> List[Pragma]: - """ list(core.declarations.Pragma): Pragma directives.""" + """list(core.declarations.Pragma): Pragma directives.""" return self._pragma_directives @property def import_directives(self) -> List[Import]: - """ list(core.declarations.Import): Import directives""" + """list(core.declarations.Import): Import directives""" return self._import_directives # endregion diff --git a/slither/core/slither_core.py b/slither/core/slither_core.py index 5bbe467cb3..dbc85276ba 100644 --- a/slither/core/slither_core.py +++ b/slither/core/slither_core.py @@ -123,7 +123,7 @@ def get_contract_from_name(self, contract_name: Union[str, Constant]) -> List[Co @property def source_code(self) -> Dict[str, str]: - """ {filename: source_code (str)}: source code """ + """{filename: source_code (str)}: source code""" return self._raw_source_code @property diff --git a/slither/utils/command_line.py b/slither/utils/command_line.py index d20ed04a83..1920d0fa0c 100644 --- a/slither/utils/command_line.py +++ b/slither/utils/command_line.py @@ -1,5 +1,6 @@ import json import os +import re import logging from collections import defaultdict from crytic_compile.cryticparser.defaults import ( @@ -332,3 +333,29 @@ def output_printers_json(printer_classes): table.append({"index": idx, "check": argument, "title": help_info}) idx = idx + 1 return table + + +def check_and_sanitize_markdown_root(markdown_root: str) -> str: + # Regex to check whether the markdown_root is a GitHub URL + match = re.search( + r"(https://)github.com/([a-zA-Z-]+)([:/][A-Za-z0-9_.-]+[:/]?)([A-Za-z0-9_.-]*)(.*)", + markdown_root, + ) + if match: + if markdown_root[-1] != "/": + logger.warning("Appending '/' in markdown_root url for better code referencing") + markdown_root = markdown_root + "/" + + if not match.group(4): + logger.warning( + "Appending 'master/tree/' in markdown_root url for better code referencing" + ) + markdown_root = markdown_root + "master/tree/" + elif match.group(4) == "tree": + logger.warning( + "Replacing 'tree' with 'blob' in markdown_root url for better code referencing" + ) + positions = match.span(4) + markdown_root = f"{markdown_root[:positions[0]]}blob{markdown_root[positions[1]:]}" + + return markdown_root