Exemplo n.º 1
0
    def _raise_semgrep_error_from_json(
        self, error_json: Dict[str, Any], patterns: List[Pattern],
    ) -> None:
        """
        See format_output_exception in semgrep O'Caml for details on schema
        """
        error_type = error_json["error"]
        if error_type == "invalid language":
            raise SemgrepError(
                f'{error_json["language"]} was accepted by semgrep but rejected by semgrep-core. {PLEASE_FILE_ISSUE_TEXT}'
            )
        elif error_type == "invalid pattern":

            matching_pattern = next(
                (p for p in patterns if p._id == error_json["pattern_id"]), None
            )
            if matching_pattern is None or matching_pattern.span is None:
                raise SemgrepError(
                    f"Pattern id from semgrep-core was missing in pattern spans. {PLEASE_FILE_ISSUE_TEXT}"
                )
            matching_span = matching_pattern.span

            raise InvalidPatternError(
                short_msg=error_type,
                long_msg=f"Pattern could not be parsed as a {error_json['language']} semgrep pattern",
                spans=[matching_span],
                help=None,
            )
        # no special formatting ought to be required for the other types; the semgrep python should be performing
        # validation for them. So if any other type of error occurs, ask the user to file an issue
        else:
            raise SemgrepError(
                f'an internal error occured while invoking semgrep-core:\n\t{error_type}: {error_json.get("message", "no message")}\n{PLEASE_FILE_ISSUE_TEXT}'
            )
Exemplo n.º 2
0
    def _raise_semgrep_error_from_json(
        self,
        error_json: Dict[str, Any],
        patterns: List[Pattern],
        rule: Rule,
    ) -> None:
        """
        See format_output_exception in semgrep O'Caml for details on schema
        """
        error_type = error_json["error"]
        if error_type == "invalid language":
            raise SemgrepError(
                f'{error_json["language"]} was accepted by semgrep but rejected by semgrep-core. {PLEASE_FILE_ISSUE_TEXT}'
            )
        elif error_type == "invalid regexp in rule":
            raise SemgrepError(
                f'Invalid regexp in rule: {error_json["message"]}')
        elif error_type == "invalid pattern":
            if self._optimizations == "all":
                raise InvalidPatternErrorNoSpan(
                    rule_id=error_json.get("pattern_id", "<no rule_id>"),
                    pattern=error_json.get("pattern", "<no pattern>"),
                    language=error_json.get("language", "<no language>"),
                )
            else:
                matching_pattern = next(
                    (p for p in patterns if p._id == error_json["pattern_id"]),
                    None)
                if matching_pattern is None or matching_pattern.span is None:
                    raise SemgrepError(
                        f"Pattern id from semgrep-core was missing in pattern spans. {PLEASE_FILE_ISSUE_TEXT}"
                    )
                matching_span = matching_pattern.span

                raise InvalidPatternError(
                    short_msg=error_type,
                    long_msg=
                    f"Pattern could not be parsed as a {error_json['language']} semgrep pattern",
                    spans=[matching_span],
                    help=None,
                )
        # no special formatting ought to be required for the other types; the semgrep python should be performing
        # validation for them. So if any other type of error occurs, ask the user to file an issue
        else:
            raise SemgrepError(
                f"an internal error occured while invoking semgrep-core while running rule '{rule.id}'. Consider skipping this rule and reporting this issue.\n\t{error_type}: {error_json.get('message', 'no message')}\n{PLEASE_FILE_ISSUE_TEXT}"
            )
Exemplo n.º 3
0
    def _raise_semgrep_error_from_json(
        self,
        error_json: Dict[str, Any],
        rule: Rule,
    ) -> None:
        """
        See format_output_exception in semgrep O'Caml for details on schema
        """
        error_type = error_json["error"]
        if error_type == "invalid language":
            raise SemgrepError(
                f'{error_json["language"]} was accepted by semgrep but rejected by semgrep-core. {PLEASE_FILE_ISSUE_TEXT}'
            )
        elif error_type == "invalid regexp in rule":
            raise SemgrepError(
                f'Invalid regexp in rule: {error_json["message"]}')
        elif error_type == "invalid pattern":
            range = error_json["range"]
            s = error_json.get("pattern", "<no pattern>")
            matching_span = Span.from_string_token(
                s=s,
                line=range.get("line", 0),
                col=range.get("col", 0),
                path=range.get("path", []),
                filename="semgrep temp file",
            )
            if error_json["message"] == "Parsing.Parse_error":
                long_msg = f"Pattern `{s.strip()}` could not be parsed as a {error_json['language']} semgrep pattern"
            else:
                long_msg = f"Error parsing {error_json['language']} pattern: {error_json['message']}"

            raise InvalidPatternError(
                short_msg=error_type,
                long_msg=long_msg,
                spans=[matching_span],
                help=None,
            )
        # no special formatting ought to be required for the other types; the semgrep python should be performing
        # validation for them. So if any other type of error occurs, ask the user to file an issue
        else:
            raise SemgrepError(
                f"an internal error occured while invoking semgrep-core while running rule '{rule.id}'. Consider skipping this rule and reporting this issue.\n\t{error_type}: {error_json.get('message', 'no message')}\n{PLEASE_FILE_ISSUE_TEXT}"
            )