Beispiel #1
0
def build_output_json(
    rule_matches: List[RuleMatch],
    semgrep_structured_errors: List[SemgrepError],
    all_targets: Set[Path],
    show_json_stats: bool,
    report_time: bool,
    filtered_rules: List[Rule],
    match_time_matrix: Dict[Tuple[str, str], float],
    profiler: Optional[ProfileManager] = None,
    debug_steps_by_rule: Optional[Dict[Rule, List[Dict[str, Any]]]] = None,
) -> str:
    output_json: Dict[str, Any] = {}
    output_json["results"] = [rm.to_json() for rm in rule_matches]
    if debug_steps_by_rule:
        output_json["debug"] = [
            {r.id: steps for r, steps in debug_steps_by_rule.items()}
        ]
    output_json["errors"] = [e.to_dict() for e in semgrep_structured_errors]
    if show_json_stats:
        output_json["stats"] = {
            "targets": make_target_stats(all_targets),
            "loc": make_loc_stats(all_targets),
            "profiler": profiler.dump_stats() if profiler else None,
        }
    if report_time:
        output_json["time"] = _build_time_json(
            filtered_rules, all_targets, match_time_matrix
        )
    return json.dumps(output_json)
Beispiel #2
0
    def build_output(
        self,
        color_output: bool,
        per_finding_max_lines_limit: Optional[int],
        per_line_max_chars_limit: Optional[int],
    ) -> str:
        output_format = self.settings.output_format

        extra: Dict[str, Any] = {}
        if self.settings.debug:
            extra["debug"] = [{
                rule.id: steps
                for rule, steps in self.debug_steps_by_rule.items()
            }]
        if self.settings.json_stats:
            extra["stats"] = {
                "targets": make_target_stats(self.all_targets),
                "loc": make_loc_stats(self.all_targets),
                "profiler":
                self.profiler.dump_stats() if self.profiler else None,
            }
        if self.settings.output_time:
            total_time = self.profiler.calls["total_time"][
                0] if self.profiler else -1.0
            extra["time"] = _build_time_json(self.filtered_rules,
                                             self.all_targets,
                                             self.profiling_data, total_time)

        structured_formatters: Dict[OutputFormat, Type[BaseFormatter]] = {
            OutputFormat.EMACS: EmacsFormatter,
            OutputFormat.JSON: JsonFormatter,
            OutputFormat.JUNIT_XML: JunitXmlFormatter,
            OutputFormat.SARIF: SarifFormatter,
            OutputFormat.VIM: VimFormatter,
        }
        formatter_type = structured_formatters.get(output_format)

        if formatter_type is not None:
            formatter = formatter_type(self.rules, self.rule_matches,
                                       self.semgrep_structured_errors, extra)
            return formatter.output()
        elif output_format == OutputFormat.TEXT:
            return "\n".join(
                list(
                    build_normal_output(
                        self.rule_matches,
                        color_output,
                        per_finding_max_lines_limit,
                        per_line_max_chars_limit,
                        self.all_targets,
                        self.settings.output_time,
                        self.filtered_rules,
                        self.profiling_data,
                    )))
        else:
            # https://github.com/python/mypy/issues/6366
            raise RuntimeError(
                f"Unhandled output format: {type(output_format).__name__}")
Beispiel #3
0
    def build_output(
        self,
        color_output: bool,
        per_finding_max_lines_limit: Optional[int],
        per_line_max_chars_limit: Optional[int],
    ) -> str:
        output_format = self.settings.output_format

        extra: Dict[str, Any] = {}
        if self.settings.debug:
            extra["debug"] = [{
                rule.id: steps
                for rule, steps in self.debug_steps_by_rule.items()
            }]
        if self.settings.json_stats:
            extra["stats"] = {
                "targets": make_target_stats(self.all_targets),
                "loc": make_loc_stats(self.all_targets),
                "profiler":
                self.profiler.dump_stats() if self.profiler else None,
            }
        if self.settings.output_time:
            extra["time"] = _build_time_json(
                self.filtered_rules,
                self.all_targets,
                self.profiling_data,
                self.profiler,
            )
        if output_format == OutputFormat.TEXT:
            extra["color_output"] = color_output
            extra["per_finding_max_lines_limit"] = per_finding_max_lines_limit
            extra["per_line_max_chars_limit"] = per_line_max_chars_limit

        formatters: Dict[OutputFormat, Type[BaseFormatter]] = {
            OutputFormat.EMACS: EmacsFormatter,
            OutputFormat.JSON: JsonFormatter,
            OutputFormat.JUNIT_XML: JunitXmlFormatter,
            OutputFormat.SARIF: SarifFormatter,
            OutputFormat.TEXT: TextFormatter,
            OutputFormat.VIM: VimFormatter,
        }
        formatter_type = formatters.get(output_format)

        if formatter_type is None:
            raise RuntimeError(f"Invalid output format: {output_format}")

        formatter = formatter_type(self.rules, self.rule_matches,
                                   self.semgrep_structured_errors, extra)
        return formatter.output()