| 1 | package Database::BI::SafeUA; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | our $VERSION = '0.024.0'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use parent 'LWP::UserAgent'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use HTTP::Response; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | 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 | 7 | Readonly my $MAX_REDIRECT => 5; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | Readonly my $TIMEOUT => 30; | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # [ network, prefix length, why ] for IPv4 and IPv6. An address in any of them | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # is refused. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | 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 | 7 | 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 | 1718 | ▶ | sub _in_net { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_in_net: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1718 | my ($packed, $net, $bits) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1718 | my $full = int($bits / 8); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 1718 | ▶ | return 0 unless substr($packed, 0, $full) eq substr($net, 0, $full); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 321 | ▶ | my $rest = $bits % 8 or return 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 190 | my $mask = (0xff << (8 - $rest)) & 0xff; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 190 | ▶ | return (ord(substr($packed, $full, 1)) & $mask) == (ord(substr($net, $full, 1)) & $mask) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 335 | ▶ | sub blocked_address { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
blocked_address: called CC=16 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 335 | my ($ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 335 | ▶ | return 'not an address' unless defined $ip && length $ip; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ip and length $ip
MC/DC100%2/2
defined $ip and length $ip
defined $ip
length $ip
Truth table100%3/3
A and B
A= defined $ipB= length $ip
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 331 | (my $bare = $ip) =~ s/\A\[|\]\z//g; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 331 | $bare =~ s/%.*\z//; # a zone index (fe80::1%eth0) | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 331 | ▶ | if ($bare =~ /:/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 89 | ▶ | my $packed = inet_pton(AF_INET6, $bare) // return 'not an address'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
inet_pton(10, $bare) // (return "not an address")
MC/DC100%1/1
inet_pton(10, $bare) // (return "not an address")
inet_pton(10, $bare)
Truth table100%2/2
A
A= inet_pton(10, $bare)
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 87 | for my $prefix ("\0" x 10 . "\xff\xff", "\0\x64\xff\x9b" . "\0" x 8, "\0" x 12) { | |||||||||||||||||||||||||||||||||||||||||
| 79 | 165 | ▶ | return blocked_address(inet_ntoa(substr($packed, 12, 4))) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/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"
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"
MC/DC66%2/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 table75%3/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"
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | if substr($packed, 0, 12) eq $prefix && $packed ne "\0" x 15 . "\1" && $packed ne "\0" x 16; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 30 | ▶ | return blocked_address(inet_ntoa(substr($packed, 2, 4))) if substr($packed, 0, 2) eq "\x20\x02"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 13 | for my $net (@V6_BLOCKED) { | |||||||||||||||||||||||||||||||||||||||||
| 84 | 61 | ▶ | return $net->[2] if _in_net($packed, inet_pton(AF_INET6, $net->[0]), $net->[1]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | # Premise: only a full dotted quad names one address on every platform; | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # Windows' inet_pton also reads 1.2.3 as 1.2.0.3. Conclusion: anything | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # else is not an address here (numeric_host spells the short forms out). | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # A leading zero (10.0.0.01) is octal to some libraries and decimal to | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # others, so it names no one address either: refused, not guessed. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 242 | ▶ | return 'not an address' unless $bare =~ /\A(?:0|[1-9][0-9]{0,2})(?:\.(?:0|[1-9][0-9]{0,2})){3}\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 223 | ▶ | my $packed = inet_pton(AF_INET, $bare) // return 'not an address'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
inet_pton(2, $bare) // (return "not an address")
MC/DC100%1/1
inet_pton(2, $bare) // (return "not an address")
inet_pton(2, $bare)
Truth table100%2/2
A
A= inet_pton(2, $bare)
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 222 | for my $net (@V4_BLOCKED) { | |||||||||||||||||||||||||||||||||||||||||
| 97 | 1657 | ▶ | return $net->[2] if _in_net($packed, inet_pton(AF_INET, $net->[0]), $net->[1]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 51 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 226 | ▶ | sub numeric_host { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
numeric_host: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 226 | my ($host) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 226 | ▶ | return undef unless defined $host; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 224 | (my $h = lc $host) =~ s/\.\z//; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 224 | ▶ | if ($h =~ /\A\[([0-9a-f:.%a-z]+)\]\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | my $inner = $1; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | return $inner =~ /:/ ? $inner : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 222 | ▶ | return $h if $h =~ /:/ && defined inet_pton(AF_INET6, $h); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$h =~ /:/ and defined inet_pton(10, $h)
MC/DC100%2/2
$h =~ /:/ and defined inet_pton(10, $h)
$h =~ /:/
defined inet_pton(10, $h)
Truth table100%3/3
A and B
A= $h =~ /:/B= defined inet_pton(10, $h)
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 206 | return _numeric_v4($h); | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | # _numeric_v4($host) -> 'a.b.c.d' | undef | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # The forms the BSD and glibc inet_aton read as an address: 127.1, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # 2130706433, 0x7f000001, 0177.0.0.1. Each part is decimal, 0x hex or | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # 0-led octal; the last fills the bits the others leave. Read here rather | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # than by the system's inet_aton, whose reading differs (on macOS 0177.0.0.1 is | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # not 127.0.0.1), so every platform checks, and connects to, the same address. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 206 | ▶ | sub _numeric_v4 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_numeric_v4: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 206 | my ($h) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 206 | ▶ | return undef unless $h =~ /\A(?:0x[0-9a-f]+|[0-9]+)(?:\.(?:0x[0-9a-f]+|[0-9]+)){0,3}\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 122 | my @parts; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 122 | for my $part (split /\./, $h) { | |||||||||||||||||||||||||||||||||||||||||
| 126 | 393 | my $n; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 393 | ▶ | if ($part =~ /\A0x([0-9a-f]*)\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 13 | my $hex = $1 =~ s/\A0+//r; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 13 | ▶ | return undef if length($hex) > 8; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 11 | ▶ | $n = length $hex ? hex $hex : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | } elsif ($part =~ /\A0([0-7]*)\z/) { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 117 | my $oct = $1 =~ s/\A0+//r; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 117 | ▶ | return undef if length($oct) > 11; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | # Eleven octal digits can pass 32 bits: perl warns, and the range check below refuses it. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 7 | ▶ | { no warnings 'portable'; $n = length $oct ? oct $oct : 0; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | } elsif ($part =~ /\A[1-9][0-9]{0,9}\z/) { | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 261 | $n = $part + 0; | |||||||||||||||||||||||||||||||||||||||||
| 138 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 2 | return undef; # 08, or too long to be a number | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 388 | push @parts, $n; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 117 | my $last = pop @parts; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 117 | for my $n (@parts) { | |||||||||||||||||||||||||||||||||||||||||
| 145 | 267 | ▶ | return undef if $n > 0xff; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 115 | ▶ | return undef if $last >= 2 ** (8 * (4 - @parts)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 114 | my $value = $last; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 114 | $value += $parts[$_] * 2 ** (8 * (3 - $_)) for 0 .. $#parts; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 114 | return join '.', unpack 'C4', pack 'N', $value; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 4 | ▶ | sub resolve_host { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve_host: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 4 | my ($host) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | local ($@, $!); | |||||||||||||||||||||||||||||||||||||||||
| 156 | # A host the resolver will not even take as text (a wide character) | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # does not resolve, which the POD states as an empty list. | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 4 | my ($err, @res) = eval { getaddrinfo($host, undef, { socktype => SOCK_STREAM }) }; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 4 | ▶ | return () if $@ || !defined($err) || $err; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$@ or not defined $err or $err
$@ or not defined $err
MC/DC100%3/3
$@ or not defined $err or $err
$@
not defined $err
$err
Truth table100%4/4
A or not(B) or C
A= $@B= not defined $errC= $err
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | my (%seen, @ips); | |||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | for my $r (@res) { | |||||||||||||||||||||||||||||||||||||||||
| 162 | 4 | my ($e, $ip) = getnameinfo($r->{addr}, NI_NUMERICHOST, NIx_NOSERV); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 4 | ▶ | push @ips, $ip if !$e && defined $ip && !$seen{$ip}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $e and defined $ip and not $seen{$ip}++
not $e and defined $ip
MC/DC66%2/3
not $e and defined $ip and not $seen{$ip}++
not $e
defined $ip
not $seen{$ip}++
Truth table75%3/4
not(A) and B and not(C)
A= not $eB= defined $ipC= not $seen{$ip}++
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | return @ips; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 1063 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 1063 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 1063 | my $resolve = delete $args{resolve}; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 1063 | my $private = delete $args{private_hosts}; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 1063 | my $self = $class->SUPER::new( | |||||||||||||||||||||||||||||||||||||||||
| 173 | timeout => $TIMEOUT, | ||||||||||||||||||||||||||||||||||||||||||
| 174 | max_redirect => $MAX_REDIRECT, | ||||||||||||||||||||||||||||||||||||||||||
| 175 | protocols_allowed => [ 'http', 'https' ], | ||||||||||||||||||||||||||||||||||||||||||
| 176 | agent => "Database-BI/$VERSION", | ||||||||||||||||||||||||||||||||||||||||||
| 177 | %args, | ||||||||||||||||||||||||||||||||||||||||||
| 178 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 1063 | ▶ | $self->{bi_resolve} = ref($resolve) eq 'CODE' ? $resolve : \&resolve_host; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1063 | ▶ | $self->{bi_private} = [ map { lc } grep { defined && !ref } @{ ref($private) eq 'ARRAY' ? $private : [] } ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_ && !ref($_)
MC/DC100%2/2
defined $_ && !ref($_)
defined $_
!ref($_)
Truth table100%3/3
A && not(B)
A= defined $_B= !ref($_)
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 1063 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 1039 | ▶ | sub private_host_allowed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
private_host_allowed: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 1039 | my ($self, $host) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 1039 | ▶ | $host = lc($host // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$host // ""
MC/DC100%1/1
$host // ""
$host
Truth table100%2/2
A
A= $host
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 1039 | for my $name (@{ $self->{bi_private} }) { | |||||||||||||||||||||||||||||||||||||||||
| 188 | 1051 | ▶ | return 1 if $name eq $host; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 1044 | ▶ | return 1 if $name =~ /\A\*(\..+)\z/ && length($host) > length($1) && substr($host, -length $1) eq $1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$name =~ /\A\*(\..+)\z/ and length $host > length $1 and substr($host, -length($1)) eq $1
$name =~ /\A\*(\..+)\z/ and length $host > length $1
MC/DC100%3/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 table100%4/4
A and B and C
A= $name =~ /\A\*(\..+)\z/B= length $host > length $1C= substr($host, -length($1)) eq $1
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 1027 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 36 | ▶ | sub _refuse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_refuse: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 36 | my ($request, $why) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 36 | my $res = HTTP::Response->new(403, "Refused: $why"); | |||||||||||||||||||||||||||||||||||||||||
| 197 | 36 | $res->header('Client-Warning' => 'Internal response'); | |||||||||||||||||||||||||||||||||||||||||
| 198 | 36 | $res->request($request); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 36 | return $res; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 41 | ▶ | sub send_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_request: called CC=17 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 41 | my ($self, $request, $arg, $size) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 41 | my $uri = $request->uri; | |||||||||||||||||||||||||||||||||||||||||
| 205 | 41 | ▶ | my $scheme = lc($uri->scheme // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$uri->scheme // ""
MC/DC100%1/1
$uri->scheme // ""
$uri->scheme
Truth table100%2/2
A
A= $uri->scheme
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 41 | ▶ | return _refuse($request, 'only http and https addresses are fetched') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$scheme eq "http" or $scheme eq "https"
MC/DC100%2/2
$scheme eq "http" or $scheme eq "https"
$scheme eq "http"
$scheme eq "https"
Truth table100%3/3
A or B
A= $scheme eq "http"B= $scheme eq "https"
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | unless $scheme eq 'http' || $scheme eq 'https'; | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 35 | ▶ | (my $name = lc($uri->host // '')) =~ s/\.\z//; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$uri->host // ""
MC/DC100%1/1
$uri->host // ""
$uri->host
Truth table100%2/2
A
A= $uri->host
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 35 | ▶ | return _refuse($request, 'the address names no host') unless length $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 33 | my $literal = numeric_host($name); | |||||||||||||||||||||||||||||||||||||||||
| 212 | 33 | ▶ | my @addrs = defined $literal ? ($literal) : $self->{bi_resolve}->($name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 33 | ▶ | return _refuse($request, "$name was not found") unless @addrs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 30 | ▶ | unless (!defined $literal && $self->private_host_allowed($name)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $literal and $self->private_host_allowed($name)
MC/DC100%2/2
not defined $literal and $self->private_host_allowed($name)
not defined $literal
$self->private_host_allowed($name)
Truth table100%3/3
not(A) and B
A= not defined $literalB= $self->private_host_allowed($name)
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 29 | for my $ip (@addrs) { | |||||||||||||||||||||||||||||||||||||||||
| 216 | 31 | ▶ | my $why = blocked_address($ip) // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
blocked_address($ip) // (next)
MC/DC100%1/1
blocked_address($ip) // (next)
blocked_address($ip)
Truth table100%2/2
A
A= blocked_address($ip)
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 25 | ▶ | my $shown = $ip // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ip // ""
MC/DC100%1/1
$ip // ""
$ip
Truth table100%2/2
A
A= $ip
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 25 | ▶ | return _refuse($request, $shown eq $name ? "$name is $why" : "$name is $shown, which is $why"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | # Connect to the address checked, not to the name again. | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 5 | my $pinned = $request->clone; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 5 | my $to = $uri->clone; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 5 | ▶ | $to->host($addrs[0] =~ /:/ ? "[$addrs[0]]" : $addrs[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 5 | $pinned->uri($to); | |||||||||||||||||||||||||||||||||||||||||
| 227 | 5 | my $authority = $uri->authority; | |||||||||||||||||||||||||||||||||||||||||
| 228 | 5 | $authority =~ s/\A[^\@]*\@//; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 5 | $pinned->header(Host => $authority); | |||||||||||||||||||||||||||||||||||||||||
| 230 | local $self->{ssl_opts} = $scheme eq 'https' | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 2 | ▶ | ? { %{ $self->{ssl_opts} // {} }, SSL_hostname => $uri->host, SSL_verifycn_name => $uri->host } | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"ssl_opts"} // {}
MC/DC0%0/1
$self->{"ssl_opts"} // {}
$self->{"ssl_opts"}
Truth table50%1/2
A
A= $self->{"ssl_opts"}
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 5 | ▶ | : $self->{ssl_opts}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 5 | my $res = $self->SUPER::send_request($pinned, $arg, $size); | |||||||||||||||||||||||||||||||||||||||||
| 235 | 5 | $res->request($request); | |||||||||||||||||||||||||||||||||||||||||
| 236 | 5 | return $res; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | __END__ | ||||||||||||||||||||||||||||||||||||||||||