| 1 | package Langertha::HTTP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: LWP::UserAgent that keeps credentials on their origin across redirects | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 11 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use MooseX::NonMoose; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 11 | extends 'LWP::UserAgent'; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Langertha::HTTP::Redirect; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 11 | has connect_host => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 11 | has connect_address => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 106 | ▶ | sub FOREIGNBUILDARGS { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FOREIGNBUILDARGS: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 106 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 106 | delete @args{qw( connect_host connect_address )}; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 106 | return %args; | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 106 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 106 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 106 | my ( $host, $address ) = ( $self->connect_host, $self->connect_address ); | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 106 | ▶ | return unless defined $host || defined $address; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $host or defined $address
MC/DC100%2/2
defined $host or defined $address
defined $host
defined $address
Truth table100%3/3
A or B
A= defined $hostB= defined $address
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 39 | ▶ | croak __PACKAGE__ . ": connect_host and connect_address go together" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $host and length $host and defined $address
defined $host and length $host
MC/DC33%1/3
defined $host and length $host and defined $address
defined $host
length $host
defined $address
Truth table50%2/4
A and B and C
A= defined $hostB= length $hostC= defined $address
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | unless defined $host && length $host && defined $address; | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 38 | my $error = connect_address_error($address); | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 38 | ▶ | croak __PACKAGE__ . ": $error" if $error; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 37 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # undef when $address is an IPv4 or IPv6 literal the pin can connect to, else | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # why not. Shared with Langertha::Role::HTTP's connect_address check. | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 110 | ▶ | sub connect_address_error { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connect_address_error: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 110 | my ($address) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | 110 | ▶ | return 'connect_address must be an IP address literal, not undef' unless defined $address; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 110 | require Socket; | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | 110 | ▶ | return undef if $address =~ /\A[0-9.]+\z/ && Socket::inet_pton( Socket::AF_INET(), $address ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$address =~ /\A[0-9.]+\z/ and Socket::inet_pton(2, $address)
MC/DC100%2/2
$address =~ /\A[0-9.]+\z/ and Socket::inet_pton(2, $address)
$address =~ /\A[0-9.]+\z/
Socket::inet_pton(2, $address)
Truth table100%3/3
A and B
A= $address =~ /\A[0-9.]+\z/B= Socket::inet_pton(2, $address)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 12 | ▶ | return undef if $address =~ /:/ && $address !~ /[\[\]%]/ && Socket::inet_pton( Socket::AF_INET6(), $address ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$address =~ /:/ and not $address =~ /[\[\]%]/ and Socket::inet_pton(10, $address)
$address =~ /:/ and not $address =~ /[\[\]%]/
MC/DC100%3/3
$address =~ /:/ and not $address =~ /[\[\]%]/ and Socket::inet_pton(10, $address)
$address =~ /:/
not $address =~ /[\[\]%]/
Socket::inet_pton(10, $address)
Truth table100%4/4
A and not(B) and C
A= $address =~ /:/B= not $address =~ /[\[\]%]/C= Socket::inet_pton(10, $address)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 9 | return "connect_address must be an IPv4 or IPv6 address literal (no brackets, port or scope), not '$address'"; | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 51 | ▶ | sub redirect_ok { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
redirect_ok: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 51 | my ( $self, $referral, $response ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | # The policy first, so its refusal (a POST, even one requests_redirectable | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # allows) names its reason on the response; then LWP's own checks. | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 51 | ▶ | return 0 unless Langertha::HTTP::Redirect::guard_referral( $referral, $response, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | defined $self->connect_address ? $self->connect_host : undef ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 35 | ▶ | return $self->SUPER::redirect_ok( $referral, $response ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # Where the pin is applied: LWP calls this once per hop (request() follows | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # redirects by calling simple_request -> send_request again), so each hop to | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # the pinned host is pinned and a hop to another host is not. | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 139 | ▶ | sub send_request { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_request: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 139 | my ( $self, $request, @rest ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | 139 | my $address = $self->connect_address; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | 139 | my $uri = $request->uri; | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 139 | ▶ | return $self->SUPER::send_request( $request, @rest ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%6/8
defined $address and $uri->can("host") and lc($uri->host // "") eq lc $self->connect_host
defined $address and $uri->can("host")
$uri->host // ""
MC/DC33%1/3
defined $address and $uri->can("host") and lc($uri->host // "") eq lc $self->connect_host
defined $address
$uri->can("host")
$uri->host
Truth table50%2/4
A and B and C
A= defined $addressB= $uri->can("host")C= $uri->host
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | unless defined $address && $uri->can('host') | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | && lc( $uri->host // '' ) eq lc $self->connect_host; | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # The proxy would resolve the name itself (prepare_request has put it on | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # the request by now); sending the request there would silently unpin it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | return _pin_failure( $request, 'connect_address ' . $address | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | . ' cannot be used for a request that goes through a proxy' ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 41 | ▶ | if $request->{proxy}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # LWP::Protocol::http::_new_socket passes $self->_extra_sock_opts($host, | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # $port) after its own PeerAddr (for https after the agent's ssl_opts), and | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # calls $self->_check_sock($request, $socket) before it writes the request: | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # the subclass override points LWP documents. They are wrapped for the | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # duration of this request only, and add something only when the protocol | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # object works for this agent and the host is the pinned one, so another | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # agent's request (from a content callback, say) is never touched. https | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # has its own _extra_sock_opts (calling SUPER without the host), so both | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # are wrapped. Checked against LWP 6.83 / LWP::Protocol::https 6.17; the | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # Client-Peer check below is the backstop if either hook stops being called. | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # LWP::Protocol::https is a hard dependency (ADR 0027). | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 40 | require LWP::Protocol::http; | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 40 | require LWP::Protocol::https; | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 40 | my $http_opts = \&LWP::Protocol::http::_extra_sock_opts; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 40 | my $https_opts = \&LWP::Protocol::https::_extra_sock_opts; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 40 | my $check = \&LWP::Protocol::http::_check_sock; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 11 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 35 | ▶ | local *LWP::Protocol::http::_extra_sock_opts = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 35 | return ( $http_opts->(@_), $self->_pin_sock_opts( 'http', @_ ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 40 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | 9 | ▶ | local *LWP::Protocol::https::_extra_sock_opts = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 9 | return ( $https_opts->(@_), $self->_pin_sock_opts( 'https', @_ ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 40 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | 40 | my $checked; | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | 31 | ▶ | local *LWP::Protocol::http::_check_sock = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 31 | $check->(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | 31 | ▶ | $checked = 1 if $self->_pin_check_sock(@_); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 40 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 40 | my $response = $self->SUPER::send_request( $request, @rest ); | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 40 | ▶ | return _pin_failure( $request, "connect_address $address: no response" ) unless $response; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 40 | my $peer = $response->header('Client-Peer'); | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | 40 | ▶ | if ( defined $peer ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 29 | $peer =~ s/:\d+\z//; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 29 | $peer =~ s/\A\[(.*)\]\z/$1/; | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | 29 | ▶ | return _pin_failure( $request, "connect_address $address was not used: LWP connected to $peer" ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | unless same_address( $peer, $address ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # A response that did not come over a checked connection (a later LWP no | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # longer calling _check_sock, a request_send handler answering in place of | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # the network) is refused too; LWP's own internal error responses (connect | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # failed, ...) never had a connection and pass as they are. | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 39 | ▶ | return _pin_failure( $request, "connect_address $address: the connection was not checked before sending" ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
$checked or ($response->header("Client-Warning") // "") eq "Internal response"
$response->header("Client-Warning") // ""
MC/DC100%2/2
$checked or ($response->header("Client-Warning") // "") eq "Internal response"
$checked
$response->header("Client-Warning")
Truth table100%3/3
A or B
A= $checkedB= $response->header("Client-Warning")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | unless $checked || ( $response->header('Client-Warning') // '' ) eq 'Internal response'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 33 | return $response; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # True when $protocol (an LWP::Protocol object) works for this agent and | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # $host (possibly [bracketed]) is the pinned host. | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 75 | ▶ | sub _pins { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pins: called CC=6 SCAR=18.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 75 | my ( $self, $protocol, $host ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | return 0 unless ref $protocol && ref $protocol->{ua} | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 75 | ▶ | && Scalar::Util::refaddr( $protocol->{ua} ) == Scalar::Util::refaddr($self); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
ref $protocol and ref $$protocol{"ua"} and builtin::refaddr($$protocol{"ua"}) == builtin::refaddr($self)
ref $protocol and ref $$protocol{"ua"}
MC/DC33%1/3
ref $protocol and ref $$protocol{"ua"} and builtin::refaddr($$protocol{"ua"}) == builtin::refaddr($self)
ref $protocol
ref $$protocol{"ua"}
builtin::refaddr($$protocol{"ua"}) == builtin::refaddr($self)
Truth table50%2/4
A and B and C
A= ref $protocolB= ref $$protocol{"ua"}C= builtin::refaddr($$protocol{"ua"}) == builtin::refaddr($self)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 72 | ▶ | return 0 unless defined $host; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 64 | $host =~ s/\A\[(.*)\]\z/$1/; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | 64 | ▶ | return lc $host eq lc $self->connect_host ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # The socket options that connect to the pinned address (the wrapped | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # _extra_sock_opts). https calls the http one through SUPER without the host, | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # which adds nothing; its own wrapper adds the TLS names. | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 44 | ▶ | sub _pin_sock_opts { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pin_sock_opts: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 44 | my ( $self, $scheme, $protocol, $host ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 44 | ▶ | return () unless $self->_pins( $protocol, $host ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 33 | $host =~ s/\A\[(.*)\]\z/$1/; | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | 33 | my $address = $self->connect_address; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # Net::HTTP reads PeerAddr (bracketed for IPv6, it parses host:port); | ||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # IO::Socket::IP prefers PeerHost when both are there. | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 33 | ▶ | PeerAddr => ( $address =~ /:/ ? "[$address]" : $address ), | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | PeerHost => $address, | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # Net::HTTPS would derive SNI and the certificate name from the peer, | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # which is now the address: name them after the host. No SNI for an | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | # address literal (RFC 6066), as LWP::Protocol::https does. | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | $scheme eq 'https' | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | ? ( SSL_hostname => ( _is_address($host) ? undef : $host ), SSL_verifycn_name => $host ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | : (), | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # Before the request is written (the wrapped _check_sock): the socket, new or | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # from a conn_cache, must be connected to the pinned address and, over https | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # with hostname verification on, hold a certificate for the host. A die here | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # becomes LWP's 500 response and nothing is sent. | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 31 | ▶ | sub _pin_check_sock { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pin_check_sock: called CC=10 SCAR=23.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 31 | my ( $self, $protocol, $request, $socket ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 31 | my $uri = $request->uri; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | 31 | ▶ | return unless $uri->can('host') && $self->_pins( $protocol, $uri->host ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$uri->can("host") and $self->_pins($protocol, $uri->host)
MC/DC0%0/2
$uri->can("host") and $self->_pins($protocol, $uri->host)
$uri->can("host")
$self->_pins($protocol, $uri->host)
Truth table33%1/3
A and B
A= $uri->can("host")B= $self->_pins($protocol, $uri->host)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 31 | my $address = $self->connect_address; | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | 31 | my $peer = eval { $socket->peerhost }; | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | 31 | ▶ | die "connect_address $address was not used: the connection goes to " . ( $peer // 'an unknown peer' ) . "\n" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
defined $peer and same_address($peer, $address)
$peer // "an unknown peer"
MC/DC50%1/2
defined $peer and same_address($peer, $address)
defined $peer
same_address($peer, $address)
MC/DC0%0/1
$peer // "an unknown peer"
$peer
Truth table66%2/3
A and B
A= defined $peerB= same_address($peer, $address)
Truth table50%1/2
A
A= $peer
| |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | unless defined $peer && same_address( $peer, $address ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # LWP verifies chain and name exactly when verify_hostname is on (it then | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # sets SSL_verify_mode), so the same condition decides both checks here. | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 30 | ▶ | if ( lc( $uri->scheme // '' ) eq 'https' && $self->ssl_opts('verify_hostname') ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
lc($uri->scheme // "") eq "https" and $self->ssl_opts("verify_hostname")
$uri->scheme // ""
MC/DC50%1/2
lc($uri->scheme // "") eq "https" and $self->ssl_opts("verify_hostname")
$uri->scheme
$self->ssl_opts("verify_hostname")
Truth table66%2/3
A and B
A= $uri->schemeB= $self->ssl_opts("verify_hostname")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 4 | my $error = tls_identity_error( $socket, $uri->host, chain => 1, name => 1 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | 4 | ▶ | die "connect_address $address: $error\n" if $error; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 28 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | # undef when the TLS connection on $handle (an IO::Socket::SSL) passed the | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # requested checks for $host, else why not. Shared with Role::AsyncHTTP. | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 12 | ▶ | sub tls_identity_error { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tls_identity_error: called CC=10 SCAR=25.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 12 | my ( $handle, $host, %check ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 187 | 12 | ▶ | if ( $check{chain} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # _get_ssl_object is IO::Socket::SSL's documented door to the OpenSSL | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # handle ("ACCESS TO INTERNALS"). The verify result is recorded even when | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # the connection was made with SSL_verify_mode => 0, so a session nobody | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # verified does not pass for one that was. | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 12 | ▶ | my $ssl = $handle && $handle->can('_get_ssl_object') ? $handle->_get_ssl_object : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$handle and $handle->can("_get_ssl_object")
MC/DC0%0/2
$handle and $handle->can("_get_ssl_object")
$handle
$handle->can("_get_ssl_object")
Truth table33%1/3
A and B
A= $handleB= $handle->can("_get_ssl_object")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 12 | ▶ | return "the connection is not a TLS session" unless $ssl; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 12 | require Net::SSLeay; | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | 12 | my $result = Net::SSLeay::get_verify_result($ssl); | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | 12 | ▶ | return "the connection's certificate chain is not verified (" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | . Net::SSLeay::X509_verify_cert_error_string($result) . ')' | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | unless $result == Net::SSLeay::X509_V_OK(); | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 10 | ▶ | if ( $check{name} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 10 | ▶ | return "the connection's certificate is not for $host" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$handle and $handle->can("verify_hostname") and $handle->verify_hostname($host, "http")
$handle and $handle->can("verify_hostname")
MC/DC33%1/3
$handle and $handle->can("verify_hostname") and $handle->verify_hostname($host, "http")
$handle
$handle->can("verify_hostname")
$handle->verify_hostname($host, "http")
Truth table50%2/4
A and B and C
A= $handleB= $handle->can("verify_hostname")C= $handle->verify_hostname($host, "http")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | unless $handle && $handle->can('verify_hostname') && $handle->verify_hostname( $host, 'http' ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 7 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 8 | ▶ | sub _is_address { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_address: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 8 | my ($host) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 211 | 8 | require Socket; | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 8 | ▶ | return ( Socket::inet_pton( Socket::AF_INET(), $host ) || Socket::inet_pton( Socket::AF_INET6(), $host ) ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
Socket::inet_pton(2, $host) or Socket::inet_pton(10, $host)
MC/DC50%1/2
Socket::inet_pton(2, $host) or Socket::inet_pton(10, $host)
Socket::inet_pton(2, $host)
Socket::inet_pton(10, $host)
Truth table66%2/3
A or B
A= Socket::inet_pton(2, $host)B= Socket::inet_pton(10, $host)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 105 | ▶ | sub same_address { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
same_address: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 105 | my ( $left, $right ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 217 | 105 | require Socket; | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | 105 | for my $family ( Socket::AF_INET(), Socket::AF_INET6() ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | 109 | my ( $l, $r ) = map { Socket::inet_pton( $family, $_ ) } $left, $right; | |||||||||||||||||||||||||||||||||||||||||||||
| 220 | 109 | ▶ | return 1 if defined $l && defined $r && $l eq $r; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $l and defined $r and $l eq $r
defined $l and defined $r
MC/DC100%3/3
defined $l and defined $r and $l eq $r
defined $l
defined $r
$l eq $r
Truth table100%4/4
A and B and C
A= defined $lB= defined $rC= $l eq $r
| |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # An IPv4 address reported IPv4-mapped (::ffff:a.b.c.d) by an IPv6 socket. | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 4 | my ($mapped) = $left =~ /\A::ffff:([0-9.]+)\z/i; | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | 4 | ▶ | return defined $mapped && $mapped eq $right ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $mapped and $mapped eq $right
MC/DC50%1/2
defined $mapped and $mapped eq $right
defined $mapped
$mapped eq $right
Truth table66%2/3
A and B
A= defined $mappedB= $mapped eq $right
| |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # The error response LWP itself gives for a request it could not send. | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 8 | ▶ | sub _pin_failure { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pin_failure: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 8 | my ( $request, $message ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | 8 | require HTTP::Response; | |||||||||||||||||||||||||||||||||||||||||||||
| 232 | 8 | my $response = HTTP::Response->new( 500, $message, [ 'Client-Warning' => 'Internal response', | |||||||||||||||||||||||||||||||||||||||||||||
| 233 | 'Content-Type' => 'text/plain' ], "$message\n" ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 8 | $response->request($request); | |||||||||||||||||||||||||||||||||||||||||||||
| 235 | 8 | return $response; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 11 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||