VPNDetection/Bogon.pm

filter: Statementstmt 100% 72 / 72 Branchbran 100% 24 / 24 Conditioncond 76.9% 10 / 13 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 3 totaltotal 93.2% 125 / 134 CC 21 SCAR 14.7
CC
21
Cov
97%
is_bogon
8.0
bogon_result
6.0
unmapped
5.1
CRAP 4.4
« VPNDetection/Error.pm ↑ Summary VPNDetection/Result.pm »
1 package VPNDetection::Bogon;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Mojo::JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Socket qw(AF_INET AF_INET6 inet_pton);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use VPNDetection::Bogons;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use VPNDetection::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 our $VERSION = '3.19.0';
13  
14 # Whether an address is private, loopback, link-local, documentation, multicast
15 # or otherwise not routable on the public internet, including the IPv6
16 # equivalents and the 6to4 and Teredo ranges that wrap them.
17 #
18 # These can never be VPN or proxy infrastructure, so the client answers them
19 # itself and they never cost a request.
20 29622 ▶sub is_bogon {
Subroutine is_bogon: called CC=8 SCAR=20.8
Pod undocumented
21 29622 my ($ip) = @_;
22 29622 ▶ return 0 unless defined $ip && length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and length $ip 1 29621 29622
Condition66%2/3
defined $ip and length $ip
!ll&&!rl&&r
0129621
MC/DC50%1/2
defined $ip and length $ip
defined $ip length $ip
Truth table66%2/3
A and B
A= defined $ipB= length $ip
ABresult
0X0
100
111
23  
24 29621 $ip = unmapped($ip);
25 29621 my $v6 = index($ip, ':') >= 0;
26 29621 ▶ my $addr = _pton($v6 ? AF_INET6 : AF_INET, $ip);
Branch: 1 branch
BranchTrueFalseTotal
$v6 ? : 417 29204 29621
27 29621 ▶ return 0 unless defined $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 3 29618 29621
28  
29 29618 ▶ for my $range (@{ $v6 ? _v6() : _v4() }) {
Branch: 1 branch
BranchTrueFalseTotal
$v6 ? : 417 29201 29618
30 484706 ▶ return 1 if ($addr & $range->[1]) eq $range->[0];
Branch: 1 branch
BranchTrueFalseTotal
if ($addr & $range->[1]) eq $range->[0] 378 484328 484706
31 }
32 29240 return 0;
33 }
34  
35 # The IPv4 address an IPv4-mapped IPv6 address (::ffff:a.b.c.d, in any spelling)
36 # carries, dotted, and any other string as given. A server listening on :: sees
37 # every IPv4 visitor in that form, which read whole is inside ::ffff:0:0/96, so
38 # judging it whole would answer every such visitor locally as a bogon. ::a.b.c.d
39 # is IPv4-compatible rather than mapped, and stays IPv6.
40 87263 ▶sub unmapped {
Subroutine unmapped: called CC=5 SCAR=16.2
Pod undocumented
41 87263 my ($ip) = @_;
42 87263 ▶ return $ip unless defined $ip && index($ip, ':') >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and index($ip, ":") != -1 86820 443 87263
Condition66%2/3
defined $ip and index($ip, ":") != -1
!ll&&!rl&&r
086820443
MC/DC50%1/2
defined $ip and index($ip, ":") != -1
defined $ip index($ip, ":") != -1
Truth table66%2/3
A and B
A= defined $ipB= index($ip, ":") != -1
ABresult
0X0
100
111
43 443 my $addr = _pton(AF_INET6, $ip);
44 443 ▶ return $ip unless defined $addr && substr($addr, 0, 12) eq ("\0" x 10) . "\xff\xff";
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr and substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377" 419 24 443
Condition66%2/3
defined $addr and substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
!ll&&!rl&&r
041924
MC/DC50%1/2
defined $addr and substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
defined $addr substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
Truth table66%2/3
A and B
A= defined $addrB= substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
ABresult
0X0
100
111
45 24 return join '.', unpack 'C4', substr($addr, 12);
46 }
47  
48 # The answer a bogon gets, in the full shape the API serves at its widest plan:
49 # every flag present and false, every detail object present and empty, web an
50 # empty list, the hostname an empty string, and a location, AS, company and abuse
51 # contact of empty strings and zeros, abuse's validated false.
52 8 ▶sub bogon_result {
Subroutine bogon_result: called CC=6 SCAR=17.9
Pod undocumented
53 8 my ($ip) = @_;
54 8 my %answer = (ip => $ip, is_bogon => 1, hostname => '');
55 8 $answer{$_} = 0 for @VPNDetection::Result::FLAGS;
56 8 ▶ $answer{$_} = $_ eq 'web' ? [] : {} for @VPNDetection::Result::DETAILS;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'web' ? : 8 128 136
57 8 my %numbers = map { $_ => 1 } qw(latitude longitude accuracy_radius asn);
58 8 ▶ $answer{location} = { map { $_ => $numbers{$_} ? 0 : '' } @VPNDetection::Result::LOCATION };
Branch: 1 branch
BranchTrueFalseTotal
$numbers{$_} ? : 24 136 160
59 8 ▶ $answer{asn} = { map { $_ => $numbers{$_} ? 0 : '' } @VPNDetection::Result::ASN };
Branch: 1 branch
BranchTrueFalseTotal
$numbers{$_} ? : 8 48 56
60 8 $answer{company} = { map { $_ => '' } @VPNDetection::Result::COMPANY };
61 8 $answer{abuse} = { map { $_ => '' } @VPNDetection::Result::ABUSE };
62 8 $answer{abuse}{validated} = Mojo::JSON::false;
63 8 return VPNDetection::Result->_new(\%answer, {});
64 }
65  
66 # Parsed on first use rather than at load: a program that never looks an address
67 # up should not pay for the table.
68 10 my ($v4, $v6);
69  
70 29201 ▶sub _v4 {
Subroutine _v4: called CC=2 SCAR=6.9
71 29201 ▶ $v4 ||= [map { _range(AF_INET, $_, 4) } @VPNDetection::Bogons::V4];
Condition100%2/2
$v4 ||= [map({_range(2, $_, 4);} @VPNDetection::Bogons::V4)]
l!l
291956
MC/DC100%1/1
$v4 ||= [map({_range(2, $_, 4);} @VPNDetection::Bogons::V4)]
$v4
Truth table100%2/2
A
A= $v4
Aresult
11
00
72 29201 return $v4;
73 }
74  
75 417 ▶sub _v6 {
Subroutine _v6: called CC=2 SCAR=6.9
76 417 ▶ $v6 ||= [map { _range(AF_INET6, $_, 16) } @VPNDetection::Bogons::V6];
Condition100%2/2
$v6 ||= [map({_range(10, $_, 16);} @VPNDetection::Bogons::V6)]
l!l
4143
MC/DC100%1/1
$v6 ||= [map({_range(10, $_, 16);} @VPNDetection::Bogons::V6)]
$v6
Truth table100%2/2
A
A= $v6
Aresult
11
00
77 417 return $v6;
78 }
79  
80 # A range is [masked network bytes, mask bytes], both packed strings, so
81 # membership is one bitwise string AND and one string comparison. Perl has no
82 # 128 bit integer, and it does not need one: `&` on two byte strings is a
83 # per-byte AND, so a /36 IPv6 mask is just a 16 byte string.
84 #
85 # That behavior is why no file in this distribution says `use v5.28` or later:
86 # those enable the `bitwise` feature, under which `&` becomes numeric-only, both
87 # operands numify to 0, and EVERY address would match EVERY range. The isBogon
88 # corpus is what catches it.
89 288 ▶sub _range {
Subroutine _range: called CC=1 SCAR=0.0
90 288 my ($family, $cidr, $width) = @_;
91 288 my ($net, $bits) = split m{/}, $cidr, 2;
92 288 my $mask = _mask($bits, $width);
93 288 return [inet_pton($family, $net) & $mask, $mask];
94 }
95  
96 288 ▶sub _mask {
Subroutine _mask: called CC=2 SCAR=6.9
97 288 my ($bits, $width) = @_;
98 288 my $mask = "\xff" x int($bits / 8);
99 288 my $partial = $bits % 8;
100 288 ▶ $mask .= chr((0xff << (8 - $partial)) & 0xff) if $partial;
Branch: 1 branch
BranchTrueFalseTotal
if $partial 114 174 288
101 288 return $mask . "\x00" x ($width - length $mask);
102 }
103  
104 # inet_pton is strict: it rejects a prefix, a shortened dotted quad and junk, so
105 # no separate validation is needed. It dies rather than returning undef on some
106 # platforms, hence the eval.
107 30064 ▶sub _pton {
Subroutine _pton: called CC=2 SCAR=6.9
108 30064 my ($family, $ip) = @_;
109 30064 my $packed = eval { inet_pton($family, $ip) };
110 30064 ▶ return undef unless defined $packed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $packed 3 30061 30064
111 30061 return $packed;
112 }
113  
114 10 1;
115  
116 __END__
« VPNDetection/Error.pm ↑ Summary VPNDetection/Result.pm »