Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate input from --markdown-root flag #988

Merged
merged 2 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions slither/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
read_config_file,
JSON_OUTPUT_TYPES,
DEFAULT_JSON_OUTPUT_TYPES,
check_and_sanitize_markdown_root,
)
from slither.exceptions import SlitherException

Expand Down Expand Up @@ -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="",
Expand Down
4 changes: 2 additions & 2 deletions slither/core/compilation_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion slither/core/slither_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions slither/utils/command_line.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import re
import logging
from collections import defaultdict
from crytic_compile.cryticparser.defaults import (
Expand Down Expand Up @@ -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