TER1 (Statement): 100.00%
TER2 (Branch): 100.00%
TER3 (LCSAJ): 100.0% (8/8)
Approximate LCSAJ segments: 79
● Covered — this LCSAJ path was executed during testing.
● Not covered — this LCSAJ path was never executed. These are the paths to focus on.
Multiple dots on a line indicate that multiple control-flow paths begin at that line. Hovering over any dot shows:
start → end → jump
Uncovered paths show [NOT COVERED] in the tooltip.
1: package Database::BI::SafeUA; 2: 3: use strict; 4: use warnings; 5: 6: our $VERSION = '0.025.0'; 7: 8: use parent 'LWP::UserAgent'; 9: 10: use HTTP::Response; 11: use Readonly; 12: use Socket qw(getaddrinfo getnameinfo inet_ntoa inet_pton AF_INET AF_INET6 SOCK_STREAM NI_NUMERICHOST NIx_NOSERV); 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: Readonly my $MAX_REDIRECT => 5; 27: Readonly my $TIMEOUT => 30; 28: 29: # [ network, prefix length, why ] for IPv4 and IPv6. An address in any of them 30: # is refused. 31: 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: 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: sub _in_net { 59: my ($packed, $net, $bits) = @_; 60: my $full = int($bits / 8); 61: return 0 unless substr($packed, 0, $full) eq substr($net, 0, $full);Mutants (Total: 2, Killed: 2, Survived: 0)
62: my $rest = $bits % 8 or return 1; 63: my $mask = (0xff << (8 - $rest)) & 0xff; 64: return (ord(substr($packed, $full, 1)) & $mask) == (ord(substr($net, $full, 1)) & $mask) ? 1 : 0;
Mutants (Total: 3, Killed: 3, Survived: 0)
65: } 66: 67: sub blocked_address { ●68 → 73 → 94 68: my ($ip) = @_; 69: return 'not an address' unless defined $ip && length $ip;
Mutants (Total: 2, Killed: 2, Survived: 0)
70: (my $bare = $ip) =~ s/\A\[|\]\z//g; 71: $bare =~ s/%.*\z//; # a zone index (fe80::1%eth0) 72: 73: if ($bare =~ /:/) {
Mutants (Total: 1, Killed: 1, Survived: 0)
74: my $packed = inet_pton(AF_INET6, $bare) // return 'not an address'; 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: for my $prefix ("\0" x 10 . "\xff\xff", "\0\x64\xff\x9b" . "\0" x 8, "\0" x 12) { 79: return blocked_address(inet_ntoa(substr($packed, 12, 4)))
Mutants (Total: 2, Killed: 2, Survived: 0)
80: if substr($packed, 0, 12) eq $prefix && $packed ne "\0" x 15 . "\1" && $packed ne "\0" x 16; 81: } 82: return blocked_address(inet_ntoa(substr($packed, 2, 4))) if substr($packed, 0, 2) eq "\x20\x02";
Mutants (Total: 2, Killed: 2, Survived: 0)
83: for my $net (@V6_BLOCKED) { 84: return $net->[2] if _in_net($packed, inet_pton(AF_INET6, $net->[0]), $net->[1]);
Mutants (Total: 2, Killed: 2, Survived: 0)
85: } 86: return undef;
Mutants (Total: 2, Killed: 2, Survived: 0)
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 → 96 → 99 94: return 'not an address' unless $bare =~ /\A(?:0|[1-9][0-9]{0,2})(?:\.(?:0|[1-9][0-9]{0,2})){3}\z/;
Mutants (Total: 2, Killed: 2, Survived: 0)
95: my $packed = inet_pton(AF_INET, $bare) // return 'not an address'; 96: for my $net (@V4_BLOCKED) { 97: return $net->[2] if _in_net($packed, inet_pton(AF_INET, $net->[0]), $net->[1]);
Mutants (Total: 2, Killed: 2, Survived: 0)
98: } 99: return undef;
Mutants (Total: 2, Killed: 2, Survived: 0)
100: } 101: 102: sub numeric_host { ●103 → 106 → 110 103: my ($host) = @_; 104: return undef unless defined $host;
Mutants (Total: 2, Killed: 2, Survived: 0)
105: (my $h = lc $host) =~ s/\.\z//; 106: if ($h =~ /\A\[([0-9a-f:.%a-z]+)\]\z/) {
Mutants (Total: 1, Killed: 1, Survived: 0)
107: my $inner = $1; 108: return $inner =~ /:/ ? $inner : undef;
Mutants (Total: 2, Killed: 2, Survived: 0)
109: } 110: return $h if $h =~ /:/ && defined inet_pton(AF_INET6, $h);
Mutants (Total: 2, Killed: 2, Survived: 0)
111: return _numeric_v4($h);
Mutants (Total: 2, Killed: 2, Survived: 0)
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: sub _numeric_v4 { ●122 → 125 → 143 122: my ($h) = @_; 123: return undef unless $h =~ /\A(?:0x[0-9a-f]+|[0-9]+)(?:\.(?:0x[0-9a-f]+|[0-9]+)){0,3}\z/;
Mutants (Total: 2, Killed: 2, Survived: 0)
124: my @parts; 125: for my $part (split /\./, $h) { 126: my $n; 127: if ($part =~ /\A0x([0-9a-f]*)\z/) {
Mutants (Total: 1, Killed: 1, Survived: 0)
128: my $hex = $1 =~ s/\A0+//r; 129: return undef if length($hex) > 8;
Mutants (Total: 5, Killed: 5, Survived: 0)
130: $n = length $hex ? hex $hex : 0; 131: } elsif ($part =~ /\A0([0-7]*)\z/) { 132: my $oct = $1 =~ s/\A0+//r; 133: return undef if length($oct) > 11;
Mutants (Total: 5, Killed: 5, Survived: 0)
134: # Eleven octal digits can pass 32 bits: perl warns, and the range check below refuses it. 135: { no warnings 'portable'; $n = length $oct ? oct $oct : 0; } 136: } elsif ($part =~ /\A[1-9][0-9]{0,9}\z/) { 137: $n = $part + 0; 138: } else { 139: return undef; # 08, or too long to be a number
Mutants (Total: 2, Killed: 2, Survived: 0)
140: } 141: push @parts, $n; 142: } ●143 → 144 → 147 143: my $last = pop @parts; 144: for my $n (@parts) { 145: return undef if $n > 0xff;
Mutants (Total: 5, Killed: 5, Survived: 0)
146: } 147: return undef if $last >= 2 ** (8 * (4 - @parts));
Mutants (Total: 5, Killed: 5, Survived: 0)
148: my $value = $last; 149: $value += $parts[$_] * 2 ** (8 * (3 - $_)) for 0 .. $#parts; 150: return join '.', unpack 'C4', pack 'N', $value;
Mutants (Total: 2, Killed: 2, Survived: 0)
151: } 152: 153: sub resolve_host { ●154 → 161 → 165 154: my ($host) = @_; 155: 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: my ($err, @res) = eval { getaddrinfo($host, undef, { socktype => SOCK_STREAM }) }; 159: return () if $@ || !defined($err) || $err; 160: my (%seen, @ips); 161: for my $r (@res) { 162: my ($e, $ip) = getnameinfo($r->{addr}, NI_NUMERICHOST, NIx_NOSERV); 163: push @ips, $ip if !$e && defined $ip && !$seen{$ip}++; 164: } 165: return @ips;
Mutants (Total: 2, Killed: 2, Survived: 0)
166: } 167: 168: sub new { 169: my ($class, %args) = @_; 170: my $resolve = delete $args{resolve}; 171: my $private = delete $args{private_hosts}; 172: 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: $self->{bi_resolve} = ref($resolve) eq 'CODE' ? $resolve : \&resolve_host; 180: $self->{bi_private} = [ map { lc } grep { defined && !ref } @{ ref($private) eq 'ARRAY' ? $private : [] } ]; 181: return $self;
Mutants (Total: 2, Killed: 2, Survived: 0)
182: } 183: 184: sub private_host_allowed { ●185 → 187 → 191 185: my ($self, $host) = @_; 186: $host = lc($host // ''); 187: for my $name (@{ $self->{bi_private} }) { 188: return 1 if $name eq $host;
Mutants (Total: 2, Killed: 2, Survived: 0)
189: return 1 if $name =~ /\A\*(\..+)\z/ && length($host) > length($1) && substr($host, -length $1) eq $1;
Mutants (Total: 5, Killed: 5, Survived: 0)
190: } 191: return 0;
Mutants (Total: 2, Killed: 2, Survived: 0)
192: } 193: 194: sub _refuse { 195: my ($request, $why) = @_; 196: my $res = HTTP::Response->new(403, "Refused: $why"); 197: $res->header('Client-Warning' => 'Internal response'); 198: $res->request($request); 199: return $res;
Mutants (Total: 2, Killed: 2, Survived: 0)
200: } 201: 202: sub send_request { ●203 → 214 → 223 203: my ($self, $request, $arg, $size) = @_; 204: my $uri = $request->uri; 205: my $scheme = lc($uri->scheme // ''); 206: return _refuse($request, 'only http and https addresses are fetched')
Mutants (Total: 2, Killed: 2, Survived: 0)
207: unless $scheme eq 'http' || $scheme eq 'https'; 208: (my $name = lc($uri->host // '')) =~ s/\.\z//; 209: return _refuse($request, 'the address names no host') unless length $name;
Mutants (Total: 2, Killed: 2, Survived: 0)
210: 211: my $literal = numeric_host($name); 212: my @addrs = defined $literal ? ($literal) : $self->{bi_resolve}->($name); 213: return _refuse($request, "$name was not found") unless @addrs;
Mutants (Total: 2, Killed: 2, Survived: 0)
214: unless (!defined $literal && $self->private_host_allowed($name)) {
Mutants (Total: 1, Killed: 1, Survived: 0)
215: for my $ip (@addrs) { 216: my $why = blocked_address($ip) // next; 217: my $shown = $ip // ''; 218: return _refuse($request, $shown eq $name ? "$name is $why" : "$name is $shown, which is $why");
Mutants (Total: 2, Killed: 2, Survived: 0)
219: } 220: } 221: 222: # Connect to the address checked, not to the name again. 223: my $pinned = $request->clone; 224: my $to = $uri->clone; 225: $to->host($addrs[0] =~ /:/ ? "[$addrs[0]]" : $addrs[0]); 226: $pinned->uri($to); 227: my $authority = $uri->authority; 228: $authority =~ s/\A[^\@]*\@//; 229: $pinned->header(Host => $authority); 230: local $self->{ssl_opts} = $scheme eq 'https' 231: ? { %{ $self->{ssl_opts} // {} }, SSL_hostname => $uri->host, SSL_verifycn_name => $uri->host } 232: : $self->{ssl_opts}; 233: 234: my $res = $self->SUPER::send_request($pinned, $arg, $size); 235: $res->request($request); 236: return $res;
Mutants (Total: 2, Killed: 2, Survived: 0)
237: } 238: 239: 1; 240: 241: __END__ 242: 243: =head1 NAME 244: 245: Database::BI::SafeUA - Fetch a URL only if it leads to the public internet 246: 247: =head1 VERSION 248: 249: 0.025.0 250: 251: =head1 SYNOPSIS 252: 253: my $ua = Database::BI::SafeUA->new(private_hosts => ['intranet.example.com']); 254: my $res = $ua->get('https://example.com/table.html'); 255: # http://localtest.me/ -> 403 "Refused: localtest.me is 127.0.0.1, which is this machine" 256: 257: =head1 DESCRIPTION 258: 259: An L<LWP::UserAgent> for C<url:> sources and C</import>. Every request, 260: including each redirect (at most 5), is resolved, refused with a C<403> 261: response if any address is on this machine, a private or shared network, 262: link-local (cloud metadata services), multicast or reserved, and then sent to 263: the address that was checked. The name stays in the C<Host> header, the TLS 264: server name (SNI) and the certificate check, so DNS that answers differently 265: on a second lookup (rebinding) changes nothing. An IPv4 address inside IPv6 266: (mapped, NAT64, compatible, 6to4) is checked as that IPv4 address. Only 267: C<http> and C<https> are fetched. 268: 269: =head1 METHODS 270: 271: =head2 new 272: 273: =head3 API SPECIFICATION 274: 275: =head4 INPUT 276: 277: private_hosts: arrayref of host names, optional; names (or *.suffix) that 278: may resolve to private addresses (an intranet). An address 279: written as such is never one of them. 280: resolve: code ref, optional; host -> list of address strings (tests) 281: anything else: as LWP::UserAgent->new (timeout 30, max_redirect 5 by default) 282: 283: =head4 OUTPUT 284: 285: The user agent. 286: 287: =head2 send_request 288: 289: As L<LWP::UserAgent/send_request>, with the checks above. LWP calls it for 290: every request and each redirect; callers use C<get> and the rest as usual. 291: 292: =head3 API SPECIFICATION 293: 294: =head4 INPUT 295: 296: $request: HTTP::Request 297: $arg, $size: as LWP::UserAgent (content file or callback, read size) 298: 299: =head4 OUTPUT 300: 301: An L<HTTP::Response> whose C<request> is the one given (not the pinned copy). 302: A refused request gets C<403 Refused: E<lt>whyE<gt>> and C<Client-Warning: Internal 303: response>, and nothing is sent: a scheme other than http or https, no host, a 304: name that does not resolve, or any address C<blocked_address> refuses (unless 305: C<private_host_allowed> allows the name). 306: 307: =head2 private_host_allowed 308: 309: $ua->private_host_allowed('reports.corp.example'); # 1 with private_hosts => ['*.corp.example'] 310: 311: =head3 API SPECIFICATION 312: 313: =head4 INPUT 314: 315: $host: a host name (case does not matter) 316: 317: =head4 OUTPUT 318: 319: 1 when C<private_hosts> names it, or a C<*.suffix> entry ends it (C<*.corp.example> 320: matches C<a.corp.example>, not C<corp.example> or C<evilcorp.example>), else 0. 321: C<send_request> asks this only for a name, never for an address written as one. 322: 323: =head1 FUNCTIONS 324: 325: =head2 blocked_address 326: 327: my $why = Database::BI::SafeUA::blocked_address('10.1.2.3'); # 'a private network' 328: 329: =head3 API SPECIFICATION 330: 331: =head4 INPUT 332: 333: $ip: an IPv4 or IPv6 address as text (brackets and a zone index are allowed) 334: 335: =head4 OUTPUT 336: 337: Why the address is refused, or C<undef> when it is public. Text that is not 338: an address is refused. 339: 340: =head2 numeric_host 341: 342: numeric_host('2130706433'); # '127.0.0.1' 343: numeric_host('example.com'); # undef 344: 345: =head3 API SPECIFICATION 346: 347: =head4 INPUT 348: 349: $host: a URL's host 350: 351: =head4 OUTPUT 352: 353: The address the host is when it is written as one, in any form the resolver 354: reads (C<127.1>, C<0x7f000001>, C<0177.0.0.1>, C<[::1]>), else C<undef>. 355: 356: =head2 resolve_host 357: 358: =head3 API SPECIFICATION 359: 360: =head4 INPUT 361: 362: $host: a host name 363: 364: =head4 OUTPUT 365: 366: Its addresses (getaddrinfo), or an empty list. 367: 368: =head1 AUTHOR 369: 370: Nigel Horne C<< <njh@nigelhorne.com> >> 371: 372: =head1 LICENCE AND COPYRIGHT 373: 374: Copyright 2026 Nigel Horne. 375: 376: Usage is subject to the GPL2 licence terms. 377: 378: =cut