| 1 | package YATT::Lite::WebMVC0::Partial::StrictIPMatch; | ||||||||||||||||||||||||||||
| 2 | sub MY () {__PACKAGE__} | ||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Socket qw(inet_pton AF_INET AF_INET6); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use YATT::Lite::Util qw(lexpand); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # Strict address matching for SiteApp's allow_debug_from and | ||||||||||||||||||||||||||||
| 13 | # trusted_proxies. The default compile_addr_matcher in SiteApp compares | ||||||||||||||||||||||||||||
| 14 | # addresses as literal strings (cheap, no parsing). This Partial parses | ||||||||||||||||||||||||||||
| 15 | # them instead, so entries can be CIDR ranges (10.0.0.0/8, | ||||||||||||||||||||||||||||
| 16 | # 2001:db8::/32), IPv6 spellings are normalized (::1 eq | ||||||||||||||||||||||||||||
| 17 | # 0:0:0:0:0:0:0:1) and IPv4-mapped IPv6 addresses (::ffff:10.1.2.3) | ||||||||||||||||||||||||||||
| 18 | # match their IPv4 entries. Unparsable entries croak at configure time. | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # use YATT::Lite::WebMVC0::SiteApp -as_base; | ||||||||||||||||||||||||||||
| 21 | # use YATT::Lite::WebMVC0::Partial::StrictIPMatch -as_base; | ||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||
| 23 | use YATT::Lite::Partial | ||||||||||||||||||||||||||||
| 24 | 2 | ▶ | (requires => [qw/compile_addr_matcher/]); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 12 | ▶ | sub compile_addr_matcher { | ||||||||||||||||||||||||||
|
Subroutine
compile_addr_matcher: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 12 | (my MY $self, my ($name, $list)) = @_; | |||||||||||||||||||||||||||
| 28 | 12 | my @ranges; | |||||||||||||||||||||||||||
| 29 | 12 | ▶ | foreach my $item (grep {defined $_ and $_ ne ''} lexpand($list)) { | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $_ && $_ ne ''
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_
$_ ne ''
Truth table33%1/3
A && B
A= defined $_B= $_ ne ''
| |||||||||||||||||||||||||||||
| 30 | 16 | ▶ | my ($addr, $bits) = $item =~ m{^([^/]+)(?:/(\d{1,3}))?\z} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | or croak "Invalid address or CIDR in $name: $item"; | ||||||||||||||||||||||||||||
| 32 | 15 | ▶ | my ($packed, $family) = $self->parse_ip_address($addr) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | or croak "Invalid address or CIDR in $name: $item"; | ||||||||||||||||||||||||||||
| 34 | 11 | ▶ | my $max = $family == AF_INET ? 32 : 128; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 11 | ▶ | $bits //= $max; | ||||||||||||||||||||||||||
|
Condition66%2/3
$bits //= $max
MC/DC0%0/2
$bits //= $max
$bits
$max
Truth table66%2/3
A //= B
A= $bitsB= $max
| |||||||||||||||||||||||||||||
| 36 | 11 | ▶ | croak "Invalid address or CIDR in $name: $item" if $bits > $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 9 | push @ranges, [$family, $bits, $self->_ip_prefix($packed, $bits)]; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return sub { 0 } unless @ranges; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 32 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 41 | 32 | ▶ | my ($packed, $family) = $self->parse_ip_address($_[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | or return 0; | ||||||||||||||||||||||||||||
| 43 | 29 | foreach my $range (@ranges) { | |||||||||||||||||||||||||||
| 44 | 56 | my ($fam, $bits, $prefix) = @$range; | |||||||||||||||||||||||||||
| 45 | 56 | ▶ | next unless $fam == $family; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 35 | ▶ | return 1 if $self->_ip_prefix($packed, $bits) eq $prefix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 12 | 0; | |||||||||||||||||||||||||||
| 49 | 5 | }; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||
| 53 | # ($packed, $family) for an address string, or () when it is not one. | ||||||||||||||||||||||||||||
| 54 | # IPv4-mapped IPv6 addresses are returned as IPv4. | ||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||
| 56 | 47 | ▶ | sub parse_ip_address { | ||||||||||||||||||||||||||
|
Subroutine
parse_ip_address: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 47 | (my MY $self, my $str) = @_; | |||||||||||||||||||||||||||
| 58 | 47 | ▶ | return unless defined $str and length $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $str and length $str
MC/DC50%1/2
defined $str and length $str
defined $str
length $str
Truth table66%2/3
A and B
A= defined $strB= length $str
| |||||||||||||||||||||||||||||
| 59 | 46 | ▶ | if ($str =~ /:/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 15 | ▶ | defined(my $packed = inet_pton(AF_INET6, $str)) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 15 | ▶ | if (substr($packed, 0, 12) eq "\0" x 10 . "\xff\xff") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 2 | return (substr($packed, 12), AF_INET); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 13 | return ($packed, AF_INET6); | |||||||||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||||||||||
| 66 | # inet_pton(AF_INET) alone accepts short forms on some platforms | ||||||||||||||||||||||||||||
| 67 | # (1.2.3 => 1.2.0.3); insist on dotted quad. | ||||||||||||||||||||||||||||
| 68 | 31 | ▶ | return unless $str =~ /^(?:\d{1,3}\.){3}\d{1,3}\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 25 | ▶ | defined(my $packed = inet_pton(AF_INET, $str)) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 25 | return ($packed, AF_INET); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 44 | ▶ | sub _ip_prefix { | ||||||||||||||||||||||||||
|
Subroutine
_ip_prefix: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 75 | 44 | (my MY $self, my ($packed, $bits)) = @_; | |||||||||||||||||||||||||||
| 76 | 44 | my $full = int($bits / 8); | |||||||||||||||||||||||||||
| 77 | 44 | my $rest = $bits % 8; | |||||||||||||||||||||||||||
| 78 | 44 | my $prefix = substr($packed, 0, $full); | |||||||||||||||||||||||||||
| 79 | 44 | ▶ | if ($rest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 6 | $prefix .= chr(ord(substr($packed, $full, 1)) & (0xff << (8 - $rest) & 0xff)); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 44 | $prefix; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | 1; | |||||||||||||||||||||||||||