Secrets.pm

filter: Statementstmt 100% 184 / 184 Branchbran 100% 64 / 64 Conditioncond 98.5% 66 / 67 MC/DCmcdc 95.0% 38 / 40 Subroutinesub 100% 35 / 35 Podpod 100% 2 / 2 totaltotal 99.2% 389 / 392 CC 69 SCAR 16.5
CC
69
Cov
100%
_secret
9.0
_scan_rule
8.0
_scan
8.0
CRAP 5.2
↑ 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.1';
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 36 ▶sub _compile_allowlist ($allowlist) {
Subroutine _compile_allowlist: called CC=5 SCAR=16.1
138 return {
139 and => ($allowlist->{condition} // 'OR') eq 'AND',
140 target => $allowlist->{regex_target} // 'secret',
141 133 ▶ regexes => [map { _regex($_) } @{ $allowlist->{regexes} // [] }],
Condition100%2/2
$allowlist->{"regexes"} // []
l!l
342
MC/DC100%1/1
$allowlist->{"regexes"} // []
$allowlist->{"regexes"}
Truth table100%2/2
A
A= $allowlist->{"regexes"}
Aresult
11
00
142 36 ▶ stopwords => [map { lc } @{ $allowlist->{stopwords} // [] }],
Condition100%6/6
$allowlist->{"condition"} // "OR"
l!l
432
$allowlist->{"regex_target"} // "secret"
l!l
828
$allowlist->{"stopwords"} // []
l!l
1224
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->{"stopwords"} // []
$allowlist->{"stopwords"}
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->{"stopwords"}
Aresult
11
00
143 };
144 }
145  
146  
147 # Return one of gitleaks' regexes compiled as D3 says: with /a, as it's
148 # written. A literal { that isn't a quantifier is read by Perl as by Go's
149 # RE2, but Perl warns of it, so the warning is kept quiet.
150 842 ▶sub _regex ($source) {
Subroutine _regex: called CC=1 SCAR=0.0
151 4 ▶ no warnings qw(regexp); ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
152 842 return qr($source)a; ## no critic (RegularExpressions::RequireExtendedFormatting)
153 }
154  
155  
156 # Return the secrets the compiled rules find in the text, as gitleaks'
157 # detector finds them in a string: each rule whose keywords are in the
158 # text, or that has none, run over it, and the findings of a generic rule
159 # that a specific rule's finding on the same line contains dropped. Each
160 # is a hash of the rule's id, the secret and its offset, in the order the
161 # secrets appear, then the rules' order.
162 360 ▶sub _scan ($text, $compiled) {
Subroutine _scan: called CC=8 SCAR=20.8
163 360 my %present;
164 360 ▶ if (my $keywords = $compiled->{keywords}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $keywords = $compiled->{"keywords"}) 320 40 360
165 320 my $lower = lc $text;
166 320 ▶ while ($lower =~ m( $keywords )xg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lower =~ / $keywords /gux) 765 320 1085
167 765 $present{$_} = 1 for $1, @{ $compiled->{prefixes}{$1} };
168 }
169 }
170  
171 360 my @found;
172 360 for my $rule (@{ $compiled->{rules} }) {
173 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
174 681 push @found, _scan_rule($text, $rule, $compiled->{allowlists});
175 }
176 # By offset, and those at the same offset in the rules' order.
177 360 my %at;
178 360 push @{ $at{$_->{offset}} }, $_ for _drop_generic(@found);
179 360 return map { @{ $at{$_} } } sort { $a <=> $b } keys %at;
180 }
181  
182  
183 # Return the findings of one rule, as gitleaks' detectRule makes them.
184 681 ▶sub _scan_rule ($text, $rule, $globals) {
Subroutine _scan_rule: called CC=8 SCAR=20.8
185 681 my $regex = $rule->{regex};
186 681 my(@found, $newlines);
187 681 ▶ while ($text =~ m( $regex )xg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ / $regex /gux) 242 681 923
188 242 my $start = $-[0];
189 242 my $match = substr $text, $start, $+[0] - $start;
190  
191 # gitleaks trims newlines from the match, and its line is found
192 # from where the match starts, to where the match trimmed only of
193 # its trailing newlines would end.
194 242 my($leading) = $match =~ m( \A (\n*) )x;
195 242 $match =~ s( \A \n+ | \n+ \z )()xg;
196 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
197 242 my $line = _line($text, $start, $start + length $match, $newlines);
198 242 ▶ next if index($line, $ALLOW) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($line, $ALLOW) != -1 1 241 242
199  
200 241 ▶ my($secret, $offset) = _secret($match, $rule) or next;
Branch: 1 branch
BranchTrueFalseTotal
() = _secret($match, $rule) or next 240 1 241
201 240 $offset += $start + length $leading;
202 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
203  
204 172 my %finding = (secret => $secret, match => $match, line => $line);
205 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
206  
207 push @found, {
208 rule => $rule->{id},
209 68 secret => $secret,
210 offset => $offset,
211 line => _line_number($newlines, $start),
212 };
213 }
214 681 return @found;
215 }
216  
217  
218 # Return the secret in a match, and where it starts in the match, as D8
219 # says: gitleaks runs the rule's regex again, on the match alone, and
220 # takes the group the rule names, or else the first group that isn't
221 # empty, or else the whole match. Return nothing if the rule names a
222 # group its regex doesn't have, as gitleaks passes the match over.
223 241 ▶sub _secret ($match, $rule) {
Subroutine _secret: called CC=9 SCAR=22.0
224 241 my $regex = $rule->{regex};
225 241 my $matched = $match =~ m( $regex )x;
226 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
227 206 my $groups = $#+;
228 206 my $group = $rule->{secret_group};
229 206 ▶ if ($group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group) 6 200 206
230 6 ▶ return if $group > $groups;
Branch: 1 branch
BranchTrueFalseTotal
if $group > $groups 1 5 6
231 return
232 5 ▶ defined $-[$group]
Branch: 1 branch
BranchTrueFalseTotal
defined $-[$group] ? : 1 4 5
233 ? (substr($match, $-[$group], $+[$group] - $-[$group]), $-[$group])
234 : ('', 0);
235 }
236 200 for my $i (1 .. $groups) {
237 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
238 199 return substr($match, $-[$i], $+[$i] - $-[$i]), $-[$i];
239 }
240 1 return ($match, 0);
241 }
242  
243  
244 # Return where each newline in a text is.
245 248 ▶sub _newlines ($text) {
Subroutine _newlines: called CC=2 SCAR=6.9
246 248 my @newlines;
247 248 ▶ push @newlines, $-[0] while $text =~ m( \n )xg;
Branch: 1 branch
BranchTrueFalseTotal
$text =~ / \n /gux and do { push @newlines, $-[0] } 94 248 342
248 248 return \@newlines;
249 }
250  
251  
252 # Return how many newlines come before an offset, so the line it's on,
253 # counting from 0.
254 563 ▶sub _line_number ($newlines, $offset) {
Subroutine _line_number: called CC=3 SCAR=11.0
255 563 my($low, $high) = (0, scalar @$newlines);
256 563 ▶ while ($low < $high) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low < $high) 577 563 1140
257 577 my $middle = int(($low + $high) / 2);
258 577 ▶ if ($newlines->[$middle] < $offset) { $low = $middle + 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($newlines->[$middle] < $offset) { } 55 522 577
259 522 else { $high = $middle }
260 }
261 563 return $low;
262 }
263  
264  
265 # Return the line gitleaks checks a match's allowlists and
266 # gitleaks:allow against, as its location() finds it: from the newline
267 # before the match, or the start of the text, to the newline after it.
268 # A match after the last newline gets the text from its start.
269 # The newlines are where _newlines found them.
270 250 ▶sub _line ($text, $start, $end, $found = _newlines($text)) {
Subroutine _line: called CC=8 SCAR=20.8
271 250 ▶ my $newlines = @$found ? $found : [length $text];
Branch: 1 branch
BranchTrueFalseTotal
@$found ? : 48 202 250
272  
273 # The newline after the match's start, if there is one.
274 250 my $after = _line_number($newlines, $start + 1);
275 250 my($from, $to);
276 250 ▶ if ($after < @$newlines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after < @$newlines) { } 245 5 250
277 245 ▶ ($from, $to) = ($after ? $newlines->[$after - 1] : 0, $newlines->[$after]);
Branch: 1 branch
BranchTrueFalseTotal
$after ? : 14 231 245
278  
279 # The newline at or after the match's end, if it ends after the
280 # text's start.
281 245 my $ends = _line_number($newlines, $end);
282 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
283 }
284 else {
285 5 my($rest) = substr($text, $end) =~ m( \A ([^\r\n]*) )x;
286 5 ($from, $to) = (0, $end + length $rest);
287 }
288 250 ▶ $to = $end if $end > $to;
Branch: 1 branch
BranchTrueFalseTotal
if $end > $to 5 245 250
289 250 return substr $text, $from, $to - $from;
290 }
291  
292  
293 # Return the Shannon entropy of a string, in bits, over its characters,
294 # as gitleaks reckons it (D5).
295 185 ▶sub _entropy ($string) {
Subroutine _entropy: called CC=4 SCAR=13.9
296 185 ▶ my $length = length $string or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $length = length $string or return 0 184 1 185
297 184 my %count;
298 184 $count{substr $string, $_, 1}++ for 0 .. $length - 1;
299 184 my $entropy = 0;
300 184 for my $count (values %count) {
301 2732 my $frequency = $count / $length;
302 2732 $entropy -= $frequency * log2($frequency);
303 }
304 184 return $entropy;
305 }
306  
307  
308 # Return true if an allowlist passes over a finding, as gitleaks'
309 # checkFindingAllowed decides: its regexes are matched against what its
310 # target says, and its stopwords looked for in the secret, lowercased;
311 # under AND, both must, where it has them, and under OR, either.
312 318 ▶sub _allowed ($allowlist, $finding) {
Subroutine _allowed: called CC=7 SCAR=19.5
313 318 my $target = $finding->{$allowlist->{target}};
314 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
315 318 my $secret = lc $finding->{secret};
316 82470 ▶ my $stop = $secret ne '' && any { index($secret, $_) >= 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
317 318 ▶ @{ $allowlist->{stopwords} };
Condition100%3/3
$secret ne '' && &any(sub { index($secret, $_) != -1; } , @{$$allowlist{'stopwords'};})
!ll&&!rl&&r
130413
MC/DC100%2/2
$secret ne '' && &any(sub { index($secret, $_) != -1; } , @{$$allowlist{'stopwords'};})
$secret ne '' &any(sub { index($secret, $_) != -1; } , @{$$allowlist{'stopwords'};})
Truth table100%3/3
A && B
A= $secret ne ''B= &any(sub { index($secret, $_) != -1; } , @{$$allowlist{'stopwords'};})
ABresult
0X0
100
111
318 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
319  
320 # Under AND, each that it has, as gitleaks' allTrue.
321 4 my @checks;
322 4 ▶ push @checks, $regex if @{ $allowlist->{regexes} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$allowlist->{"regexes"};} 3 1 4
323 4 ▶ push @checks, $stop if @{ $allowlist->{stopwords} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$allowlist->{"stopwords"};} 3 1 4
324 4 ▶ return all { $_ } @checks;
Subroutine __ANON__: called CC=1 SCAR=0.0
325 }
326  
327  
328 # Return the findings but those of a generic rule that a specific rule's
329 # finding, starting on the same line, contains, as gitleaks' filter drops
330 # them.
331 360 ▶sub _drop_generic (@found) {
Subroutine _drop_generic: called CC=4 SCAR=13.9
332 360 my @kept;
333 360 for my $finding (@found) {
334 68 ▶ next if _generic($finding) && any { _supersedes($_, $finding) } @found;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if _generic($finding) and &any(sub { _supersedes($_, $finding); } , @found) 1 67 68
Condition100%3/3
_generic($finding) and &any(sub { _supersedes($_, $finding); } , @found)
!ll&&!rl&&r
50171
MC/DC100%2/2
_generic($finding) and &any(sub { _supersedes($_, $finding); } , @found)
_generic($finding) &any(sub { _supersedes($_, $finding); } , @found)
Truth table100%3/3
A and B
A= _generic($finding)B= &any(sub { _supersedes($_, $finding); } , @found)
ABresult
0X0
100
111
335 67 push @kept, $finding;
336 }
337 360 return @kept;
338 }
339  
340  
341 # Return true if a finding is by a generic rule.
342 72 ▶sub _generic ($finding) {
Subroutine _generic: called CC=1 SCAR=0.0
343 72 return index(lc $finding->{rule}, 'generic') >= 0;
344 }
345  
346  
347 # Return true if a specific rule's finding takes the place of a generic
348 # rule's: on the same line, by another rule, and holding its secret.
349 26 ▶sub _supersedes ($specific, $generic) {
Subroutine _supersedes: called CC=4 SCAR=13.9
350 return
351 $specific->{line} == $generic->{line}
352 && $specific->{rule} ne $generic->{rule}
353 && !_generic($specific)
354 26 ▶ && index($specific->{secret}, $generic->{secret}) >= 0;
Condition100%9/9
$specific->{'line'} == $generic->{'line'} && $specific->{'rule'} ne $generic->{'rule'} && !_generic($specific) && index($specific->{'secret'}, $generic->{'secret'}) != -1
!ll&&!rl&&r
2411
$specific->{"line"} == $generic->{"line"} and $specific->{"rule"} ne $generic->{"rule"} and not _generic($specific)
!ll&&!rl&&r
2222
$specific->{"line"} == $generic->{"line"} and $specific->{"rule"} ne $generic->{"rule"}
!ll&&!rl&&r
4184
MC/DC100%4/4
$specific->{'line'} == $generic->{'line'} && $specific->{'rule'} ne $generic->{'rule'} && !_generic($specific) && index($specific->{'secret'}, $generic->{'secret'}) != -1
$specific->{"line"} == $generic->{"line"} $specific->{"rule"} ne $generic->{"rule"} not _generic($specific) index($specific->{'secret'}, $generic->{'secret'}) != -1
Truth table100%5/5
A and B and not(C) && D
A= $specific->{"line"} == $generic->{"line"}B= $specific->{"rule"} ne $generic->{"rule"}C= not _generic($specific)D= index($specific->{'secret'}, $generic->{'secret'}) != -1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
355 }
356  
357 4 1;
358  
359 __END__
↑ Summary Secrets/Rules/Dist.pm »