lib/App/WebPerimeterScan/Scope.pm

filter: Statementstmt 100% 171 / 171 Branchbran 84.2% 59 / 70 Conditioncond 69.2% 45 / 65 MC/DCmcdc 48.7% 19 / 39 Subroutinesub 100% 32 / 32 Podpod 100% 9 / 9 totaltotal 86.7% 335 / 386 CC 60 SCAR 19.7
CC
60
Cov
90%
_add_entry
17.3
_addresses
10.9
_os_error
7.5
CRAP 7.1
« lib/App/WebPerimeterScan/TLS.pm ↑ Summary lib/App/WebPerimeterScan.pm »
1 package App::WebPerimeterScan::Scope;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 26 ▶use Data::Validate::IP qw(is_ipv4 is_ipv6 is_public_ipv4 is_public_ipv6);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶use Encode qw(decode FB_CROAK LEAVE_SRC);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 ▶use Params::Get qw(get_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use Params::Validate::Strict qw(validate_strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use Socket qw(getaddrinfo getnameinfo NI_NUMERICHOST NIx_NOSERV SOCK_STREAM);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use Sub::Private { mode => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
14 26 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 26 ▶use App::WebPerimeterScan::I18N ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 26 ▶use parent -norequire, 'App::WebPerimeterScan::I18N';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 26 our $VERSION = '0.001.0';
20  
21 # The scope file is the authorisation boundary. Nothing in this distribution
22 # sends traffic to a host that does not appear here, so a crawler cannot
23 # wander onto a third party's site.
24  
25 # Scope-file syntax. A hostname label run, as accepted in a scope line.
26 26 Readonly::Scalar my $HOST_RE => qr/[A-Za-z0-9][A-Za-z0-9._-]*/;
27 26 Readonly::Scalar my $URL_LINE => qr{\Ahttps?://}i;
28 26 Readonly::Scalar my $WILDCARD => qr/\A\*\.($HOST_RE)\z/;
29 26 Readonly::Scalar my $HOST_PORT => qr/\A($HOST_RE)(?::(\d+))?\z/;
30 26 Readonly::Scalar my $COMMENT => qr/\s*#.*/s;
31  
32 # Any character outside ASCII: such a host name needs its Punycode form.
33 26 Readonly::Scalar my $NON_ASCII => qr/[^\x00-\x7f]/;
34  
35 # A bare scope line split into its parts, so that only the host is converted:
36 # an optional "*." wildcard prefix, the host, and an optional ":port".
37 26 Readonly::Scalar my $BARE_LINE => qr/\A(\*\.)?([^:\/\s]+)(:\d+)?\z/;
38  
39 # Valid TCP port numbers, for an explicit port in a scope line.
40 26 Readonly::Scalar my $MIN_PORT => 1;
41 26 Readonly::Scalar my $MAX_PORT => 65_535;
42  
43 =encoding utf8
44  
45 =head1 NAME
46  
47 App::WebPerimeterScan::Scope - authorisation boundary for an external scan
48  
49 =head1 VERSION
50  
51 Version 0.001.0
52  
53 =head1 SYNOPSIS
54  
55 Load a scope file and stop at once if a host may not be scanned:
56  
57 use App::WebPerimeterScan::Scope;
58  
59 my $scope = App::WebPerimeterScan::Scope->new;
60 $scope->load_file('scope.txt')->assert_target_allowed('www.example.com');
61  
62 Ask before following a link, without stopping:
63  
64 for my $link ('https://api.example.com/', 'https://other.example.net/') {
65 my $host = URI->new($link)->host;
66 next unless $scope->allows($host);
67 print "may visit $link\n";
68 }
69  
70 Scan a test server on your own machine. Loopback and private addresses
71 are refused unless you say so:
72  
73 my $lab = App::WebPerimeterScan::Scope->new(allow_private => 1);
74 $lab->load_file('lab-scope.txt');
75 print "allowed\n" if $lab->allows('127.0.0.1');
76  
77 List the hosts that will be scanned:
78  
79 for my $host (@{ $scope->hosts }) {
80 print "$host\n";
81 }
82  
83 =head1 DESCRIPTION
84  
85 This module decides which hosts the scanner may contact. The scanner asks
86 it before every network request.
87  
88 A host may be scanned only when both of these are true:
89  
90 =over 4
91  
92 =item 1. The host is listed in the scope file (or matches a wildcard line
93 in it).
94  
95 =item 2. The host does not resolve to a loopback, private, link-local,
96 documentation or other non-public address. The C<allow_private> option
97 turns this second test off, for scanning your own test servers.
98  
99 =back
100  
101 =head2 The scope file
102  
103 The scope file is plain text, one entry per line. Each line is one of:
104  
105 example.com a host name
106 example.com:8443 a host name and a port
107 203.0.113.10 an IPv4 address
108 https://shop.example.com/ a URL: the scheme, host and any port are used
109 *.example.com a wildcard: example.com and every name below it
110  
111 Blank lines are ignored. A C<#> starts a comment, which runs to the end of
112 the line. Host names are compared without regard to case, and a final dot
113 (C<example.com.>) is removed. International names may be
114 written as they are, in UTF-8; see L</ENCODING>. A line that cannot be understood is skipped
115 with a warning. A file with no usable line at all is an error.
116  
117 =head1 REQUIREMENTS
118  
119 C<URI> 1.70 and C<Data::Validate::IP> 0.31, which classifies addresses
120 against the IANA special-purpose registries for both IPv4 and IPv6.
121 Addresses are resolved with C<Socket> 2.020's C<getaddrinfo>.
122  
123 =head1 SUBROUTINES/METHODS
124  
125 =head2 new
126  
127 Purpose: create an empty scope.
128  
129 Args: named C<allow_private> (boolean, default 0) and C<source> (string,
130 the name to quote in refusals, default C<(none)>).
131  
132 Returns: the new object.
133  
134 Side Effects: none.
135  
136 Usage: C<< my $scope = App::WebPerimeterScan::Scope->new(allow_private => 1); >>
137  
138 =head3 EXAMPLE
139  
140 my $scope = App::WebPerimeterScan::Scope->new;
141 $scope->load_file('scope.txt');
142  
143 =head3 API SPECIFICATION
144  
145 =head4 INPUT
146  
147 {
148 allow_private => { type => 'boolean', optional => 1, default => 0 },
149 source => { type => 'string', optional => 1 },
150 }
151  
152 =head4 OUTPUT
153  
154 { type => 'object', isa => 'App::WebPerimeterScan::Scope' }
155  
156 =head3 MESSAGES
157  
158 Message Meaning / resolution
159 -------------------------- -------------------------------------
160 Unknown parameter 'X' A misspelt argument name.
161  
162 =cut
163  
164 sub new
165 34 ▶{
Subroutine new: called CC=4 SCAR=14.2
Pod documented
166 34 my $class = shift;
167  
168 34 ▶ my $args = validate_strict(
Condition100%2/2
get_params(undef, \@_) // {}
l!l
1222
MC/DC100%1/1
get_params(undef, \@_) // {}
get_params(undef, \@_)
Truth table100%2/2
A
A= get_params(undef, \@_)
Aresult
11
00
169 schema => {
170 allow_private => { type => 'boolean', optional => 1, default => 0 },
171 source => { type => 'string', optional => 1 },
172 },
173 input => get_params(undef, \@_) // {},
174 );
175  
176 return bless {
177 entries => [],
178 allow_private => $args->{allow_private} ? 1 : 0,
179 33 ▶ source => $args->{source} // $class->i18n('scope.no_source'),
Branch: 1 branch
BranchTrueFalseTotal
$args->{'allow_private'} ? : 7 26 33
Condition33%1/3
$args->{"source"} // $class->i18n("scope.no_source")
l!l&&r!l&&!r
0330
MC/DC0%0/2
$args->{"source"} // $class->i18n("scope.no_source")
$args->{"source"} $class->i18n("scope.no_source")
Truth table33%1/3
A // B
A= $args->{"source"}B= $class->i18n("scope.no_source")
ABresult
1X1
011
000
180 }, $class;
181 }
182  
183 =head2 load_file
184  
185 Purpose: read a scope file.
186  
187 Args: C<$file>, the path.
188  
189 Returns: C<$self>, for chaining.
190  
191 Side Effects: appends the file's entries; records C<$file> as the source
192 quoted in refusals. Lines that cannot be parsed are skipped with a warning.
193  
194 Usage: C<< $scope->load_file('scope.txt') >>
195  
196 Accepted lines, one per line, C<#> starts a comment:
197  
198 example.com
199 example.com:8443
200 https://example.com/
201 *.example.com (the domain and any sub-domain)
202 192.0.2.10
203  
204 =head3 EXAMPLE
205  
206 my $scope = eval { App::WebPerimeterScan::Scope->new->load_file($path) }
207 or die App::WebPerimeterScan::I18N::clean_error($@), "\n";
208  
209 =head3 API SPECIFICATION
210  
211 =head4 INPUT
212  
213 { file => { type => 'string', min => 1, position => 0 } }
214  
215 =head4 OUTPUT
216  
217 { type => 'object', isa => 'App::WebPerimeterScan::Scope' }
218  
219 =head3 MESSAGES
220  
221 Message Meaning / resolution
222 ------------------------------------------ ------------------------------
223 Cannot read scope file 'F': E (fatal) F is missing or unreadable; E
224 is the operating system's text.
225 Ignoring unparsable scope line: L (warning) L is not a host, host:port,
226 URL or *.domain; fix the line.
227 Scope file 'F' contains no usable hosts Every line was blank, a
228 (fatal) comment or unparsable.
229  
230 =head3 PSEUDOCODE
231  
232 open the file, or croak with the operating system's reason
233 for each line
234 strip comments and surrounding white space; skip if empty
235 parse it as a URL, a wildcard or host[:port]
236 warn and skip if it does not parse
237 croak if no line produced an entry
238 remember the file name for refusal messages
239  
240 =cut
241  
242 sub load_file
243 22 ▶{
Subroutine load_file: called CC=6 SCAR=17.9
Pod documented
244 22 my ($self, @rest) = @_;
245  
246 my $file = validate_strict(
247 schema => { file => { type => 'string', min => 1 } },
248 input => get_params('file', \@rest),
249 22 )->{file};
250  
251 # autodie throws on failure; turn that into our message, keeping the
252 # operating system's reason exactly as Perl reported it.
253 22 my $fh = eval { open my $handle, '<', $file; $handle };
254 22 ▶ $self->_throw('scope.unreadable', { file => $file, error => _os_error($@) }) unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 5 17 22
255  
256 17 my $count = 0;
257 17 ▶ while(my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 32 17 49
258 32 $line =~ s/$COMMENT//;
259 32 $line =~ s/\A\s+|\s+\z//g;
260 32 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 5 27 32
261  
262 27 ▶ if($self->_add_entry($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_add_entry($line)) { } 23 4 27
263 23 $count++;
264 } else {
265 4 $self->_warn('scope.unparsable', { line => $line });
266 }
267 }
268 17 close $fh;
269  
270 17 ▶ $self->_throw('scope.empty', { file => $file }) unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 1 16 17
271  
272 16 $self->{source} = $file;
273  
274 16 return $self;
275 }
276  
277 =head2 entries
278  
279 Purpose: every parsed entry, wildcards included.
280  
281 Args: none.
282  
283 Returns: an array reference of hash references with C<host> and, where
284 given, C<port>, C<scheme> and C<wildcard>.
285  
286 Side Effects: none.
287  
288 Usage: C<< for my $entry (@{ $scope->entries }) { ... } >>
289  
290 =head3 EXAMPLE
291  
292 my @wildcards = grep { $_->{wildcard} } @{ $scope->entries };
293  
294 =head3 API SPECIFICATION
295  
296 =head4 INPUT
297  
298 {}
299  
300 =head4 OUTPUT
301  
302 { type => 'arrayref', element_type => 'hashref' }
303  
304 =head3 MESSAGES
305  
306 None.
307  
308 =cut
309  
310 sub entries
311 1 ▶{
Subroutine entries: called CC=1 SCAR=0.0
Pod documented
312 1 my ($self) = @_;
313  
314 1 return [ @{ $self->{entries} } ];
315 }
316  
317 =head2 source
318  
319 Purpose: where the scope came from, for messages and reports.
320  
321 Args: none.
322  
323 Returns: the file name given to C<load_file>, or the C<source> given to
324 C<new>, or C<(none)>.
325  
326 Side Effects: none.
327  
328 Usage: C<< print 'Scope: ', $scope->source, "\n"; >>
329  
330 =head3 EXAMPLE
331  
332 printf "Loaded %s\n", $scope->source;
333  
334 =head3 API SPECIFICATION
335  
336 =head4 INPUT
337  
338 {}
339  
340 =head4 OUTPUT
341  
342 { type => 'string' }
343  
344 =head3 MESSAGES
345  
346 None.
347  
348 =cut
349  
350 sub source
351 16 ▶{
Subroutine source: called CC=1 SCAR=0.0
Pod documented
352 16 my ($self) = @_;
353  
354 16 return $self->{source};
355 }
356  
357 =head2 hosts
358  
359 Purpose: the concrete hosts to scan.
360  
361 Args: none.
362  
363 Returns: an array reference of entries, wildcards removed and the first
364 entry for each host kept. A wildcard still authorises its sub-domains, but
365 sub-domain names are not guessed: only links the site serves are followed.
366  
367 Side Effects: none.
368  
369 Usage: C<< for my $entry (@{ $scope->hosts }) { ... } >>
370  
371 =head3 EXAMPLE
372  
373 print "$_->{host}\n" for @{ $scope->hosts };
374  
375 =head3 API SPECIFICATION
376  
377 =head4 INPUT
378  
379 {}
380  
381 =head4 OUTPUT
382  
383 { type => 'arrayref', element_type => 'hashref' }
384  
385 =head3 MESSAGES
386  
387 None.
388  
389 =cut
390  
391 sub hosts
392 11 ▶{
Subroutine hosts: called CC=2 SCAR=7.0
Pod documented
393 11 my ($self) = @_;
394  
395 11 my %seen;
396  
397 11 ▶ return [ grep { !$_->{wildcard} && !$seen{ $_->{host} }++ } @{ $self->{entries} } ];
Condition66%2/3
$_->{'wildcard'} || $seen{$_->{'host'}}++
l!l&&r!l&&!r
1014
MC/DC50%1/2
$_->{'wildcard'} || $seen{$_->{'host'}}++
$_->{'wildcard'} $seen{$_->{'host'}}++
Truth table66%2/3
A || B
A= $_->{'wildcard'}B= $seen{$_->{'host'}}++
ABresult
1X1
011
000
398 }
399  
400 =head2 covers
401  
402 Purpose: say whether a hostname is inside the authorised scope.
403  
404 Args: C<$host> (string). Case and a trailing dot are ignored.
405  
406 Returns: 1 or 0.
407  
408 Side Effects: none; this does not resolve the name.
409  
410 Usage: C<< next unless $scope->covers($uri->host); >>
411  
412 =head3 EXAMPLE
413  
414 # with "*.example.com" in the scope file
415 $scope->covers('api.example.com'); # 1
416 $scope->covers('notexample.com'); # 0, the match is on a label boundary
417  
418 =head3 API SPECIFICATION
419  
420 =head4 INPUT
421  
422 { host => { type => 'string', optional => 1, position => 0 } }
423  
424 =head4 OUTPUT
425  
426 { type => 'boolean' }
427  
428 =head3 MESSAGES
429  
430 None.
431  
432 =cut
433  
434 sub covers
435 32 ▶{
Subroutine covers: called CC=6 SCAR=17.9
Pod documented
436 32 my ($self, $host) = @_;
437  
438 # A caller may pass an international name as text; entries are ASCII.
439 32 $host = _ascii_host($host);
440 32 ▶ return 0 unless defined $host && length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 1 31 32
Condition66%2/3
defined $host and length $host
!ll&&!rl&&r
1031
MC/DC50%1/2
defined $host and length $host
defined $host length $host
Truth table66%2/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
441  
442 31 $host = lc $host;
443 31 $host =~ s/\.\z//;
444  
445 # A wildcard matches on a label boundary only, so *.example.com does
446 # not cover notexample.com.
447 my $hit = grep {
448 69 ▶ $host eq $_->{host} || ($_->{wildcard} && $host =~ /\.\Q$_->{host}\E\z/)
Condition100%6/6
$host eq $_->{'host'} || $_->{'wildcard'} && $host =~ /\.\Q$_->{'host'}\E\z/
l!l&&r!l&&!r
20247
$_->{"wildcard"} and $host =~ /\.\Q$_->{"host"}\E\z/
!ll&&!rl&&r
36112
MC/DC100%3/3
$host eq $_->{'host'} || $_->{'wildcard'} && $host =~ /\.\Q$_->{'host'}\E\z/
$host eq $_->{'host'} $_->{"wildcard"} $host =~ /\.\Q$_->{"host"}\E\z/
Truth table100%4/4
A || B and C
A= $host eq $_->{'host'}B= $_->{"wildcard"}C= $host =~ /\.\Q$_->{"host"}\E\z/
ABCresult
1XX1
0111
00X0
0100
449 31 } @{ $self->{entries} };
450  
451 31 ▶ return $hit ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$hit ? : 22 9 31
452 }
453  
454 =head2 is_private_target
455  
456 Purpose: say whether a host resolves into address space that is not
457 publicly routable.
458  
459 Args: C<$host>, a hostname or an IPv4 or IPv6 literal.
460  
461 Returns: 1 if B<any> address the name resolves to is loopback, private
462 (RFC 1918, unique-local), link-local, shared (CGNAT), documentation,
463 benchmarking, multicast, reserved or otherwise non-public; 0 if every
464 address is public or the name does not resolve.
465  
466 Side Effects: a DNS lookup.
467  
468 Usage: C<< warn "private\n" if $scope->is_private_target($host); >>
469  
470 Checking every address matters: a name with one public and one internal A
471 record would otherwise be judged on whichever the resolver listed first.
472  
473 =head3 EXAMPLE
474  
475 $scope->is_private_target('127.0.0.1'); # 1
476 $scope->is_private_target('::1'); # 1
477 $scope->is_private_target('192.30.252.1'); # 0
478  
479 =head3 API SPECIFICATION
480  
481 =head4 INPUT
482  
483 { host => { type => 'string', min => 1, position => 0 } }
484  
485 =head4 OUTPUT
486  
487 { type => 'boolean' }
488  
489 =head3 MESSAGES
490  
491 None.
492  
493 =cut
494  
495 sub is_private_target
496 31 ▶{
Subroutine is_private_target: called CC=4 SCAR=13.9
Pod documented
497 31 my ($self, $host) = @_;
498  
499 my $private = grep {
500 28 ▶ is_ipv4($_) ? !is_public_ipv4($_) : is_ipv6($_) ? !is_public_ipv6($_) : 1
Branch: 2 branches
BranchTrueFalseTotal
is_ipv4($_) ? : 20 8 28
is_ipv6($_) ? : 8 0 8
501 31 } @{ _addresses($host) };
502  
503 31 ▶ return $private ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$private ? : 21 10 31
504 }
505  
506 =head2 allows
507  
508 Purpose: the full authorisation test for one host: in scope, and not in
509 private address space unless that was permitted.
510  
511 Args: C<$host> (string).
512  
513 Returns: 1 or 0.
514  
515 Side Effects: a DNS lookup when the host is covered and C<allow_private> is
516 off.
517  
518 Usage: C<< next unless $scope->allows($uri->host); >>
519  
520 This is what the crawler asks before following a link: a wildcard entry
521 covers names nobody listed by hand, and one of them resolving to an internal
522 address must be refused like a listed one would be.
523  
524 =head3 EXAMPLE
525  
526 my @fetchable = grep { $scope->allows(URI->new($_)->host) } @links;
527  
528 =head3 API SPECIFICATION
529  
530 =head4 INPUT
531  
532 { host => { type => 'string', optional => 1, position => 0 } }
533  
534 =head4 OUTPUT
535  
536 { type => 'boolean' }
537  
538 =head3 MESSAGES
539  
540 None.
541  
542 =cut
543  
544 sub allows
545 7 ▶{
Subroutine allows: called CC=4 SCAR=13.9
Pod documented
546 7 my ($self, $host) = @_;
547  
548 my $allowed = $self->covers($host)
549 7 ▶ && ($self->{allow_private} || !$self->is_private_target($host));
Condition100%6/6
$self->covers($host) && ($self->{'allow_private'} || !$self->is_private_target($host))
!ll&&!rl&&r
412
$self->{"allow_private"} or not $self->is_private_target($host)
l!l&&r!l&&!r
111
MC/DC100%3/3
$self->covers($host) && ($self->{'allow_private'} || !$self->is_private_target($host))
$self->covers($host) $self->{"allow_private"} not $self->is_private_target($host)
Truth table100%4/4
A && B or not(C)
A= $self->covers($host)B= $self->{"allow_private"}C= not $self->is_private_target($host)
ABCresult
0XX0
1000
11X1
1011
550  
551 7 ▶ return $allowed ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$allowed ? : 2 5 7
552 }
553  
554 =head2 assert_target_allowed
555  
556 Purpose: refuse, loudly, to scan a host that C<allows> would reject.
557  
558 Args: C<$host> (string).
559  
560 Returns: C<$self>, for chaining.
561  
562 Side Effects: croaks when the host is out of scope or private.
563  
564 Usage: C<< $scope->assert_target_allowed($host); >>
565  
566 =head3 EXAMPLE
567  
568 eval { $scope->assert_target_allowed('evil.example') };
569 print App::WebPerimeterScan::I18N::clean_error($@), "\n" if $@;
570  
571 =head3 API SPECIFICATION
572  
573 =head4 INPUT
574  
575 { host => { type => 'string', min => 1, position => 0 } }
576  
577 =head4 OUTPUT
578  
579 { type => 'object', isa => 'App::WebPerimeterScan::Scope' }
580  
581 =head3 MESSAGES
582  
583 Message Meaning / resolution
584 -------------------------------------------- ----------------------------
585 Refusing to scan 'H': not listed in F Add H to the scope file F
586 (fatal) if you are authorised to.
587 Refusing to scan 'H': it resolves into H is loopback/private; pass
588 private address space (fatal) --allow-private if intended.
589  
590 =cut
591  
592 sub assert_target_allowed
593 12 ▶{
Subroutine assert_target_allowed: called CC=4 SCAR=13.9
Pod documented
594 12 my ($self, $host) = @_;
595  
596 $self->_throw('scope.not_listed', { host => $host, source => $self->{source} })
597 12 ▶ unless $self->covers($host);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->covers($host) 1 11 12
598  
599 $self->_throw('scope.private', { host => $host })
600 11 ▶ if !$self->{allow_private} && $self->is_private_target($host);
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"allow_private"} and $self->is_private_target($host) 3 8 11
Condition100%3/3
not $self->{"allow_private"} and $self->is_private_target($host)
!ll&&!rl&&r
713
MC/DC100%2/2
not $self->{"allow_private"} and $self->is_private_target($host)
not $self->{"allow_private"} $self->is_private_target($host)
Truth table100%3/3
not(A) and B
A= not $self->{"allow_private"}B= $self->is_private_target($host)
ABresult
0X0
100
111
601  
602 8 return $self;
603 }
604  
605 # _add_entry
606 # Purpose: parse one non-empty, comment-free scope line.
607 # Entry: $line is trimmed.
608 # Exit: 1 if an entry was added, 0 if the line was not understood.
609 # Side effects: appends to $self->{entries}.
610 27 ▶sub _add_entry :Private
Subroutine _add_entry: called CC=17 SCAR=28.5
611 {
612 27 my ($self, $line) = @_;
613  
614 27 my %entry;
615  
616 # An international name is stored in its ASCII (Punycode) form, which is
617 # what DNS, URI and the crawler's links all use. The line arrives as
618 # bytes from the file, so it is decoded first: URI given UTF-8 bytes
619 # reads each byte as a character and produces the Punycode of a
620 # different, wrong name.
621 27 ▶ if($line =~ $NON_ASCII) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$NON_ASCII/) 4 23 27
622 4 ▶ if($line =~ $URL_LINE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$URL_LINE/) { } 1 3 4
623 1 $line = _characters($line);
624 } else {
625 3 ▶ my ($wildcard, $host, $port) = $line =~ $BARE_LINE or return 0;
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /$BARE_LINE/ or return 0 3 0 3
626 3 ▶ my $ascii = _ascii_host($host) // return 0;
Condition50%1/2
_ascii_host($host) // (return 0)
l!l
30
MC/DC0%0/1
_ascii_host($host) // (return 0)
_ascii_host($host)
Truth table50%1/2
A
A= _ascii_host($host)
Aresult
11
00
627 3 ▶ $line = ($wildcard // '') . $ascii . ($port // '');
Condition100%4/4
$wildcard // ""
l!l
12
$port // ""
l!l
12
MC/DC100%1/1
$wildcard // ""
$wildcard
MC/DC100%1/1
$port // ""
$port
Truth table100%2/2
A
A= $wildcard
Aresult
11
00
Truth table100%2/2
A
A= $port
Aresult
11
00
628 }
629 }
630  
631 27 ▶ if($line =~ $URL_LINE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /$URL_LINE/) { } 4 23 27
elsif ($line =~ /$WILDCARD/) { } 2 21 23
elsif ($line =~ /$HOST_PORT/) { } 20 1 21
632 4 my $uri = URI->new($line);
633 4 my $host = eval { $uri->host };
634 4 ▶ return 0 unless defined $host && length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 4 4
Condition33%1/3
defined $host and length $host
!ll&&!rl&&r
004
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table33%1/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
635 4 $entry{host} = lc $host;
636 4 $entry{scheme} = lc $uri->scheme;
637 # Only a port the line actually names. URI fills in the scheme's
638 # default, and recording that made every URL entry look as though
639 # a port had been given, which skipped the HTTP-to-HTTPS check.
640 4 ▶ $entry{port} = $uri->port + 0 if $uri->port != $uri->default_port;
Branch: 1 branch
BranchTrueFalseTotal
if $uri->port != $uri->default_port 2 2 4
641 } elsif($line =~ $WILDCARD) {
642 2 $entry{host} = lc $1;
643 2 $entry{wildcard} = 1;
644 } elsif($line =~ $HOST_PORT) {
645 20 $entry{host} = lc $1;
646 20 ▶ if(defined $2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) 12 8 20
647 12 ▶ return 0 unless $2 >= $MIN_PORT && $2 <= $MAX_PORT;
Branch: 1 branch
BranchTrueFalseTotal
unless $2 >= $MIN_PORT and $2 <= $MAX_PORT 3 9 12
Condition100%3/3
$2 >= $MIN_PORT and $2 <= $MAX_PORT
!ll&&!rl&&r
129
MC/DC100%2/2
$2 >= $MIN_PORT and $2 <= $MAX_PORT
$2 >= $MIN_PORT $2 <= $MAX_PORT
Truth table100%3/3
A and B
A= $2 >= $MIN_PORTB= $2 <= $MAX_PORT
ABresult
0X0
100
111
648 9 $entry{port} = $2 + 0;
649 }
650 } else {
651 1 return 0;
652 }
653  
654 23 $entry{host} =~ s/\.\z//;
655 23 ▶ return 0 unless length $entry{host};
Branch: 1 branch
BranchTrueFalseTotal
unless length $entry{"host"} 0 23 23
656  
657 23 push @{ $self->{entries} }, \%entry;
658  
659 23 return 1;
660 26 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
661  
662 # _characters
663 # Purpose: a string as Perl characters, whatever form it arrived in.
664 # Entry: $text is a character string, or bytes (normally UTF-8).
665 # Exit: the decoded text if $text is valid UTF-8 bytes; otherwise
666 # $text unchanged (it is already characters, or is in another
667 # encoding that cannot be guessed).
668 # Side effects: none.
669 8 ▶sub _characters :Private
Subroutine _characters: called CC=3 SCAR=11.4
670 {
671 8 my ($text) = @_;
672  
673 # LEAVE_SRC: decode must not empty the caller's string. FB_CROAK:
674 # invalid UTF-8 dies inside the eval and the text is kept as it is.
675 8 ▶ return utf8::is_utf8($text) ? $text : eval { decode('UTF-8', $text, FB_CROAK | LEAVE_SRC) } // $text;
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($text) ? : 0 8 8
Condition66%2/3
eval { do { &decode("UTF-8", $text, 9) } } // $text
l!l&&r!l&&!r
710
MC/DC0%0/2
eval { do { &decode("UTF-8", $text, 9) } } // $text
eval { do { &decode("UTF-8", $text, 9) } } $text
Truth table66%2/3
A // B
A= eval { do { &decode("UTF-8", $text, 9) } }B= $text
ABresult
1X1
011
000
676 26 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
677  
678 # _ascii_host
679 # Purpose: the ASCII (Punycode) form of a host name, so names can be
680 # compared and looked up whichever way they were written.
681 # Entry: $host is undef, ASCII, UTF-8 bytes or a character string.
682 # Exit: the lower-case ASCII name; the name unchanged if it was
683 # already ASCII; undef if it cannot be converted.
684 # Side effects: none.
685 66 ▶sub _ascii_host :Private
Subroutine _ascii_host: called CC=6 SCAR=19.1
686 {
687 66 my ($host) = @_;
688  
689 66 ▶ return $host unless defined $host && $host =~ $NON_ASCII;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and $host =~ /$NON_ASCII/ 59 7 66
Condition100%3/3
defined $host and $host =~ /$NON_ASCII/
!ll&&!rl&&r
1587
MC/DC100%2/2
defined $host and $host =~ /$NON_ASCII/
defined $host $host =~ /$NON_ASCII/
Truth table100%3/3
A and B
A= defined $hostB= $host =~ /$NON_ASCII/
ABresult
0X0
100
111
690  
691 # URI applies the IDNA rules (lower case, then Punycode per label).
692 7 my $ascii = eval { URI->new('http://' . _characters($host) . '/')->host };
693  
694 7 ▶ return (defined $ascii && length $ascii && $ascii !~ $NON_ASCII) ? lc $ascii : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $ascii && length $ascii && !($ascii =~ /$NON_ASCII/) ? : 7 0 7
Condition33%2/6
defined $ascii and length $ascii and not $ascii =~ /$NON_ASCII/
!ll&&!rl&&r
007
defined $ascii and length $ascii
!ll&&!rl&&r
007
MC/DC0%0/3
defined $ascii and length $ascii and not $ascii =~ /$NON_ASCII/
defined $ascii length $ascii not $ascii =~ /$NON_ASCII/
Truth table25%1/4
A and B and not(C)
A= defined $asciiB= length $asciiC= not $ascii =~ /$NON_ASCII/
ABCresult
0XX0
10X0
1100
1111
695 26 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
696  
697 # _addresses
698 # Purpose: every numeric address a host resolves to, IPv4 and IPv6.
699 # Entry: $host is a hostname or address literal (brackets allowed).
700 # Exit: arrayref of address strings; empty if it does not resolve.
701 # Side effects: a DNS lookup.
702 31 ▶sub _addresses :Private
Subroutine _addresses: called CC=10 SCAR=23.9
703 {
704 31 my ($host) = @_;
705  
706 # getaddrinfo needs the ASCII form of an international name.
707 31 $host = _ascii_host($host);
708 31 ▶ return [] unless defined $host && length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 31 31
Condition33%1/3
defined $host and length $host
!ll&&!rl&&r
0031
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table33%1/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
709 31 (my $name = $host) =~ s/\A\[(.*)\]\z/$1/;
710 31 (my $literal = $name) =~ s/%.*\z//; # an IPv6 zone index such as %eth0
711  
712 # An address literal needs no lookup, and must not depend on one: some
713 # Windows builds of getaddrinfo fail on IPv6 literals, and an empty
714 # result here would let ::1 through as "not private".
715 31 ▶ return [ $literal ] if is_ipv4($literal) || is_ipv6($literal);
Branch: 1 branch
BranchTrueFalseTotal
if is_ipv4($literal) or is_ipv6($literal) 27 4 31
Condition100%3/3
is_ipv4($literal) or is_ipv6($literal)
l!l&&r!l&&!r
1984
MC/DC100%2/2
is_ipv4($literal) or is_ipv6($literal)
is_ipv4($literal) is_ipv6($literal)
Truth table100%3/3
A or B
A= is_ipv4($literal)B= is_ipv6($literal)
ABresult
1X1
011
000
716  
717 4 my ($error, @results) = getaddrinfo($name, '', { socktype => SOCK_STREAM });
718 4 ▶ return [] if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 3 1 4
719  
720 1 my %seen;
721 my @addresses;
722 1 for my $result (@results) {
723 1 my ($name_error, $address) = getnameinfo($result->{addr}, NI_NUMERICHOST, NIx_NOSERV);
724 1 ▶ next if $name_error || !defined $address;
Branch: 1 branch
BranchTrueFalseTotal
if $name_error or not defined $address 0 1 1
Condition33%1/3
$name_error or not defined $address
l!l&&r!l&&!r
001
MC/DC0%0/2
$name_error or not defined $address
$name_error not defined $address
Truth table33%1/3
A or not(B)
A= $name_errorB= not defined $address
ABresult
1X1
011
000
725 1 $address =~ s/%.*\z//; # drop an IPv6 zone index such as %eth0
726 1 ▶ push @addresses, $address unless $seen{$address}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$address}++ 1 0 1
727 }
728  
729 1 return \@addresses;
730 26 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
731  
732 # _os_error
733 # Purpose: the operating system's reason from a failed autodie call.
734 # Entry: $exception is $@ after an autodie failure (object or string).
735 # Exit: the errno text as Perl stringified it, or the cleaned message.
736 # Side effects: none.
737 5 ▶sub _os_error :Private
Subroutine _os_error: called CC=5 SCAR=20.1
738 {
739 5 my ($exception) = @_;
740  
741 # autodie::exception records $! as it was at the moment of failure;
742 # reading $! here instead could give a later, unrelated error.
743 5 ▶ my $errno = (ref $exception && $exception->can('errno')) ? $exception->errno : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $exception && $exception->can('errno') ? : 5 0 5
Condition33%1/3
ref $exception and $exception->can("errno")
!ll&&!rl&&r
005
MC/DC0%0/2
ref $exception and $exception->can("errno")
ref $exception $exception->can("errno")
Truth table33%1/3
A and B
A= ref $exceptionB= $exception->can("errno")
ABresult
0X0
100
111
744  
745 5 ▶ return (defined $errno && length $errno) ? "$errno" : App::WebPerimeterScan::I18N::clean_error($exception);
Branch: 1 branch
BranchTrueFalseTotal
defined $errno && length $errno ? : 5 0 5
Condition33%1/3
defined $errno and length $errno
!ll&&!rl&&r
005
MC/DC0%0/2
defined $errno and length $errno
defined $errno length $errno
Truth table33%1/3
A and B
A= defined $errnoB= length $errno
ABresult
0X0
100
111
746 26 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
747  
748 26 1;
749  
750 __END__
« lib/App/WebPerimeterScan/TLS.pm ↑ Summary lib/App/WebPerimeterScan.pm »