lib/Net/BitTorrent/Tracker/HTTP.pm

filter: Statementstmt 65.8% 108 / 164 Branchbran 46.5% 27 / 58 Conditioncond 50.0% 22 / 44 MC/DCmcdc 30.7% 8 / 26 Subroutinesub 100% 18 / 18 Podpod 100% 5 / 5 totaltotal 59.6% 188 / 315 CC 50 SCAR 35.8
CC
50
Cov
59%
perform_scrape
128.7
parse_response
25.4
perform_announce
22.7
CRAP 36.0
« lib/Net/BitTorrent/Tracker/UDP.pm ↑ Summary lib/Net/BitTorrent/Torrent.pm »
1 29 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 29 ▶use feature 'class', 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 29 ▶no warnings 'experimental::class', 'experimental::try';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶class Net::BitTorrent::Tracker::HTTP v2.1.1 : isa(Net::BitTorrent::Tracker::Base) {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶ use Net::BitTorrent::Protocol::BEP03::Bencode qw[bdecode];
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶ use Net::BitTorrent::Protocol::BEP23;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶ use Net::BitTorrent::SSRF qw[is_safe_url is_safe_ip resolve_and_pin];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶ use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶ use URI::Escape qw[uri_escape];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶ use constant MAX_TRACKER_RESPONSE_SIZE => 1024 * 1024; # 1MB
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶ use constant MAX_TRACKER_PEERS => 1000; # Max peers from compact response
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶ method build_announce_url ($params) {
Subroutine build_announce_url: called CC=7 SCAR=19.5
Pod documented
15 6 my $full_url = $self->url;
16 6 ▶ $full_url .= ( $full_url =~ /\?/ ? '&' : '?' );
Branch: 1 branch
BranchTrueFalseTotal
$full_url =~ /\?/u ? : 0 6 6
17 6 my @query;
18 6 for my $key ( sort keys %$params ) {
19 32 ▶ next if $key eq 'ua';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "ua" 1 31 32
20 31 ▶ my $val = $params->{$key} // '';
Condition50%1/2
$params->{$key} // ""
l!l
310
MC/DC0%0/1
$params->{$key} // ""
$params->{$key}
Truth table50%1/2
A
A= $params->{$key}
Aresult
11
00
21 31 ▶ if ( $key eq 'info_hash' || $key eq 'peer_id' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'info_hash' or $key eq 'peer_id') { } 8 23 31
Condition100%3/3
$key eq "info_hash" or $key eq "peer_id"
l!l&&r!l&&!r
2623
MC/DC100%2/2
$key eq "info_hash" or $key eq "peer_id"
$key eq "info_hash" $key eq "peer_id"
Truth table100%3/3
A or B
A= $key eq "info_hash"B= $key eq "peer_id"
ABresult
1X1
011
000
22 8 $val = join( '', map { sprintf( '%%%02x', ord($_) ) } split( '', $val ) );
23 }
24 else {
25 23 $val = uri_escape($val);
26 }
27 31 push @query, "$key=$val";
28 }
29 6 return $full_url . join( '&', @query );
30 }
31  
32 1 ▶ method build_scrape_url ($infohashes) {
Subroutine build_scrape_url: called CC=4 SCAR=14.2
Pod documented
33 1 my $scrape_url = $self->url;
34 1 ▶ if ( $scrape_url =~ /\/announce$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scrape_url =~ m[/announce$]u) 0 1 1
35 0 $scrape_url =~ s/\/announce$/\/scrape/;
36 }
37 1 my $full_url = $scrape_url;
38 1 ▶ $full_url .= ( $scrape_url =~ /\?/ ? '&' : '?' );
Branch: 1 branch
BranchTrueFalseTotal
$scrape_url =~ /\?/u ? : 0 1 1
39 1 my @query;
40 1 for my $ih (@$infohashes) {
41 1 my $val = join( '', map { sprintf( '%%%02x', ord($_) ) } split( '', $ih ) );
42 1 push @query, "info_hash=$val";
43 }
44 1 return $full_url . join( '&', @query );
45 }
46  
47 5 ▶ method parse_response ($data) {
Subroutine parse_response: called CC=12 SCAR=32.3
Pod documented
48 5 my $dict;
49 5 try { $dict = bdecode($data) }
50 catch ($e) {
51 0 $self->_emit_log( 'error', 'Malformed tracker response: ' . $e );
52 0 return { failure_reason => 'Malformed tracker response: ' . $e };
53 }
54 5 ▶ if ( !defined $dict || ref $dict ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dict or ref $dict ne "HASH") 2 3 5
Condition100%3/3
not defined $dict or ref $dict ne "HASH"
l!l&&r!l&&!r
113
MC/DC100%2/2
not defined $dict or ref $dict ne "HASH"
not defined $dict ref $dict ne "HASH"
Truth table100%3/3
not(A) or B
A= not defined $dictB= ref $dict ne "HASH"
ABresult
1X1
011
000
55 2 return { failure_reason => 'Tracker response is not a valid dictionary' };
56 }
57 3 ▶ if ( $dict->{failure_reason} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dict->{"failure_reason"}) 0 3 3
58 0 $self->_emit_log( 'error', 'Tracker failure: ' . $dict->{failure_reason} );
59 0 return $dict;
60 }
61 3 ▶ if ( defined $dict->{peers} && !ref $dict->{peers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dict->{"peers"} and not ref $dict->{"peers"}) 3 0 3
Condition33%1/3
defined $dict->{"peers"} and not ref $dict->{"peers"}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $dict->{"peers"} and not ref $dict->{"peers"}
defined $dict->{"peers"} not ref $dict->{"peers"}
Truth table33%1/3
A and not(B)
A= defined $dict->{"peers"}B= not ref $dict->{"peers"}
ABresult
0X0
100
111
62 3 try { $dict->{peers} = Net::BitTorrent::Protocol::BEP23::unpack_peers_ipv4( $dict->{peers} ) }
63 catch ($e) {
64 1 $self->_emit_log( 'error', 'Malformed compact IPv4 peer list: ' . $e );
65 1 return { failure_reason => 'Malformed compact peer list: ' . $e };
66 }
67 }
68 2 ▶ if ( defined $dict->{peers6} && !ref $dict->{peers6} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dict->{"peers6"} and not ref $dict->{"peers6"}) 0 2 2
Condition33%1/3
defined $dict->{"peers6"} and not ref $dict->{"peers6"}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $dict->{"peers6"} and not ref $dict->{"peers6"}
defined $dict->{"peers6"} not ref $dict->{"peers6"}
Truth table33%1/3
A and not(B)
A= defined $dict->{"peers6"}B= not ref $dict->{"peers6"}
ABresult
0X0
100
111
69 0 try {
70 0 my $p6 = Net::BitTorrent::Protocol::BEP23::unpack_peers_ipv6( $dict->{peers6} );
71 0 ▶ $dict->{peers} = [ @{ $dict->{peers} // [] }, @$p6 ];
Condition0%0/2
$dict->{"peers"} // []
l!l
00
MC/DC0%0/1
$dict->{"peers"} // []
$dict->{"peers"}
Truth table0%0/2
A
A= $dict->{"peers"}
Aresult
11
00
72 }
73 catch ($e) {
74 0 $self->_emit_log( 'error', 'Malformed compact IPv6 peer list: ' . $e );
75 0 return { failure_reason => 'Malformed compact peer6 list: ' . $e };
76 }
77 }
78 0 $dict->{peers} = [ @{ $dict->{peers} }[ 0 .. MAX_TRACKER_PEERS - 1 ] ]
79 2 ▶ if ref $dict->{peers} eq 'ARRAY' && @{ $dict->{peers} } > MAX_TRACKER_PEERS;
Branch: 1 branch
BranchTrueFalseTotal
if ref $dict->{"peers"} eq "ARRAY" and @{$dict->{"peers"};} > 1000 0 2 2
Condition33%1/3
ref $dict->{"peers"} eq "ARRAY" and @{$dict->{"peers"};} > 1000
!ll&&!rl&&r
020
MC/DC0%0/2
ref $dict->{"peers"} eq "ARRAY" and @{$dict->{"peers"};} > 1000
ref $dict->{"peers"} eq "ARRAY" @{$dict->{"peers"};} > 1000
Truth table33%1/3
A and B
A= ref $dict->{"peers"} eq "ARRAY"B= @{$dict->{"peers"};} > 1000
ABresult
0X0
100
111
80 2 ▶ $dict->{peers} //= []; # Ensure it is an array ref
Condition50%1/2
$dict->{"peers"} //= []
l!l
20
MC/DC0%0/1
$dict->{"peers"} //= []
$dict->{"peers"}
Truth table50%1/2
A
A= $dict->{"peers"}
Aresult
11
00
81 2 return $dict;
82 }
83  
84 5 ▶ method perform_announce ( $params, $cb = undef ) {
Subroutine perform_announce: called CC=14 SCAR=31.2
Pod documented
85 5 my $target = $self->build_announce_url($params);
86 5 ▶ if ( !$self->ssrf_bypass && !is_safe_url($target) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ssrf_bypass or is_safe_url($target)) 3 2 5
Condition100%3/3
$self->ssrf_bypass or is_safe_url($target)
l!l&&r!l&&!r
113
MC/DC100%2/2
$self->ssrf_bypass or is_safe_url($target)
$self->ssrf_bypass is_safe_url($target)
Truth table100%3/3
A or B
A= $self->ssrf_bypassB= is_safe_url($target)
ABresult
1X1
011
000
87 3 $self->_emit_log( 'warn', 'HTTP announce blocked by SSRF policy: ' . $target );
88 3 return undef;
89 }
90  
91 # Resolve and pin the IP to prevent TOCTOU DNS rebinding.
92 2 my $uri = URI->new($target);
93 2 my $host = $uri->host;
94 2 my $port = $uri->port;
95 2 ▶ if ( !$self->ssrf_bypass && $host && !is_safe_ip($host) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->ssrf_bypass and $host and not is_safe_ip($host)) 1 1 2
Condition66%4/6
not $self->ssrf_bypass and $host and not is_safe_ip($host)
!ll&&!rl&&r
101
not $self->ssrf_bypass and $host
!ll&&!rl&&r
101
MC/DC33%1/3
not $self->ssrf_bypass and $host and not is_safe_ip($host)
not $self->ssrf_bypass $host not is_safe_ip($host)
Truth table50%2/4
not(A) and B and not(C)
A= not $self->ssrf_bypassB= $hostC= not is_safe_ip($host)
ABCresult
0XX0
10X0
1100
1111
96 1 my ( $pinned_ip, $pinned_port ) = resolve_and_pin( $host, $port );
97 1 ▶ if ( !defined $pinned_ip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pinned_ip) 0 1 1
98 0 $self->_emit_log( 'warn', 'HTTP announce DNS pinning failed (unsafe resolution): ' . $host );
99 0 return undef;
100 }
101 1 ▶ unless ( is_safe_ip($pinned_ip) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_safe_ip($pinned_ip)) 0 1 1
102 0 $self->_emit_log( 'warn', 'HTTP announce pinned IP failed re-validation: ' . $pinned_ip );
103 0 return undef;
104 }
105 1 $uri->host($pinned_ip);
106 1 ▶ $uri->port($pinned_port) if defined $pinned_port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pinned_port 1 0 1
107 1 $target = $uri->as_string();
108 }
109 2 ▶ if ( $params->{ua} && $params->{ua}->can('get') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"ua"} and $params->{"ua"}->can("get")) 1 1 2
Condition66%2/3
$params->{"ua"} and $params->{"ua"}->can("get")
!ll&&!rl&&r
101
MC/DC50%1/2
$params->{"ua"} and $params->{"ua"}->can("get")
$params->{"ua"} $params->{"ua"}->can("get")
Truth table66%2/3
A and B
A= $params->{"ua"}B= $params->{"ua"}->can("get")
ABresult
0X0
100
111
110 $params->{ua}->get(
111 $target,
112 1 ▶ sub ( $res, @ ) {
Subroutine __ANON__: called CC=5 SCAR=20.3
113 1 ▶ if ( $res->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'success'}) { } 1 0 1
114 1 ▶ if ( length( $res->{content} // '' ) > MAX_TRACKER_RESPONSE_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($res->{"content"} // "") > 1048576) 0 1 1
Condition50%1/2
$res->{"content"} // ""
l!l
10
MC/DC0%0/1
$res->{"content"} // ""
$res->{"content"}
Truth table50%1/2
A
A= $res->{"content"}
Aresult
11
00
115 0 $self->_emit_log( 'warn', 'Async tracker response exceeded max size' );
116 0 return;
117 }
118 1 try {
119 1 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 1 0 1
120 1 $cb->( $self->parse_response( $res->{content} ) );
121 }
122 }
123 catch ($e) {
124 0 $self->_emit_log( 'error', 'Error in HTTP announce callback: ' . $e );
125 }
126 }
127 else {
128 0 $self->_emit_log( 'error', "Async HTTP error during announce: $res->{status} $res->{reason}" );
129 }
130 }
131 1 );
132 1 return;
133 }
134 1 my $http = HTTP::Tiny->new( max_size => MAX_TRACKER_RESPONSE_SIZE );
135 1 my $response = $http->get($target);
136 1 ▶ if ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'success'}) { } 0 1 1
137 0 my $parsed;
138 0 try { $parsed = $self->parse_response( $response->{content} ) }
139 catch ($e) {
140 0 $self->_emit_log( 'error', "Error parsing tracker announce response: $e" );
141 0 return { failure_reason => "Error parsing tracker response: $e" };
142 }
143 0 ▶ $cb->($parsed) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
144 0 return $parsed;
145 }
146 else {
147 1 $self->_emit_log( 'error', "HTTP error during announce: $response->{status} $response->{reason}" );
148 1 return undef;
149 }
150 }
151  
152 1 ▶ method perform_scrape ( $infohashes, $cb = undef ) {
Subroutine perform_scrape: called CC=13 SCAR=48.6
Pod documented
153 1 my $target = $self->build_scrape_url($infohashes);
154 1 ▶ if ( !$self->ssrf_bypass && !is_safe_url($target) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->ssrf_bypass or is_safe_url($target)) 1 0 1
Condition33%1/3
$self->ssrf_bypass or is_safe_url($target)
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->ssrf_bypass or is_safe_url($target)
$self->ssrf_bypass is_safe_url($target)
Truth table33%1/3
A or B
A= $self->ssrf_bypassB= is_safe_url($target)
ABresult
1X1
011
000
155 1 $self->_emit_log( 'warn', 'HTTP scrape blocked by SSRF policy: ' . $target );
156 1 return undef;
157 }
158  
159 # Resolve and pin the IP to prevent TOCTOU DNS rebinding.
160 0 my $uri = URI->new($target);
161 0 my $host = $uri->host;
162 0 my $port = $uri->port;
163 0 ▶ if ( !$self->ssrf_bypass && $host && !is_safe_ip($host) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->ssrf_bypass and $host and not is_safe_ip($host)) 0 0 0
Condition0%0/6
not $self->ssrf_bypass and $host and not is_safe_ip($host)
!ll&&!rl&&r
000
not $self->ssrf_bypass and $host
!ll&&!rl&&r
000
MC/DC0%0/3
not $self->ssrf_bypass and $host and not is_safe_ip($host)
not $self->ssrf_bypass $host not is_safe_ip($host)
Truth table0%0/4
not(A) and B and not(C)
A= not $self->ssrf_bypassB= $hostC= not is_safe_ip($host)
ABCresult
0XX0
10X0
1100
1111
164 0 my ( $pinned_ip, $pinned_port ) = resolve_and_pin( $host, $port );
165 0 ▶ if ( !defined $pinned_ip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pinned_ip) 0 0 0
166 0 $self->_emit_log( 'warn', 'HTTP scrape DNS pinning failed (unsafe resolution): ' . $host );
167 0 return undef;
168 }
169 0 ▶ unless ( is_safe_ip($pinned_ip) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_safe_ip($pinned_ip)) 0 0 0
170 0 $self->_emit_log( 'warn', 'HTTP scrape pinned IP failed re-validation: ' . $pinned_ip );
171 0 return undef;
172 }
173 0 $uri->host($pinned_ip);
174 0 ▶ $uri->port($pinned_port) if defined $pinned_port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pinned_port 0 0 0
175 0 $target = $uri->as_string();
176 }
177 0 my $http = HTTP::Tiny->new( max_size => MAX_TRACKER_RESPONSE_SIZE );
178 0 my $response = $http->get($target);
179 0 ▶ if ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'success'}) { } 0 0 0
180 0 my $parsed;
181 0 try { $parsed = bdecode( $response->{content} ) }
182 catch ($e) {
183 0 $self->_emit_log( 'error', 'Malformed HTTP scrape response: ' . $e );
184 0 return undef;
185 }
186 0 ▶ if ( ref $parsed ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parsed ne "HASH") 0 0 0
187 0 $self->_emit_log( 'warn', 'HTTP scrape response is not a dictionary' );
188 0 return undef;
189 }
190 0 ▶ $cb->($parsed) if $parsed;
Branch: 1 branch
BranchTrueFalseTotal
if $parsed 0 0 0
191 0 return $parsed;
192 }
193 else {
194 0 $self->_emit_log( 'error', "HTTP scrape error: $response->{status} $response->{reason}" );
195 0 return undef;
196 }
197 }
198 };
199 29 1;
« lib/Net/BitTorrent/Tracker/UDP.pm ↑ Summary lib/Net/BitTorrent/Torrent.pm »