| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | my @query; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 6 | for my $key ( sort keys %$params ) { | |||||||||||||||||||||||||||||||||||||||||
| 19 | 32 | ▶ | next if $key eq 'ua'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 31 | ▶ | my $val = $params->{$key} // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{$key} // ""
MC/DC0%0/1
$params->{$key} // ""
$params->{$key}
Truth table50%1/2
A
A= $params->{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 31 | ▶ | if ( $key eq 'info_hash' || $key eq 'peer_id' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key eq "info_hash" or $key eq "peer_id"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | $scrape_url =~ s/\/announce$/\/scrape/; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | my $full_url = $scrape_url; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | $full_url .= ( $scrape_url =~ /\?/ ? '&' : '?' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $dict or ref $dict ne "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | return { failure_reason => 'Tracker response is not a valid dictionary' }; | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | if ( $dict->{failure_reason} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $dict->{"peers"} and not ref $dict->{"peers"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $dict->{"peers6"} and not ref $dict->{"peers6"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // []
MC/DC0%0/1
$dict->{"peers"} // []
$dict->{"peers"}
Truth table0%0/2
A
A= $dict->{"peers"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $dict->{"peers"} eq "ARRAY" and @{$dict->{"peers"};} > 1000
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
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | ▶ | $dict->{peers} //= []; # Ensure it is an array ref | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$dict->{"peers"} //= []
MC/DC0%0/1
$dict->{"peers"} //= []
$dict->{"peers"}
Truth table50%1/2
A
A= $dict->{"peers"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->ssrf_bypass or is_safe_url($target)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
not $self->ssrf_bypass and $host and not is_safe_ip($host)
not $self->ssrf_bypass and $host
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | my ( $pinned_ip, $pinned_port ) = resolve_and_pin( $host, $port ); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | if ( !defined $pinned_ip ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | $target = $uri->as_string(); | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | if ( $params->{ua} && $params->{ua}->can('get') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$params->{"ua"} and $params->{"ua"}->can("get")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | if ( length( $res->{content} // '' ) > MAX_TRACKER_RESPONSE_SIZE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$res->{"content"} // ""
MC/DC0%0/1
$res->{"content"} // ""
$res->{"content"}
Truth table50%1/2
A
A= $res->{"content"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->ssrf_bypass or is_safe_url($target)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
not $self->ssrf_bypass and $host and not is_safe_ip($host)
not $self->ssrf_bypass and $host
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my ( $pinned_ip, $pinned_port ) = resolve_and_pin( $host, $port ); | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if ( !defined $pinned_ip ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||