Esempio n. 1
0
def test_update_policy_break_matches(
    matches: List[Match],
    lines: List[Line],
    is_patch: bool,
    expected_matches: List[Match],
) -> None:
    update_policy_break_matches(matches, lines, is_patch=is_patch)

    for i, match in enumerate(expected_matches):
        assert match.index_start == matches[i].index_start
        assert match.line_start == matches[i].line_start
        assert match.index_end == matches[i].index_end
        assert match.line_end == matches[i].line_end
Esempio n. 2
0
    def process_result(self, result: Result) -> str:
        """
        Build readable message on the found policy breaks.

        :param result: The result from scanning API
        :param nb_lines: The number of lines to display before and after a secret in the
        patch
        :param show_secrets: Option to show secrets value
        :return: The formatted message to display
        """
        result_buf = StringIO()
        policy_breaks = result.scan.policy_breaks
        is_patch = result.filemode != Filemode.FILE
        sha_dict = leak_dictionary_by_ignore_sha(policy_breaks)

        if self.show_secrets:
            content = result.content
        else:
            content = censor_content(result.content, result.scan.policy_breaks)

        lines = get_lines_from_content(content, result.filemode, is_patch,
                                       self.show_secrets)
        padding = get_padding(lines)
        offset = get_offset(padding, is_patch)

        if len(lines) == 0:
            raise click.ClickException("Parsing of scan result failed.")

        result_buf.write(file_info(result.filename, len(sha_dict)))

        for issue_n, (ignore_sha,
                      policy_breaks) in enumerate(sha_dict.items(), 1):
            result_buf.write(
                policy_break_header(issue_n, policy_breaks, ignore_sha))
            for policy_break in policy_breaks:
                update_policy_break_matches(policy_break.matches, lines,
                                            is_patch)

            if policy_breaks[0].policy == "Secrets detection":
                result_buf.write(
                    leak_message_located(
                        flatten_policy_breaks_by_line(policy_breaks),
                        lines,
                        padding,
                        offset,
                        is_patch,
                        self.nb_lines,
                    ))

        return result_buf.getvalue()
Esempio n. 3
0
    def flattened_policy_break(
        self,
        ignore_sha: str,
        policy_breaks: List[PolicyBreak],
        lines: List[Line],
        is_patch: bool,
    ) -> Dict[str, Any]:
        flattened_dict: Dict[str, Any] = {
            "occurrences": [],
            "ignore_sha": ignore_sha,
            "policy": policy_breaks[0].policy,
            "break_type": policy_breaks[0].break_type,
            "total_occurrences": len(policy_breaks),
        }
        for policy_break in policy_breaks:
            update_policy_break_matches(policy_break.matches, lines, is_patch)
            flattened_dict["occurrences"].extend(policy_break.matches)

        return flattened_dict