| 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.0 : 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 | ▶ | method fetch_piece ($segments) { | |||||||||||||||||||||||||||
|
Subroutine
fetch_piece: called CC=13 SCAR=36.9
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | return undef if $disabled; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition50%1/2
$response->{"status"} // ""
MC/DC0%0/1
$response->{"status"} // ""
$response->{"status"}
Truth table50%1/2
A
A= $response->{"status"}
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $loc = $response->{headers}{location} // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$response->{"headers"}{"location"} // ""
MC/DC0%0/1
$response->{"headers"}{"location"} // ""
$response->{"headers"}{"location"}
Truth table0%0/2
A
A= $response->{"headers"}{"location"}
| |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | unless ( is_safe_url($loc) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | $full_data .= $response->{content} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$response->{"content"} // ""
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | if ( length($full_data) > MAX_WEEDSEED_RESPONSE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | method _build_url ($seg) { | |||||||||||||||||||||||||||
|
Subroutine
_build_url: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 55 | 2 | my $target_url = $url; | |||||||||||||||||||||||||||
| 56 | 2 | ▶ | if ( $target_url =~ m{/$} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | my $rel = $seg->{rel_path} // $seg->{file}->path->basename; | ||||||||||||||||||||||||||
|
Condition33%1/3
$seg->{"rel_path"} // $seg->{"file"}->path->basename
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
| |||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_ ne "" and $_ ne "."
MC/DC0%0/2
$_ ne "" and $_ ne "."
$_ ne ""
$_ ne "."
Truth table33%1/3
A and B
A= $_ ne ""B= $_ ne "."
| |||||||||||||||||||||||||||||
| 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 | ▶ | 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
| |||||||||||||||||||||||||||||
| 73 | 1 | return $self->fetch_piece( [ { file => undef, offset => $start, length => ( $end - $start + 1 ), rel_path => undef } ] ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | }; | ||||||||||||||||||||||||||||
| 76 | 28 | 1; | |||||||||||||||||||||||||||