lib/Net/BitTorrent/SSRF.pm

filter: Statementstmt 100% 56 / 56 Branchbran 91.3% 42 / 46 Conditioncond 60.0% 27 / 45 MC/DCmcdc 32.0% 8 / 25 Subroutinesub 100% 7 / 7 Podpod 100% 4 / 4 totaltotal 78.6% 144 / 183 CC 42 SCAR 28.5
CC
42
Cov
85%
is_safe_ip
24.5
is_safe_host
13.3
resolve_and_pin
11.0
CRAP 17.4
« lib/Net/BitTorrent/Tracker/WebSeed.pm ↑ Summary lib/Net/BitTorrent/Protocol/BEP52.pm »
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
BranchTrueFalseTotal
if ($packed) 1837 455 2292
11 1837 my @o = unpack( 'C4', $packed );
12 1837 ▶ return 0 if $o[0] == 127; # 127.0.0.0/8
Branch: 1 branch
BranchTrueFalseTotal
if $o[0] == 127 61 1776 1837
13 1776 ▶ return 0 if $o[0] == 10; # 10.0.0.0/8
Branch: 1 branch
BranchTrueFalseTotal
if $o[0] == 10 119 1657 1776
14 1657 ▶ return 0 if $o[0] == 172 && $o[1] >= 16 && $o[1] <= 31; # 172.16.0.0/12
Branch: 1 branch
BranchTrueFalseTotal
if $o[0] == 172 and $o[1] >= 16 and $o[1] <= 31 2 1655 1657
Condition100%6/6
$o[0] == 172 and $o[1] >= 16 and $o[1] <= 31
!ll&&!rl&&r
1643122
$o[0] == 172 and $o[1] >= 16
!ll&&!rl&&r
1642114
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
ABCresult
0XX0
10X0
1100
1111
15 1655 ▶ return 0 if $o[0] == 192 && $o[1] == 168; # 192.168.0.0/16
Branch: 1 branch
BranchTrueFalseTotal
if $o[0] == 192 and $o[1] == 168 11 1644 1655
Condition66%2/3
$o[0] == 192 and $o[1] == 168
!ll&&!rl&&r
1644011
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
ABresult
0X0
100
111
16 1644 ▶ return 0 if $o[0] == 169 && $o[1] == 254; # 169.254.0.0/16
Branch: 1 branch
BranchTrueFalseTotal
if $o[0] == 169 and $o[1] == 254 9 1635 1644
Condition66%2/3
$o[0] == 169 and $o[1] == 254
!ll&&!rl&&r
163509
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
ABresult
0X0
100
111
17 1635 ▶ return 0 if ( $o[0] & 0xF0 ) == 224; # 224.0.0.0/4 multicast
Branch: 1 branch
BranchTrueFalseTotal
if ($o[0] & 240) == 224 2 1633 1635
18 1633 ▶ return 0 if $o[0] == 0; # 0.0.0.0
Branch: 1 branch
BranchTrueFalseTotal
if $o[0] == 0 1 1632 1633
19 1632 return 1;
20 }
21 455 $packed = inet_pton( AF_INET6, $ip );
22 455 ▶ if ($packed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packed) 195 260 455
23 195 my @w = unpack( 'n8', $packed );
24 195 ▶ return 0 if $packed eq ( "\0" x 15 ) . "\1"; # ::1
Branch: 1 branch
BranchTrueFalseTotal
if $packed eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1" 3 192 195
25 192 ▶ return 0 if $packed eq "\0" x 16; # ::
Branch: 1 branch
BranchTrueFalseTotal
if $packed eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" 0 192 192
26 192 ▶ return 0 if ( $w[0] & 0xFFC0 ) == 0xFE80; # fe80::/10 link-local
Branch: 1 branch
BranchTrueFalseTotal
if ($w[0] & 65472) == 65152 3 189 192
27 189 ▶ return 0 if ( $w[0] & 0xFE00 ) == 0xFC00; # fc00::/7 ULA
Branch: 1 branch
BranchTrueFalseTotal
if ($w[0] & 65024) == 64512 2 187 189
28 187 ▶ return 0 if ( $w[0] & 0xFF00 ) == 0xFF00; # ff00::/8 multicast
Branch: 1 branch
BranchTrueFalseTotal
if ($w[0] & 65280) == 65280 1 186 187
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
BranchTrueFalseTotal
if $w[0] == 0 and $w[1] == 0 and $w[2] == 0 and $w[3] == 0 and $w[5] == 65535 5 181 186
Condition66%8/12
$w[0] == 0 and $w[1] == 0 and $w[2] == 0 and $w[3] == 0 and $w[5] == 65535
!ll&&!rl&&r
18105
$w[0] == 0 and $w[1] == 0 and $w[2] == 0 and $w[3] == 0
!ll&&!rl&&r
18105
$w[0] == 0 and $w[1] == 0 and $w[2] == 0
!ll&&!rl&&r
18105
$w[0] == 0 and $w[1] == 0
!ll&&!rl&&r
18105
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
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
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
BranchTrueFalseTotal
unless defined $host and length $host 0 320 320
Condition33%1/3
defined $host and length $host
!ll&&!rl&&r
00320
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
ABresult
0X0
100
111
37 320 ▶ return is_safe_ip($host) if is_safe_ip($host);
Branch: 1 branch
BranchTrueFalseTotal
if is_safe_ip($host) 51 269 320
38 269 my ( $err, @results ) = getaddrinfo( $host, undef, { family => AF_UNSPEC, socktype => SOCK_STREAM } );
39 269 ▶ return 0 if $err || !@results;
Branch: 1 branch
BranchTrueFalseTotal
if $err or not @results 0 269 269
Condition33%1/3
$err or not @results
l!l&&r!l&&!r
00269
MC/DC0%0/2
$err or not @results
$err not @results
Truth table33%1/3
A or not(B)
A= $errB= not @results
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $gerr or not defined $ip or not is_safe_ip($ip) 15 442 457
Condition50%3/6
$gerr or not defined $ip or not is_safe_ip($ip)
l!l&&r!l&&!r
015442
$gerr or not defined $ip
l!l&&r!l&&!r
00457
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)
ABCresult
1XX1
01X1
0011
0000
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
BranchTrueFalseTotal
if is_safe_ip($host) 1 10 11
56 10 my ( $err, @results ) = getaddrinfo( $host, $port, { family => AF_UNSPEC, socktype => SOCK_STREAM } );
57 10 ▶ return () if $err || !@results;
Branch: 1 branch
BranchTrueFalseTotal
if $err or not @results 2 8 10
Condition66%2/3
$err or not @results
l!l&&r!l&&!r
208
MC/DC50%1/2
$err or not @results
$err not @results
Truth table66%2/3
A or not(B)
A= $errB= not @results
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $gerr or not defined $ip 0 8 8
Condition33%1/3
$gerr or not defined $ip
l!l&&r!l&&!r
008
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
ABresult
1X1
011
000
61 8 ▶ next unless is_safe_ip($ip);
Branch: 1 branch
BranchTrueFalseTotal
unless is_safe_ip($ip) 7 1 8
62 1 ▶ my $resolved_port = $res->{port} // $port;
Condition33%1/3
$res->{"port"} // $port
l!l&&r!l&&!r
010
MC/DC0%0/2
$res->{"port"} // $port
$res->{"port"} $port
Truth table33%1/3
A // B
A= $res->{"port"}B= $port
ABresult
1X1
011
000
63 1 return ( $ip, $resolved_port );
64 }
65 7 return ();
66 }
67 };
68 44 1;
« lib/Net/BitTorrent/Tracker/WebSeed.pm ↑ Summary lib/Net/BitTorrent/Protocol/BEP52.pm »