lib/AWS/Signature/V4.pm

filter: Statementstmt 100% 348 / 348 Branchbran 97.7% 213 / 218 Conditioncond 87.4% 132 / 151 MC/DCmcdc 73.3% 66 / 90 Subroutinesub 100% 56 / 56 Podpod 80.0% 4 / 5 totaltotal 94.3% 819 / 868 CC 188 SCAR 23.7
CC
188
Cov
97%
sign
27.0
encoded_length
20.0
presign
17.0
CRAP 10.7
« lib/AWS/Signature/V4/Checksum.pm ↑ Summary blib/lib/AWS/Signature/V4/X509.pm »
1 package AWS::Signature::V4;
2 9 { our $VERSION = '0.003' }
3  
4 9 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use experimental qw< signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Digest::SHA qw< sha256_hex >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use POSIX qw< strftime >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Scalar::Util qw< openhandle >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 ▶use AWS::Signature::V4::Error qw< fail shown >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use AWS::Signature::V4::Checksum ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use AWS::Signature::V4::Chunker ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use AWS::Signature::V4::Credentials ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use AWS::Signature::V4::X509 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Headers that intermediaries commonly rewrite or that should never be
20 # signed
21 AUTH_PARAM => +{
22 81 map { lc($_) => $_ } qw<
23 X-Amz-Algorithm
24 X-Amz-Credential
25 X-Amz-Date
26 X-Amz-Expires
27 X-Amz-Security-Token
28 X-Amz-Signature
29 X-Amz-SignedHeaders
30 X-Amz-X509
31 X-Amz-X509-Chain
32 >
33 },
34  
35 # Headers that intermediaries commonly rewrite or that should never be
36 # signed
37 UNSIGNED => +{
38 117 map { lc($_) => 1 } qw<
39 Authorization
40 Connection
41 Expect
42 Keep-Alive
43 Proxy-Authenticate
44 Proxy-Authorization
45 Proxy-Connection
46 TE
47 Trailer
48 Transfer-Encoding
49 Upgrade
50 User-Agent
51 X-Amzn-Trace-Id
52 >
53 },
54  
55 # the arguments of some methods; anything else is an error
56 ALLOWED_ARGS => +{
57 encoded_length => +{
58 27 map { $_ => 1 } qw<
59 checksum
60 signed
61 trailers
62 >
63 },
64  
65 sign => +{
66 117 map { $_ => 1 } qw<
67 body
68 body_fh
69 checksum
70 decoded_content_length
71 headers
72 method
73 payload_hash
74 signed_headers
75 streaming
76 time
77 trailers
78 unsigned_payload
79 url
80 >
81 },
82  
83 presign => +{
84 9 map { $_ => 1 } qw<
85 body
86 body_fh
87 expires
88 headers
89 method
90 payload_hash
91 signed_headers
92 time
93 unsigned_payload
94 url
95 >
96 },
97  
98 payload => [ qw< body body_fh payload_hash unsigned_payload > ],
99 streaming => [ qw< decoded_content_length checksum trailers > ],
100 },
101  
102 MAX_EPOCH => 253402300799, # 9999-12-31T23:59:59Z
103 9 };
104  
105 94 ▶sub _copy ($value) { ref $value eq 'HASH' ? {$value->%*} : $value }
Subroutine _copy: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 93 1 94
106  
107 94 ▶sub _hash_or_undef ($name, $value) {
Subroutine _hash_or_undef: called CC=3 SCAR=11.1
108 94 ▶ fail 400, qq{"$name" must be a hash reference}
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $value or ref $value eq "HASH" 1 93 94
Condition66%2/3
not defined $value or ref $value eq "HASH"
l!l&&r!l&&!r
0931
MC/DC50%1/2
not defined $value or ref $value eq "HASH"
not defined $value ref $value eq "HASH"
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value eq "HASH"
ABresult
1X1
011
000
109 unless !defined $value || ref $value eq 'HASH';
110 }
111  
112 # arguments of sign/presign as a hash: name => value pairs or a hash
113 # reference; undefined values are like missing ones, unknown names an error
114 609 ▶sub _args ($what, @args) {
Subroutine _args: called CC=8 SCAR=20.9
115 609 ▶ @args = $args[0]->%* if @args == 1 && ref $args[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 and ref $args[0] eq "HASH" 2 607 609
Condition66%2/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
60702
MC/DC50%1/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
116 609 ▶ fail 400, "$what takes name => value pairs or a hash reference" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 1 608 609
117 608 my %args = @args;
118 608 ▶ defined $args{$_} or delete $args{$_} for keys %args;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$_} or delete $args{$_} 2206 2 2208
119  
120 # go looking for argument names that are outside the allowed ones
121 608 ▶ my $allowed = ALLOWED_ARGS->{$what}
Branch: 1 branch
BranchTrueFalseTotal
my $allowed = {"encoded_length" => {"checksum" => 1,"signed" => 1,"trailers" => 1},"payload" => ["body","body_fh","payload_hash","unsigned_payload"],"presign" => {"body" => 1,"body_fh" => 1,"expires" => 1,"headers" => 1,"method" => 1,"payload_hash" => 1,"signed_headers" => 1,"time" => 1,"unsigned_payload" => 1,"url" => 1},"sign" => {"body" => 1,"body_fh" => 1,"checksum" => 1,"decoded_content_length" => 1,"headers" => 1,"method" => 1,"payload_hash" => 1,"signed_headers" => 1,"streaming" => 1,"time" => 1,"trailers" => 1,"unsigned_payload" => 1,"url" => 1},"streaming" => ["decoded_content_length","checksum","trailers"]}->{$what} or die "ALLOWED_ARGS does not support <$what>" 608 0 608
122 or die "ALLOWED_ARGS does not support <$what>";
123 608 ▶ if (my @l = grep { ! $allowed->{$_} } keys(%args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@l) = grep({not $allowed->{$_};} keys %args)) 9 599 608
124 9 fail 400, "unsupported for $what: " . _shown_list(@l);
125 }
126 599 return %args;
127 }
128  
129 435 ▶sub _check_url ($url) {
Subroutine _check_url: called CC=5 SCAR=16.1
130 435 ▶ defined $url or fail 400, 'missing parameter "url"';
Branch: 1 branch
BranchTrueFalseTotal
defined $url or fail(400, "missing parameter \"url\"") 434 1 435
131 434 ▶ fail 400, 'url must be a string' if ref $url;
Branch: 1 branch
BranchTrueFalseTotal
if ref $url 1 433 434
132 433 ▶ fail 400, 'url must be ASCII: percent-encode non-ASCII characters'
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /[^\x00-\x7F]/u 5 428 433
133 if $url =~ m{[^\x00-\x7F]};
134 428 ▶ fail 400, 'url must not contain control characters: percent-encode them'
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /[\x00-\x1F\x7F]/u 1 427 428
135 if $url =~ m{[\x00-\x1F\x7F]};
136 427 return $url;
137 }
138  
139 # an HTTP token (RFC 9110), like header names and methods
140 1941 ▶sub _is_token ($s) {
Subroutine _is_token: called CC=3 SCAR=11.1
141 1941 ▶ defined $s && !ref $s && $s =~ m{\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z};
Branch: 1 branch
BranchTrueFalseTotal
defined $s and not ref $s and $s =~ /\A[!#\$%&'*+.^_`|~0-9A-Za-z-]+\z/u 1940 1 1941
Condition66%2/3
defined $s and not ref $s
!ll&&!rl&&r
011940
MC/DC50%1/2
defined $s and not ref $s
defined $s not ref $s
Truth table66%2/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
142 }
143  
144 440 ▶sub _method ($method) {
Subroutine _method: called CC=2 SCAR=6.9
145 440 ▶ fail 400, 'invalid method ' . shown($method) . ': it must be an HTTP token'
Branch: 1 branch
BranchTrueFalseTotal
unless _is_token($method) 5 435 440
146 unless _is_token($method);
147 435 return uc $method;
148 }
149  
150 sub _is_count ($n) {
151 defined $n && !ref $n && $n =~ m{\A (?: 0 | [1-9][0-9]* ) \z}mxs;
152 }
153  
154 # headers as a hash or array of pairs, to a hash with lowercase names;
155 # undefined values are skipped, like missing headers
156 414 ▶sub _headers_hash ($in) {
Subroutine _headers_hash: called CC=10 SCAR=23.0
157 414 my @pairs;
158 414 ▶ if (ref $in eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $in eq 'HASH') { } 38 376 414
elsif (ref $in eq 'ARRAY') { } 2 374 376
elsif (defined $in) { } 1 373 374
159 38 @pairs = map { $_ => $in->{$_} } sort keys $in->%*; # repeatable order
160 }
161 elsif (ref $in eq 'ARRAY') {
162 2 ▶ fail 400, '"headers" as an array reference must hold name => value pairs'
Branch: 1 branch
BranchTrueFalseTotal
if @$in % 2 1 1 2
163 if $in->@* % 2;
164 1 @pairs = $in->@*;
165 }
166 elsif (defined $in) {
167 1 fail 400, '"headers" must be a hash or array reference';
168 }
169 412 my %h;
170 412 ▶ while (my ($k, $v) = splice @pairs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@pairs, 0, 2)) 53 400 453
171 53 _check_header_name($k);
172 48 ▶ my @values = grep { defined } ref $v eq 'ARRAY' ? $v->@* : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 2 46 48
173 48 _check_header_value($k, $_) for @values;
174 41 ▶ next unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 3 38 41
175 38 $v = join ',', @values;
176 38 $k = lc $k;
177 38 ▶ $h{$k} = exists $h{$k} ? "$h{$k},$v" : $v;
Branch: 1 branch
BranchTrueFalseTotal
exists $h{$k} ? : 3 35 38
178 }
179 400 return %h;
180 }
181  
182 # so that it cannot change the canonical request
183 1501 ▶sub _check_header_name ($name) {
Subroutine _check_header_name: called CC=2 SCAR=6.9
184 1501 ▶ fail 400, 'invalid header name ' . shown($name) unless _is_token($name);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_token($name) 6 1495 1501
185 }
186  
187 # what is wrong with a value that gets signed, if anything: CR, LF and NUL
188 # would forge lines in the canonical request, and a character beyond a byte
189 # cannot go on the wire (in a query string _uri_encode would turn it into an
190 # invalid percent escape, silently mangling e.g. a session token)
191 2008 ▶sub _bad_value ($value) {
Subroutine _bad_value: called CC=3 SCAR=11.0
192 2008 ▶ return 'no CR, LF or NUL' if $value =~ m{[\r\n\0]};
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[\r\n\0]/u 7 2001 2008
193 2001 ▶ return 'a byte string, encode characters first' if $value =~ m{[^\x00-\xFF]};
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[^\x00-\xFF]/u 5 1996 2001
194 1996 return undef;
195 }
196  
197 # so that it cannot inject headers or lines in the canonical request
198 1869 ▶sub _check_header_value ($name, $value) {
Subroutine _check_header_value: called CC=3 SCAR=11.0
199 1869 ▶ my $invalid = ref $value ? 'a string or an array reference of strings'
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 1 1868 1869
Condition100%2/2
_bad_value($value) // (return)
l!l
101858
MC/DC100%1/1
_bad_value($value) // (return)
_bad_value($value)
Truth table100%2/2
A
A= _bad_value($value)
Aresult
11
00
200 : _bad_value($value) // return;
201 11 fail 400, 'invalid value for header ' . shown($name) . ": $invalid";
202 }
203  
204 # the same, for what presign signs into the query string instead of headers
205 401 ▶sub _check_query_values (@pairs) {
Subroutine _check_query_values: called CC=5 SCAR=16.1
206 401 ▶ while (my ($name, $value) = splice @pairs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@pairs, 0, 2)) 514 25 539
207 145 ▶ my $invalid = ref $value ? 'a string' : _bad_value($value // '') // next;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 1 144 145
Condition100%5/5
_bad_value($value // "") // (next)
l!l
2139372
$value // ""
l!l
216190
MC/DC100%1/1
_bad_value($value // "") // (next)
_bad_value($value // "")
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= _bad_value($value // "")
Aresult
11
00
Truth table100%2/2
A
A= $value
Aresult
11
00
208 14 fail 400, 'invalid value for query parameter ' . shown($name) . ": $invalid";
209 }
210 }
211  
212 # all the headers to send, including those that the module adds itself
213 348 ▶sub _check_header_values ($h) {
Subroutine _check_header_values: called CC=2 SCAR=6.9
214 348 _check_header_value($_, $h->{$_}) for sort keys $h->%*;
215 }
216  
217 # sorted, lowercase names of the headers to sign. Always included: host, all
218 # the x-amz-* headers (AWS wants them signed) and the names in @also
219 347 ▶sub _signed_headers ($h, $names, @also) {
Subroutine _signed_headers: called CC=5 SCAR=16.1
220 347 ▶ fail 400, '"signed_headers" must be an array reference' unless ref $names eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names eq "ARRAY" 2 345 347
221 345 my %signed = map { $_ => 1 } 'host', @also, grep { m{\Ax-amz-} } keys $h->%*;
222 345 for my $name ($names->@*) {
223 1448 _check_header_name($name);
224 1447 $signed{lc $name} = 1;
225 }
226 344 for my $name (sort keys %signed) {
227 1497 ▶ fail 400, 'cannot sign missing header ' . shown($name) unless exists $h->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h->{$name} 2 1495 1497
228 }
229 342 return sort keys %signed;
230 }
231  
232 143 ▶sub _is_sha256 ($hash) { $hash =~ m{\A[0-9a-f]{64}\z}mxs }
Subroutine _is_sha256: called CC=1 SCAR=0.0
233  
234 # a hex SHA-256 (lowercased), UNSIGNED-PAYLOAD or a STREAMING-* marker
235 270 ▶sub _check_payload_hash ($what, $value) {
Subroutine _check_payload_hash: called CC=4 SCAR=13.9
236 4 ▶ if (!ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 1 3 4
237 4 ▶ return lc $value if $value =~ m{\A[0-9A-Fa-f]{64}\z};
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A[0-9A-Fa-f]{64}\z/u 2 1 3
238 3 ▶ return $value if $value =~ m{\A(?:UNSIGNED-PAYLOAD|STREAMING-[A-Z0-9-]+)\z};
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A(?:UNSIGNED-PAYLOAD|STREAMING-[A-Z0-9-]+)\z/u 2 0 2
239 }
240 3 fail 400, "$what must be a SHA-256 in hex, UNSIGNED-PAYLOAD or a STREAMING-* marker";
241 }
242  
243 # the payload hash in the x-amz-content-sha256 header provided by the
244 # caller, if any; it is put back later, when needed
245 392 ▶sub _given_payload_hash ($h) {
Subroutine _given_payload_hash: called CC=2 SCAR=6.9
246 392 ▶ my $given = delete $h->{'x-amz-content-sha256'} // return undef;
Condition100%2/2
delete $h->{"x-amz-content-sha256"} // (return undef)
l!l
5373
MC/DC100%1/1
delete $h->{"x-amz-content-sha256"} // (return undef)
delete $h->{"x-amz-content-sha256"}
Truth table100%2/2
A
A= delete $h->{"x-amz-content-sha256"}
Aresult
11
00
247 18 return _check_payload_hash('the x-amz-content-sha256 header', $given);
248 }
249  
250 # payload hash from the arguments, undef if they say nothing about it
251 191 ▶sub _payload_hash ($args) {
Subroutine _payload_hash: called CC=3 SCAR=11.0
252 return _check_payload_hash('"payload_hash"', $args->{payload_hash})
253 200 ▶ if defined $args->{payload_hash};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"payload_hash"} 22 183 205
254 188 ▶ return 'UNSIGNED-PAYLOAD' if $args->{unsigned_payload};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"unsigned_payload"} 8 187 195
255 178 return _body_hash($args);
256 }
257  
258 # the payload hash from the arguments and the one in the header must agree;
259 # the default applies if there is neither
260 350 ▶sub _agree ($from_args, $given, $default) {
Subroutine _agree: called CC=5 SCAR=16.1
261 350 ▶ return $from_args // $default unless defined $given;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $given 350 9 359
Condition66%2/3
$from_args // $default
l!l&&r!l&&!r
2021440
MC/DC0%0/2
$from_args // $default
$from_args $default
Truth table66%2/3
A // B
A= $from_argsB= $default
ABresult
1X1
011
000
262 4 ▶ fail 400, 'the x-amz-content-sha256 header does not match the payload'
Branch: 1 branch
BranchTrueFalseTotal
if defined $from_args and $from_args ne $given 1 3 4
Condition100%3/3
defined $from_args and $from_args ne $given
!ll&&!rl&&r
211
MC/DC100%2/2
defined $from_args and $from_args ne $given
defined $from_args $from_args ne $given
Truth table100%3/3
A and B
A= defined $from_argsB= $from_args ne $given
ABresult
0X0
100
111
263 if defined $from_args && $from_args ne $given;
264 3 return $given;
265 }
266  
267 # hex SHA-256 of the "body" or "body_fh" argument, undef if none is given
268 178 ▶sub _body_hash ($args) {
Subroutine _body_hash: called CC=8 SCAR=20.8
269 178 my $fh = $args->{body_fh};
270 fail 400, 'give either "body" or "body_fh", not both'
271 178 ▶ if defined $fh && defined $args->{body};
Branch: 1 branch
BranchTrueFalseTotal
if defined $fh and defined $args->{"body"} 1 177 178
Condition100%3/3
defined $fh and defined $args->{"body"}
!ll&&!rl&&r
17161
MC/DC100%2/2
defined $fh and defined $args->{"body"}
defined $fh defined $args->{"body"}
Truth table100%3/3
A and B
A= defined $fhB= defined $args->{"body"}
ABresult
0X0
100
111
272 177 ▶ return _fh_hash($fh) if defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fh 6 171 177
273 171 ▶ return undef unless defined $args->{body};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"body"} 146 25 171
274  
275 # the hash must match what goes on the wire, so no characters allowed;
276 # a copy is made only for internally-UTF-8 strings, never modify input
277 25 my $ref = _body_ref($args);
278 23 ▶ return sha256_hex($$ref // '') unless utf8::is_utf8($$ref);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($$ref) 19 4 23
Condition100%2/2
$$ref // ""
l!l
181
MC/DC100%1/1
$$ref // ""
$$ref
Truth table100%2/2
A
A= $$ref
Aresult
11
00
279 4 my $copy = $$ref;
280 4 ▶ utf8::downgrade($copy, 1)
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($copy, 1) or fail(400, "body must be a byte string: encode characters first") 1 3 4
281 or fail 400, 'body must be a byte string: encode characters first';
282 1 return sha256_hex($copy);
283 }
284  
285 # the handle is hashed from where it is to its end, then put back there
286 sub _fh_hash ($fh) {
287 2 ▶ openhandle($fh) or fail 400, '"body_fh" must be an open filehandle';
Branch: 1 branch
BranchTrueFalseTotal
&openhandle($fh) or fail(400, "\"body_fh\" must be an open filehandle") 199 71 270
288 fail 400, '"body_fh" must be a binary handle: binmode it'
289 270 ▶ if grep { m{\A(?:encoding|utf8|crlf)} } PerlIO::get_layers($fh);
Branch: 1 branch
BranchTrueFalseTotal
if grep {/\A(?:encoding|utf8|crlf)/u;} &PerlIO::get_layers($fh) 4 195 199
290 199 my $pos = tell $fh;
291 195 ▶ fail 400, '"body_fh" must be seekable (a file, not a pipe or a socket)'
Branch: 1 branch
BranchTrueFalseTotal
unless $pos >= 0 and seek $fh, $pos, 0 0 266 266
Condition33%1/3
$pos >= 0 and seek $fh, $pos, 0
!ll&&!rl&&r
7700
MC/DC0%0/2
$pos >= 0 and seek $fh, $pos, 0
$pos >= 0 seek $fh, $pos, 0
Truth table0%0/3
A and B
A= $pos >= 0B= seek $fh, $pos, 0
ABresult
0X0
100
111
292 unless $pos >= 0 && seek $fh, $pos, 0;
293 266 my $sha = Digest::SHA->new(256);
294 266 ▶ eval { $sha->addfile($fh); 1 } or fail 400, qq{"body_fh" cannot be read: $!};
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $sha->addfile($fh); 1 } } or fail(400, qq["body_fh" cannot be read: $!]) 1 74 75
295 77 ▶ seek $fh, $pos, 0 or fail 400, qq{"body_fh" cannot be put back where it was: $!};
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $pos, 0 or fail(400, qq["body_fh" cannot be put back where it was: $!]) 3 133 136
296 75 return $sha->hexdigest;
297 }
298  
299 # names of the trailers: built-in checksum first, then the declared ones
300 sub _trailer_names ($args) {
301 48 my @names;
302 74 if (defined(my $algo = $args->{checksum})) {
303 263 fail 400, 'unknown checksum ' . shown($algo) . ' (supported: '
304 . join(', ', AWS::Signature::V4::Checksum->supported_algorithms) . ')'
305 unless AWS::Signature::V4::Checksum->is_supported_algorithm($algo);
306 136 push @names, "x-amz-checksum-$algo";
307 }
308 my $trailers = $args->{trailers} // [];
309 fail 400, '"trailers" must be an array reference' unless ref $trailers eq 'ARRAY';
310 for my $name (map { lc($_ // '') } $trailers->@*) {
311 _check_trailer_name($name);
312 fail 400, "trailer '$name' declared twice" if grep { $_ eq $name } @names;
313 push @names, $name;
314 }
315 return @names;
316 }
317  
318 sub _check_trailer_name ($name) {
319 fail 400, 'invalid trailer name ' . shown($name) unless $name =~ m{\A[a-z0-9][a-z0-9-]*\z};
320 }
321  
322 340 ▶sub _canonical_query ($query) {
Subroutine _canonical_query: called CC=5 SCAR=16.1
323 340 ▶ return '' unless defined $query && length $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query and length $query 297 43 340
Condition100%3/3
defined $query and length $query
!ll&&!rl&&r
296143
MC/DC100%2/2
defined $query and length $query
defined $query length $query
Truth table100%3/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
324 my @pairs = map {
325 166 my ($k, $v) = split /=/, $_, 2;
326 166 ▶ [_uri_encode(_uri_decode($k)), _uri_encode(_uri_decode($v // ''))];
Condition100%2/2
$v // ""
l!l
1651
MC/DC100%1/1
$v // ""
$v
Truth table100%2/2
A
A= $v
Aresult
11
00
327 43 } grep { length } split /&/, $query;
328 166 return join '&', map { "$_->[0]=$_->[1]" }
329 43 ▶ sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @pairs;
Condition66%2/3
$AWS::Signature::V4::a->[0] cmp $AWS::Signature::V4::b->[0] || $AWS::Signature::V4::a->[1] cmp $AWS::Signature::V4::b->[1]
l!l&&r!l&&!r
22340
MC/DC0%0/2
$AWS::Signature::V4::a->[0] cmp $AWS::Signature::V4::b->[0] || $AWS::Signature::V4::a->[1] cmp $AWS::Signature::V4::b->[1]
$AWS::Signature::V4::a->[0] cmp $AWS::Signature::V4::b->[0] $AWS::Signature::V4::a->[1] cmp $AWS::Signature::V4::b->[1]
Truth table66%2/3
A || B
A= $AWS::Signature::V4::a->[0] cmp $AWS::Signature::V4::b->[0]B= $AWS::Signature::V4::a->[1] cmp $AWS::Signature::V4::b->[1]
ABresult
1X1
011
000
330 }
331  
332 867 ▶sub _uri_encode ($s) {
Subroutine _uri_encode: called CC=1 SCAR=0.0
333 867 $s =~ s{([^A-Za-z0-9\-_.~])}{sprintf '%%%02X', ord $1}gmse;
334 867 return $s;
335 }
336  
337 629 ▶sub _uri_decode ($s) {
Subroutine _uri_decode: called CC=1 SCAR=0.0
338 629 $s =~ s{%([0-9A-Fa-f]{2})}{chr hex $1}gse;
339 629 return $s;
340 }
341  
342 # body is either a plain scalar or a reference to one; only in the latter
343 # case the caller's data is not copied at all
344 25 ▶sub _body_ref ($args) {
Subroutine _body_ref: called CC=3 SCAR=11.0
345 25 my $body = $args->{body};
346 25 ▶ return \$args->{body} unless ref $body;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body 18 7 25
347 7 ▶ fail 400, 'body must be a scalar or a reference to a scalar'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "SCALAR" 2 5 7
348 unless ref($body) eq 'SCALAR';
349 5 return $body;
350 }
351  
352 # AWS trims and collapses spaces, so ASCII whitespace only (/a): with
353 # unicode_strings, \s would also take the bytes 0xA0 and 0x85 of UTF-8 text
354 1489 ▶sub _trim ($v) {
Subroutine _trim: called CC=1 SCAR=0.0
355 1489 $v =~ s{\A\s+|\s+\z}{}gmxa; # remove leading/trailing whitespaces
356 1489 $v =~ s{\s+}{ }gas; # normalize internal whitespaces
357 1489 return $v;
358 }
359  
360 # (host, path, query) of a url; the host is lowercased and the port is
361 # removed if it is the default one, as user agents do before sending it
362 427 ▶sub _split_url ($url) {
Subroutine _split_url: called CC=16 SCAR=27.8
363 # every part is optional, so this always matches
364 427 my ($scheme, $auth, $path, $query) =
365 $url =~ m{\A (?: ([A-Za-z][A-Za-z0-9+.-]*) :// ([^/?\#]*) )?
366 ([^?\#]*) (?: \? ([^\#]*) )? }x;
367 427 ▶ fail 400, 'url path must start with "/": give an absolute url, or a path with a Host header'
Branch: 1 branch
BranchTrueFalseTotal
if length $path and not $path =~ m[\A/]u 4 423 427
Condition100%3/3
length $path and not $path =~ m[\A/]u
!ll&&!rl&&r
44194
MC/DC100%2/2
length $path and not $path =~ m[\A/]u
length $path not $path =~ m[\A/]u
Truth table100%3/3
A and not(B)
A= length $pathB= not $path =~ m[\A/]u
ABresult
0X0
100
111
368 if length $path && $path !~ m{\A/};
369 423 ▶ fail 400, 'url query must not contain "+": write %20 for a space, %2B for a plus sign'
Branch: 1 branch
BranchTrueFalseTotal
if defined $query and $query =~ /\+/u 1 422 423
Condition100%3/3
defined $query and $query =~ /\+/u
!ll&&!rl&&r
387351
MC/DC100%2/2
defined $query and $query =~ /\+/u
defined $query $query =~ /\+/u
Truth table100%3/3
A and B
A= defined $queryB= $query =~ /\+/u
ABresult
0X0
100
111
370 if defined $query && $query =~ m{\+};
371 422 ▶ return (undef, $path, $query) unless defined $auth && length $auth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $auth and length $auth 3 419 422
Condition66%2/3
defined $auth and length $auth
!ll&&!rl&&r
30419
MC/DC50%1/2
defined $auth and length $auth
defined $auth length $auth
Truth table66%2/3
A and B
A= defined $authB= length $auth
ABresult
0X0
100
111
372  
373 419 ▶ fail 400, 'url must not have user information (user@host)' if $auth =~ m{@};
Branch: 1 branch
BranchTrueFalseTotal
if $auth =~ /@/u 3 416 419
374 416 ▶ my ($host, $port) = $auth =~ m{\A
Branch: 1 branch
BranchTrueFalseTotal
() = $auth =~ /\A (\[[0-9A-Fa-f:.]+\] | [A-Za-z0-9._~-]+) # host (?: : ([0-9]*) )? # optional port \z/ux or fail(400, 'invalid url: ' . shown($auth)) 412 4 416
375 (\[[0-9A-Fa-f:.]+\] | [A-Za-z0-9._~-]+) # host
376 (?: : ([0-9]*) )? # optional port
377 \z}x or fail 400, 'invalid url: ' . shown($auth);
378 412 $host = lc $host;
379 412 ▶ if (length($port // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($port // "")) 9 403 412
Condition100%2/2
$port // ""
l!l
10402
MC/DC100%1/1
$port // ""
$port
Truth table100%2/2
A
A= $port
Aresult
11
00
380 9 $port += 0;
381 9 ▶ fail 400, "invalid port: $port" if $port < 1 || $port > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $port < 1 or $port > 65535 1 8 9
Condition66%2/3
$port < 1 or $port > 65535
l!l&&r!l&&!r
018
MC/DC50%1/2
$port < 1 or $port > 65535
$port < 1 $port > 65535
Truth table66%2/3
A or B
A= $port < 1B= $port > 65535
ABresult
1X1
011
000
382 8 my $lcscheme = lc($scheme);
383 8 ▶ my $default = $lcscheme eq 'https' ? 443
Branch: 2 branches
BranchTrueFalseTotal
$lcscheme eq 'https' ? : 6 2 8
$lcscheme eq 'http' ? : 2 0 2
384 : $lcscheme eq 'http' ? 80
385 : 0;
386 8 ▶ $host .= ':' . $port if $port != $default;
Branch: 1 branch
BranchTrueFalseTotal
if $port != $default 3 5 8
387 }
388 411 return ($host, $path, $query);
389 }
390  
391 136 ▶sub _shown_list (@list) {
Subroutine _shown_list: called CC=1 SCAR=0.0
392 return join ', ',
393 2 map { join '', q{"}, shown($_), q{"} }
394 3 sort { $a cmp $b } @list;
395 }
396  
397  
398 9 ▶use namespace::clean; # imported or plain functions must not become methods
Subroutine BEGIN: called CC=1 SCAR=0.0
399  
400  
401 9 has service => (is => 'ro');
402 9 has region => (is => 'ro');
403  
404 # credentials: {access_key_id, secret_access_key, session_token?}
405 46 ▶has credentials => (is => 'ro', coerce => \&_copy, isa => sub { _hash_or_undef(credentials => @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
406  
407 # Certificate-based variant, as used by IAM Roles Anywhere.
408 # certificate / certificate_file: PEM or DER text, or path of a file with it
409 # chain / chain_files: optional arrayref of PEM/DER intermediate certificates,
410 # or of paths of files with them; a PEM item can be a bundle.
411 # chain can also be a plain string: a PEM-encoded bundle, and
412 # chain_files a plain string: the path of one file
413 # key_type: 'RSA' or 'ECDSA'
414 # signer: sub ($bytes_to_sign) -> signature (DER for ECDSA, PKCS#1 v1.5
415 # for RSA), computed with SHA-256; or, alternatively,
416 # private_key_file / private_key: PEM or DER key (path or content), signed
417 # with CryptX; private_key_password if it is encrypted
418 67 ▶has x509 => (is => 'ro', coerce => \&_copy, isa => sub { _hash_or_undef(x509 => @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
419  
420 # S3 is the odd one out: this is the only place that knows how to tell. S3
421 # Object Lambda, S3 on Outposts and S3 Express sign with names of their own,
422 # under the same rules.
423 9 has _is_s3 => (is => 'lazy', init_arg => undef);
424 61 ▶sub _build__is_s3 ($self) { $self->service =~ m{\As3(?:-object-lambda|-outposts|express)?\z} }
Subroutine _build__is_s3: called CC=1 SCAR=0.0
425  
426 28 ▶has double_encode => (is => 'ro', lazy => 1, default => sub ($self) { !$self->_is_s3 });
Subroutine __ANON__: called CC=1 SCAR=0.0
427 58 ▶has normalize_path => (is => 'ro', lazy => 1, default => sub ($self) { !$self->_is_s3 });
Subroutine __ANON__: called CC=1 SCAR=0.0
428 60 ▶has payload_header => (is => 'ro', lazy => 1, default => sub ($self) { $self->_is_s3 });
Subroutine __ANON__: called CC=1 SCAR=0.0
429  
430 # FIXME review this decision: "undef" in Perl has a history of meaning
431 # "false" but here we're saying that it means "do the default".
432 # an undefined option is like a missing one: the default applies
433 95 ▶around BUILDARGS => sub ($orig, $class, @args) {
Subroutine __ANON__: called CC=3 SCAR=11.2
434 95 my $args = $class->$orig(@args);
435 defined $args->{$_} or delete $args->{$_}
436 95 ▶ for qw< double_encode normalize_path payload_header >;
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{$_} or delete $args->{$_} 0 285 285
437 95 return $args;
438 9 };
439  
440 # The variant in use, credentials or x509, which knows how to sign and what
441 # goes with the request: see AWS::Signature::V4::Credentials and ::X509
442 9 has _auth => (is => 'lazy', init_arg => undef);
443  
444 94 ▶sub BUILD ($self, $args) {
Subroutine BUILD: called CC=9 SCAR=22.0
Pod undocumented
445 94 for my $name (qw< service region >) {
446 185 my $value = $self->$name;
447 185 ▶ defined($value) || fail 400, qq{missing parameter "$name"};
Branch: 1 branch
BranchTrueFalseTotal
defined $value or fail(400, qq[missing parameter "$name"]) 181 4 185
448 181 ▶ fail 400, qq{invalid "$name" } . shown($value)
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[-A-Za-z0-9._]+\z/u 2 179 181
449 . ': only letters, digits, ".", "_" and "-" are allowed'
450 unless $value =~ m{\A[-A-Za-z0-9._]+\z};
451 }
452  
453 # make sure the caller provides us *exactly* one of credentials/X509
454 88 my $x509_params = $self->x509;
455 88 my $credentials_params = $self->credentials;
456 88 ▶ fail 400, 'provide either "credentials" or "x509", not both'
Branch: 1 branch
BranchTrueFalseTotal
if $credentials_params and $x509_params 1 87 88
Condition100%3/3
$credentials_params and $x509_params
!ll&&!rl&&r
58291
MC/DC100%2/2
$credentials_params and $x509_params
$credentials_params $x509_params
Truth table100%3/3
A and B
A= $credentials_paramsB= $x509_params
ABresult
0X0
100
111
457 if $credentials_params && $x509_params;
458 87 ▶ fail 400, 'provide (exactly) one of "credentials" or "x509"'
Branch: 1 branch
BranchTrueFalseTotal
unless $credentials_params or $x509_params 2 85 87
Condition100%3/3
$credentials_params or $x509_params
l!l&&r!l&&!r
29562
MC/DC100%2/2
$credentials_params or $x509_params
$credentials_params $x509_params
Truth table100%3/3
A or B
A= $credentials_paramsB= $x509_params
ABresult
1X1
011
000
459 unless $credentials_params || $x509_params;
460  
461 85 $self->_auth; # force building and fail fast
462  
463 # after using the parameters, we get rid of sensitive data in the input
464 # hash for X509
465 64 delete($x509_params->{$_}) for qw< private_key private_key_password >;
466  
467 64 return;
468 }
469  
470 85 ▶sub _build__auth ($self) {
Subroutine _build__auth: called CC=2 SCAR=6.9
471 85 ▶ return $self->credentials
Branch: 1 branch
BranchTrueFalseTotal
$self->credentials ? : 29 56 85
472 ? AWS::Signature::V4::Credentials->new($self->credentials->%*)
473 : AWS::Signature::V4::X509->new($self->x509->%*);
474 }
475  
476 678 ▶sub algorithm ($self) { $self->_auth->algorithm }
Subroutine algorithm: called CC=1 SCAR=0.0
Pod documented
477  
478 # sign(method => ..., url => ..., headers => ..., body => ..., time => ...)
479 # Returns a hashref:
480 # headers: all headers to set on the request (name => value)
481 # including Authorization, X-Amz-Date, Host if missing...
482 # authorization: value of the Authorization header
483 # signature, canonical_request, string_to_sign, signed_headers, scope
484 400 ▶sub sign ($self, @args) {
Subroutine sign: called CC=27 SCAR=33.0
Pod documented
485 400 my %args = _args(sign => @args);
486  
487 # some arguments apply to the "payload" variant and some to the
488 # "streaming" variant only, we need some more input validation.
489 # $not_allowed below collects the name of the parameter that are not
490 # allowed for the variant that is requested.
491 397 my $streaming = $args{streaming};
492 397 ▶ my $not_allowed = ALLOWED_ARGS->{$streaming ? 'payload' : 'streaming'};
Branch: 1 branch
BranchTrueFalseTotal
$streaming ? : 189 208 397
493 397 my @misplaced = grep { exists $args{$_} } $not_allowed->@*;
494 397 ▶ fail 400, qq{"$misplaced[0]" }
Branch: 2 branches
BranchTrueFalseTotal
if @misplaced 6 391 397
$streaming ? : 3 3 6
495 . ($streaming ? 'does not apply to streaming' : 'needs streaming')
496 if @misplaced;
497  
498 391 ▶ my $method = _method($args{method} // fail 400, 'missing parameter "method"');
Condition66%2/3
$args{"method"} // fail(400, "missing parameter \"method\"")
l!l&&r!l&&!r
38920
MC/DC0%0/2
$args{"method"} // fail(400, "missing parameter \"method\"")
$args{"method"} fail(400, "missing parameter \"method\"")
Truth table33%1/3
A // B
A= $args{"method"}B= fail(400, "missing parameter \"method\"")
ABresult
1X1
011
000
499 385 my ($h, $path, $query, $amzdate, $scope) = $self->_request(\%args);
500 350 $h->{'x-amz-date'} = $amzdate;
501 350 my $given = _given_payload_hash($h);
502  
503 349 my ($unsigned_chunks, @trailer_names);
504 349 my $payload_hash;
505 349 ▶ if ($streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if ($streaming) { } 186 163 349
506 186 ▶ fail 400, 'streaming must be false, 1, "signed", or "unsigned"'
Branch: 1 branch
BranchTrueFalseTotal
unless $streaming =~ /\A(?:1|signed|unsigned)\z/u 1 185 186
507 unless $streaming =~ m{\A(?:1|signed|unsigned)\z};
508 185 $unsigned_chunks = $streaming eq 'unsigned';
509 185 @trailer_names = _trailer_names(\%args);
510 179 ▶ fail 400, 'unsigned streaming needs "checksum" or "trailers"'
Branch: 1 branch
BranchTrueFalseTotal
if $unsigned_chunks and not @trailer_names 1 178 179
Condition100%3/3
$unsigned_chunks and not @trailer_names
!ll&&!rl&&r
104741
MC/DC100%2/2
$unsigned_chunks and not @trailer_names
$unsigned_chunks not @trailer_names
Truth table100%3/3
A and not(B)
A= $unsigned_chunksB= not @trailer_names
ABresult
0X0
100
111
511 if $unsigned_chunks && !@trailer_names;
512 178 ▶ fail 400, 'signed streaming needs the credentials variant, not x509'
Branch: 1 branch
BranchTrueFalseTotal
unless $unsigned_chunks or $self->_auth->can_sign_chunks 2 176 178
Condition100%3/3
$unsigned_chunks or $self->_auth->can_sign_chunks
l!l&&r!l&&!r
741022
MC/DC100%2/2
$unsigned_chunks or $self->_auth->can_sign_chunks
$unsigned_chunks $self->_auth->can_sign_chunks
Truth table100%3/3
A or B
A= $unsigned_chunksB= $self->_auth->can_sign_chunks
ABresult
1X1
011
000
513 if !$unsigned_chunks && !$self->_auth->can_sign_chunks;
514 my $len = $args{decoded_content_length}
515 176 ▶ // fail 400, 'streaming needs "decoded_content_length"';
Condition66%2/3
$args{"decoded_content_length"} // fail(400, "streaming needs \"decoded_content_length\"")
l!l&&r!l&&!r
17510
MC/DC0%0/2
$args{"decoded_content_length"} // fail(400, "streaming needs \"decoded_content_length\"")
$args{"decoded_content_length"} fail(400, "streaming needs \"decoded_content_length\"")
Truth table33%1/3
A // B
A= $args{"decoded_content_length"}B= fail(400, "streaming needs \"decoded_content_length\"")
ABresult
1X1
011
000
516 175 ▶ fail 400, 'decoded_content_length must be a non-negative integer'
Branch: 1 branch
BranchTrueFalseTotal
unless $len =~ /\A[0-9]+\z/u 1 174 175
517 unless $len =~ m{\A[0-9]+\z};
518 174 ▶ $payload_hash =
Branch: 2 branches
BranchTrueFalseTotal
$unsigned_chunks ? : 74 100 174
@trailer_names ? : 63 37 100
519 $unsigned_chunks ? 'STREAMING-UNSIGNED-PAYLOAD-TRAILER'
520 : @trailer_names ? 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER'
521 : 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD';
522 174 $h->{'x-amz-decoded-content-length'} = $len;
523 174 ▶ $h->{'x-amz-trailer'} = join ',', @trailer_names if @trailer_names;
Branch: 1 branch
BranchTrueFalseTotal
if @trailer_names 137 37 174
524  
525 # whatever the caller has, e.g. gzip, then aws-chunked: RFC 9110 wants
526 # the encodings in the order they were applied, and this one is the
527 # outermost, applied to the already-gzipped data (as botocore does)
528 4 ▶ my @encodings = grep { length && lc($_) ne 'aws-chunked' }
Condition66%2/3
length $_ && lc $_ ne 'aws-chunked'
!ll&&!rl&&r
022
MC/DC50%1/2
length $_ && lc $_ ne 'aws-chunked'
length $_ lc $_ ne 'aws-chunked'
Truth table66%2/3
A && B
A= length $_B= lc $_ ne 'aws-chunked'
ABresult
0X0
100
111
529 174 ▶ map { _trim($_) } split m{,}, $h->{'content-encoding'} // '';
Condition100%2/2
$h->{"content-encoding"} // ""
l!l
3171
MC/DC100%1/1
$h->{"content-encoding"} // ""
$h->{"content-encoding"}
Truth table100%2/2
A
A= $h->{"content-encoding"}
Aresult
11
00
530 174 $h->{'content-encoding'} = join ',', @encodings, 'aws-chunked';
531 }
532 else {
533 163 $payload_hash = _payload_hash(\%args);
534 }
535 323 $payload_hash = _agree($payload_hash, $given, sha256_hex(''));
536 322 ▶ $h->{'x-amz-content-sha256'} = $payload_hash
Branch: 1 branch
BranchTrueFalseTotal
if $self->payload_header or $streaming or defined $given or not _is_sha256($payload_hash) 200 122 322
Condition88%8/9
$self->payload_header or $streaming or defined $given or not _is_sha256($payload_hash)
l!l&&r!l&&!r
1964122
$self->payload_header or $streaming or defined $given
l!l&&r!l&&!r
1951126
$self->payload_header or $streaming
l!l&&r!l&&!r
1950127
MC/DC75%3/4
$self->payload_header or $streaming or defined $given or not _is_sha256($payload_hash)
$self->payload_header $streaming defined $given not _is_sha256($payload_hash)
Truth table80%4/5
A or B or C or not(D)
A= $self->payload_headerB= $streamingC= defined $givenD= not _is_sha256($payload_hash)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
537 if $self->payload_header || $streaming || defined $given || !_is_sha256($payload_hash);
538  
539 322 my %extra = $self->_auth->extra_fields;
540 322 $h->{lc $_} = $extra{$_} for keys %extra;
541 322 _check_header_values($h); # before they end up in the canonical request
542  
543 my @signed = _signed_headers($h, $args{signed_headers}
544 320 ▶ // [grep { ! UNSIGNED->{$_} } keys $h->%*], $streaming ? 'content-encoding' : ());
Branch: 1 branch
BranchTrueFalseTotal
$streaming ? : 174 146 320
Condition100%2/2
$args{"signed_headers"} // [grep({not {"authorization" => 1,"connection" => 1,"expect" => 1,"keep-alive" => 1,"proxy-authenticate" => 1,"proxy-authorization" => 1,"proxy-connection" => 1,"te" => 1,"trailer" => 1,"transfer-encoding" => 1,"upgrade" => 1,"user-agent" => 1,"x-amzn-trace-id" => 1}->{$_};} keys %$h)]
l!l
10310
MC/DC100%1/1
$args{"signed_headers"} // [grep({not {"authorization" => 1,"connection" => 1,"expect" => 1,"keep-alive" => 1,"proxy-authenticate" => 1,"proxy-authorization" => 1,"proxy-connection" => 1,"te" => 1,"trailer" => 1,"transfer-encoding" => 1,"upgrade" => 1,"user-agent" => 1,"x-amzn-trace-id" => 1}->{$_};} keys %$h)]
$args{"signed_headers"}
Truth table100%2/2
A
A= $args{"signed_headers"}
Aresult
11
00
545 316 my $r = $self->_signed($method, $path, _canonical_query($query), $h,
546 \@signed, $payload_hash, $amzdate, $scope);
547  
548 312 my $credential = $self->_auth->credential_id . "/$scope";
549 312 $r->{authorization} = $self->algorithm . " Credential=$credential, "
550 . "SignedHeaders=$r->{signed_headers}, Signature=$r->{signature}";
551 312 _check_header_value(authorization => $r->{authorization});
552 310 $r->{headers} = {$h->%*, authorization => $r->{authorization}};
553  
554 $r->{chunker} = AWS::Signature::V4::Chunker->new(
555 ($unsigned_chunks ? () : (key => $self->_auth->signing_key($scope))),
556 signed => !$unsigned_chunks, amzdate => $amzdate,
557 scope => $scope, previous => $r->{signature},
558 expected => $args{decoded_content_length},
559 310 ▶ checksum => $args{checksum}, trailer_names => \@trailer_names,
Branch: 2 branches
BranchTrueFalseTotal
if $streaming 174 136 310
$unsigned_chunks ? : 74 100 174
560 ) if $streaming;
561 310 return $r;
562 }
563  
564 # presign(method => 'GET', url => ..., expires => 3600, time => ...)
565 # Returns a hashref:
566 # url: the URL, with the signature in its query string
567 # headers: headers that the client must send with it (Host, ...)
568 # signature, canonical_request, string_to_sign, signed_headers, scope
569 57 ▶sub presign ($self, @args) {
Subroutine presign: called CC=17 SCAR=28.3
Pod documented
570 57 my %args = _args(presign => @args);
571 51 ▶ my $method = _method($args{method} // 'GET');
Condition100%2/2
$args{"method"} // "GET"
l!l
447
MC/DC100%1/1
$args{"method"} // "GET"
$args{"method"}
Truth table100%2/2
A
A= $args{"method"}
Aresult
11
00
572 50 my ($h, $path, $query, $amzdate, $scope) = $self->_request(\%args);
573  
574 45 ▶ my $expires = $args{expires} // 3600;
Condition100%2/2
$args{"expires"} // 3600
l!l
639
MC/DC100%1/1
$args{"expires"} // 3600
$args{"expires"}
Truth table100%2/2
A
A= $args{"expires"}
Aresult
11
00
575 45 ▶ fail 400, 'expires must be an integer between 1 and 604800 seconds'
Branch: 1 branch
BranchTrueFalseTotal
unless $expires =~ /\A[0-9]+\z/u and $expires >= 1 and $expires <= 604800 4 41 45
Condition100%6/6
$expires =~ /\A[0-9]+\z/u and $expires >= 1 and $expires <= 604800
!ll&&!rl&&r
3141
$expires =~ /\A[0-9]+\z/u and $expires >= 1
!ll&&!rl&&r
1242
MC/DC100%3/3
$expires =~ /\A[0-9]+\z/u and $expires >= 1 and $expires <= 604800
$expires =~ /\A[0-9]+\z/u $expires >= 1 $expires <= 604800
Truth table100%4/4
A and B and C
A= $expires =~ /\A[0-9]+\z/uB= $expires >= 1C= $expires <= 604800
ABCresult
0XX0
10X0
1100
1111
576 unless $expires =~ m{\A[0-9]+\z} && $expires >= 1 && $expires <= 604800;
577  
578 41 ▶ for my $pair (split m{&}, $query // '') {
Condition100%2/2
$query // ""
l!l
1526
MC/DC100%1/1
$query // ""
$query
Truth table100%2/2
A
A= $query
Aresult
11
00
579 17 my ($name) = split m{=}, $pair, 2;
580 17 ▶ my $known = AUTH_PARAM->{lc _uri_decode($name // '')} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $known = {"x-amz-algorithm" => "X-Amz-Algorithm","x-amz-credential" => "X-Amz-Credential","x-amz-date" => "X-Amz-Date","x-amz-expires" => "X-Amz-Expires","x-amz-security-token" => "X-Amz-Security-Token","x-amz-signature" => "X-Amz-Signature","x-amz-signedheaders" => "X-Amz-SignedHeaders","x-amz-x509" => "X-Amz-X509","x-amz-x509-chain" => "X-Amz-X509-Chain"}->{lc _uri_decode($name // "")} or next 13 4 17
Condition50%1/2
$name // ""
l!l
170
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
581 13 fail 400, "url already carries $known: presign adds it";
582 }
583  
584 # S3 takes UNSIGNED-PAYLOAD for granted in presigned URLs, the others the
585 # hash of the body they get: anything else needs the header, signed
586 28 my $given = _given_payload_hash($h);
587 28 ▶ my $payload_hash = _agree(_payload_hash(\%args), $given,
Branch: 1 branch
BranchTrueFalseTotal
$self->payload_header ? : 10 17 27
588 $self->payload_header ? 'UNSIGNED-PAYLOAD' : sha256_hex(''));
589 27 ▶ my $taken_for_granted = $self->payload_header
Branch: 1 branch
BranchTrueFalseTotal
$self->payload_header ? : 10 17 27
590 ? $payload_hash eq 'UNSIGNED-PAYLOAD' : _is_sha256($payload_hash);
591 27 ▶ $h->{'x-amz-content-sha256'} = $payload_hash
Branch: 1 branch
BranchTrueFalseTotal
if defined $given or not $taken_for_granted 3 24 27
Condition66%2/3
defined $given or not $taken_for_granted
l!l&&r!l&&!r
0324
MC/DC50%1/2
defined $given or not $taken_for_granted
defined $given not $taken_for_granted
Truth table66%2/3
A or not(B)
A= defined $givenB= not $taken_for_granted
ABresult
1X1
011
000
592 if defined $given || !$taken_for_granted;
593  
594 27 ▶ my @signed = _signed_headers($h, $args{signed_headers} // []);
Condition100%2/2
$args{"signed_headers"} // []
l!l
324
MC/DC100%1/1
$args{"signed_headers"} // []
$args{"signed_headers"}
Truth table100%2/2
A
A= $args{"signed_headers"}
Aresult
11
00
595 26 _check_header_values($h);
596  
597 26 my @params = (
598 'X-Amz-Algorithm' => $self->algorithm,
599 'X-Amz-Credential' => $self->_auth->credential_id . "/$scope",
600 'X-Amz-Date' => $amzdate,
601 'X-Amz-Expires' => $expires,
602 'X-Amz-SignedHeaders' => join(';', @signed),
603 $self->_auth->extra_fields,
604 );
605 26 _check_query_values(@params);
606  
607 24 ▶ my @query = grep { length } $query // '';
Condition100%2/2
$query // ""
l!l
222
MC/DC100%1/1
$query // ""
$query
Truth table100%2/2
A
A= $query
Aresult
11
00
608 24 ▶ while (my ($k, $v) = splice @params, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@params, 0, 2)) 132 24 156
609 132 push @query, _uri_encode($k) . '=' . _uri_encode($v);
610 }
611 24 my $canonical_query = _canonical_query(join '&', @query);
612  
613 24 my $r = $self->_signed($method, $path, $canonical_query, $h,
614 \@signed, $payload_hash, $amzdate, $scope);
615  
616 # whatever is not allowed in a URL path (e.g. a space) is percent-encoded
617 24 my ($prefix) = $args{url} =~ m{\A([^?\#]*)};
618 24 $prefix =~ s{([^A-Za-z0-9\-._~!\$&'()*+,;=:@/%\[\]])}{sprintf '%%%02X', ord $1}ge;
619 24 $r->{url} = "$prefix?$canonical_query&X-Amz-Signature=$r->{signature}";
620 24 $r->{headers} = $h;
621 24 return $r;
622 }
623  
624 # pieces of the request that sign and presign have in common: the headers
625 # (with host), the path and query from the url, the date and the scope
626 435 ▶sub _request ($self, $args) {
Subroutine _request: called CC=8 SCAR=20.8
627 435 my ($host, $path, $query) = _split_url(_check_url($args->{url}));
628 414 my %h = _headers_hash($args->{headers});
629 400 ▶ $h{host} //= $host;
Condition100%3/3
$h{"host"} //= $host
l!l&&r!l&&!r
23962
MC/DC100%2/2
$h{"host"} //= $host
$h{"host"} $host
Truth table100%3/3
A //= B
A= $h{"host"}B= $host
ABresult
1X1
011
000
630 400 ▶ length($h{host} // '')
Branch: 1 branch
BranchTrueFalseTotal
length($h{"host"} // "") or fail(400, "url has no host: give an absolute url or a Host header") 398 2 400
Condition100%2/2
$h{"host"} // ""
l!l
3982
MC/DC100%1/1
$h{"host"} // ""
$h{"host"}
Truth table100%2/2
A
A= $h{"host"}
Aresult
11
00
631 or fail 400, 'url has no host: give an absolute url or a Host header';
632  
633 398 ▶ my $epoch = $args->{time} // time;
Condition66%2/3
$args->{"time"} // time
l!l&&r!l&&!r
349490
MC/DC0%0/2
$args->{"time"} // time
$args->{"time"} time
Truth table66%2/3
A // B
A= $args->{"time"}B= time
ABresult
1X1
011
000
634 398 ▶ fail 400, 'time must be an epoch in seconds, e.g. from time()'
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $epoch and $epoch =~ /\A[0-9]+(?:\.[0-9]*)?\z/u and $epoch <= '253402300799' 3 395 398
Condition83%5/6
not ref $epoch and $epoch =~ /\A[0-9]+(?:\.[0-9]*)?\z/u and $epoch <= '253402300799'
!ll&&!rl&&r
21395
not ref $epoch and $epoch =~ /\A[0-9]+(?:\.[0-9]*)?\z/u
!ll&&!rl&&r
02396
MC/DC66%2/3
not ref $epoch and $epoch =~ /\A[0-9]+(?:\.[0-9]*)?\z/u and $epoch <= '253402300799'
not ref $epoch $epoch =~ /\A[0-9]+(?:\.[0-9]*)?\z/u $epoch <= '253402300799'
Truth table75%3/4
not(A) and B and C
A= not ref $epochB= $epoch =~ /\A[0-9]+(?:\.[0-9]*)?\z/uC= $epoch <= '253402300799'
ABCresult
0XX0
10X0
1100
1111
635 unless !ref $epoch && $epoch =~ m{\A[0-9]+(?:\.[0-9]*)?\z} && $epoch <= MAX_EPOCH;
636 395 my $amzdate = strftime('%Y%m%dT%H%M%SZ', gmtime int $epoch);
637 395 my $scope = join '/', substr($amzdate, 0, 8), $self->region, $self->service, 'aws4_request';
638 395 return (\%h, $path, $query, $amzdate, $scope);
639 }
640  
641 # canonical request, string to sign and signature, as returned to the caller
642 340 ▶sub _signed ($self, $method, $path, $canonical_query, $h, $signed, $payload_hash, $amzdate, $scope) {
Subroutine _signed: called CC=1 SCAR=0.0
643 340 my $signed_headers = join ';', $signed->@*;
644 my $canonical_request = join "\n",
645 $method,
646 $self->_canonical_path($path),
647 $canonical_query,
648 340 join('', map { "$_:" . _trim($h->{$_}) . "\n" } $signed->@*), # ends with blank line
649 $signed_headers,
650 $payload_hash;
651 337 my $string_to_sign = join "\n",
652 $self->algorithm, $amzdate, $scope, sha256_hex($canonical_request);
653 return {
654 337 signature => $self->_auth->signature($scope, $string_to_sign),
655 signed_headers => $signed_headers,
656 scope => $scope,
657 canonical_request => $canonical_request,
658 string_to_sign => $string_to_sign,
659 };
660 }
661  
662 # size of an aws-chunked body: every chunk but the last one is $chunk_size
663 # signed => 1 (default) or 0, checksum => $algo, trailers => {name => length}
664 159 ▶sub encoded_length ($class, $decoded, $chunk_size, @args) {
Subroutine encoded_length: called CC=20 SCAR=30.0
Pod documented
665 158 ▶ fail 400, 'the decoded size must be a non-negative integer'
Branch: 1 branch
BranchTrueFalseTotal
unless _is_count($decoded) 3 155 158
666 unless _is_count($decoded);
667 155 ▶ fail 400, 'the chunk size must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless _is_count($chunk_size) and $chunk_size > 0 2 153 155
Condition100%3/3
_is_count($chunk_size) and $chunk_size > 0
!ll&&!rl&&r
11153
MC/DC100%2/2
_is_count($chunk_size) and $chunk_size > 0
_is_count($chunk_size) $chunk_size > 0
Truth table100%3/3
A and B
A= _is_count($chunk_size)B= $chunk_size > 0
ABresult
0X0
100
111
668 unless _is_count($chunk_size) && $chunk_size > 0;
669  
670 153 ▶ fail 400, 'encoded_length named options must be name => value pairs' if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 1 152 153
671 152 my %opts = _args(encoded_length => @args);
672  
673 151 ▶ my $signed = exists($opts{signed}) ? ($opts{signed} || 0) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'signed'} ? : 139 12 151
Condition100%2/2
$opts{"signed"} or 0
l!l
8455
MC/DC100%1/1
$opts{"signed"} or 0
$opts{"signed"}
Truth table100%2/2
A
A= $opts{"signed"}
Aresult
11
00
674 151 ▶ fail 400, 'signed must be false, 1, "signed", or "unsigned"'
Branch: 1 branch
BranchTrueFalseTotal
if ref $signed or not $signed =~ /\A(?:1|0|signed|unsigned|)\z/msux 1 150 151
Condition66%2/3
ref $signed or not $signed =~ /\A(?:1|0|signed|unsigned|)\z/msux
l!l&&r!l&&!r
01150
MC/DC50%1/2
ref $signed or not $signed =~ /\A(?:1|0|signed|unsigned|)\z/msux
ref $signed not $signed =~ /\A(?:1|0|signed|unsigned|)\z/msux
Truth table66%2/3
A or not(B)
A= ref $signedB= not $signed =~ /\A(?:1|0|signed|unsigned|)\z/msux
ABresult
1X1
011
000
675 if ref $signed || $signed !~ m{\A(?:1|0|signed|unsigned|)\z}mxs;
676 150 ▶ $signed = $signed eq '1' || $signed eq 'signed';
Condition100%3/3
$signed eq '1' || $signed eq 'signed'
l!l&&r!l&&!r
93156
MC/DC100%2/2
$signed eq '1' || $signed eq 'signed'
$signed eq '1' $signed eq 'signed'
Truth table100%3/3
A || B
A= $signed eq '1'B= $signed eq 'signed'
ABresult
1X1
011
000
677  
678 150 my $trailer = 0; # sum of length of each trailer line "name:value\r\n"
679 150 my %seen; # catch duplicates
680 150 ▶ if (defined(my $algo = $opts{checksum})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $algo = $opts{"checksum"})) 85 65 150
681 85 my ($name) = _trailer_names({checksum => $algo});
682 84 $seen{$name} = 1;
683 84 $trailer += length($name) + 1 + AWS::Signature::V4::Checksum->encoded_size($algo) + 2;
684 }
685 149 ▶ my $declared = $opts{trailers} // {};
Condition100%2/2
$opts{"trailers"} // {}
l!l
5990
MC/DC100%1/1
$opts{"trailers"} // {}
$opts{"trailers"}
Truth table100%2/2
A
A= $opts{"trailers"}
Aresult
11
00
686 149 ▶ fail 400, '"trailers" must be a hash reference from names to lengths'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $declared eq "HASH" 1 148 149
687 unless ref $declared eq 'HASH';
688 148 for my $given (sort keys $declared->%*) {
689 59 my $name = lc $given;
690 59 _check_trailer_name($name);
691 # sign() refuses the same repetition, so do not count a line twice
692 58 ▶ fail 400, "trailer '$name' declared twice" if $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}++ 2 56 58
693 fail 400, 'the length of trailer ' . shown($given) . ' must be a non-negative integer'
694 56 ▶ unless _is_count($declared->{$given});
Branch: 1 branch
BranchTrueFalseTotal
unless _is_count($declared->{$given}) 1 55 56
695 55 $trailer += length($name) + 1 + $declared->{$given} + 2;
696 }
697  
698 251 ▶ my $chunk = sub ($size) { # everything around the data of a chunk
Subroutine __ANON__: called CC=2 SCAR=6.9
699 251 ▶ length(sprintf '%x', $size) + ($signed ? 17 + 64 : 0) + 2 + 2;
Branch: 1 branch
BranchTrueFalseTotal
$signed ? : 153 98 251
700 144 };
701 144 my $total = 0;
702 144 my $full = int($decoded / $chunk_size);
703 144 $total += $full * ($chunk->($chunk_size) + $chunk_size);
704 144 my $rest = $decoded - $full * $chunk_size;
705 144 ▶ $total += $chunk->($rest) + $rest if $rest;
Branch: 1 branch
BranchTrueFalseTotal
if $rest 107 37 144
706  
707 # final chunk: "0[;chunk-signature=..]\r\n", trailers, [signature line,] "\r\n"
708 144 ▶ $total += 1 + ($signed ? 17 + 64 : 0) + 2 + $trailer + 2;
Branch: 1 branch
BranchTrueFalseTotal
$signed ? : 88 56 144
709 144 ▶ $total += length('x-amz-trailer-signature') + 1 + 64 + 2 if $signed && $trailer;
Branch: 1 branch
BranchTrueFalseTotal
if $signed and $trailer 54 90 144
Condition100%3/3
$signed and $trailer
!ll&&!rl&&r
563454
MC/DC100%2/2
$signed and $trailer
$signed $trailer
Truth table100%3/3
A and B
A= $signedB= $trailer
ABresult
0X0
100
111
710 144 return $total;
711 }
712  
713 # the path is empty or starts with "/", see _split_url
714 340 ▶sub _canonical_path ($self, $path) {
Subroutine _canonical_path: called CC=14 SCAR=26.4
715 340 ▶ $path = '/' unless length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 3 337 340
716 340 ▶ my $trailing = $path =~ m{/\z} && $path ne '/';
Condition100%3/3
$path =~ m[/\z]u && $path ne '/'
!ll&&!rl&&r
2171167
MC/DC100%2/2
$path =~ m[/\z]u && $path ne '/'
$path =~ m[/\z]u $path ne '/'
Truth table100%3/3
A && B
A= $path =~ m[/\z]uB= $path ne '/'
ABresult
0X0
100
111
717 340 my @parts = split m{/}, $path, -1;
718 340 shift @parts; # leading empty item, before the first /
719 340 ▶ pop @parts if @parts && $parts[-1] eq ''; # trailing, re-added below
Branch: 1 branch
BranchTrueFalseTotal
if @parts and $parts[-1] eq "" 123 217 340
Condition66%2/3
@parts and $parts[-1] eq ""
!ll&&!rl&&r
0217123
MC/DC50%1/2
@parts and $parts[-1] eq ""
@parts $parts[-1] eq ""
Truth table66%2/3
A and B
A= @partsB= $parts[-1] eq ""
ABresult
0X0
100
111
720  
721 340 ▶ if ($self->normalize_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->normalize_path) 136 204 340
722 136 my @out;
723 136 for my $p (@parts) {
724 48 ▶ next if $p eq '' || $p eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $p eq "" or $p eq "." 5 43 48
Condition100%3/3
$p eq "" or $p eq "."
l!l&&r!l&&!r
2343
MC/DC100%2/2
$p eq "" or $p eq "."
$p eq "" $p eq "."
Truth table100%3/3
A or B
A= $p eq ""B= $p eq "."
ABresult
1X1
011
000
725 43 ▶ if ($p eq '..') { pop @out; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($p eq "..") 4 39 43
726 # AWS might or might not take them as dot segments: refuse them
727 39 ▶ fail 400, 'url path has a percent-encoded dot segment ' . shown($p)
Branch: 1 branch
BranchTrueFalseTotal
if _uri_decode($p) =~ /\A\.\.?\z/u 3 36 39
728 . ': write it as it is, or remove it'
729 if _uri_decode($p) =~ m{\A\.\.?\z};
730 36 push @out, $p;
731 }
732 133 @parts = @out;
733 }
734  
735 # incoming path is percent-encoded already: decode then encode once,
736 # and a second time for everything but S3
737 @parts = map {
738 337 my $e = _uri_encode(_uri_decode($_));
739 241 ▶ $e = _uri_encode($e) if $self->double_encode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->double_encode 30 211 241
740 241 $e;
741 } @parts;
742 337 my $out = '/' . join '/', @parts;
743 337 ▶ $out .= '/' if $trailing && @parts;
Branch: 1 branch
BranchTrueFalseTotal
if $trailing and @parts 6 331 337
Condition100%3/3
$trailing and @parts
!ll&&!rl&&r
33016
MC/DC100%2/2
$trailing and @parts
$trailing @parts
Truth table100%3/3
A and B
A= $trailingB= @parts
ABresult
0X0
100
111
744 337 return $out;
745 }
746  
747 9 1;
« lib/AWS/Signature/V4/Checksum.pm ↑ Summary blib/lib/AWS/Signature/V4/X509.pm »