Secrets.pm

filter: Statementstmt 100% 188 / 188 Branchbran 100% 68 / 68 Conditioncond 98.4% 62 / 63 MC/DCmcdc 94.7% 36 / 38 Subroutinesub 100% 33 / 33 Podpod 100% 2 / 2 totaltotal 99.2% 389 / 392 CC 71 SCAR 17.2
CC
71
Cov
100%
_secret
9.0
_scan_rule
8.0
_scan
8.0
CRAP 5.6
↑ Summary Secrets/Rules/Dist.pm »
1 4 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Text::Redact::Secrets;
4  
5 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use List::Util qw(all any);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use POSIX qw(log2);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Text::Redact::Secrets::Rules::Dist;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Text::Redact::Secrets::Rules::Gitleaks;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = 'v1.0.2';
14  
15 4 our @EXPORT_OK = qw(find redact);
16  
17 4 my $REDACTED = '*REDACTED*';
18  
19 # A line holding this is passed over, as gitleaks passes it over.
20 4 my $ALLOW = 'gitleaks:allow';
21  
22 # Return a copy of the string with each secret found replaced by
23 # *REDACTED*. Secrets that overlap, found by two rules, are replaced once.
24 5 ▶sub redact ($string) {
Subroutine redact: called CC=1 SCAR=0.0
Pod documented
25 5 _check_string($string, 'redact');
26 3 return _redact($string, _rules());
27 }
28  
29  
30 # Return a copy of the string with each secret the compiled rules find
31 # replaced by *REDACTED*.
32 8 ▶sub _redact ($string, $compiled) {
Subroutine _redact: called CC=7 SCAR=19.5
33 8 my $redacted = $string;
34 8 my @spans;
35 8 for my $found (_scan($string, $compiled)) {
36 my($start, $end)
37 13 = ($found->{offset}, $found->{offset} + length $found->{secret});
38 13 ▶ next if $start == $end;
Branch: 1 branch
BranchTrueFalseTotal
if $start == $end 1 12 13
39 12 ▶ if (@spans && $start < $spans[-1][1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@spans and $start < $spans[-1][1]) { } 2 10 12
Condition100%3/3
@spans and $start < $spans[-1][1]
!ll&&!rl&&r
642
MC/DC100%2/2
@spans and $start < $spans[-1][1]
@spans $start < $spans[-1][1]
Truth table100%3/3
A and B
A= @spansB= $start < $spans[-1][1]
ABresult
0X0
100
111
40 2 ▶ $spans[-1][1] = $end if $end > $spans[-1][1];
Branch: 1 branch
BranchTrueFalseTotal
if $end > $spans[-1][1] 1 1 2
41 }
42 else {
43 10 push @spans, [$start, $end];
44 }
45 }
46 8 for my $span (reverse @spans) {
47 10 substr $redacted, $span->[0], $span->[1] - $span->[0], $REDACTED;
48 }
49 8 return $redacted;
50 }
51  
52  
53 # Return each secret found in the string, in the order it appears, as a
54 # hash of its rule's id, the secret and its offset.
55 315 ▶sub find ($string) {
Subroutine find: called CC=1 SCAR=0.0
Pod documented
56 315 _check_string($string, 'search');
57 return map {
58 313 +{rule => $_->{rule}, secret => $_->{secret}, offset => $_->{offset}}
59 23 } _scan($string, _rules());
60 }
61  
62  
63 # Croak, at the caller's line, unless what's given is a string.
64 320 ▶sub _check_string ($string, $verb) {
Subroutine _check_string: called CC=3 SCAR=11.0
65 320 ▶ croak "Expected a string to $verb" if !defined $string || ref $string;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $string or ref $string 4 316 320
Condition100%3/3
not defined $string or ref $string
l!l&&r!l&&!r
22316
MC/DC100%2/2
not defined $string or ref $string
not defined $string ref $string
Truth table100%3/3
not(A) or B
A= not defined $stringB= ref $string
ABresult
1X1
011
000
66 316 return;
67 }
68  
69  
70 # Return gitleaks' rules, then the module's own, and gitleaks' global
71 # allowlists, compiled the first time they're wanted.
72 316 ▶sub _rules () {
Subroutine _rules: called CC=1 SCAR=0.0
73 state $rules = _compile(
74 [
75 3 @{ Text::Redact::Secrets::Rules::Gitleaks::rules() },
76 316 @{ Text::Redact::Secrets::Rules::Dist::rules() },
77 ],
78 Text::Redact::Secrets::Rules::Gitleaks::global_allowlists(),
79 );
80 316 return $rules;
81 }
82  
83  
84 # Return rules and global allowlists, as
85 # Text::Redact::Secrets::Rules::Gitleaks gives them, compiled for _scan:
86 # each regex with /a, keywords and stopwords lowercased, as gitleaks does
87 # when it loads them.
88 47 ▶sub _compile ($rules, $globals) {
Subroutine _compile: called CC=5 SCAR=16.1
89 my @compiled = map {
90 47 +{
91 id => $_->{id},
92 regex => _regex($_->{regex}),
93 entropy => $_->{entropy} // 0,
94 secret_group => $_->{secret_group} // 0,
95 keywords => _keywords($_->{keywords}),
96 709 ▶ allowlists => [map { _compile_allowlist($_) } @{ $_->{allowlists} // [] }],
Condition100%6/6
$_->{"entropy"} // 0
l!l
388321
$_->{"secret_group"} // 0
l!l
9700
$_->{"allowlists"} // []
l!l
26683
MC/DC100%1/1
$_->{"entropy"} // 0
$_->{"entropy"}
MC/DC100%1/1
$_->{"secret_group"} // 0
$_->{"secret_group"}
MC/DC100%1/1
$_->{"allowlists"} // []
$_->{"allowlists"}
Truth table100%2/2
A
A= $_->{"entropy"}
Aresult
11
00
Truth table100%2/2
A
A= $_->{"secret_group"}
Aresult
11
00
Truth table100%2/2
A
A= $_->{"allowlists"}
Aresult
11
00
97 }
98 } @$rules;
99 return {
100 rules => \@compiled,
101 4 allowlists => [map { _compile_allowlist($_) } @$globals],
102 47 ▶ _compile_keywords(map { @{ $_->{keywords} // [] } } @compiled),
Condition100%2/2
$_->{"keywords"} // []
l!l
66049
MC/DC100%1/1
$_->{"keywords"} // []
$_->{"keywords"}
Truth table100%2/2
A
A= $_->{"keywords"}
Aresult
11
00
103 };
104 }
105  
106  
107 # Return a rule's keywords, lowercased, as gitleaks does when it loads
108 # them, or nothing if it has none, as a rule with none runs on every
109 # text.
110 709 ▶sub _keywords ($keywords) {
Subroutine _keywords: called CC=3 SCAR=11.0
111 709 ▶ return undef unless $keywords && @$keywords; ## no critic (Subroutines::ProhibitExplicitReturnUndef)
Branch: 1 branch
BranchTrueFalseTotal
unless $keywords and @$keywords 49 660 709
Condition100%3/3
$keywords and @$keywords
!ll&&!rl&&r
481660
MC/DC100%2/2
$keywords and @$keywords
$keywords @$keywords
Truth table100%3/3
A and B
A= $keywordsB= @$keywords
ABresult
0X0
100
111
112 660 return [map { lc } @$keywords];
113 }
114  
115  
116 # Return the regex that finds the keywords in a text, and the keywords
117 # each found stands for, as D4 says: one regex of every keyword, tried
118 # at every place in the text. Two keywords found at one place are a
119 # shorter and a longer, the shorter a prefix of the longer, and the
120 # regex, trying the longest first, finds only the longer, so each
121 # keyword stands for its prefixes too. So the keywords found are those
122 # in the text, as gitleaks finds them, overlapping ones too.
123 47 ▶sub _compile_keywords (@keywords) {
Subroutine _compile_keywords: called CC=4 SCAR=13.9
124 47 my %unique = map { $_ => 1 } @keywords;
125 47 my @longest = sort { length $b <=> length $a } sort keys %unique;
126 47 my %prefixes;
127 47 for my $keyword (@longest) {
128 $prefixes{$keyword}
129 732 ▶ = [grep { $_ ne $keyword && index($keyword, $_) == 0 } @longest];
Condition100%3/3
$_ ne $keyword && index($keyword, $_) == 0
!ll&&!rl&&r
73217493634
MC/DC100%2/2
$_ ne $keyword && index($keyword, $_) == 0
$_ ne $keyword index($keyword, $_) == 0
Truth table100%3/3
A && B
A= $_ ne $keywordB= index($keyword, $_) == 0
ABresult
0X0
100
111
130 }
131 47 ▶ return (keywords => undef) unless @longest;
Branch: 1 branch
BranchTrueFalseTotal
unless @longest 40 7 47
132 7 my $any = join '|', map { quotemeta } @longest;
133 7 return (keywords => qr( (?= ($any) ) )x, prefixes => \%prefixes);
134 }
135  
136  
137 # Return an allowlist compiled for _allowed: its regexes, and its
138 # stopwords, lowercased, as one regex that finds any of them, which Perl
139 # makes into one fast search, where looking for each in turn would cost
140 # as much as a rule.
141 36 ▶sub _compile_allowlist ($allowlist) {
Subroutine _compile_allowlist: called CC=6 SCAR=17.9
142 36 ▶ my @stopwords = map { lc } @{ $allowlist->{stopwords} // [] };
Condition100%2/2
$allowlist->{"stopwords"} // []
l!l
1224
MC/DC100%1/1
$allowlist->{"stopwords"} // []
$allowlist->{"stopwords"}
Truth table100%2/2
A
A= $allowlist->{"stopwords"}
Aresult
11
00
143 36 my $any = join '|', map { quotemeta } @stopwords;
144 return {
145 and => ($allowlist->{condition} // 'OR') eq 'AND',
146 target => $allowlist->{regex_target} // 'secret',
147 36 ▶ regexes => [map { _regex($_) } @{ $allowlist->{regexes} // [] }],
Branch: 1 branch
BranchTrueFalseTotal
@stopwords ? : 12 24 36
Condition100%6/6
$allowlist->{"condition"} // "OR"
l!l
432
$allowlist->{"regex_target"} // "secret"
l!l
828
$allowlist->{"regexes"} // []
l!l
342
MC/DC100%1/1
$allowlist->{"condition"} // "OR"
$allowlist->{"condition"}
MC/DC100%1/1
$allowlist->{"regex_target"} // "secret"
$allowlist->{"regex_target"}
MC/DC100%1/1
$allowlist->{"regexes"} // []
$allowlist->{"regexes"}
Truth table100%2/2
A
A= $allowlist->{"condition"}
Aresult
11
00
Truth table100%2/2
A
A= $allowlist->{"regex_target"}
Aresult
11
00
Truth table100%2/2
A
A= $allowlist->{"regexes"}
Aresult
11
00
148 stopwords => @stopwords ? qr( $any )x : undef,
149 };
150 }
151  
152  
153 # Return one of gitleaks' regexes compiled as D3 says: with /a, as it's
154 # written. A literal { that isn't a quantifier is read by Perl as by Go's
155 # RE2, but Perl warns of it, so the warning is kept quiet.
156 842 ▶sub _regex ($source) {
Subroutine _regex: called CC=1 SCAR=0.0
157 4 ▶ no warnings qw(regexp); ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
158 842 return qr($source)a; ## no critic (RegularExpressions::RequireExtendedFormatting)
159 }
160  
161  
162 # Return the secrets the compiled rules find in the text, as gitleaks'
163 # detector finds them in a string: each rule whose keywords are in the
164 # text, or that has none, run over it, and the findings of a generic rule
165 # that a specific rule's finding on the same line contains dropped. Each
166 # is a hash of the rule's id, the secret and its offset, in the order the
167 # secrets appear, then the rules' order.
168 360 ▶sub _scan ($text, $compiled) {
Subroutine _scan: called CC=8 SCAR=20.8
169 360 my %present;
170 360 ▶ if (my $keywords = $compiled->{keywords}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $keywords = $compiled->{"keywords"}) 320 40 360
171 320 my $lower = lc $text;
172 320 ▶ while ($lower =~ m( $keywords )xg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lower =~ / $keywords /gux) 765 320 1085
173 765 $present{$_} = 1 for $1, @{ $compiled->{prefixes}{$1} };
174 }
175 }
176  
177 360 my @found;
178 360 for my $rule (@{ $compiled->{rules} }) {
179 68943 ▶ next if $rule->{keywords} && !any { $present{$_} } @{ $rule->{keywords} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"keywords"} and not &any(sub { $present{$_}; } , @{$$rule{"keywords"};}) 68262 681 68943
Condition100%3/3
$rule->{"keywords"} and not &any(sub { $present{$_}; } , @{$$rule{"keywords"};})
!ll&&!rl&&r
4963268262
MC/DC100%2/2
$rule->{"keywords"} and not &any(sub { $present{$_}; } , @{$$rule{"keywords"};})
$rule->{"keywords"} not &any(sub { $present{$_}; } , @{$$rule{"keywords"};})
Truth table100%3/3
A and not(B)
A= $rule->{"keywords"}B= not &any(sub { $present{$_}; } , @{$$rule{"keywords"};})
ABresult
0X0
100
111
180 681 push @found, _scan_rule($text, $rule, $compiled->{allowlists});
181 }
182 # By offset, and those at the same offset in the rules' order.
183 360 my %at;
184 360 push @{ $at{$_->{offset}} }, $_ for _drop_generic(@found);
185 360 return map { @{ $at{$_} } } sort { $a <=> $b } keys %at;
186 }
187  
188  
189 # Return the findings of one rule, as gitleaks' detectRule makes them.
190 681 ▶sub _scan_rule ($text, $rule, $globals) {
Subroutine _scan_rule: called CC=8 SCAR=20.8
191 681 my $regex = $rule->{regex};
192 681 my(@found, $newlines);
193 681 ▶ while ($text =~ m( $regex )xg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ / $regex /gux) 242 681 923
194 242 my $start = $-[0];
195 242 my $match = substr $text, $start, $+[0] - $start;
196  
197 # gitleaks trims newlines from the match, and its line is found
198 # from where the match starts, to where the match trimmed only of
199 # its trailing newlines would end.
200 242 my($leading) = $match =~ m( \A (\n*) )x;
201 242 $match =~ s( \A \n+ | \n+ \z )()xg;
202 242 ▶ $newlines //= _newlines($text);
Condition66%2/3
$newlines //= _newlines($text)
l!l&&r!l&&!r
22400
MC/DC0%0/2
$newlines //= _newlines($text)
$newlines _newlines($text)
Truth table66%2/3
A //= B
A= $newlinesB= _newlines($text)
ABresult
1X1
011
000
203 242 my $line = _line($text, $start, $start + length $match, $newlines);
204 242 ▶ next if index($line, $ALLOW) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($line, $ALLOW) != -1 1 241 242
205  
206 241 ▶ my($secret, $offset) = _secret($match, $rule) or next;
Branch: 1 branch
BranchTrueFalseTotal
() = _secret($match, $rule) or next 240 1 241
207 240 $offset += $start + length $leading;
208 240 ▶ next if $rule->{entropy} && _entropy($secret) <= $rule->{entropy};
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"entropy"} and _entropy($secret) <= $rule->{"entropy"} 68 172 240
Condition100%3/3
$rule->{"entropy"} and _entropy($secret) <= $rule->{"entropy"}
!ll&&!rl&&r
5611668
MC/DC100%2/2
$rule->{"entropy"} and _entropy($secret) <= $rule->{"entropy"}
$rule->{"entropy"} _entropy($secret) <= $rule->{"entropy"}
Truth table100%3/3
A and B
A= $rule->{"entropy"}B= _entropy($secret) <= $rule->{"entropy"}
ABresult
0X0
100
111
209  
210 172 my %finding = (secret => $secret, match => $match, line => $line);
211 172 ▶ next if any { _allowed($_, \%finding) } @$globals, @{ $rule->{allowlists} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { _allowed($_, \%finding); } , @$globals, @{$$rule{"allowlists"};}) 104 68 172
212  
213 push @found, {
214 rule => $rule->{id},
215 68 secret => $secret,
216 offset => $offset,
217 line => _line_number($newlines, $start),
218 };
219 }
220 681 return @found;
221 }
222  
223  
224 # Return the secret in a match, and where it starts in the match, as D8
225 # says: gitleaks runs the rule's regex again, on the match alone, and
226 # takes the group the rule names, or else the first group that isn't
227 # empty, or else the whole match. Return nothing if the rule names a
228 # group its regex doesn't have, as gitleaks passes the match over.
229 241 ▶sub _secret ($match, $rule) {
Subroutine _secret: called CC=9 SCAR=22.0
230 241 my $regex = $rule->{regex};
231 241 my $matched = $match =~ m( $regex )x;
232 241 ▶ return ($match, 0) if !$matched || $#+ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $matched or $#+ < 1 35 206 241
Condition100%3/3
not $matched or $#+ < 1
l!l&&r!l&&!r
233206
MC/DC100%2/2
not $matched or $#+ < 1
not $matched $#+ < 1
Truth table100%3/3
not(A) or B
A= not $matchedB= $#+ < 1
ABresult
1X1
011
000
233 206 my $groups = $#+;
234 206 my $group = $rule->{secret_group};
235 206 ▶ if ($group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group) 6 200 206
236 6 ▶ return if $group > $groups;
Branch: 1 branch
BranchTrueFalseTotal
if $group > $groups 1 5 6
237 return
238 5 ▶ defined $-[$group]
Branch: 1 branch
BranchTrueFalseTotal
defined $-[$group] ? : 1 4 5
239 ? (substr($match, $-[$group], $+[$group] - $-[$group]), $-[$group])
240 : ('', 0);
241 }
242 200 for my $i (1 .. $groups) {
243 203 ▶ next if !defined $-[$i] || $+[$i] == $-[$i];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $-[$i] or $+[$i] == $-[$i] 4 199 203
Condition100%3/3
not defined $-[$i] or $+[$i] == $-[$i]
l!l&&r!l&&!r
13199
MC/DC100%2/2
not defined $-[$i] or $+[$i] == $-[$i]
not defined $-[$i] $+[$i] == $-[$i]
Truth table100%3/3
not(A) or B
A= not defined $-[$i]B= $+[$i] == $-[$i]
ABresult
1X1
011
000
244 199 return substr($match, $-[$i], $+[$i] - $-[$i]), $-[$i];
245 }
246 1 return ($match, 0);
247 }
248  
249  
250 # Return where each newline in a text is.
251 248 ▶sub _newlines ($text) {
Subroutine _newlines: called CC=2 SCAR=6.9
252 248 my @newlines;
253 248 ▶ push @newlines, $-[0] while $text =~ m( \n )xg;
Branch: 1 branch
BranchTrueFalseTotal
$text =~ / \n /gux and do { push @newlines, $-[0] } 94 248 342
254 248 return \@newlines;
255 }
256  
257  
258 # Return how many newlines come before an offset, so the line it's on,
259 # counting from 0.
260 563 ▶sub _line_number ($newlines, $offset) {
Subroutine _line_number: called CC=3 SCAR=11.0
261 563 my($low, $high) = (0, scalar @$newlines);
262 563 ▶ while ($low < $high) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low < $high) 577 563 1140
263 577 my $middle = int(($low + $high) / 2);
264 577 ▶ if ($newlines->[$middle] < $offset) { $low = $middle + 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($newlines->[$middle] < $offset) { } 55 522 577
265 522 else { $high = $middle }
266 }
267 563 return $low;
268 }
269  
270  
271 # Return the line gitleaks checks a match's allowlists and
272 # gitleaks:allow against, as its location() finds it: from the newline
273 # before the match, or the start of the text, to the newline after it.
274 # A match after the last newline gets the text from its start.
275 # The newlines are where _newlines found them.
276 250 ▶sub _line ($text, $start, $end, $found = _newlines($text)) {
Subroutine _line: called CC=8 SCAR=20.8
277 250 ▶ my $newlines = @$found ? $found : [length $text];
Branch: 1 branch
BranchTrueFalseTotal
@$found ? : 48 202 250
278  
279 # The newline after the match's start, if there is one.
280 250 my $after = _line_number($newlines, $start + 1);
281 250 my($from, $to);
282 250 ▶ if ($after < @$newlines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after < @$newlines) { } 245 5 250
283 245 ▶ ($from, $to) = ($after ? $newlines->[$after - 1] : 0, $newlines->[$after]);
Branch: 1 branch
BranchTrueFalseTotal
$after ? : 14 231 245
284  
285 # The newline at or after the match's end, if it ends after the
286 # text's start.
287 245 my $ends = _line_number($newlines, $end);
288 245 ▶ $to = $newlines->[$ends] if $ends < @$newlines && $end > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ends < @$newlines and $end > 0 239 6 245
Condition100%3/3
$ends < @$newlines and $end > 0
!ll&&!rl&&r
51239
MC/DC100%2/2
$ends < @$newlines and $end > 0
$ends < @$newlines $end > 0
Truth table100%3/3
A and B
A= $ends < @$newlinesB= $end > 0
ABresult
0X0
100
111
289 }
290 else {
291 5 my($rest) = substr($text, $end) =~ m( \A ([^\r\n]*) )x;
292 5 ($from, $to) = (0, $end + length $rest);
293 }
294 250 ▶ $to = $end if $end > $to;
Branch: 1 branch
BranchTrueFalseTotal
if $end > $to 5 245 250
295 250 return substr $text, $from, $to - $from;
296 }
297  
298  
299 # Return the Shannon entropy of a string, in bits, over its characters,
300 # as gitleaks reckons it (D5).
301 185 ▶sub _entropy ($string) {
Subroutine _entropy: called CC=4 SCAR=13.9
302 185 ▶ my $length = length $string or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $length = length $string or return 0 184 1 185
303 184 my %count;
304 184 $count{substr $string, $_, 1}++ for 0 .. $length - 1;
305 184 my $entropy = 0;
306 184 for my $count (values %count) {
307 2732 my $frequency = $count / $length;
308 2732 $entropy -= $frequency * log2($frequency);
309 }
310 184 return $entropy;
311 }
312  
313  
314 # Return true if an allowlist passes over a finding, as gitleaks'
315 # checkFindingAllowed decides: its regexes are matched against what its
316 # target says, and its stopwords looked for in the secret, lowercased;
317 # under AND, both must, where it has them, and under OR, either.
318 318 ▶sub _allowed ($allowlist, $finding) {
Subroutine _allowed: called CC=8 SCAR=20.8
319 318 my $target = $finding->{$allowlist->{target}};
320 318 ▶ my $regex = $target ne '' && any { $target =~ $_ } @{ $allowlist->{regexes} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
$target ne '' && &any(sub { $target =~ /$_/u; } , @{$$allowlist{'regexes'};})
!ll&&!rl&&r
122295
MC/DC100%2/2
$target ne '' && &any(sub { $target =~ /$_/u; } , @{$$allowlist{'regexes'};})
$target ne '' &any(sub { $target =~ /$_/u; } , @{$$allowlist{'regexes'};})
Truth table100%3/3
A && B
A= $target ne ''B= &any(sub { $target =~ /$_/u; } , @{$$allowlist{'regexes'};})
ABresult
0X0
100
111
321 318 my $secret = lc $finding->{secret};
322 my $stop
323 = $secret ne ''
324 && $allowlist->{stopwords}
325 318 ▶ && $secret =~ $allowlist->{stopwords};
Condition100%6/6
$secret ne '' && $allowlist->{'stopwords'} && $secret =~ /$allowlist->{'stopwords'}/u
!ll&&!rl&&r
12917613
$secret ne "" and $allowlist->{"stopwords"}
!ll&&!rl&&r
1128189
MC/DC100%3/3
$secret ne '' && $allowlist->{'stopwords'} && $secret =~ /$allowlist->{'stopwords'}/u
$secret ne "" $allowlist->{"stopwords"} $secret =~ /$allowlist->{'stopwords'}/u
Truth table100%4/4
A and B && C
A= $secret ne ""B= $allowlist->{"stopwords"}C= $secret =~ /$allowlist->{'stopwords'}/u
ABCresult
0XX0
10X0
1100
1111
326 318 ▶ return $regex || $stop unless $allowlist->{and};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowlist->{"and"} 314 4 318
Condition100%3/3
$regex || $stop
l!l&&r!l&&!r
929213
MC/DC100%2/2
$regex || $stop
$regex $stop
Truth table100%3/3
A || B
A= $regexB= $stop
ABresult
1X1
011
000
327  
328 # Under AND, each that it has, as gitleaks' allTrue.
329 4 my @checks;
330 4 ▶ push @checks, $regex if @{ $allowlist->{regexes} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$allowlist->{"regexes"};} 3 1 4
331 4 ▶ push @checks, $stop if $allowlist->{stopwords};
Branch: 1 branch
BranchTrueFalseTotal
if $allowlist->{"stopwords"} 3 1 4
332 4 ▶ return all { $_ } @checks;
Subroutine __ANON__: called CC=1 SCAR=0.0
333 }
334  
335  
336 # Return the findings but those of a generic rule that a specific rule's
337 # finding, starting on the same line, contains, as gitleaks' filter drops
338 # them. The specific rules' findings are looked up by their line, so that
339 # each generic finding is checked against only those on its own line.
340 360 ▶sub _drop_generic (@found) {
Subroutine _drop_generic: called CC=7 SCAR=19.5
341 360 my %specific;
342 360 for my $finding (@found) {
343 68 ▶ push @{ $specific{$finding->{line}} }, $finding unless _generic($finding);
Branch: 1 branch
BranchTrueFalseTotal
unless _generic($finding) 50 18 68
344 }
345 360 my @kept;
346 360 for my $finding (@found) {
347 next
348 if _generic($finding)
349 2 ▶ && any { index($_->{secret}, $finding->{secret}) >= 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
350 68 ▶ @{ $specific{$finding->{line}} // [] };
Branch: 1 branch
BranchTrueFalseTotal
if _generic($finding) and &any(sub { index($_->{"secret"}, $finding->{"secret"}) != -1; } , @{$specific{$finding->{"line"}} // [];}) 1 67 68
Condition100%5/5
_generic($finding) and &any(sub { index($_->{"secret"}, $finding->{"secret"}) != -1; } , @{$specific{$finding->{"line"}} // [];})
!ll&&!rl&&r
50171
$specific{$finding->{"line"}} // []
l!l
216
MC/DC100%2/2
_generic($finding) and &any(sub { index($_->{"secret"}, $finding->{"secret"}) != -1; } , @{$specific{$finding->{"line"}} // [];})
_generic($finding) &any(sub { index($_->{"secret"}, $finding->{"secret"}) != -1; } , @{$specific{$finding->{"line"}} // [];})
MC/DC100%1/1
$specific{$finding->{"line"}} // []
$specific{$finding->{"line"}}
Truth table100%3/3
A and B
A= _generic($finding)B= &any(sub { index($_->{"secret"}, $finding->{"secret"}) != -1; } , @{$specific{$finding->{"line"}} // [];})
ABresult
0X0
100
111
Truth table100%2/2
A
A= $specific{$finding->{"line"}}
Aresult
11
00
351 67 push @kept, $finding;
352 }
353 360 return @kept;
354 }
355  
356  
357 # Return true if a finding is by a generic rule.
358 136 ▶sub _generic ($finding) {
Subroutine _generic: called CC=1 SCAR=0.0
359 136 return index(lc $finding->{rule}, 'generic') >= 0;
360 }
361  
362  
363 4 1;
364  
365 __END__
↑ Summary Secrets/Rules/Dist.pm »