lib/Net/BitTorrent/Tracker/WebSeed.pm

filter: Statementstmt 80.0% 56 / 70 Branchbran 50.0% 9 / 18 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 66.9% 83 / 124 CC 18 SCAR 29.9
CC
18
Cov
68%
fetch_piece
39.9
_build_url
5.5
fetch_piece_legacy
2.0
CRAP 19.9
« lib/Net/BitTorrent/Protocol/BEP03/Bencode.pm ↑ Summary lib/Net/BitTorrent/SSRF.pm »
1 28 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 28 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 28 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 class Net::BitTorrent::Tracker::WebSeed v2.1.1 : isa(Net::BitTorrent::Emitter) {
6 28 ▶ use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶ use Net::BitTorrent::SSRF qw[is_safe_url];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 field $url : param : reader; # Base URL
9 28 field $disabled : reader = !is_safe_url($url);
10 28 ▶ use constant MAX_WEEDSEED_RESPONSE => 16 * 1024 * 1024; # 16 MB max per piece response
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶ use constant MAX_WEEDSEED_SINGLE_RESP => 16 * 1024 * 1024; # 16 MB max per single HTTP response
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶ method fetch_piece ($segments) {
Subroutine fetch_piece: called CC=13 SCAR=36.9
Pod documented
14 3 ▶ return undef if $disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $disabled 1 2 3
15 2 my $http = HTTP::Tiny->new( max_redirect => 0, max_size => MAX_WEEDSEED_SINGLE_RESP );
16 2 my $full_data = '';
17 2 for my $seg (@$segments) {
18 2 my $target_url = $self->_build_url($seg);
19 2 ▶ unless ( is_safe_url($target_url) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_safe_url($target_url)) 0 2 2
20 0 $self->_emit_log( 'warn', 'URL blocked by SSRF policy: ' . $target_url );
21 0 return undef;
22 }
23 2 my $response;
24 2 for ( 1 .. 5 ) {
25 2 $response = $http->get( $target_url, { headers => { Range => "bytes=$seg->{offset}-" . ( $seg->{offset} + $seg->{length} - 1 ) } } );
26 2 ▶ last unless ( $response->{status} // '' ) =~ /^3/;
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"status"} // "") =~ /^3/u 2 0 2
Condition50%1/2
$response->{"status"} // ""
l!l
02
MC/DC0%0/1
$response->{"status"} // ""
$response->{"status"}
Truth table50%1/2
A
A= $response->{"status"}
Aresult
11
00
27 0 ▶ my $loc = $response->{headers}{location} // '';
Condition0%0/2
$response->{"headers"}{"location"} // ""
l!l
00
MC/DC0%0/1
$response->{"headers"}{"location"} // ""
$response->{"headers"}{"location"}
Truth table0%0/2
A
A= $response->{"headers"}{"location"}
Aresult
11
00
28 0 ▶ unless ( is_safe_url($loc) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_safe_url($loc)) 0 0 0
29 0 $self->_emit_log( 'warn', 'Redirect blocked by SSRF policy: ' . $loc );
30 0 return undef;
31 }
32 0 $target_url = $loc;
33 }
34 2 ▶ if ( $response->{success} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->{'success'}) { } 2 0 2
elsif ($response->{'status'} == 410) { } 0 0 0
35 2 ▶ $full_data .= $response->{content} // '';
Condition50%1/2
$response->{"content"} // ""
l!l
20
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
Aresult
11
00
36 2 ▶ if ( length($full_data) > MAX_WEEDSEED_RESPONSE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $full_data > 16777216) 0 2 2
37 0 $self->_emit_log( 'warn', 'WebSeed response exceeded max size, aborting' );
38 0 return undef;
39 }
40 }
41 elsif ( $response->{status} == 410 ) {
42 0 $disabled = 1;
43 0 $self->_emit_log( 'warn', "Resource 410 Gone: $target_url. Disabling webseed." );
44 0 return undef;
45 }
46 else {
47 0 $self->_emit_log( 'error', "WebSeed fetch failed: $response->{status} $response->{reason} (URL: $target_url)" );
48 0 return undef;
49 }
50 }
51 2 return $full_data;
52 }
53  
54 2 ▶ method _build_url ($seg) {
Subroutine _build_url: called CC=5 SCAR=17.1
55 2 my $target_url = $url;
56 2 ▶ if ( $target_url =~ m{/$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_url =~ m[/$]u) 1 1 2
57 1 ▶ my $rel = $seg->{rel_path} // $seg->{file}->path->basename;
Condition33%1/3
$seg->{"rel_path"} // $seg->{"file"}->path->basename
l!l&&r!l&&!r
100
MC/DC0%0/2
$seg->{"rel_path"} // $seg->{"file"}->path->basename
$seg->{"rel_path"} $seg->{"file"}->path->basename
Truth table33%1/3
A // B
A= $seg->{"rel_path"}B= $seg->{"file"}->path->basename
ABresult
1X1
011
000
58 1 $rel =~ s{\\}{/}g; # Normalize backslashes
59 1 $rel =~ s{/+}{/}g; # Collapse multiple slashes
60 1 $rel =~ s{^/}{}; # Remove leading slash
61 1 ▶ my @parts = grep { $_ ne '' && $_ ne '.' && $_ ne '..' } split /\//, $rel;
Condition33%2/6
$_ ne '' && $_ ne '.' && $_ ne '..'
!ll&&!rl&&r
001
$_ ne "" and $_ ne "."
!ll&&!rl&&r
001
MC/DC0%0/3
$_ ne '' && $_ ne '.' && $_ ne '..'
$_ ne "" $_ ne "." $_ ne '..'
Truth table25%1/4
A and B && C
A= $_ ne ""B= $_ ne "."C= $_ ne '..'
ABCresult
0XX0
10X0
1100
1111
62 1 $rel = join( '/', @parts );
63 1 $target_url .= $rel;
64 }
65 2 return $target_url;
66 }
67  
68 # Backward compatibility for single-file v1
69 1 ▶ method fetch_piece_legacy ( $index, $piece_length, $total_size ) {
Subroutine fetch_piece_legacy: called CC=2 SCAR=7.0
Pod documented
70 1 my $start = $index * $piece_length;
71 1 my $end = $start + $piece_length - 1;
72 1 ▶ $end = $total_size - 1 if $end >= $total_size;
Branch: 1 branch
BranchTrueFalseTotal
if $end >= $total_size 0 1 1
73 1 return $self->fetch_piece( [ { file => undef, offset => $start, length => ( $end - $start + 1 ), rel_path => undef } ] );
74 }
75 };
76 28 1;
« lib/Net/BitTorrent/Protocol/BEP03/Bencode.pm ↑ Summary lib/Net/BitTorrent/SSRF.pm »