S3/Roles/Request.pm

filter: Statementstmt 100% 127 / 127 Branchbran 92.5% 50 / 54 Conditioncond 86.3% 19 / 22 MC/DCmcdc 78.5% 11 / 14 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 1 totaltotal 95.4% 233 / 244 CC 36 SCAR 20.1
CC
36
Cov
97%
_is_trusted_redirect
18.2
_send_request
6.0
__ANON__
6.0
CRAP 7.5
« S3/Bucket.pm ↑ Summary S3/FileIterator.pm »
1 package AWS::S3::Roles::Request;
2 9 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use AWS::S3::ResponseParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use MooseX::Types::URI qw(Uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use URI::Escape qw/ uri_escape /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use AWS::S3::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use AWS::S3::Signer::V4;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Log::Any qw( $LOG );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 has 's3' => (
17 is => 'ro',
18 isa => 'AWS::S3',
19 required => 1,
20 );
21  
22 9 has 'type' => (
23 is => 'ro',
24 isa => 'Str',
25 required => 1,
26 );
27  
28 has 'protocol' => (
29 is => 'ro',
30 isa => 'Str',
31 lazy => 1,
32 89 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
33 89 ▶ shift->s3->secure ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
(shift())->s3->secure ? : 68 21 89
34 }
35 9 );
36  
37 has 'endpoint' => (
38 is => 'ro',
39 isa => 'Str',
40 lazy => 1,
41 118 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 118 shift->s3->endpoint;
43 }
44 9 );
45  
46 # XXX should be required=>1; https://rt.cpan.org/Ticket/Display.html?id=77863
47 9 has "_action" => (
48 isa => 'Str',
49 is => 'ro',
50 init_arg => undef,
51 #required => 1
52 );
53  
54 9 has '_expect_nothing' => ( isa => 'Bool', is => 'ro', init_arg => undef );
55  
56 has '_uri' => (
57 isa => Uri,
58 is => 'ro',
59 lazy => 1,
60 45 ▶ default => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
61 45 my $self = shift;
62 45 my $m = $self->meta;
63  
64 45 ▶ my $uri = URI->new(
Branch: 1 branch
BranchTrueFalseTotal
$m->has_attribute('bucket') ? : 33 12 45
65 $self->protocol . '://'
66 . ( $m->has_attribute('bucket') ? $self->bucket . '.' : '' )
67 . $self->endpoint
68 . '/'
69 );
70  
71 # note we add some extra exceptions to uri_escape to prevent
72 # encoding of things like "/", ":", "="
73 45 ▶ if ( $m->has_attribute('key') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->has_attribute("key")) 15 30 45
74 15 my $escaped_path = $self->_escape_key( $self->key );
75  
76 # if we have a leading slash in the key name we need to add *another*
77 # slash in the call to ->path to ensure it is retained
78 15 ▶ if ( $escaped_path =~ m!^/! && $self->s3->honor_leading_slashes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escaped_path =~ m[^/] and $self->s3->honor_leading_slashes) { } 3 12 15
Condition100%3/3
$escaped_path =~ m[^/] and $self->s3->honor_leading_slashes
!ll&&!rl&&r
1023
MC/DC100%2/2
$escaped_path =~ m[^/] and $self->s3->honor_leading_slashes
$escaped_path =~ m[^/] $self->s3->honor_leading_slashes
Truth table100%3/3
A and B
A= $escaped_path =~ m[^/]B= $self->s3->honor_leading_slashes
ABresult
0X0
100
111
79 3 $uri->path( '/'.$escaped_path )
80 } else {
81 12 $uri->path( $escaped_path )
82 }
83 }
84  
85 45 ▶ $uri->query_keywords( $self->_subresource )
Branch: 1 branch
BranchTrueFalseTotal
if $m->has_attribute("_subresource") 14 31 45
86 if $m->has_attribute('_subresource');
87  
88 45 $uri;
89 }
90 9 );
91  
92 has 'signerv4' => (
93 is => 'ro',
94 isa => 'AWS::S3::Signer::V4',
95 lazy => 1,
96 98 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
97 98 my $s = shift;
98 98 AWS::S3::Signer::V4->new(
99 -access_key => $s->s3->access_key_id,
100 -secret_key => $s->s3->secret_access_key,
101 );
102 }
103 9 );
104  
105 # uri_escape() croaks on characters above 0xFF, so encode those as UTF-8
106 # first, which is what URI does too. Byte strings are escaped as they were.
107 49 ▶sub _uri_escape {
Subroutine _uri_escape: called CC=4 SCAR=13.9
108 49 my ( $s, $str, $unsafe ) = @_;
109  
110 49 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 1 48 49
111 48 ▶ utf8::encode( $str ) if $str =~ /[^\x00-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /[^\x00-\xFF]/ 4 44 48
112  
113 48 ▶ return defined $unsafe ? uri_escape( $str, $unsafe ) : uri_escape( $str );
Branch: 1 branch
BranchTrueFalseTotal
defined $unsafe ? : 37 11 48
114 }
115  
116 # Escape an object key for use as the path of a URI; note we add some extra
117 # exceptions to uri_escape to prevent encoding of things like "/", ":", "="
118 36 ▶sub _escape_key {
Subroutine _escape_key: called CC=1 SCAR=0.0
119 36 my ( $s, $key ) = @_;
120  
121 36 return $s->_uri_escape( $key, "^A-Za-z0-9\-\._~\/:=" );
122 }
123  
124 94 ▶sub _send_request {
Subroutine _send_request: called CC=6 SCAR=17.9
125 94 my ( $s, $method, $uri, $headers, $content ) = @_;
126 94 $LOG->debug('Making AWS request', {method => $method, uri => "$uri"});
127  
128 94 my $req = HTTP::Request->new( $method => $uri );
129 94 ▶ $req->content( $content ) if $content;
Branch: 1 branch
BranchTrueFalseTotal
if $content 31 63 94
130  
131 94 delete($headers->{Authorization}); # we will use a v4 signature
132 94 map { $req->header( $_ => $headers->{$_} ) } keys %$headers;
133  
134 94 $s->_sign($req);
135 94 my $res = $s->s3->ua->request( $req );
136  
137 # After creating a bucket and setting its location constraint, we get this
138 # strange 'TemporaryRedirect' response. Deal with it.
139 94 ▶ if ( $res->header( 'location' ) && $res->content =~ m{>TemporaryRedirect<}s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->header("location") and $res->content =~ />TemporaryRedirect</s) 12 82 94
Condition100%3/3
$res->header("location") and $res->content =~ />TemporaryRedirect</s
!ll&&!rl&&r
661612
MC/DC100%2/2
$res->header("location") and $res->content =~ />TemporaryRedirect</s
$res->header("location") $res->content =~ />TemporaryRedirect</s
Truth table100%3/3
A and B
A= $res->header("location")B= $res->content =~ />TemporaryRedirect</s
ABresult
0X0
100
111
140 12 my $location = eval { URI->new_abs( $res->header( 'location' ), $req->uri )->as_string };
141  
142 # never replay a signed request (which includes the session token and
143 # the body) to somewhere we have no reason to trust, the response may
144 # have been tampered with, particularly if we are not using https
145 12 ▶ unless ( defined $location && $s->_is_trusted_redirect( $location ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $location and $s->_is_trusted_redirect($location)) 6 6 12
Condition66%2/3
defined $location and $s->_is_trusted_redirect($location)
!ll&&!rl&&r
066
MC/DC50%1/2
defined $location and $s->_is_trusted_redirect($location)
defined $location $s->_is_trusted_redirect($location)
Truth table66%2/3
A and B
A= defined $locationB= $s->_is_trusted_redirect($location)
ABresult
0X0
100
111
146 6 ( my $safe = $res->header( 'location' ) ) =~ s/[^\x21-\x7e]/?/g;
147 6 $LOG->error( 'Refusing to follow redirect to untrusted location', { location => $safe } );
148 6 die "Refusing to follow redirect to untrusted location '$safe'";
149 }
150  
151 # the signature covers the host so the request must be signed again,
152 # and the region may differ, a bucket in another region redirects us to
153 # the regional endpoint, so take it from the host where we can
154 6 $req->uri( $location );
155 6 $req->remove_header( qw( Authorization Host X-Amz-Date ) );
156 6 my ( undef, $region ) = $s->signerv4->parse_host( $req->uri->host );
157 6 $s->_sign( $req, $region );
158 6 $res = $s->s3->ua->request( $req );
159 }
160 88 return $s->parse_response( $res );
161 }
162  
163 # A redirect is only trusted if it stays within the configured endpoint (or
164 # is an S3 hostname when that is an AWS endpoint), keeps to the same port, and
165 # doesn't downgrade from https.
166 47 ▶sub _is_trusted_redirect {
Subroutine _is_trusted_redirect: called CC=18 SCAR=29.0
167 47 my ( $s, $location ) = @_;
168  
169 47 ▶ my $uri = eval { URI->new( $location ) } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $uri = eval { do { "URI"->new($location) } } or return 0 46 1 47
170  
171 46 ▶ my $scheme = $uri->scheme // '';
Condition50%1/2
$uri->scheme // ""
l!l
460
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
Aresult
11
00
172 46 ▶ return 0 unless $scheme eq 'http' || $scheme eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme eq "http" or $scheme eq "https" 2 44 46
Condition100%3/3
$scheme eq "http" or $scheme eq "https"
l!l&&r!l&&!r
6382
MC/DC100%2/2
$scheme eq "http" or $scheme eq "https"
$scheme eq "http" $scheme eq "https"
Truth table100%3/3
A or B
A= $scheme eq "http"B= $scheme eq "https"
ABresult
1X1
011
000
173 44 ▶ return 0 if $s->s3->secure && $scheme ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $s->s3->secure and $scheme ne "https" 2 42 44
Condition100%3/3
$s->s3->secure and $scheme ne "https"
!ll&&!rl&&r
5372
MC/DC100%2/2
$s->s3->secure and $scheme ne "https"
$s->s3->secure $scheme ne "https"
Truth table100%3/3
A and B
A= $s->s3->secureB= $scheme ne "https"
ABresult
0X0
100
111
174 42 ▶ return 0 if defined $uri->userinfo;
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->userinfo 2 40 42
175  
176 40 ▶ my $host = lc( $uri->host // '' );
Condition50%1/2
$uri->host // ""
l!l
400
MC/DC0%0/1
$uri->host // ""
$uri->host
Truth table50%1/2
A
A= $uri->host
Aresult
11
00
177 40 $host =~ s/\.\z//;
178 40 ▶ return 0 unless length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless length $host 0 40 40
179  
180 40 ▶ my ( $endpoint_host, $endpoint_port ) = lc( $s->endpoint ) =~ m{\A([^:/]+)(?::(\d+))?\z}
Branch: 1 branch
BranchTrueFalseTotal
() = lc($s->endpoint) =~ m[\A([^:/]+)(?::(\d+))?\z] or return 0 40 0 40
181 or return 0;
182 40 $endpoint_host =~ s/\.\z//;
183 40 ▶ return 0 unless length $endpoint_host;
Branch: 1 branch
BranchTrueFalseTotal
unless length $endpoint_host 0 40 40
184  
185 40 ▶ my $port = defined $endpoint_port ? $endpoint_port : $uri->default_port;
Branch: 1 branch
BranchTrueFalseTotal
defined $endpoint_port ? : 3 37 40
186 40 ▶ return 0 unless $uri->port == $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->port == $port 3 37 40
187  
188 # S3 redirects to regional endpoints (e.g. bucket.s3-us-west-2.amazonaws.com)
189 # so for AWS only trust S3 hostnames, not anything under amazonaws.com
190 # (which includes things like ec2 and api gateway hostnames)
191 37 ▶ if ( $endpoint_host =~ m{(?:\A|\.)(amazonaws\.com(?:\.cn)?)\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endpoint_host =~ /(?:\A|\.)(amazonaws\.com(?:\.cn)?)\z/) 27 10 37
192 27 my $aws_domain = $1;
193 27 ▶ return $host =~ m{(?:\A|\.)s3(?:[.-][a-z0-9-]+)*\.\Q$aws_domain\E\z} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$host =~ /(?:\A|\.)s3(?:[.-][a-z0-9-]+)*\.\Q$aws_domain\E\z/ ? : 14 13 27
194 }
195  
196 10 ▶ return ( $host eq $endpoint_host || $host =~ m{\.\Q$endpoint_host\E\z} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$host eq $endpoint_host || $host =~ /\.\Q$endpoint_host\E\z/ ? : 5 5 10
Condition100%3/3
$host eq $endpoint_host or $host =~ /\.\Q$endpoint_host\E\z/
l!l&&r!l&&!r
145
MC/DC100%2/2
$host eq $endpoint_host or $host =~ /\.\Q$endpoint_host\E\z/
$host eq $endpoint_host $host =~ /\.\Q$endpoint_host\E\z/
Truth table100%3/3
A or B
A= $host eq $endpoint_hostB= $host =~ /\.\Q$endpoint_host\E\z/
ABresult
1X1
011
000
197 }
198  
199 100 ▶sub _sign {
Subroutine _sign: called CC=3 SCAR=11.0
200 100 my ($s, $request, $region) = @_;
201 100 ▶ $region = $s->s3->region unless defined $region;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $region 95 5 100
202 100 my $signer = $s->signerv4;
203 100 ▶ if (defined $s->s3->session_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->s3->session_token) 39 61 100
204 39 $request->header('X-Amz-Security-Token', $s->s3->session_token);
205 }
206 100 my $digest = Digest::SHA::sha256_hex($request->content);
207 100 $request->header('X-Amz-Content-SHA256', $digest);
208 100 $signer->sign($request, $region, $digest);
209 100 $request;
210 }
211  
212 # the value of the Date header of a request
213 94 ▶sub _http_date {
Subroutine _http_date: called CC=1 SCAR=0.0
214 94 return HTTP::Date::time2str( time );
215 }
216  
217 # the value of a Content-MD5 header for the content (an empty string has an
218 # MD5 too, so only a missing value gives no header value)
219 26 ▶sub _content_md5 {
Subroutine _content_md5: called CC=2 SCAR=7.1
220 26 my ( $s, $content ) = @_;
221  
222 26 ▶ return '' unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 26 26
223 26 return MIME::Base64::encode_base64( Digest::MD5::md5( $content ), '' );
224 }
225  
226 # Escape a value for safe use as XML character data (or inside an attribute).
227 # "\r" must be a character reference or the receiving XML parser will
228 # normalise it to "\n", silently changing the value.
229 22 ▶sub _xml_escape {
Subroutine _xml_escape: called CC=2 SCAR=6.9
230 22 my ( $s, $str ) = @_;
231  
232 22 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 1 21 22
233 21 return HTML::Entities::encode_entities( $str, qq{<>&"'\r} );
234 }
235  
236 88 ▶sub parse_response {
Subroutine parse_response: called CC=1 SCAR=0.0
Pod undocumented
237 88 my ( $self, $res ) = @_;
238  
239 88 AWS::S3::ResponseParser->new(
240 response => $res,
241 expect_nothing => $self->_expect_nothing,
242 type => $self->type,
243 );
244 }
245  
246 9 1;
« S3/Bucket.pm ↑ Summary S3/FileIterator.pm »