blib/lib/Langertha/Raider/Provider/Fetch.pm

filter: Statementstmt 95.0% 231 / 243 Branchbran 93.1% 95 / 102 Conditioncond 68.4% 39 / 57 MC/DCmcdc 45.4% 15 / 33 Subroutinesub 93.0% 40 / 43 Podpod 100% 6 / 6 totaltotal 88.0% 426 / 484 CC 77 SCAR 19.0
CC
77
Cov
91%
target_url
18.2
_hops_f
10.1
address_kind
8.0
CRAP 6.7
« blib/lib/Langertha/Raider/Plugin/Trace.pm ↑ Summary blib/lib/Langertha/Raider/Packs/Collection.pm »
1 package Langertha::Raider::Provider::Fetch;
2 # ABSTRACT: Internal bounded fetch of a provider manifest from /.well-known/langertha.json
3 58 our $VERSION = '0.503';
4 58 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 58 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 58 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 58 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 58 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 58 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 58 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 58 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 58 ▶use Socket qw( AF_INET AF_INET6 inet_pton getnameinfo NI_NUMERICHOST NI_NUMERICSERV );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 58 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 58 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 58 ▶use Langertha::Raider::ConnectCheck qw( connect_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 58 ▶use constant WELL_KNOWN_PATH => '/.well-known/langertha.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 58 has allow_internal => (
22 is => 'ro',
23 isa => 'Bool',
24 default => 0,
25 );
26  
27  
28 58 has max_bytes => (
29 is => 'ro',
30 isa => 'Int',
31 default => 1_048_576,
32 );
33  
34  
35 58 has timeout => (
36 is => 'ro',
37 isa => 'Num',
38 default => 10,
39 );
40  
41  
42 58 has max_redirects => (
43 is => 'ro',
44 isa => 'Int',
45 default => 3,
46 );
47  
48  
49 58 has loop => (
50 is => 'ro',
51 lazy => 1,
52 builder => '_build_loop',
53 );
54  
55 85 ▶sub _build_loop { IO::Async::Loop->new }
Subroutine _build_loop: called CC=1 SCAR=0.0
56  
57  
58 58 has resolver => (
59 is => 'ro',
60 isa => 'CodeRef',
61 lazy => 1,
62 builder => '_build_resolver',
63 );
64  
65 0 ▶sub _build_resolver {
Subroutine _build_resolver: not called CC=1 SCAR=6.9
66 0 my ( $self ) = @_;
67 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 my ( $host ) = @_;
69 return $self->loop->resolver->getaddrinfo(
70 host => $host,
71 service => 443,
72 socktype => 'stream',
73 timeout => $self->timeout,
74 0 ▶ )->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
75 0 my %seen;
76 0 ▶ my @addresses = grep { defined && !$seen{$_}++ }
Condition0%0/3
defined $_ && !$seen{$_}++
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && !$seen{$_}++
defined $_ !$seen{$_}++
Truth table0%0/3
A && not(B)
A= defined $_B= !$seen{$_}++
ABresult
0X0
100
111
77 0 map { ( getnameinfo($_->{addr}, NI_NUMERICHOST | NI_NUMERICSERV) )[1] } @_;
78 0 return Future->done(@addresses);
79 0 });
80 0 };
81 }
82  
83  
84 has ssl_options => (
85 is => 'ro',
86 isa => 'HashRef',
87 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
88 58 );
89  
90  
91 75 ▶sub target_url {
Subroutine target_url: called CC=18 SCAR=29.0
Pod documented
92 75 my ( $self, $target ) = @_;
93 75 ▶ croak 'no provider given' unless defined $target && length $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target and length $target 1 74 75
Condition66%2/3
defined $target and length $target
!ll&&!rl&&r
0174
MC/DC50%1/2
defined $target and length $target
defined $target length $target
Truth table66%2/3
A and B
A= defined $targetB= length $target
ABresult
0X0
100
111
94 74 ▶ croak 'provider target contains spaces or control characters' if $target =~ /[\s\x00-\x1f\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if $target =~ /[\s\x00-\x1f\x7f]/ 1 73 74
95 73 my $uri;
96 73 ▶ if ( $target =~ m{\A[A-Za-z][A-Za-z0-9+.-]*://} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ m[\A[A-Za-z][A-Za-z0-9+.-]*://]) { } 14 59 73
97 14 $uri = URI->new($target);
98 14 ▶ croak 'only https is allowed for a provider manifest, not '.$uri->scheme.'://'
Branch: 1 branch
BranchTrueFalseTotal
unless lc $uri->scheme eq "https" 5 9 14
99 unless lc $uri->scheme eq 'https';
100 }
101 else {
102 59 ▶ croak "provider target '".$target."' is not HOST, HOST:PORT or an https URL" if $target =~ m{[/?#@]};
Branch: 1 branch
BranchTrueFalseTotal
if $target =~ m[[/?#@]] 2 57 59
103 57 $uri = URI->new('https://'.$target);
104 }
105 66 ▶ croak 'provider target must not carry credentials (userinfo)' if defined $uri->userinfo;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->userinfo 1 65 66
106 65 ▶ croak 'provider target must not carry a query' if defined $uri->query;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->query 1 64 65
107 64 ▶ croak 'provider target must not carry a fragment' if defined $uri->fragment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->fragment 1 63 64
108 63 my $host = $uri->host;
109 63 ▶ croak "provider target '".$target."' names no host" unless defined $host && length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 63 63
Condition33%1/3
defined $host and length $host
!ll&&!rl&&r
0063
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
110 63 ▶ croak "provider target '".$target."' has an invalid port" unless $uri->port =~ /\A\d+\z/ && $uri->port > 0 && $uri->port < 65536;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->port =~ /\A\d+\z/ and $uri->port > 0 and $uri->port < 65536 2 61 63
Condition83%5/6
$uri->port =~ /\A\d+\z/ and $uri->port > 0 and $uri->port < 65536
!ll&&!rl&&r
1161
$uri->port =~ /\A\d+\z/ and $uri->port > 0
!ll&&!rl&&r
0162
MC/DC66%2/3
$uri->port =~ /\A\d+\z/ and $uri->port > 0 and $uri->port < 65536
$uri->port =~ /\A\d+\z/ $uri->port > 0 $uri->port < 65536
Truth table75%3/4
A and B and C
A= $uri->port =~ /\A\d+\z/B= $uri->port > 0C= $uri->port < 65536
ABCresult
0XX0
10X0
1100
1111
111 61 my $path = $uri->path;
112 61 ▶ croak 'the manifest lives at '.WELL_KNOWN_PATH.' of the origin; give the host or the origin, not '.$path
Branch: 1 branch
BranchTrueFalseTotal
unless $path eq "" or $path eq "/" or $path eq "/.well-known/langertha.json" 2 59 61
Condition100%6/6
$path eq "" or $path eq "/" or $path eq "/.well-known/langertha.json"
l!l&&r!l&&!r
5812
$path eq "" or $path eq "/"
l!l&&r!l&&!r
5713
MC/DC100%3/3
$path eq "" or $path eq "/" or $path eq "/.well-known/langertha.json"
$path eq "" $path eq "/" $path eq "/.well-known/langertha.json"
Truth table100%4/4
A or B or C
A= $path eq ""B= $path eq "/"C= $path eq "/.well-known/langertha.json"
ABCresult
1XX1
01X1
0011
0000
113 unless $path eq '' || $path eq '/' || $path eq WELL_KNOWN_PATH;
114 59 my $url = URI->new('https://'.$uri->host_port.WELL_KNOWN_PATH);
115 59 return $url->canonical->as_string;
116 }
117  
118  
119 # [ first address, prefix length, kind ]; the first match wins, so the
120 # single metadata addresses come before the ranges holding them.
121 58 my @V4_RANGES = (
122 [ '169.254.169.254', 32, 'metadata' ], # AWS, GCP, Azure, OpenStack, DigitalOcean, ...
123 [ '100.100.100.200', 32, 'metadata' ], # Alibaba Cloud
124 [ '0.0.0.0', 8, 'unspecified' ],
125 [ '10.0.0.0', 8, 'private' ],
126 [ '100.64.0.0', 10, 'private' ], # shared address space (carrier-grade NAT)
127 [ '127.0.0.0', 8, 'loopback' ],
128 [ '169.254.0.0', 16, 'link-local' ],
129 [ '172.16.0.0', 12, 'private' ],
130 [ '192.0.0.0', 24, 'reserved' ], # IETF protocol assignments
131 [ '192.0.2.0', 24, 'reserved' ], # documentation
132 [ '192.168.0.0', 16, 'private' ],
133 [ '198.18.0.0', 15, 'reserved' ], # benchmarking
134 [ '198.51.100.0', 24, 'reserved' ], # documentation
135 [ '203.0.113.0', 24, 'reserved' ], # documentation
136 [ '224.0.0.0', 4, 'multicast' ],
137 [ '240.0.0.0', 4, 'reserved-never' ], # reserved, including broadcast
138 );
139  
140 58 my @V6_RANGES = (
141 [ 'fd00:ec2::254', 128, 'metadata' ], # AWS IPv6 instance metadata
142 [ '::', 128, 'unspecified' ],
143 [ '::1', 128, 'loopback' ],
144 [ 'fc00::', 7, 'private' ], # unique local
145 [ 'fe80::', 10, 'link-local' ],
146 [ 'fec0::', 10, 'private' ], # site-local (deprecated)
147 [ 'ff00::', 8, 'multicast' ],
148 [ '100::', 64, 'reserved' ], # discard
149 [ '2001:db8::', 32, 'reserved' ], # documentation
150 );
151  
152 58 my %RELEASABLE = map { $_ => 1 } qw( loopback private link-local reserved );
153  
154 162 ▶sub address_kind {
Subroutine address_kind: called CC=8 SCAR=20.8
Pod documented
155 162 my ( $self, $address ) = @_;
156 162 ▶ ( my $bare = $address // '' ) =~ s/%.*\z//s;
Condition50%1/2
$address // ""
l!l
1620
MC/DC0%0/1
$address // ""
$address
Truth table50%1/2
A
A= $address
Aresult
11
00
157 162 my $kind = 'invalid';
158 162 ▶ if ( my $packed = inet_pton(AF_INET, $bare) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $packed = inet_pton(2, $bare)) { } 138 24 162
elsif ($packed = inet_pton(10, $bare)) { } 14 10 24
159 138 $kind = $self->_kind_in($packed, AF_INET, \@V4_RANGES);
160 }
161 elsif ( $packed = inet_pton(AF_INET6, $bare) ) {
162 14 my $v4 = $self->_embedded_v4($packed);
163 14 ▶ $kind = defined $v4
Branch: 1 branch
BranchTrueFalseTotal
defined $v4 ? : 5 9 14
164 ? $self->_kind_in($v4, AF_INET, \@V4_RANGES)
165 : $self->_kind_in($packed, AF_INET6, \@V6_RANGES);
166 }
167 return $kind eq 'public' ? ('public')
168 : $kind eq 'reserved-never' ? ('reserved', 0)
169 162 ▶ : ( $kind, $RELEASABLE{$kind} ? 1 : 0 );
Branch: 3 branches
BranchTrueFalseTotal
$kind eq 'public' ? : 9 153 162
$kind eq 'reserved-never' ? : 1 152 153
$RELEASABLE{$kind} ? : 128 24 152
170 }
171  
172 # The IPv4 address an IPv6 address carries, packed, or undef.
173 14 ▶sub _embedded_v4 {
Subroutine _embedded_v4: called CC=6 SCAR=18.0
174 14 my ( $self, $packed ) = @_;
175 14 my $prefix96 = substr($packed, 0, 12);
176 14 ▶ return substr($packed, 12, 4) if $prefix96 eq ("\0" x 10)."\xff\xff"; # ::ffff:a.b.c.d
Branch: 1 branch
BranchTrueFalseTotal
if $prefix96 eq "\0\0\0\0\0\0\0\0\0\0\377\377" 3 11 14
177 11 ▶ return substr($packed, 12, 4) if $prefix96 eq ("\0" x 12) && substr($packed, 12, 4) !~ /\A\0\0\0[\0\1]\z/; # ::a.b.c.d
Branch: 1 branch
BranchTrueFalseTotal
if $prefix96 eq "\0\0\0\0\0\0\0\0\0\0\0\0" and not substr($packed, 12, 4) =~ /\A\0\0\0[\0\1]\z/ 0 11 11
Condition66%2/3
$prefix96 eq "\0\0\0\0\0\0\0\0\0\0\0\0" and not substr($packed, 12, 4) =~ /\A\0\0\0[\0\1]\z/
!ll&&!rl&&r
920
MC/DC0%0/2
$prefix96 eq "\0\0\0\0\0\0\0\0\0\0\0\0" and not substr($packed, 12, 4) =~ /\A\0\0\0[\0\1]\z/
$prefix96 eq "\0\0\0\0\0\0\0\0\0\0\0\0" not substr($packed, 12, 4) =~ /\A\0\0\0[\0\1]\z/
Truth table66%2/3
A and not(B)
A= $prefix96 eq "\0\0\0\0\0\0\0\0\0\0\0\0"B= not substr($packed, 12, 4) =~ /\A\0\0\0[\0\1]\z/
ABresult
0X0
100
111
178 11 ▶ return substr($packed, 12, 4) if $prefix96 eq "\0\x64\xff\x9b".("\0" x 8); # 64:ff9b::/96
Branch: 1 branch
BranchTrueFalseTotal
if $prefix96 eq "\0d\377\233\0\0\0\0\0\0\0\0" 1 10 11
179 10 ▶ return substr($packed, 2, 4) if substr($packed, 0, 2) eq "\x20\x02"; # 2002::/16
Branch: 1 branch
BranchTrueFalseTotal
if substr($packed, 0, 2) eq " \2" 1 9 10
180 9 return;
181 }
182  
183 152 ▶sub _kind_in {
Subroutine _kind_in: called CC=3 SCAR=11.0
184 152 my ( $self, $packed, $family, $ranges ) = @_;
185 152 my $bits = unpack 'B*', $packed;
186 152 for my $range (@$ranges) {
187 954 my ( $first, $length, $kind ) = @$range;
188 954 my $net = unpack 'B*', inet_pton($family, $first);
189 954 ▶ return $kind if substr($bits, 0, $length) eq substr($net, 0, $length);
Branch: 1 branch
BranchTrueFalseTotal
if substr($bits, 0, $length) eq substr($net, 0, $length) 143 811 954
190 }
191 9 return 'public';
192 }
193  
194  
195 114 ▶sub check_addresses {
Subroutine check_addresses: called CC=8 SCAR=20.8
Pod documented
196 114 my ( $self, $host, @addresses ) = @_;
197 114 ▶ return $host.' resolves to no address' unless @addresses;
Branch: 1 branch
BranchTrueFalseTotal
unless @addresses 1 113 114
198 113 for my $address (@addresses) {
199 120 my ( $kind, $releasable ) = $self->address_kind($address);
200 120 ▶ next if $kind eq 'public';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "public" 4 116 120
201 116 ▶ next if $releasable && $self->allow_internal;
Branch: 1 branch
BranchTrueFalseTotal
if $releasable and $self->allow_internal 103 13 116
Condition100%3/3
$releasable and $self->allow_internal
!ll&&!rl&&r
58103
MC/DC100%2/2
$releasable and $self->allow_internal
$releasable $self->allow_internal
Truth table100%3/3
A and B
A= $releasableB= $self->allow_internal
ABresult
0X0
100
111
202 13 ▶ my $where = ( $address eq $host ? $address.' is' : $host.' resolves to '.$address ).' ('.$kind.' address)';
Branch: 1 branch
BranchTrueFalseTotal
$address eq $host ? : 4 9 13
203 13 ▶ return $where.'; only --allow-internal releases loopback, private, link-local and reserved addresses, for a deliberately released internal origin'
Branch: 1 branch
BranchTrueFalseTotal
if $releasable 8 5 13
204 if $releasable;
205 5 return $where.'; never allowed, not even with --allow-internal';
206 }
207 100 return;
208 }
209  
210  
211 86 ▶async sub fetch_f {
Subroutine fetch_f: called CC=3 SCAR=11.0
Pod documented
212 86 my ( $self, $url ) = @_;
213 86 my $t0 = Time::HiRes::time();
214 86 my $timeout = $self->timeout;
215 # Made here, not in _fetch_f, so it leaves the loop also when the
216 # timeout cancels the fetch half-way.
217 86 my $http = Net::Async::HTTP->new(
218 user_agent => 'raider/'.$self->VERSION,
219 max_redirects => 0, # followed here, one origin check per hop
220 max_connections_per_host => 1,
221 decode_content => 0, # no Accept-Encoding, nothing to decompress
222 fail_on_error => 0,
223 );
224 86 $self->loop->add($http);
225 my $settled = await Future->wait_any(
226 $self->_fetch_f($url, $http),
227 2 ▶ $self->loop->delay_future(after => $timeout)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
228 2 Future->done({ status => 'failed', url => $url, error => 'timed out after '.$timeout.'s' });
229 }),
230 86 ▶ )->followed_by(sub { Future->done($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
231 86 $self->loop->remove($http);
232 86 my ( $report ) = $settled->get;
233 86 ▶ $report->{url} //= $url;
Condition33%1/3
$$report{"url"} //= $url
l!l&&r!l&&!r
8600
MC/DC0%0/2
$$report{"url"} //= $url
$$report{"url"} $url
Truth table33%1/3
A //= B
A= $$report{"url"}B= $url
ABresult
1X1
011
000
234 86 ▶ $report->{redirects} //= [];
Condition100%2/2
$$report{"redirects"} //= []
l!l
842
MC/DC100%1/1
$$report{"redirects"} //= []
$$report{"redirects"}
Truth table100%2/2
A
A= $$report{"redirects"}
Aresult
11
00
235 86 $report->{elapsed} = 0 + sprintf('%.3f', Time::HiRes::time() - $t0);
236 86 return $report;
237 58 }
238  
239 86 ▶async sub _fetch_f {
Subroutine _fetch_f: called CC=4 SCAR=14.0
240 86 my ( $self, $url, $http ) = @_;
241 86 my $uri = URI->new($url);
242 86 my $host = $uri->host;
243 86 my %report = ( url => $url, redirects => [] );
244 86 ▶ my $failed = sub { return { %report, status => 'failed', error => $_[0], @_[ 1 .. $#_ ] } };
Subroutine __ANON__: called CC=1 SCAR=0.0
245 86 ▶ my $refused = sub { return { %report, status => 'refused', error => $_[0], @_[ 1 .. $#_ ] } };
Subroutine __ANON__: called CC=1 SCAR=0.0
246  
247 86 ▶ return $failed->('only https is allowed for a provider manifest') unless $uri->scheme eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme eq "https" 0 86 86
248 # Net::Async::HTTP loads its connector and TLS modules only when it
249 # connects; a load failure then leaks the host's connection slot (k107).
250 86 ▶ if ( my $error = connect_error($url) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = connect_error($url)) 0 86 86
251 0 return $failed->($error);
252 }
253  
254 86 my $checked = await $self->check_host_f($host);
255 85 ▶ return { %report, %$checked } if $checked->{status};
Branch: 1 branch
BranchTrueFalseTotal
if $$checked{"status"} 10 75 85
256  
257 75 return await $self->_hops_f($http, $uri, $checked->{addresses}, \%report, $failed, $refused);
258 58 }
259  
260  
261 111 ▶async sub check_host_f {
Subroutine check_host_f: called CC=4 SCAR=13.9
Pod documented
262 111 my ( $self, $host ) = @_;
263 111 my @addresses;
264 111 ▶ if ( $self->_is_literal($host) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_literal($host)) { } 17 94 111
265 17 @addresses = ( $host );
266 }
267 else {
268 94 ▶ my $resolved = await $self->resolver->($host)->followed_by(sub { Future->done($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
269 93 ▶ if ( $resolved->is_failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolved->is_failed) 1 92 93
270 1 my ( $error ) = $resolved->failure;
271 1 $error =~ s/\s+\z//;
272 1 return { status => 'failed', error => 'cannot resolve '.$host.': '.$error };
273 }
274 92 @addresses = $resolved->get;
275 }
276 109 ▶ if ( my $refusal = $self->check_addresses($host, @addresses) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refusal = $self->check_addresses($host, @addresses)) 11 98 109
277 11 return { status => 'refused', error => $refusal };
278 }
279 98 return { addresses => \@addresses };
280 58 }
281  
282 75 ▶async sub _hops_f {
Subroutine _hops_f: called CC=10 SCAR=23.1
283 75 my ( $self, $http, $uri, $addresses, $report, $failed, $refused ) = @_;
284 75 my $origin = $self->origin($uri);
285 75 for my $hop ( 0 .. $self->max_redirects ) {
286 78 my $got = await $self->_get_f($http, $uri, $addresses);
287 77 ▶ $report->{address} = $got->{address} if defined $got->{address};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$got{"address"} 77 0 77
288 77 ▶ return $failed->($got->{error}) if defined $got->{error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$got{"error"} 6 71 77
289 71 my $response = $got->{response};
290 71 ▶ if ( $response->is_redirect && defined( my $location = $response->header('Location') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_redirect and defined(my $location = $response->header("Location"))) 8 63 71
Condition66%2/3
$response->is_redirect and defined(my $location = $response->header("Location"))
!ll&&!rl&&r
6308
MC/DC50%1/2
$response->is_redirect and defined(my $location = $response->header("Location"))
$response->is_redirect defined(my $location = $response->header("Location"))
Truth table66%2/3
A and B
A= $response->is_redirectB= defined(my $location = $response->header("Location"))
ABresult
0X0
100
111
291 8 my $next = URI->new_abs($location, $uri);
292 8 ▶ unless ( ( $self->origin($next) // '' ) eq $origin ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($self->origin($next) // "") eq $origin) 4 4 8
Condition50%1/2
$self->origin($next) // ""
l!l
80
MC/DC0%0/1
$self->origin($next) // ""
$self->origin($next)
Truth table50%1/2
A
A= $self->origin($next)
Aresult
11
00
293 4 return $refused->('redirect to '.$next->as_string.' leaves the origin '.$origin.': not followed',
294 location => $next->as_string);
295 }
296 4 ▶ if ( $hop == $self->max_redirects ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hop == $self->max_redirects) 1 3 4
297 1 return $failed->('more than '.$self->max_redirects.' redirects');
298 }
299 3 push @{ $report->{redirects} }, $next->as_string;
300 3 $uri = $next;
301 3 next;
302 }
303 63 ▶ return $failed->('HTTP '.$response->status_line.' from '.$uri->as_string)
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 3 60 63
304 unless $response->is_success;
305 60 return { %$report, status => 'completed', final_url => $uri->as_string, body => $response->content };
306 }
307 0 return $failed->('more than '.$self->max_redirects.' redirects');
308 58 }
309  
310 # One GET to the checked addresses in turn (the next one only when the
311 # connection itself failed). Resolves to { response, address } or { error }.
312 78 ▶async sub _get_f {
Subroutine _get_f: called CC=4 SCAR=13.9
313 78 my ( $self, $http, $uri, $addresses ) = @_;
314 78 my $error;
315 78 for my $i ( 0 .. $#$addresses ) {
316 79 my $address = $addresses->[$i];
317 79 my $got = await $self->_get_one_f($http, $uri, $address);
318 78 ▶ return { %$got, address => $address } unless $got->{connect_failed} && $i < $#$addresses;
Branch: 1 branch
BranchTrueFalseTotal
unless $$got{"connect_failed"} and $i < $addresses->$#* 77 1 78
Condition100%3/3
$$got{"connect_failed"} and $i < $addresses->$#*
!ll&&!rl&&r
7611
MC/DC100%2/2
$$got{"connect_failed"} and $i < $addresses->$#*
$$got{"connect_failed"} $i < $addresses->$#*
Truth table100%3/3
A and B
A= $$got{"connect_failed"}B= $i < $addresses->$#*
ABresult
0X0
100
111
319 1 $error = $got->{error};
320 }
321 0 return { error => $error };
322 58 }
323  
324 79 ▶async sub _get_one_f {
Subroutine _get_one_f: called CC=2 SCAR=6.9
325 79 my ( $self, $http, $uri, $address ) = @_;
326 79 my $host = $uri->host;
327 79 my $max = $self->max_bytes;
328 79 my $request = HTTP::Request->new(GET => $uri->as_string, [ Accept => 'application/json' ]);
329 79 my $too_large = $self->loop->new_future;
330 79 my $limit_error;
331 79 ( my $bare = $address ) =~ s/%.*\z//s;
332 my $req = $http->do_request(
333 request => $request,
334 host => $bare, # the checked address: no second resolution
335 port => $uri->port,
336 SSL => 1,
337 79 %{ $self->ssl_options },
338 # After ssl_options, so nothing can switch the checks off.
339 SSL_hostname => $host,
340 SSL_verifycn_name => $host,
341 SSL_verifycn_scheme => 'http',
342 SSL_verify_mode => 1, # SSL_VERIFY_PEER; IO::Socket::SSL loads only on connect
343 73 ▶ on_header => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
344 73 my ( $response ) = @_;
345 # The abort waits for the next loop tick: cancelling the request from
346 # inside the connection's read handler leaves it reading for a
347 # request that is gone ("Spurious on_read of connection while idle").
348 # The error is kept at once, so a response that completes before the
349 # abort still counts as too large.
350 2 ▶ my $abort = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
351 2 ( $limit_error ) = @_;
352 2 ▶ $self->loop->later(sub { $too_large->done({ error => $limit_error }) unless $too_large->is_ready });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $too_large->is_ready 1 1 2
353 73 };
354 73 my $length = $response->header('Content-Length');
355 73 ▶ if ( defined $length && $length =~ /\A\d+\z/ && $length > $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $length and $length =~ /\A\d+\z/ and $length > $max) 1 72 73
Condition83%5/6
defined $length and $length =~ /\A\d+\z/ and $length > $max
!ll&&!rl&&r
1711
defined $length and $length =~ /\A\d+\z/
!ll&&!rl&&r
1072
MC/DC66%2/3
defined $length and $length =~ /\A\d+\z/ and $length > $max
defined $length $length =~ /\A\d+\z/ $length > $max
Truth table75%3/4
A and B and C
A= defined $lengthB= $length =~ /\A\d+\z/C= $length > $max
ABCresult
0XX0
10X0
1100
1111
356 1 $abort->('manifest too large: Content-Length '.$length.' exceeds '.$max.' bytes');
357 1 ▶ return sub { return @_ ? () : $response };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
358 }
359 72 my $body = '';
360 72 my $over;
361 135 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
362 135 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 64 71 135
363 64 ▶ return if $over;
Branch: 1 branch
BranchTrueFalseTotal
if $over 0 64 64
364 64 $body .= $_[0];
365 64 ▶ if ( length $body > $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body > $max) 1 63 64
366 1 $over = 1;
367 1 $body = '';
368 1 $abort->('manifest too large: more than '.$max.' bytes');
369 }
370 64 return;
371 }
372 71 $response->content($body);
373 71 return $response;
374 72 };
375 },
376 79 );
377 my $got = await Future->wait_any(
378 $req->then(
379 72 ▶ sub { Future->done({ response => $_[0] }) },
Subroutine __ANON__: called CC=1 SCAR=0.0
380 5 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
381 5 my ( $message ) = @_;
382 5 $message =~ s/\s+\z//;
383 5 $message =~ s/ failed \[[^\]]*\]\z//; # Net::Async::HTTP's "... failed [ssl]" suffix
384 # Net::Async::HTTP flattens a connect failure into one message:
385 # "ADDRESS:PORT - connect: REASON".
386 5 ▶ Future->done({ error => 'fetching '.$uri->as_string.': '.$message,
Branch: 1 branch
BranchTrueFalseTotal
$message =~ / - connect: / ? : 2 3 5
387 connect_failed => ( $message =~ / - connect: / ? 1 : 0 ) });
388 },
389 79 ),
390 $too_large,
391 );
392 78 ▶ return defined $limit_error ? { error => $limit_error } : $got;
Branch: 1 branch
BranchTrueFalseTotal
defined $limit_error ? : 2 76 78
393 58 }
394  
395 575 ▶sub origin {
Subroutine origin: called CC=6 SCAR=19.2
Pod documented
396 575 my ( $self, $uri ) = @_;
397 575 ▶ $uri = URI->new($uri) unless ref $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uri 56 519 575
398 575 ▶ return unless $uri->can('host');
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->can("host") 0 575 575
399 575 ▶ return lc( $uri->scheme // '' ).'://'.lc( $uri->host // '' ).':'.( $uri->port // '' );
Condition50%3/6
$uri->scheme // ""
l!l
5750
$uri->host // ""
l!l
5750
$uri->port // ""
l!l
5750
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
MC/DC0%0/1
$uri->host // ""
$uri->host
MC/DC0%0/1
$uri->port // ""
$uri->port
Truth table50%1/2
A
A= $uri->scheme
Aresult
11
00
Truth table50%1/2
A
A= $uri->host
Aresult
11
00
Truth table50%1/2
A
A= $uri->port
Aresult
11
00
400 }
401  
402  
403 111 ▶sub _is_literal {
Subroutine _is_literal: called CC=3 SCAR=11.0
404 111 my ( $self, $host ) = @_;
405 111 ( my $bare = $host ) =~ s/%.*\z//s;
406 111 ▶ return inet_pton(AF_INET, $bare) || inet_pton(AF_INET6, $bare) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
inet_pton(2, $bare) || inet_pton(10, $bare) ? : 17 94 111
Condition66%2/3
inet_pton(2, $bare) or inet_pton(10, $bare)
l!l&&r!l&&!r
17094
MC/DC50%1/2
inet_pton(2, $bare) or inet_pton(10, $bare)
inet_pton(2, $bare) inet_pton(10, $bare)
Truth table66%2/3
A or B
A= inet_pton(2, $bare)B= inet_pton(10, $bare)
ABresult
1X1
011
000
407 }
408  
409 58 __PACKAGE__->meta->make_immutable;
410  
411 58 1;
412  
413 __END__
414  
415 =pod
416  
417 =encoding UTF-8
418  
419 =head1 NAME
420  
421 Langertha::Raider::Provider::Fetch - Internal bounded fetch of a provider manifest from /.well-known/langertha.json
422  
423 =head1 VERSION
424  
425 version 0.503
426  
427 =head1 SYNOPSIS
428  
429 # Internal to Langertha-Raider -- no API promise.
430 my $fetch = Langertha::Raider::Provider::Fetch->new( allow_internal => 0 );
431 my $url = $fetch->target_url('provider.example'); # croaks on a bad target
432 my $got = $fetch->fetch_f($url)->get;
433 # { status => 'completed', url => ..., address => ..., body => ..., redirects => [...] }
434 # { status => 'refused' | 'failed', url => ..., error => ..., ... }
435  
436 =head1 DESCRIPTION
437  
438 B<Internal module.> Its interface may change without notice.
439  
440 Fetches a provider manifest (ADR 0007) the way a document from the network
441 has to be fetched before anything trusts it:
442  
443 =over
444  
445 =item * B<https only>, from C</.well-known/langertha.json> of the origin
446 the target names. A target with userinfo, a query or a fragment is refused
447 before anything is sent.
448  
449 =item * B<The target address is checked, and the connection goes to the
450 checked address.> The host is resolved once, every address it resolves to
451 is classified (L</address_kind>), and the request is sent to one of those
452 addresses, never to a name that could resolve differently a second time.
453 TLS still verifies the certificate against the host name (SNI and
454 hostname check use the name, not the address).
455  
456 =item * B<Internal addresses are refused> -- loopback, private (RFC 1918,
457 shared address space, IPv6 unique local), link-local and reserved ranges --
458 unless L</allow_internal> is set for a deliberately released internal
459 Knarr or Skeid origin. Cloud metadata addresses, the unspecified address,
460 multicast and the 240/4 range are refused always: none of them is ever a
461 provider. A host whose addresses include one refused address is refused as
462 a whole.
463  
464 =item * B<Limits>: L</max_bytes> of body (a larger C<Content-Length> stops
465 the fetch at the header, a longer body while it arrives), L</timeout>
466 seconds for everything from resolving to the last byte, L</max_redirects>
467 redirects. No content encoding is asked for, so nothing is decompressed.
468  
469 =item * B<A redirect is followed only within the origin> (same scheme,
470 host and port). A redirect to another origin is not followed and reported
471 with its target, which is how a credential can never reach another origin
472 through a redirect -- and this fetch sends none anyway: no
473 C<Authorization>, no cookies, no API key, only C<Accept> and a
474 C<User-Agent>.
475  
476 =back
477  
478 L</fetch_f> never fails for a network or policy reason; it resolves to a
479 report whose C<status> says how the fetch ended. Validation of the body is
480 the caller's job (L<Langertha::Manifest>).
481  
482 =head2 allow_internal
483  
484 Release loopback, private, link-local and reserved addresses (see
485 L</address_kind>). Default false. Never releases a C<never> address.
486  
487 =head2 max_bytes
488  
489 Largest body accepted, in bytes. Default 1 MiB.
490  
491 =head2 timeout
492  
493 Seconds the whole fetch may take, resolving included. Default 10.
494  
495 =head2 max_redirects
496  
497 How many same-origin redirects are followed. Default 3.
498  
499 =head2 loop
500  
501 The L<IO::Async::Loop>. Defaults to C<< IO::Async::Loop->new >>.
502  
503 =head2 resolver
504  
505 Code reference taking a host name and returning a L<Future> of its
506 addresses as strings. Defaults to the loop's resolver (C<getaddrinfo>).
507 An address literal is never handed to it.
508  
509 =head2 ssl_options
510  
511 Extra C<SSL_*> arguments for the TLS connection, such as C<SSL_ca_file>.
512 Default none: the system's CA store. Cannot switch off the certificate or
513 hostname check -- those are set after it.
514  
515 =head2 target_url
516  
517 my $url = $fetch->target_url('provider.example'); # https://provider.example/.well-known/langertha.json
518 my $url = $fetch->target_url('knarr.internal:8443');
519 my $url = $fetch->target_url('https://provider.example/');
520  
521 The manifest URL a command-line target names: C<HOST>, C<HOST:PORT>,
522 C<[IPv6]:PORT> or an C<https> URL whose path is empty, C</> or the
523 well-known path itself. Croaks with the reason on anything else: another
524 scheme (C<http> included), userinfo, a query, a fragment or another path.
525  
526 =head2 address_kind
527  
528 my ( $kind, $releasable ) = $fetch->address_kind('10.0.0.5'); # ( 'private', 1 )
529 my ( $kind ) = $fetch->address_kind('93.184.216.34'); # ( 'public' )
530  
531 Classifies an IPv4 or IPv6 address (a C<%scope> suffix is ignored):
532 C<public>, or one of C<loopback>, C<private>, C<link-local>, C<reserved>
533 (releasable with L</allow_internal>) or C<metadata>, C<unspecified>,
534 C<multicast>, C<invalid> (never). IPv4-mapped, IPv4-compatible, NAT64
535 (C<64:ff9b::/96>) and 6to4 (C<2002::/16>) IPv6 addresses are classified by
536 the IPv4 address they carry. The second value is true for a releasable
537 kind.
538  
539 =head2 check_addresses
540  
541 my $refusal = $fetch->check_addresses($host, @addresses);
542  
543 C<undef> when every address may be connected to, else the reason the
544 first one that may not is refused.
545  
546 =head2 fetch_f
547  
548 my $report = await $fetch->fetch_f($url);
549  
550 Fetches the manifest at C<$url> (from L</target_url>) and resolves to a
551 report hash:
552  
553 =over
554  
555 =item C<status> -- C<completed>, C<refused> (an address or a redirect the
556 policy does not allow) or C<failed> (network, TLS, HTTP status, a limit).
557  
558 =item C<url> -- the URL asked for; C<final_url> -- the one the body came
559 from (after same-origin redirects).
560  
561 =item C<address> -- the address connected to (once there is one).
562  
563 =item C<redirects> -- the same-origin locations followed, in order.
564  
565 =item C<body> -- the body octets (C<completed> only).
566  
567 =item C<error> -- why (C<refused> and C<failed>); C<location> -- the
568 target of a redirect that was not followed.
569  
570 =back
571  
572 =head2 check_host_f
573  
574 my $checked = await $fetch->check_host_f('provider.example');
575 # { addresses => [ '93.184.216.34' ] }
576 # { status => 'refused' | 'failed', error => ... }
577  
578 Resolves C<$host> (an address literal stands for itself) and checks every
579 address with L</check_addresses>. Resolves to the addresses, or to the
580 C<refused> or C<failed> status and why.
581  
582 =head2 origin
583  
584 my $origin = $fetch->origin('https://Provider.example/v1'); # https://provider.example:443
585  
586 Scheme, host and port of a URL (string or L<URI>), lower-cased and with
587 the default port spelled out, so two origins compare with C<eq>; C<undef>
588 for a URL without a host.
589  
590 =head1 SEE ALSO
591  
592 =over
593  
594 =item * L<Langertha::Raider::CLI::Provider> -- C<raider provider inspect>
595  
596 =item * L<Langertha::Manifest> -- the manifest's schema and validator (core)
597  
598 =back
599  
600 =head1 SUPPORT
601  
602 =head2 Issues
603  
604 Please report bugs and feature requests on GitHub at
605 L<https://github.com/Getty/langertha-raider/issues>.
606  
607 =head2 IRC
608  
609 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
610  
611 =head1 CONTRIBUTING
612  
613 Contributions are welcome! Please fork the repository and submit a pull request.
614  
615 =head1 AUTHOR
616  
617 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
618  
619 =head1 COPYRIGHT AND LICENSE
620  
621 This software is copyright (c) 2026 by Torsten Raudssus.
622  
623 This is free software; you can redistribute it and/or modify it under
624 the same terms as the Perl 5 programming language system itself.
625  
626 =cut
« blib/lib/Langertha/Raider/Plugin/Trace.pm ↑ Summary blib/lib/Langertha/Raider/Packs/Collection.pm »