blib/lib/Robots/Validate.pm

filter: Statementstmt 90.9% 280 / 308 Branchbran 79.0% 98 / 124 Conditioncond 66.6% 56 / 84 MC/DCmcdc 39.2% 20 / 51 Subroutinesub 88.5% 62 / 70 Podpod 100% 2 / 2 totaltotal 81.0% 518 / 639 CC 106 SCAR 19.8
CC
106
Cov
84%
_add_rule
16.5
_strict_revalidate
14.1
validate
12.9
CRAP 7.3
↑ Summary
1 package Robots::Validate;
2  
3 # ABSTRACT: Validate that IP addresses are associated with known robots
4  
5 13 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Moo 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶use Algorithm::AhoCorasick::SearchMachine;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use File::ShareDir qw( dist_file );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use File::Slurper qw( read_binary );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use List::Util 1.33 qw( all any );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Module::Load qw( load );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Net::DNS::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Net::IP qw( ip_expand_address ip_is_ipv4 ip_is_ipv6 ip_splitprefix );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Net::IP::LPM;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use PerlX::Maybe qw( maybe );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use Ref::Util qw( is_plain_arrayref is_plain_hashref is_regexpref );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use Scalar::Util 1.18 qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use Sub::Util 1.40 qw( set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 ▶use TOML::Tiny 0.20 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 13 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 13 ▶use Types::Common qw( ArrayRef Bool ConsumerOf Enum HashRef InstanceOf Maybe PositiveInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # RECOMMEND PREREQ: CHI 0.40
26 # RECOMMEND PREREQ: Ref::Util::XS
27 # RECOMMEND PREREQ: Type::Tiny::XS
28  
29 13 ▶use experimental qw( lexical_subs signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 13 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 13 our $VERSION = 'v0.4.16';
34  
35  
36 13 has resolver => (
37 is => 'bare',
38 isa => InstanceOf ['Net::DNS::Resolver'],
39 builder => 1,
40 handles => {
41 _dns_query => 'query',
42 _dns_search => 'search',
43 },
44 );
45  
46 4 ▶sub _build_resolver($self) {
Subroutine _build_resolver: called CC=1 SCAR=0.0
47 4 return Net::DNS::Resolver->new;
48 }
49  
50  
51 13 has max_forward_lookups => (
52 is => 'ro',
53 isa => PositiveInt,
54 default => 4,
55 );
56  
57  
58 13 has networks => (
59 is => 'bare',
60 isa => InstanceOf ['Net::IP::LPM'],
61 builder => 1,
62 handles => {
63 _add_string => 'add',
64 _match_string => 'lookup',
65 },
66 );
67  
68 39 ▶sub _build_networks($self) {
Subroutine _build_networks: called CC=1 SCAR=0.0
69 39 return Net::IP::LPM->new;
70 }
71  
72 has _validators => (
73 is => 'ro',
74 isa => HashRef,
75 init_arg => undef,
76 31 ▶ builder => sub($self) { return {} },
Subroutine _build__validators: called CC=1 SCAR=0.0
77 13 );
78  
79 13 has _agents => (
80 is => 'lazy',
81 isa => InstanceOf [qw/ Algorithm::AhoCorasick::SearchMachine Algorithm::AhoCorasick::XS /],
82 init_arg => undef,
83 builder => \&_build_agents,
84 );
85  
86 34 ▶sub _build_agents($self) {
Subroutine _build__agents: called CC=2 SCAR=7.5
87 # We need to ensure the validators are initialised with the rules
88 34 $self->_init_validators_from_config;
89  
90 34 my @names = keys $self->_validators->%*;
91  
92  
93 34 ▶ if ( eval { load "Algorithm::AhoCorasick::XS" } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { &load('Algorithm::AhoCorasick::XS') } }) { } 0 34 34
94  
95 0 ▶ *_first_match = set_subname "_first_match", sub( $self, $str ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 return $self->_agents->first_match($str)
97 0 };
98  
99 0 ▶ *_all_matches = set_subname "_all_matches", sub( $self, $str ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 return $self->_agents->matches($str)
101 0 };
102  
103 0 return Algorithm::AhoCorasick::XS->new(\@names);
104  
105 }
106 else {
107  
108 52 ▶ *_reset_feed = set_subname "_reset_feed", sub( $self, $str, $cb ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _reset_feed: not called CC=1 SCAR=0.0
109 52 my $machine = $self->_agents;
110 52 $machine->{state} = $machine->{root};
111 0 $machine->feed( $str, $cb );
112 34 };
113  
114 11 ▶ *_first_match = set_subname "_first_match", sub( $self, $str ) {
Subroutine __ANON__: called CC=1 SCAR=0.8
115 0 my $match;
116 0 ▶ $self->_reset_feed( $str, sub( $, $name ) { $match //= $name } );
Subroutine __ANON__: called CC=2 SCAR=9.2
Condition33%1/3
$match //= $name
l!l&&r!l&&!r
070
MC/DC0%0/2
$match //= $name
$match $name
Truth table33%1/3
A //= B
A= $matchB= $name
ABresult
1X1
011
000
117 11 return $match;
118 34 };
119  
120 100 ▶ *_all_matches = set_subname "_all_matches", sub( $self, $str ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 100 my @matches;
122 100 ▶ $self->_reset_feed( $str, sub( $, $name ) { push @matches, $name; return undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
123 100 return @matches;
124 34 };
125  
126 34 return Algorithm::AhoCorasick::SearchMachine->new(@names);
127  
128 }
129  
130  
131 }
132  
133  
134 has config => (
135 is => 'lazy',
136 isa => ArrayRef [HashRef],
137 39 ▶ coerce => sub($ref) {
Subroutine __ANON__: called CC=4 SCAR=13.9
138  
139 39 ▶ return $ref if is_plain_arrayref($ref);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_plain_arrayref($ref) 34 5 39
140  
141 5 ▶ unless ( is_plain_hashref($ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Ref::Util::XS::is_plain_hashref($ref)) 4 1 5
142 4 my $toml = read_binary("$ref");
143 4 $ref = _from_toml($toml);
144 }
145  
146 5 ▶ if ( is_plain_hashref($ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_hashref($ref)) 5 0 5
147  
148 532 ▶ state sub _normalise( $key, $val ) {
Subroutine _normalise: called CC=5 SCAR=16.1
149 532 my %item = $val->%*;
150 532 ▶ $item{name} //= $key;
Condition66%2/3
$item{"name"} //= $key
l!l&&r!l&&!r
25300
MC/DC0%0/2
$item{"name"} //= $key
$item{"name"} $key
Truth table66%2/3
A //= B
A= $item{"name"}B= $key
ABresult
1X1
011
000
151 532 for my $key (qw/ agents network /) {
152 1064 ▶ $item{$key} = [ $item{$key} ] unless !exists $item{$key} || is_plain_arrayref( $item{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless not exists $item{$key} or Ref::Util::XS::is_plain_arrayref($item{$key}) 484 580 1064
Condition100%3/3
not exists $item{$key} or Ref::Util::XS::is_plain_arrayref($item{$key})
l!l&&r!l&&!r
135445484
MC/DC100%2/2
not exists $item{$key} or Ref::Util::XS::is_plain_arrayref($item{$key})
not exists $item{$key} Ref::Util::XS::is_plain_arrayref($item{$key})
Truth table100%3/3
not(A) or B
A= not exists $item{$key}B= Ref::Util::XS::is_plain_arrayref($item{$key})
ABresult
1X1
011
000
153 }
154 532 return \%item;
155 }
156  
157 5 return [ map { _normalise( $_ => $ref->{$_} ) } sort keys $ref->%* ];
158 }
159  
160 0 return $ref;
161 },
162 3 ▶ builder => sub($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
163 3 return dist_file( __PACKAGE__ =~ s/::/-/gr, 'robots.toml' );
164 }
165 13 );
166  
167  
168 has index => (
169 is => 'ro',
170 isa => HashRef,
171 init_arg => undef,
172 17 ▶ builder => sub($self) { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_index: called CC=1 SCAR=0.0
173 13 );
174  
175  
176  
177 13 has locked => (
178 is => 'rwp',
179 isa => Bool,
180 init_arg => undef,
181 );
182  
183  
184 13 has cache => (
185 is => 'ro',
186 isa => ConsumerOf ['CHI::Driver::Role::Universal'],
187 predicate => 1,
188 );
189  
190  
191 has cache_options => (
192 is => 'lazy',
193 isa => Maybe [HashRef],
194 3 ▶ coerce => sub($val) {
Subroutine __ANON__: called CC=3 SCAR=11.7
195 3 ▶ return $val if is_plain_hashref($val);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_plain_hashref($val) 2 1 3
196 1 ▶ return { expires_in => "$val" } if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 1 0 1
197 0 return undef;
198 },
199 13 builder => 1,
200 );
201  
202 1 ▶sub _build_cache_options($) {
Subroutine _build_cache_options: called CC=1 SCAR=0.0
203 1 return {};
204 }
205  
206  
207 13 has validation_mode => (
208 is => 'ro',
209 isa => Enum [qw/ first relaxed strict /],
210 default => 'relaxed',
211 );
212  
213  
214 13 has max_matches => (
215 is => 'ro',
216 isa => PositiveInt,
217 default => 4,
218 );
219  
220  
221 126 ▶sub validate( $self, $ip, $agent = undef, $opts = undef ) {
Subroutine validate: called CC=12 SCAR=25.5
Pod documented
222  
223 126 ▶ if ( is_plain_hashref($agent) && !$opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_hashref($agent) and not $opts) 11 115 126
Condition66%2/3
Ref::Util::XS::is_plain_hashref($agent) and not $opts
!ll&&!rl&&r
115011
MC/DC50%1/2
Ref::Util::XS::is_plain_hashref($agent) and not $opts
Ref::Util::XS::is_plain_hashref($agent) not $opts
Truth table66%2/3
A and not(B)
A= Ref::Util::XS::is_plain_hashref($agent)B= not $opts
ABresult
0X0
100
111
224 11 ( $agent, $opts ) = ( $opts, $agent );
225 11 ▶ $agent //= $opts->{agent}; # DEPRECATED
Condition33%1/3
$agent //= $opts->{"agent"}
l!l&&r!l&&!r
0110
MC/DC0%0/2
$agent //= $opts->{"agent"}
$agent $opts->{"agent"}
Truth table33%1/3
A //= B
A= $agentB= $opts->{"agent"}
ABresult
1X1
011
000
226 }
227  
228 126 ▶ if ( is_plain_hashref($ip) && !$agent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_hashref($ip) and not $agent) 9 117 126
Condition66%2/3
Ref::Util::XS::is_plain_hashref($ip) and not $agent
!ll&&!rl&&r
11709
MC/DC50%1/2
Ref::Util::XS::is_plain_hashref($ip) and not $agent
Ref::Util::XS::is_plain_hashref($ip) not $agent
Truth table66%2/3
A and not(B)
A= Ref::Util::XS::is_plain_hashref($ip)B= not $agent
ABresult
0X0
100
111
229 9 $agent = $ip->{HTTP_USER_AGENT};
230 9 $ip = $ip->{REMOTE_ADDR};
231 }
232  
233 126 ▶ if ( !$opts->{no_cache} && $self->has_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts->{"no_cache"} and $self->has_cache) 12 114 126
Condition66%2/3
not $opts->{"no_cache"} and $self->has_cache
!ll&&!rl&&r
011412
MC/DC50%1/2
not $opts->{"no_cache"} and $self->has_cache
not $opts->{"no_cache"} $self->has_cache
Truth table66%2/3
not(A) and B
A= not $opts->{"no_cache"}B= $self->has_cache
ABresult
0X0
100
111
234 12 ▶ return $self->_cache_compute( $ip, $agent, $opts // { } );
Condition50%1/2
$opts // {}
l!l
120
MC/DC0%0/1
$opts // {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
235 }
236  
237 114 ▶ return $self->_revalidate( $ip, $agent // "" );
Condition100%2/2
$agent // ""
l!l
10212
MC/DC100%1/1
$agent // ""
$agent
Truth table100%2/2
A
A= $agent
Aresult
11
00
238 }
239  
240 # Note: we implement our own _cache_compute method rather than use the CHI compute method so that we can modify the
241 # caching options based on the value.
242  
243 13 ▶use constant _GET_OPTS => ( qw/ expire_if busy_lock / );
Subroutine BEGIN: called CC=1 SCAR=0.0
244  
245 12 ▶sub _cache_compute( $self, $ip, $agent, $opts ) {
Subroutine _cache_compute: called CC=11 SCAR=24.1
246  
247 12 ▶ my $key = join( $;, $ip, $agent // '' );
Condition50%1/2
$agent // ""
l!l
120
MC/DC0%0/1
$agent // ""
$agent
Truth table50%1/2
A
A= $agent
Aresult
11
00
248  
249 12 my $cache = $self->cache;
250  
251 12 my %set_opts = $self->cache_options->%*;
252 12 ▶ my %get_opts = map { maybe $_ => delete $set_opts{$_} // undef } _GET_OPTS;
Condition50%1/2
delete $set_opts{$_} // undef
l!l
024
MC/DC0%0/1
delete $set_opts{$_} // undef
delete $set_opts{$_}
Truth table50%1/2
A
A= delete $set_opts{$_}
Aresult
11
00
253  
254 12 ▶ $opts->{cache_failure} //= delete $set_opts{cache_failure};
Condition100%3/3
$opts->{"cache_failure"} //= delete $set_opts{"cache_failure"}
l!l&&r!l&&!r
147
MC/DC100%2/2
$opts->{"cache_failure"} //= delete $set_opts{"cache_failure"}
$opts->{"cache_failure"} delete $set_opts{"cache_failure"}
Truth table100%3/3
A //= B
A= $opts->{"cache_failure"}B= delete $set_opts{"cache_failure"}
ABresult
1X1
011
000
255  
256 12 my $value = $cache->get( $key, %get_opts );
257 12 ▶ unless ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 9 3 12
258  
259 9 ▶ $value = $self->_revalidate( $ip, $agent // "" );
Condition50%1/2
$agent // ""
l!l
90
MC/DC0%0/1
$agent // ""
$agent
Truth table50%1/2
A
A= $agent
Aresult
11
00
260 9 ▶ if ( $value || $opts->{cache_failure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value or $opts->{"cache_failure"}) 6 3 9
Condition100%3/3
$value or $opts->{"cache_failure"}
l!l&&r!l&&!r
333
MC/DC100%2/2
$value or $opts->{"cache_failure"}
$value $opts->{"cache_failure"}
Truth table100%3/3
A or B
A= $valueB= $opts->{"cache_failure"}
ABresult
1X1
011
000
261 $set_opts{expires_in} = $opts->{cache_failure}
262 6 ▶ if !$value && $opts->{cache_failure} && $opts->{cache_failure} ne "1";
Branch: 1 branch
BranchTrueFalseTotal
if not $value and $opts->{"cache_failure"} and $opts->{"cache_failure"} ne 1 2 4 6
Condition83%5/6
not $value and $opts->{"cache_failure"} and $opts->{"cache_failure"} ne 1
!ll&&!rl&&r
312
not $value and $opts->{"cache_failure"}
!ll&&!rl&&r
303
MC/DC66%2/3
not $value and $opts->{"cache_failure"} and $opts->{"cache_failure"} ne 1
not $value $opts->{"cache_failure"} $opts->{"cache_failure"} ne 1
Truth table75%3/4
not(A) and B and C
A= not $valueB= $opts->{"cache_failure"}C= $opts->{"cache_failure"} ne 1
ABCresult
0XX0
10X0
1100
1111
263 6 $cache->set( $key, $value, \%set_opts );
264 }
265  
266 }
267  
268 12 return $value;
269 }
270  
271 124 ▶sub _revalidate( $self, @args ) {
Subroutine _revalidate: called CC=2 SCAR=7.1
272 124 my $mode = $self->validation_mode;
273 124 ▶ my $method = $self->can("_${mode}_revalidate") or die "Unsupported mode: '${mode}'";
Branch: 1 branch
BranchTrueFalseTotal
my $method = $self->can("_${mode}_revalidate") or die "Unsupported mode: '${mode}'" 124 0 124
274 124 return $self->$method(@args);
275 }
276  
277 11 ▶sub _first_revalidate( $self, $ip, $agent ) {
Subroutine _first_revalidate: called CC=8 SCAR=24.4
278  
279 11 ▶ if ( $agent ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agent ne '') { } 11 0 11
280  
281 11 $self->_agents; # ensure agents are instantiated
282  
283 11 ▶ if ( my $str = $self->_first_match( lc $agent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $str = $self->_first_match(lc $agent)) 7 4 11
284 7 my $res = $self->_validators->{$str}->($ip);
285 7 ▶ my $rule = $res && $self->index->{$res};
Condition66%2/3
$res && $self->index->{$res}
!ll&&!rl&&r
403
MC/DC50%1/2
$res && $self->index->{$res}
$res $self->index->{$res}
Truth table66%2/3
A && B
A= $resB= $self->index->{$res}
ABresult
0X0
100
111
286 7 ▶ if ( $rule && $rule->{ignore} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule and $rule->{"ignore"}) 0 7 7
Condition66%2/3
$rule and $rule->{"ignore"}
!ll&&!rl&&r
430
MC/DC0%0/2
$rule and $rule->{"ignore"}
$rule $rule->{"ignore"}
Truth table66%2/3
A and B
A= $ruleB= $rule->{"ignore"}
ABresult
0X0
100
111
287 0 return undef;
288 }
289 7 ▶ return $res && [ $res => $str ];
Condition100%2/2
$res && [$res, $str]
!ll
43
MC/DC100%1/1
$res && [$res, $str]
$res
Truth table100%2/2
A
A= $res
Aresult
00
11
290 }
291  
292 }
293 else {
294 0 my $res = $self->_match_ip($ip);
295 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
296 0 return [ $res => undef ];
297 }
298 0 return $res;
299 }
300  
301 4 return undef;
302 }
303  
304 102 ▶sub _relaxed_revalidate( $self, $ip, $agent ) {
Subroutine _relaxed_revalidate: called CC=12 SCAR=25.5
305  
306 102 ▶ if ( $agent ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agent ne '') { } 89 13 102
307  
308 89 $self->_agents; # ensure agents are instantiated
309  
310 89 my %seen;
311 89 my $fails = 0;
312  
313 89 my @matches = $self->_all_matches( lc $agent );
314 89 splice @matches, $self->max_matches;
315 89 for my $str (@matches) {
316 77 my $fn = $self->_validators->{$str};
317 77 ▶ next if exists $seen{ refaddr $fn };
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{builtin::refaddr($fn)} 0 77 77
318 77 my $res = $seen{ refaddr $fn } = $fn->($ip);
319 77 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 29 48 77
320 29 ▶ $fails++ if defined $res;
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 20 9 29
321 29 next;
322 }
323 48 ▶ my $rule = $res && $self->index->{$res};
Condition33%1/3
$res && $self->index->{$res}
!ll&&!rl&&r
0048
MC/DC0%0/2
$res && $self->index->{$res}
$res $self->index->{$res}
Truth table33%1/3
A && B
A= $resB= $self->index->{$res}
ABresult
0X0
100
111
324 48 ▶ if ( $rule && $rule->{ignore} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule and $rule->{"ignore"}) 0 48 48
Condition33%1/3
$rule and $rule->{"ignore"}
!ll&&!rl&&r
0480
MC/DC0%0/2
$rule and $rule->{"ignore"}
$rule $rule->{"ignore"}
Truth table33%1/3
A and B
A= $ruleB= $rule->{"ignore"}
ABresult
0X0
100
111
325 0 return undef;
326 }
327 48 ▶ return $res && [ $res => $str ];
Condition50%1/2
$res && [$res, $str]
!ll
048
MC/DC0%0/1
$res && [$res, $str]
$res
Truth table50%1/2
A
A= $res
Aresult
00
11
328 }
329  
330 41 ▶ return "" if $fails;
Branch: 1 branch
BranchTrueFalseTotal
if $fails 20 21 41
331  
332 }
333 else {
334 13 my $res = $self->_match_ip($ip);
335 13 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 3 10 13
336 3 return [ $res => undef ];
337 }
338 10 return $res;
339 }
340  
341 21 return undef;
342 }
343  
344 11 ▶sub _strict_revalidate( $self, $ip, $agent ) {
Subroutine _strict_revalidate: called CC=12 SCAR=26.5
345  
346 11 ▶ if ( $agent ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agent ne '') { } 11 0 11
347  
348 11 $self->_agents; # ensure agents are instantiated
349  
350 11 my %seen;
351 my @checks;
352  
353 11 my @matches = $self->_all_matches( lc $agent );
354 11 splice @matches, $self->max_matches;
355 11 for my $str (@matches) {
356 7 my $fn = $self->_validators->{$str};
357 7 ▶ next if exists $seen{ refaddr $fn };
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{builtin::refaddr($fn)} 0 7 7
358 7 my $res = $seen{ refaddr $fn } = $fn->($ip);
359 7 ▶ next unless defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 2 5 7
360 5 ▶ my $rule = $res && $self->index->{$res};
Condition66%2/3
$res && $self->index->{$res}
!ll&&!rl&&r
203
MC/DC50%1/2
$res && $self->index->{$res}
$res $self->index->{$res}
Truth table66%2/3
A && B
A= $resB= $self->index->{$res}
ABresult
0X0
100
111
361 5 ▶ next if $rule && $rule->{ignore};
Branch: 1 branch
BranchTrueFalseTotal
if $rule and $rule->{"ignore"} 0 5 5
Condition66%2/3
$rule and $rule->{"ignore"}
!ll&&!rl&&r
230
MC/DC0%0/2
$rule and $rule->{"ignore"}
$rule $rule->{"ignore"}
Truth table66%2/3
A and B
A= $ruleB= $rule->{"ignore"}
ABresult
0X0
100
111
362 5 ▶ push @checks, $res && [ $res => $str ];
Condition100%2/2
$res && [$res, $str]
!ll
23
MC/DC100%1/1
$res && [$res, $str]
$res
Truth table100%2/2
A
A= $res
Aresult
00
11
363 }
364  
365 11 ▶ if (@checks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@checks) 5 6 11
366 5 ▶ if ( all { !!$_ } @checks ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&all(sub { not !$_; } , @checks)) { } 3 2 5
367 3 return $checks[0];
368 }
369 else {
370 2 return "";
371 }
372 }
373  
374 }
375 else {
376 0 my $res = $self->_match_ip($ip);
377 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
378 0 return [ $res => undef ];
379 }
380 0 return $res;
381 }
382  
383 6 return undef;
384 }
385  
386  
387 14 ▶sub bad_robot( $self, $ip, $agent = undef, $opts = undef ) {
Subroutine bad_robot: called CC=4 SCAR=13.9
Pod documented
388 14 my $value = $self->validate( $ip, $agent, $opts );
389 14 ▶ return defined($value) ? !$value : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 10 4 14
390 }
391  
392 388 ▶sub _add_rule( $self, $rule ) {
Subroutine _add_rule: called CC=15 SCAR=28.1
393  
394 388 ▶ die "The rules are locked" if $self->locked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->locked 0 388 388
395  
396 388 my $name = lc $rule->{name};
397 388 ▶ die "A rule name is required" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 388 388
398  
399 388 my @fns;
400  
401 388 ▶ if ( $rule->{ignore} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'ignore'}) { } 2 386 388
402  
403 0 ▶ push @fns, set_subname "_ignore_${name}", sub($) { 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
404  
405 }
406 else {
407  
408 386 my $domain = $rule->{domain};
409 386 my $network = $rule->{network};
410  
411 386 ▶ if ($network) {
Branch: 1 branch
BranchTrueFalseTotal
if ($network) 266 120 386
412  
413 266 $self->_add_network( $_, $name ) for ( $network->@* );
414  
415 266 ▶ push @fns, set_subname "_check_ip_${name}", sub($ip) { $self->_check_ip( $name, $ip ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
416 }
417  
418 386 ▶ if ($domain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain) 237 149 386
419  
420 263 ▶ state sub _to_regexp($domain) {
Subroutine _to_regexp: called CC=3 SCAR=11.0
421 263 ▶ return $domain if is_regexpref($domain);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_regexpref($domain) 0 263 263
422 263 my ($re) = $domain =~ m[ \A / (.+) / \z ]x;
423 263 ▶ $re //= quotemeta($domain) . '\z';
Condition100%2/2
$re //= quotemeta($domain) . '\\z'
l!l
5258
MC/DC100%1/1
$re //= quotemeta($domain) . '\\z'
$re
Truth table100%2/2
A
A= $re
Aresult
11
00
424 263 return qr/${re}/an;
425 }
426  
427 237 my $fn;
428  
429 237 ▶ if ( is_plain_arrayref($domain) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($domain)) { } 31 206 237
430 31 my @res = map { _to_regexp($_) } $domain->@*;
431 31 ▶ $fn = sub($ip) { $self->_check_dns( $name => \@res, $ip ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
432 }
433 else {
434 206 my $re = _to_regexp($domain);
435 206 ▶ $fn = sub($ip) { $self->_check_dns( $name => $re, $ip ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
436 }
437  
438 237 push @fns, set_subname "_check_dns_${name}", $fn;
439 }
440 }
441  
442 388 ▶ if (@fns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fns) { } 388 0 388
443 388 ▶ my $type = $rule->{match} // "any";
Condition50%1/2
$rule->{"match"} // "any"
l!l
0388
MC/DC0%0/1
$rule->{"match"} // "any"
$rule->{"match"}
Truth table50%1/2
A
A= $rule->{"match"}
Aresult
11
00
444  
445 # TODO: add option for partial matching where false returns undef, i.e. "yes or unknown"
446  
447 my $fn = set_subname "_verify_${name}", (
448  
449 ( @fns == 1 )
450 64 ▶ ? sub($ip) { $fns[0]->($ip) and $name }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$fns[0]->($ip) and $name 39 25 64
451 : (
452  
453 $type eq "any"
454 27 ▶ ? sub($ip) {
Subroutine __ANON__: called CC=2 SCAR=6.9
455 27 ▶ any { $_->($ip) } @fns and $name;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
&any(sub { &$_($ip); } , @fns) and $name 15 12 27
456 }
457 0 ▶ : sub($ip) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
458 0 ▶ all { $_->($ip) } @fns and $name;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&all(sub { &$_($ip); } , @fns) and $name 0 0 0
459 }
460 )
461 388 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
@fns == 1 ? : 271 117 388
$type eq 'any' ? : 117 0 117
462  
463 388 my $validators = $self->_validators;
464  
465 388 ▶ if ( my $agents = $rule->{agents} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $agents = $rule->{'agents'}) { } 388 0 388
466 388 for my $str ( map { lc $_ } $agents->@* ) {
467 568 ▶ die "string ${str} already exists in the rules" if exists $validators->{$str};
Branch: 1 branch
BranchTrueFalseTotal
if exists $validators->{$str} 0 568 568
468 568 $validators->{$str} = $fn;
469 }
470 }
471 else {
472 0 die "an agent substring is required";
473 }
474  
475 388 $self->index->{$name} = $rule;
476  
477 }
478 else {
479  
480 0 die "no rules found for ${name}";
481  
482 }
483  
484 }
485  
486 15046 ▶sub _add_network( $self, $cidr, $name ) {
Subroutine _add_network: called CC=1 SCAR=0.4
487 15046 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
488 15046 $self->_add_string( $cidr, $name );
489 }
490 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
491 0 die "add_string failed for '$cidr' with '$name': $_";
492 15046 };
493 }
494  
495 44 ▶sub _match_ip( $self, $ip ) {
Subroutine _match_ip: called CC=1 SCAR=0.0
496 44 return $self->_match_string( $ip );
497 }
498  
499 28 ▶sub _check_ip( $self, $name, $ip ) {
Subroutine _check_ip: called CC=3 SCAR=11.0
500 28 my $check = $self->_match_ip($ip);
501 28 ▶ return $name if $check && $check eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $check and $check eq $name 15 13 28
Condition100%3/3
$check and $check eq $name
!ll&&!rl&&r
12115
MC/DC100%2/2
$check and $check eq $name
$check $check eq $name
Truth table100%3/3
A and B
A= $checkB= $check eq $name
ABresult
0X0
100
111
502 13 return undef;
503 }
504  
505 # The canonical form used to compare addresses: everything is mapped into the
506 # ::ffff: IPv6 space already used for Net::Patricia (see _match_ip), then fully
507 # expanded. Net::DNS renders an AAAA address as "2001:db8:0:0:0:0:0:7", which
508 # never string-equals the "2001:db8::7" a web server puts in REMOTE_ADDR, so
509 # comparing the two textually is not meaningful without this.
510 126 ▶sub _normalise_ip($ip) {
Subroutine _normalise_ip: called CC=5 SCAR=16.7
511 126 ▶ return undef unless defined $ip && length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and length $ip 0 126 126
Condition33%1/3
defined $ip and length $ip
!ll&&!rl&&r
00126
MC/DC0%0/2
defined $ip and length $ip
defined $ip length $ip
Truth table33%1/3
A and B
A= defined $ipB= length $ip
ABresult
0X0
100
111
512 126 ▶ $ip = "::ffff:" . ip_expand_address( $ip, 4 ) if ip_is_ipv4($ip);
Branch: 1 branch
BranchTrueFalseTotal
if ip_is_ipv4($ip) 113 13 126
513 126 ▶ return undef unless ip_is_ipv6($ip); # ip_expand_address does not validate
Branch: 1 branch
BranchTrueFalseTotal
unless ip_is_ipv6($ip) 1 125 126
514 125 return ip_expand_address( $ip, 6 );
515 }
516  
517 # Build the reverse-lookup name here rather than passing an address literal to
518 # the resolver and relying on it to special-case one. Net::DNS::Question does
519 # convert a literal, but Net::DNS::Resolver::Mock -- the resolver the tests use
520 # -- only does so for IPv4, so relying on that behaviour makes the IPv6 path
521 # untestable. Being explicit also stops a hostname that merely looks like an
522 # address from being silently reinterpreted.
523 76 ▶sub _arpa($norm) {
Subroutine _arpa: called CC=2 SCAR=6.9
524 76 ( my $nibbles = $norm ) =~ s/://g;
525 76 ▶ if ( $nibbles =~ /\A0{20}ffff([[:xdigit:]]{8})\z/a ) { # IPv4-mapped
Branch: 1 branch
BranchTrueFalseTotal
if ($nibbles =~ /\A0{20}ffff([[:xdigit:]]{8})\z/a) 70 6 76
526 70 return join( ".", reverse map { hex } $1 =~ /(..)/g ) . ".in-addr.arpa";
527 }
528 6 return join( ".", reverse split //, $nibbles ) . ".ip6.arpa";
529 }
530  
531 77 ▶sub _check_dns( $self, $name, $domain, $ip ) {
Subroutine _check_dns: called CC=12 SCAR=24.9
532  
533 77 ▶ my $wanted = _normalise_ip($ip) // return undef;
Condition100%2/2
_normalise_ip($ip) // (return undef)
l!l
761
MC/DC100%1/1
_normalise_ip($ip) // (return undef)
_normalise_ip($ip)
Truth table100%2/2
A
A= _normalise_ip($ip)
Aresult
11
00
534  
535 76 ▶ my $reply = $self->_dns_query( _arpa($wanted), "PTR" ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $reply = $self->_dns_query(_arpa($wanted), "PTR") or return undef 54 22 76
536  
537 154 my @hostnames = grep { !!$_ }
538 54 ▶ map { $_->can("ptrdname") && $_->ptrdname } $reply->answer;
Condition33%1/3
$_->can('ptrdname') && $_->ptrdname
!ll&&!rl&&r
00154
MC/DC0%0/2
$_->can('ptrdname') && $_->ptrdname
$_->can('ptrdname') $_->ptrdname
Truth table33%1/3
A && B
A= $_->can('ptrdname')B= $_->ptrdname
ABresult
0X0
100
111
539  
540  
541 54 ▶ if ( is_plain_arrayref($domain) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($domain)) 20 34 54
542 20 ▶ my @domains = $domain->@* or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my(@domains) = @$domain or return undef 19 1 20
543  
544 19 ▶ if ( @domains == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@domains == 1) { } 14 5 19
545 14 $domain = $domains[0];
546 }
547 else {
548 5 my $re = "(" . join( "|", @domains ) . ")";
549 5 $domain = qr/$re/n;
550 }
551 }
552  
553 53 my @matched = grep { $_ =~ $domain } @hostnames;
554  
555 53 ▶ return "" unless @matched;
Branch: 1 branch
BranchTrueFalseTotal
unless @matched 8 45 53
556  
557 45 splice @matched, $self->max_forward_lookups;
558  
559 # Only a record of the client's own family can confirm it, so ask for one
560 # type rather than both: an IPv4 client normalises into ::ffff:/96.
561 45 ▶ my $type = $wanted =~ /\A0000:0000:0000:0000:0000:ffff:/ ? "A" : "AAAA";
Branch: 1 branch
BranchTrueFalseTotal
$wanted =~ /\A0000:0000:0000:0000:0000:ffff:/u ? : 40 5 45
562  
563 45 for my $hostname (@matched) {
564  
565 59 ▶ my $forward = $self->_dns_query( $hostname, $type ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $forward = $self->_dns_query($hostname, $type) or next 43 16 59
566  
567 return $name
568 49 ▶ if any { ( _normalise_ip($_) // "" ) eq $wanted }
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
_normalise_ip($_) // ""
l!l
490
MC/DC0%0/1
_normalise_ip($_) // ""
_normalise_ip($_)
Truth table50%1/2
A
A= _normalise_ip($_)
Aresult
11
00
569 55 map { $_->address }
570 43 ▶ grep { $_->can("address") } $forward->answer;
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { (_normalise_ip($_) // "") eq $wanted; } , map({$_->address;} grep({$_->can("address");} $forward->answer))) 41 2 43
571 }
572  
573 4 return "";
574 }
575  
576 34 ▶sub _init_validators_from_config($self) {
Subroutine _init_validators_from_config: called CC=2 SCAR=6.9
577 34 for my $rule ( $self->config->@* ) {
578 388 $self->_add_rule( { $rule->%* } );
579 }
580 34 $self->_set_locked(1);
581 }
582  
583 13 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.4
584  
585 13 ▶ if ( eval { load "TOML::XS" } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { &load('TOML::XS') } }) { } 0 13 13
586 0 ▶ *_from_toml = sub($toml) { TOML::XS::from_toml($toml)->get() };
Subroutine __ANON__: not called CC=1 SCAR=6.9
587 }
588 else {
589 13 *_from_toml = \&TOML::Tiny::from_toml;
590 }
591 }
592  
593  
594 13 1;
595  
596 __END__
↑ Summary