blib/lib/Database/BI/SafeUA.pm

filter: Statementstmt 58.3% 77 / 132 Branchbran 43.2% 32 / 74 Conditioncond 17.0% 8 / 47 MC/DCmcdc 3.8% 1 / 26 Subroutinesub 73.3% 11 / 15 Podpod 100% 6 / 6 totaltotal 45.0% 135 / 300 CC 65 SCAR 42.7
CC
65
Cov
46%
send_request
272.0
private_host_allowed
56.0
resolve_host
42.0
CRAP 71.8
« blib/lib/Database/BI/Model/DataSource.pm ↑ Summary blib/lib/Database/BI.pm »
1 package Database::BI::SafeUA;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.023.1';
7  
8 2 ▶use parent 'LWP::UserAgent';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Socket qw(getaddrinfo getnameinfo inet_ntoa inet_pton AF_INET AF_INET6 SOCK_STREAM NI_NUMERICHOST NIx_NOSERV);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # The fetcher for url: sources and /import. A page that names a URL gets the
15 # server to fetch it, so the URL must not lead to this machine or a private
16 # network (SSRF): an admin page on 127.0.0.1, a database on 10.x, a cloud's
17 # metadata service on 169.254.169.254. Checking the URL's text is not enough
18 # (a name such as localtest.me, or one whose DNS answers 127.0.0.1, says
19 # nothing in its text), and resolving it first is not enough either: LWP
20 # resolves again when it connects, and a name can answer differently the
21 # second time (DNS rebinding). So every request, redirects included, is
22 # resolved here, refused if any address is not public, and sent to the
23 # address that was checked, with the name kept for the Host header, the TLS
24 # server name and the certificate check.
25  
26 2 Readonly my $MAX_REDIRECT => 5;
27 2 Readonly my $TIMEOUT => 30;
28  
29 # [ network, prefix length, why ] for IPv4 and IPv6. An address in any of them
30 # is refused.
31 2 Readonly my @V4_BLOCKED => (
32 [ '0.0.0.0', 8, 'not an address' ],
33 [ '10.0.0.0', 8, 'a private network' ],
34 [ '100.64.0.0', 10, 'a shared (carrier) network' ],
35 [ '127.0.0.0', 8, 'this machine' ],
36 [ '169.254.0.0', 16, 'link-local, where cloud metadata services are' ],
37 [ '172.16.0.0', 12, 'a private network' ],
38 [ '192.0.0.0', 24, 'reserved' ],
39 [ '192.0.2.0', 24, 'reserved for documentation' ],
40 [ '192.168.0.0', 16, 'a private network' ],
41 [ '198.18.0.0', 15, 'reserved for testing' ],
42 [ '198.51.100.0', 24, 'reserved for documentation' ],
43 [ '203.0.113.0', 24, 'reserved for documentation' ],
44 [ '224.0.0.0', 4, 'multicast' ],
45 [ '240.0.0.0', 4, 'reserved' ],
46 );
47 2 Readonly my @V6_BLOCKED => (
48 [ '::', 128, 'not an address' ],
49 [ '::1', 128, 'this machine' ],
50 [ '100::', 64, 'reserved' ],
51 [ '2001:db8::', 32, 'reserved for documentation' ],
52 [ 'fc00::', 7, 'a private network' ],
53 [ 'fe80::', 10, 'link-local' ],
54 [ 'fec0::', 10, 'a private network' ],
55 [ 'ff00::', 8, 'multicast' ],
56 );
57  
58 149 ▶sub _in_net {
Subroutine _in_net: called CC=4 SCAR=13.9
59 149 my ($packed, $net, $bits) = @_;
60 149 my $full = int($bits / 8);
61 149 ▶ return 0 unless substr($packed, 0, $full) eq substr($net, 0, $full);
Branch: 1 branch
BranchTrueFalseTotal
unless substr($packed, 0, $full) eq substr($net, 0, $full) 120 29 149
62 29 ▶ my $rest = $bits % 8 or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $rest = $bits % 8 or return 1 18 11 29
63 18 my $mask = (0xff << (8 - $rest)) & 0xff;
64 18 ▶ return (ord(substr($packed, $full, 1)) & $mask) == (ord(substr($net, $full, 1)) & $mask) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(ord substr($packed, $full, 1) & $mask) == (ord substr($net, $full, 1) & $mask) ? : 4 14 18
65 }
66  
67 21 ▶sub blocked_address {
Subroutine blocked_address: called CC=15 SCAR=29.6
Pod documented
68 21 my ($ip) = @_;
69 21 ▶ return 'not an address' unless defined $ip && length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and length $ip 0 21 21
Condition33%1/3
defined $ip and length $ip
!ll&&!rl&&r
0021
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
70 21 (my $bare = $ip) =~ s/\A\[|\]\z//g;
71 21 $bare =~ s/%.*\z//; # a zone index (fe80::1%eth0)
72  
73 21 ▶ if ($bare =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bare =~ /:/) 1 20 21
74 1 ▶ my $packed = inet_pton(AF_INET6, $bare) // return 'not an address';
Condition50%1/2
inet_pton(10, $bare) // (return "not an address")
l!l
10
MC/DC0%0/1
inet_pton(10, $bare) // (return "not an address")
inet_pton(10, $bare)
Truth table50%1/2
A
A= inet_pton(10, $bare)
Aresult
11
00
75 # IPv4 inside IPv6: mapped (::ffff:a.b.c.d), NAT64 (64:ff9b::a.b.c.d),
76 # compatible (::a.b.c.d) and 6to4 (2002:aabb:ccdd::) all reach that
77 # IPv4 address, so it is the one checked.
78 1 for my $prefix ("\0" x 10 . "\xff\xff", "\0\x64\xff\x9b" . "\0" x 8, "\0" x 12) {
79 3 ▶ return blocked_address(inet_ntoa(substr($packed, 12, 4)))
Branch: 1 branch
BranchTrueFalseTotal
if substr($packed, 0, 12) eq $prefix and $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1" and $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" 0 3 3
Condition50%3/6
substr($packed, 0, 12) eq $prefix and $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1" and $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
!ll&&!rl&&r
300
substr($packed, 0, 12) eq $prefix and $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1"
!ll&&!rl&&r
210
MC/DC0%0/3
substr($packed, 0, 12) eq $prefix and $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1" and $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
substr($packed, 0, 12) eq $prefix $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1" $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
Truth table50%2/4
A and B and C
A= substr($packed, 0, 12) eq $prefixB= $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1"C= $packed ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
ABCresult
0XX0
10X0
1100
1111
80 if substr($packed, 0, 12) eq $prefix && $packed ne "\0" x 15 . "\1" && $packed ne "\0" x 16;
81 }
82 1 ▶ return blocked_address(inet_ntoa(substr($packed, 2, 4))) if substr($packed, 0, 2) eq "\x20\x02";
Branch: 1 branch
BranchTrueFalseTotal
if substr($packed, 0, 2) eq " \2" 0 1 1
83 1 for my $net (@V6_BLOCKED) {
84 2 ▶ return $net->[2] if _in_net($packed, inet_pton(AF_INET6, $net->[0]), $net->[1]);
Branch: 1 branch
BranchTrueFalseTotal
if _in_net($packed, inet_pton(10, $$net[0]), $$net[1]) 1 1 2
85 }
86 0 return undef;
87 }
88  
89 20 ▶ my $packed = inet_pton(AF_INET, $bare) // return 'not an address';
Condition50%1/2
inet_pton(2, $bare) // (return "not an address")
l!l
200
MC/DC0%0/1
inet_pton(2, $bare) // (return "not an address")
inet_pton(2, $bare)
Truth table50%1/2
A
A= inet_pton(2, $bare)
Aresult
11
00
90 20 for my $net (@V4_BLOCKED) {
91 147 ▶ return $net->[2] if _in_net($packed, inet_pton(AF_INET, $net->[0]), $net->[1]);
Branch: 1 branch
BranchTrueFalseTotal
if _in_net($packed, inet_pton(2, $$net[0]), $$net[1]) 14 133 147
92 }
93 6 return undef;
94 }
95  
96 26 ▶sub numeric_host {
Subroutine numeric_host: called CC=6 SCAR=20.5
Pod documented
97 26 my ($host) = @_;
98 26 ▶ return undef unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 26 26
99 26 (my $h = lc $host) =~ s/\.\z//;
100 26 ▶ if ($h =~ /\A\[([0-9a-f:.%a-z]+)\]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h =~ /\A\[([0-9a-f:.%a-z]+)\]\z/) 0 26 26
101 0 my $inner = $1;
102 0 ▶ return $inner =~ /:/ ? $inner : undef;
Branch: 1 branch
BranchTrueFalseTotal
$inner =~ /:/ ? : 0 0 0
103 }
104 26 ▶ return $h if $h =~ /:/ && defined inet_pton(AF_INET6, $h);
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /:/ and defined inet_pton(10, $h) 2 24 26
Condition66%2/3
$h =~ /:/ and defined inet_pton(10, $h)
!ll&&!rl&&r
2402
MC/DC50%1/2
$h =~ /:/ and defined inet_pton(10, $h)
$h =~ /:/ defined inet_pton(10, $h)
Truth table66%2/3
A and B
A= $h =~ /:/B= defined inet_pton(10, $h)
ABresult
0X0
100
111
105 24 return _numeric_v4($h);
106 }
107  
108 # _numeric_v4($host) -> 'a.b.c.d' | undef
109 #
110 # The forms the BSD and glibc inet_aton read as an address: 127.1,
111 # 2130706433, 0x7f000001, 0177.0.0.1. Each part is decimal, 0x hex or
112 # 0-led octal; the last fills the bits the others leave. Read here rather
113 # than by the system's inet_aton, whose reading differs (on macOS 0177.0.0.1 is
114 # not 127.0.0.1), so every platform checks, and connects to, the same address.
115 24 ▶sub _numeric_v4 {
Subroutine _numeric_v4: called CC=14 SCAR=30.6
116 24 my ($h) = @_;
117 24 ▶ return undef unless $h =~ /\A(?:0x[0-9a-f]+|[0-9]+)(?:\.(?:0x[0-9a-f]+|[0-9]+)){0,3}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $h =~ /\A(?:0x[0-9a-f]+|[0-9]+)(?:\.(?:0x[0-9a-f]+|[0-9]+)){0,3}\z/ 4 20 24
118 20 my @parts;
119 20 for my $part (split /\./, $h) {
120 80 my $n;
121 80 ▶ if ($part =~ /\A0x([0-9a-f]*)\z/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($part =~ /\A0x([0-9a-f]*)\z/) { } 0 80 80
elsif ($part =~ /\A0([0-7]*)\z/) { } 19 61 80
elsif ($part =~ /\A[1-9][0-9]{0,9}\z/) { } 61 0 61
122 0 my $hex = $1 =~ s/\A0+//r;
123 0 ▶ return undef if length($hex) > 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $hex > 8 0 0 0
124 0 ▶ $n = length $hex ? hex $hex : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $hex ? : 0 0 0
125 } elsif ($part =~ /\A0([0-7]*)\z/) {
126 19 my $oct = $1 =~ s/\A0+//r;
127 19 ▶ return undef if length($oct) > 11;
Branch: 1 branch
BranchTrueFalseTotal
if length $oct > 11 0 19 19
128 19 ▶ $n = length $oct ? oct $oct : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $oct ? : 0 19 19
129 } elsif ($part =~ /\A[1-9][0-9]{0,9}\z/) {
130 61 $n = $part + 0;
131 } else {
132 0 return undef; # 08, or too long to be a number
133 }
134 80 push @parts, $n;
135 }
136 20 my $last = pop @parts;
137 20 for my $n (@parts) {
138 60 ▶ return undef if $n > 0xff;
Branch: 1 branch
BranchTrueFalseTotal
if $n > 255 0 60 60
139 }
140 20 ▶ return undef if $last >= 2 ** (8 * (4 - @parts));
Branch: 1 branch
BranchTrueFalseTotal
if $last >= 2 ** (8 * (4 - @parts)) 0 20 20
141 20 my $value = $last;
142 20 $value += $parts[$_] * 2 ** (8 * (3 - $_)) for 0 .. $#parts;
143 20 return join '.', unpack 'C4', pack 'N', $value;
144 }
145  
146 0 ▶sub resolve_host {
Subroutine resolve_host: not called CC=6 SCAR=37.4
Pod documented
147 0 my ($host) = @_;
148 0 local ($@, $!);
149 0 my ($err, @res) = getaddrinfo($host, undef, { socktype => SOCK_STREAM });
150 0 ▶ return () if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
151 0 my (%seen, @ips);
152 0 for my $r (@res) {
153 0 my ($e, $ip) = getnameinfo($r->{addr}, NI_NUMERICHOST, NIx_NOSERV);
154 0 ▶ push @ips, $ip if !$e && defined $ip && !$seen{$ip}++;
Branch: 1 branch
BranchTrueFalseTotal
if not $e and defined $ip and not $seen{$ip}++ 0 0 0
Condition0%0/6
not $e and defined $ip and not $seen{$ip}++
!ll&&!rl&&r
000
not $e and defined $ip
!ll&&!rl&&r
000
MC/DC0%0/3
not $e and defined $ip and not $seen{$ip}++
not $e defined $ip not $seen{$ip}++
Truth table0%0/4
not(A) and B and not(C)
A= not $eB= defined $ipC= not $seen{$ip}++
ABCresult
0XX0
10X0
1100
1111
155 }
156 0 return @ips;
157 }
158  
159 4 ▶sub new {
Subroutine new: called CC=4 SCAR=15.5
Pod documented
160 4 my ($class, %args) = @_;
161 4 my $resolve = delete $args{resolve};
162 4 my $private = delete $args{private_hosts};
163 4 my $self = $class->SUPER::new(
164 timeout => $TIMEOUT,
165 max_redirect => $MAX_REDIRECT,
166 protocols_allowed => [ 'http', 'https' ],
167 agent => "Database-BI/$VERSION",
168 %args,
169 );
170 4 ▶ $self->{bi_resolve} = ref($resolve) eq 'CODE' ? $resolve : \&resolve_host;
Branch: 1 branch
BranchTrueFalseTotal
ref $resolve eq 'CODE' ? : 0 4 4
171 0 ▶ $self->{bi_private} = [ map { lc } grep { defined && !ref } @{ ref($private) eq 'ARRAY' ? $private : [] } ];
Branch: 1 branch
BranchTrueFalseTotal
ref $private eq 'ARRAY' ? : 2 2 4
Condition0%0/3
defined $_ && !ref($_)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table0%0/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
172 4 return $self;
173 }
174  
175 0 ▶sub private_host_allowed {
Subroutine private_host_allowed: not called CC=7 SCAR=40.3
Pod documented
176 0 my ($self, $host) = @_;
177 0 ▶ $host = lc($host // '');
Condition0%0/2
$host // ""
l!l
00
MC/DC0%0/1
$host // ""
$host
Truth table0%0/2
A
A= $host
Aresult
11
00
178 0 for my $name (@{ $self->{bi_private} }) {
179 0 ▶ return 1 if $name eq $host;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $host 0 0 0
180 0 ▶ return 1 if $name =~ /\A\*(\..+)\z/ && length($host) > length($1) && substr($host, -length $1) eq $1;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A\*(\..+)\z/ and length $host > length $1 and substr($host, -length($1)) eq $1 0 0 0
Condition0%0/6
$name =~ /\A\*(\..+)\z/ and length $host > length $1 and substr($host, -length($1)) eq $1
!ll&&!rl&&r
000
$name =~ /\A\*(\..+)\z/ and length $host > length $1
!ll&&!rl&&r
000
MC/DC0%0/3
$name =~ /\A\*(\..+)\z/ and length $host > length $1 and substr($host, -length($1)) eq $1
$name =~ /\A\*(\..+)\z/ length $host > length $1 substr($host, -length($1)) eq $1
Truth table0%0/4
A and B and C
A= $name =~ /\A\*(\..+)\z/B= length $host > length $1C= substr($host, -length($1)) eq $1
ABCresult
0XX0
10X0
1100
1111
181 }
182 0 return 0;
183 }
184  
185 0 ▶sub _refuse {
Subroutine _refuse: not called CC=1 SCAR=6.9
186 0 my ($request, $why) = @_;
187 0 my $res = HTTP::Response->new(403, "Refused: $why");
188 0 $res->header('Client-Warning' => 'Internal response');
189 0 $res->request($request);
190 0 return $res;
191 }
192  
193 0 ▶sub send_request {
Subroutine send_request: not called CC=16 SCAR=56.1
Pod documented
194 0 my ($self, $request, $arg, $size) = @_;
195 0 my $uri = $request->uri;
196 0 ▶ my $scheme = lc($uri->scheme // '');
Condition0%0/2
$uri->scheme // ""
l!l
00
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table0%0/2
A
A= $uri->scheme
Aresult
11
00
197 0 ▶ return _refuse($request, 'only http and https addresses are fetched')
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme eq "http" or $scheme eq "https" 0 0 0
Condition0%0/3
$scheme eq "http" or $scheme eq "https"
l!l&&r!l&&!r
000
MC/DC0%0/2
$scheme eq "http" or $scheme eq "https"
$scheme eq "http" $scheme eq "https"
Truth table0%0/3
A or B
A= $scheme eq "http"B= $scheme eq "https"
ABresult
1X1
011
000
198 unless $scheme eq 'http' || $scheme eq 'https';
199 0 ▶ (my $name = lc($uri->host // '')) =~ s/\.\z//;
Condition0%0/2
$uri->host // ""
l!l
00
MC/DC0%0/1
$uri->host // ""
$uri->host
Truth table0%0/2
A
A= $uri->host
Aresult
11
00
200 0 ▶ return _refuse($request, 'the address names no host') unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 0 0
201  
202 0 my $literal = numeric_host($name);
203 0 ▶ my @addrs = defined $literal ? ($literal) : $self->{bi_resolve}->($name);
Branch: 1 branch
BranchTrueFalseTotal
defined $literal ? : 0 0 0
204 0 ▶ return _refuse($request, "$name was not found") unless @addrs;
Branch: 1 branch
BranchTrueFalseTotal
unless @addrs 0 0 0
205 0 ▶ unless (!defined $literal && $self->private_host_allowed($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $literal and $self->private_host_allowed($name)) 0 0 0
Condition0%0/3
not defined $literal and $self->private_host_allowed($name)
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $literal and $self->private_host_allowed($name)
not defined $literal $self->private_host_allowed($name)
Truth table0%0/3
not(A) and B
A= not defined $literalB= $self->private_host_allowed($name)
ABresult
0X0
100
111
206 0 for my $ip (@addrs) {
207 0 ▶ my $why = blocked_address($ip) // next;
Condition0%0/2
blocked_address($ip) // (next)
l!l
00
MC/DC0%0/1
blocked_address($ip) // (next)
blocked_address($ip)
Truth table0%0/2
A
A= blocked_address($ip)
Aresult
11
00
208 0 ▶ return _refuse($request, $ip eq $name ? "$name is $why" : "$name is $ip, which is $why");
Branch: 1 branch
BranchTrueFalseTotal
$ip eq $name ? : 0 0 0
209 }
210 }
211  
212 # Connect to the address checked, not to the name again.
213 0 my $pinned = $request->clone;
214 0 my $to = $uri->clone;
215 0 ▶ $to->host($addrs[0] =~ /:/ ? "[$addrs[0]]" : $addrs[0]);
Branch: 1 branch
BranchTrueFalseTotal
$addrs[0] =~ /:/ ? : 0 0 0
216 0 $pinned->uri($to);
217 0 my $authority = $uri->authority;
218 0 $authority =~ s/\A[^\@]*\@//;
219 0 $pinned->header(Host => $authority);
220 local $self->{ssl_opts} = $scheme eq 'https'
221 0 ▶ ? { %{ $self->{ssl_opts} // {} }, SSL_hostname => $uri->host, SSL_verifycn_name => $uri->host }
Condition0%0/2
$$self{"ssl_opts"} // {}
l!l
00
MC/DC0%0/1
$$self{"ssl_opts"} // {}
$$self{"ssl_opts"}
Truth table0%0/2
A
A= $$self{"ssl_opts"}
Aresult
11
00
222 0 ▶ : $self->{ssl_opts};
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'https' ? : 0 0 0
223  
224 0 my $res = $self->SUPER::send_request($pinned, $arg, $size);
225 0 $res->request($request);
226 0 return $res;
227 }
228  
229 2 1;
230  
231 __END__
« blib/lib/Database/BI/Model/DataSource.pm ↑ Summary blib/lib/Database/BI.pm »