| 1 | 44 | package Net::BitTorrent::SSRF v2.1.1 { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 44 | ▶ | use v5.40; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 44 | ▶ | use Exporter qw[import]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 44 | ▶ | use Socket qw[inet_pton inet_ntop AF_INET AF_INET6 AF_UNSPEC SOCK_STREAM getaddrinfo getnameinfo NI_NUMERICHOST]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 44 | our @EXPORT_OK = qw[is_safe_ip is_safe_host is_safe_url resolve_and_pin]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # This should honestly be part of a dist all on its own... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2292 | ▶ | sub is_safe_ip ($ip) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_safe_ip: called CC=24 SCAR=32.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 2292 | my $packed = inet_pton( AF_INET, $ip ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2292 | ▶ | if ($packed) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1837 | my @o = unpack( 'C4', $packed ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1837 | ▶ | return 0 if $o[0] == 127; # 127.0.0.0/8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1776 | ▶ | return 0 if $o[0] == 10; # 10.0.0.0/8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1657 | ▶ | return 0 if $o[0] == 172 && $o[1] >= 16 && $o[1] <= 31; # 172.16.0.0/12 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$o[0] == 172 and $o[1] >= 16 and $o[1] <= 31
$o[0] == 172 and $o[1] >= 16
MC/DC100%3/3
$o[0] == 172 and $o[1] >= 16 and $o[1] <= 31
$o[0] == 172
$o[1] >= 16
$o[1] <= 31
Truth table100%4/4
A and B and C
A= $o[0] == 172B= $o[1] >= 16C= $o[1] <= 31
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1655 | ▶ | return 0 if $o[0] == 192 && $o[1] == 168; # 192.168.0.0/16 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$o[0] == 192 and $o[1] == 168
MC/DC50%1/2
$o[0] == 192 and $o[1] == 168
$o[0] == 192
$o[1] == 168
Truth table66%2/3
A and B
A= $o[0] == 192B= $o[1] == 168
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1644 | ▶ | return 0 if $o[0] == 169 && $o[1] == 254; # 169.254.0.0/16 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$o[0] == 169 and $o[1] == 254
MC/DC50%1/2
$o[0] == 169 and $o[1] == 254
$o[0] == 169
$o[1] == 254
Truth table66%2/3
A and B
A= $o[0] == 169B= $o[1] == 254
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1635 | ▶ | return 0 if ( $o[0] & 0xF0 ) == 224; # 224.0.0.0/4 multicast | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1633 | ▶ | return 0 if $o[0] == 0; # 0.0.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1632 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 455 | $packed = inet_pton( AF_INET6, $ip ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 455 | ▶ | if ($packed) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 195 | my @w = unpack( 'n8', $packed ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 195 | ▶ | return 0 if $packed eq ( "\0" x 15 ) . "\1"; # ::1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 192 | ▶ | return 0 if $packed eq "\0" x 16; # :: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 192 | ▶ | return 0 if ( $w[0] & 0xFFC0 ) == 0xFE80; # fe80::/10 link-local | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 189 | ▶ | return 0 if ( $w[0] & 0xFE00 ) == 0xFC00; # fc00::/7 ULA | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 187 | ▶ | return 0 if ( $w[0] & 0xFF00 ) == 0xFF00; # ff00::/8 multicast | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 186 | ▶ | return 0 if $w[0] == 0 && $w[1] == 0 && $w[2] == 0 && $w[3] == 0 && $w[5] == 0xFFFF; # ::ffff:0:0/96 IPv4-mapped | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%8/12
$w[0] == 0 and $w[1] == 0 and $w[2] == 0 and $w[3] == 0 and $w[5] == 65535
$w[0] == 0 and $w[1] == 0 and $w[2] == 0 and $w[3] == 0
$w[0] == 0 and $w[1] == 0 and $w[2] == 0
$w[0] == 0 and $w[1] == 0
MC/DC20%1/5
$w[0] == 0 and $w[1] == 0 and $w[2] == 0 and $w[3] == 0 and $w[5] == 65535
$w[0] == 0
$w[1] == 0
$w[2] == 0
$w[3] == 0
$w[5] == 65535
Truth table33%2/6
A and B and C and D and E
A= $w[0] == 0B= $w[1] == 0C= $w[2] == 0D= $w[3] == 0E= $w[5] == 65535
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 181 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 260 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 320 | ▶ | sub is_safe_host ($host) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_safe_host: called CC=10 SCAR=25.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 320 | ▶ | return 0 unless defined $host && length $host; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $host and length $host
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 320 | ▶ | return is_safe_ip($host) if is_safe_ip($host); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 269 | my ( $err, @results ) = getaddrinfo( $host, undef, { family => AF_UNSPEC, socktype => SOCK_STREAM } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 269 | ▶ | return 0 if $err || !@results; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$err or not @results
MC/DC0%0/2
$err or not @results
$err
not @results
Truth table33%1/3
A or not(B)
A= $errB= not @results
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 269 | for my $res (@results) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 457 | my ( $gerr, $ip ) = getnameinfo( $res->{addr}, NI_NUMERICHOST ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 457 | ▶ | return 0 if $gerr || !defined $ip || !is_safe_ip($ip); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$gerr or not defined $ip or not is_safe_ip($ip)
$gerr or not defined $ip
MC/DC33%1/3
$gerr or not defined $ip or not is_safe_ip($ip)
$gerr
not defined $ip
not is_safe_ip($ip)
Truth table50%2/4
A or not(B) or not(C)
A= $gerrB= not defined $ipC= not is_safe_ip($ip)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 254 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 17 | ▶ | sub is_safe_url ($url) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_safe_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 17 | require URI; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 17 | my $uri = URI->new($url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 17 | my $host = $uri->host; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 17 | return is_safe_host($host); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 11 | ▶ | sub resolve_and_pin ( $host, $port = undef ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve_and_pin: called CC=10 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 11 | ▶ | return ( $host, $port ) if is_safe_ip($host); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 10 | my ( $err, @results ) = getaddrinfo( $host, $port, { family => AF_UNSPEC, socktype => SOCK_STREAM } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 10 | ▶ | return () if $err || !@results; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$err or not @results
MC/DC50%1/2
$err or not @results
$err
not @results
Truth table66%2/3
A or not(B)
A= $errB= not @results
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 8 | for my $res (@results) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 8 | my ( $gerr, $ip ) = getnameinfo( $res->{addr}, NI_NUMERICHOST ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 8 | ▶ | next if $gerr || !defined $ip; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$gerr or not defined $ip
MC/DC0%0/2
$gerr or not defined $ip
$gerr
not defined $ip
Truth table33%1/3
A or not(B)
A= $gerrB= not defined $ip
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 8 | ▶ | next unless is_safe_ip($ip); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | my $resolved_port = $res->{port} // $port; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$res->{"port"} // $port
MC/DC0%0/2
$res->{"port"} // $port
$res->{"port"}
$port
Truth table33%1/3
A // B
A= $res->{"port"}B= $port
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | return ( $ip, $resolved_port ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | return (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 44 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||