def test_format_policy_break(): policy = PolicyBreak( "PayPal", "Secrets detection", "valid", [ Match("AZERTYUIOP", "client_id", line_start=123), Match("abcdefghijk", "client_secret", line_start=456), ], ) out = format_policy_break(policy) assert policy.break_type in out assert "Validity: Valid" in out for match in policy.matches: assert match.match_type in out # match value itself must be obfuscated assert match.match not in out
def test_do_not_add_policy_breaks_to_last_found(client): """ GIVEN 2 policy breaks on different files with the same ignore sha WHEN add_found_policy_break is called THEN only one element should be added """ policy_break = PolicyBreak("a", "gitignore", [Match("apikey", "apikey", 0, 0, 0, 0)]) cache = Cache() cache.add_found_policy_break(policy_break, "a") assert len(cache.last_found_secrets) == 0
def test_do_not_duplicate_last_found_secrets(client, isolated_fs): """ GIVEN 2 policy breaks on different files with the same ignore sha WHEN add_found_policy_break is called THEN only one element should be added """ policy_break = PolicyBreak( "a", "Secrets detection", None, [Match("apikey", "apikey", 0, 0, 0, 0)] ) cache = Cache() cache.add_found_policy_break(policy_break, "a") cache.add_found_policy_break(policy_break, "b") assert len(cache.last_found_secrets) == 1
def from_match( cls, match: Match, pre_line_start: Optional[int] = None, pre_line_end: Optional[int] = None, post_line_start: Optional[int] = None, post_line_end: Optional[int] = None, ) -> "ExtendedMatch": match_dict = match.to_dict() match_dict["match_type"] = match_dict["type"] return cls( pre_line_start=pre_line_start, pre_line_end=pre_line_end, post_line_start=post_line_start, post_line_end=post_line_end, **match_dict, )
def make_matches(matches: List[Match], lines: List[Line], is_patch: bool) -> List[Match]: res = [] for match in matches: if match.index_start is None or match.index_end is None: res.append(match) continue indices = find_match_indices(match, lines, is_patch) res.append( Match( match=match.match, match_type=match.match_type, index_start=indices.index_start, index_end=indices.index_end, line_start=indices.line_index_start, line_end=indices.line_index_end, )) return res
import pytest from pygitguardian.models import Match from ggshield.text_utils import Line, LineCategory from ggshield.utils import update_policy_break_matches @pytest.mark.parametrize( "matches,lines,is_patch,expected_matches", [ pytest.param( [ Match( "368ac3edf9e850d1c0ff9d6c526496f8237ddf91", "GitHub Token", index_start=22, index_end=62, ) ], [ Line(pre_index=1, content="GutHub:", category=LineCategory.data), Line( pre_index=2, content= "github_token: 368ac3edf9e850d1c0ff9d6c526496f8237ddf91", category=LineCategory.data, ), Line(pre_index=3, content="", category=LineCategory.data), ],