lib/AWS/Signature/V4/Chunker.pm

filter: Statementstmt 100% 91 / 91 Branchbran 97.6% 41 / 42 Conditioncond 88.4% 23 / 26 MC/DCmcdc 68.7% 11 / 16 Subroutinesub 100% 16 / 16 Podpod 66.6% 2 / 3 totaltotal 94.8% 184 / 194 CC 33 SCAR 22.9
CC
33
Cov
97%
finish
18.0
chunk
11.0
BUILD
3.0
CRAP 9.9
« blib/lib/AWS/Signature/V4/X509.pm ↑ Summary blib/lib/AWS/Signature/V4/Checksum.pm »
1 package AWS::Signature::V4::Chunker;
2 9 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use AWS::Signature::V4::Error qw< fail shown >;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Digest::SHA qw< sha256_hex hmac_sha256_hex >;
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 namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use AWS::Signature::V4::Checksum ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # The chunker is created by AWS::Signature::V4::sign(), which knows all of
12 # this: the arguments are not a public interface.
13 9 has signed => (is => 'ro', required => 1);
14 9 has amzdate => (is => 'ro');
15 9 has scope => (is => 'ro');
16  
17 # The signing key, for signed chunks only, can sign any request for the same
18 # service, region and day: it is kept in a closure, so that no accessor gives
19 # it back and dumping the object does not show it. The closure can still sign
20 # with it, so the chunker is no safer to share than the signer.
21 has _mac => (
22 is => 'ro',
23 init_arg => 'key',
24 100 ▶ coerce => sub ($key) {
Subroutine __ANON__: called CC=2 SCAR=7.2
25 100 ▶ ref $key eq 'CODE' ? $key : sub ($data) { hmac_sha256_hex($data, $key) };
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
ref $key eq 'CODE' ? : 0 100 100
26 },
27 9 );
28  
29 # signature of the previous chunk: the chain must not be changed from outside
30 9 has _previous => (is => 'rw', init_arg => 'previous');
31 9 has expected => (is => 'ro', required => 1); # declared size of the data
32 9 has checksum => (is => 'ro'); # name of the built-in checksum, if any
33 1 ▶has trailer_names => (is => 'ro', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
34  
35 9 has _seen => (is => 'rw', init_arg => undef, default => 0);
36 9 has _done => (is => 'rw', init_arg => undef, default => 0);
37 9 has _sum => (is => 'lazy', init_arg => undef);
38  
39 175 ▶sub BUILD ($self, $args) {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
40 175 ▶ fail 500, 'signed chunks need a "key"' if $self->signed && !$self->_mac;
Branch: 1 branch
BranchTrueFalseTotal
if $self->signed and not $self->_mac 1 174 175
Condition100%3/3
$self->signed and not $self->_mac
!ll&&!rl&&r
741001
MC/DC100%2/2
$self->signed and not $self->_mac
$self->signed not $self->_mac
Truth table100%3/3
A and not(B)
A= $self->signedB= not $self->_mac
ABresult
0X0
100
111
41 174 return;
42 }
43  
44 166 ▶sub _build__sum ($self) {
Subroutine _build__sum: called CC=2 SCAR=6.9
45 166 my $algo = $self->checksum;
46 166 ▶ return defined $algo ? AWS::Signature::V4::Checksum->new($algo) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $algo ? : 106 60 166
47 }
48  
49 1333 ▶sub _sign ($self, $data_ref) {
Subroutine _sign: called CC=1 SCAR=0.0
50 1333 my $string_to_sign = join "\n", 'AWS4-HMAC-SHA256-PAYLOAD',
51 $self->amzdate, $self->scope, $self->_previous,
52 sha256_hex(''), sha256_hex($$data_ref);
53 1333 return $self->_previous($self->_mac->($string_to_sign));
54 }
55  
56 # encoded chunk for the data (a byte string or a reference to one)
57 2093 ▶sub chunk ($self, $data) {
Subroutine chunk: called CC=11 SCAR=24.0
Pod documented
58 2093 ▶ fail 400, 'chunker already finished' if $self->_done;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_done 1 2092 2093
59 2092 ▶ fail 400, 'chunk must be a byte string or a reference to one'
Branch: 1 branch
BranchTrueFalseTotal
if ref $data and ref $data ne "SCALAR" 2 2090 2092
Condition100%3/3
ref $data and ref $data ne "SCALAR"
!ll&&!rl&&r
208822
MC/DC100%2/2
ref $data and ref $data ne "SCALAR"
ref $data ref $data ne "SCALAR"
Truth table100%3/3
A and B
A= ref $dataB= ref $data ne "SCALAR"
ABresult
0X0
100
111
60 if ref $data && ref $data ne 'SCALAR';
61 2090 ▶ my $ref = ref $data ? $data : \$data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 2 2088 2090
62 2090 ▶ my $len = length($$ref) // 0;
Condition50%1/2
length $$ref // 0
l!l
20900
MC/DC0%0/1
length $$ref // 0
length $$ref
Truth table50%1/2
A
A= length $$ref
Aresult
11
00
63 2090 ▶ fail 400, 'empty chunk: call finish() to terminate the stream' unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 1 2089 2090
64 2089 ▶ _is_bytes($ref) or fail 400, 'chunk must be a byte string: encode characters first';
Branch: 1 branch
BranchTrueFalseTotal
_is_bytes($ref) or fail(400, "chunk must be a byte string: encode characters first") 2087 2 2089
65 2087 my $seen = $self->_seen + $len;
66 2087 ▶ fail 400, "more data than the declared @{[ $self->expected ]} bytes"
Branch: 1 branch
BranchTrueFalseTotal
if $seen > $self->expected 2 2085 2087
67 if $seen > $self->expected;
68 2085 $self->_seen($seen);
69 2085 ▶ $self->_sum->add($ref) if $self->_sum;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_sum 1260 825 2085
70 2085 ▶ my $head = $self->signed
Branch: 1 branch
BranchTrueFalseTotal
$self->signed ? : 1241 844 2085
71 ? sprintf('%x;chunk-signature=%s', $len, $self->_sign($ref))
72 : sprintf('%x', $len);
73 2085 return "$head\r\n" . $$ref . "\r\n";
74 }
75  
76 # the final, empty chunk, followed by the trailers if any; the values of the
77 # trailers that were declared with "trailers" are passed by name, in any case
78 178 ▶sub finish ($self, %given) {
Subroutine finish: called CC=18 SCAR=28.9
Pod documented
79 178 ▶ fail 400, 'chunker already finished' if $self->_done;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_done 2 176 178
80 176 ▶ fail 400, "got @{[ $self->_seen ]} bytes instead of the declared @{[ $self->expected ]}"
Branch: 1 branch
BranchTrueFalseTotal
if $self->_seen != $self->expected 1 175 176
81 if $self->_seen != $self->expected;
82 175 my %values;
83 175 for my $name (sort keys %given) {
84 86 ▶ fail 400, "trailer '" . shown(lc $name) . "' given twice" if exists $values{lc $name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $values{lc $name} 1 85 86
85 85 $values{lc $name} = $given{$name};
86 }
87  
88 # all checks come before the checksum is taken, which is not repeatable,
89 # and before the chain of signatures moves on: after an error, the chunker
90 # is as it was
91 174 ▶ my $computed = $self->_sum ? 'x-amz-checksum-' . $self->checksum : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_sum ? : 116 58 174
92 174 my @pairs;
93 174 for my $name ($self->trailer_names->@*) {
94 199 my $value;
95 199 ▶ if (defined $computed && $name eq $computed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $computed and $name eq $computed) { } 116 83 199
Condition100%3/3
defined $computed and $name eq $computed
!ll&&!rl&&r
2855116
MC/DC100%2/2
defined $computed and $name eq $computed
defined $computed $name eq $computed
Truth table100%3/3
A and B
A= defined $computedB= $name eq $computed
ABresult
0X0
100
111
96 116 ▶ fail 400, "trailer '$name' is computed, not to be given" if exists $values{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $values{$name} 1 115 116
97 }
98 else {
99 83 ▶ $value = delete $values{$name} // fail 400, "missing value for trailer '$name'";
Condition66%2/3
delete $values{$name} // fail(400, "missing value for trailer '${name}'")
l!l&&r!l&&!r
8120
MC/DC0%0/2
delete $values{$name} // fail(400, "missing value for trailer '${name}'")
delete $values{$name} fail(400, "missing value for trailer '${name}'")
Truth table33%1/3
A // B
A= delete $values{$name}B= fail(400, "missing value for trailer '${name}'")
ABresult
1X1
011
000
100 81 ▶ fail 400, "invalid value for trailer '$name': a byte string, no CR, LF or NUL"
Branch: 1 branch
BranchTrueFalseTotal
if ref $value or $value =~ /[\r\n\0]/u or not _is_bytes(\$value) 10 71 81
Condition100%6/6
ref $value or $value =~ /[\r\n\0]/u or not _is_bytes(\$value)
l!l&&r!l&&!r
8271
ref $value or $value =~ /[\r\n\0]/u
l!l&&r!l&&!r
2673
MC/DC100%3/3
ref $value or $value =~ /[\r\n\0]/u or not _is_bytes(\$value)
ref $value $value =~ /[\r\n\0]/u not _is_bytes(\$value)
Truth table100%4/4
A or B or not(C)
A= ref $valueB= $value =~ /[\r\n\0]/uC= not _is_bytes(\$value)
ABCresult
1XX1
01X1
0011
0000
101 if ref $value || $value =~ m{[\r\n\0]} || !_is_bytes(\$value);
102 71 utf8::downgrade($value);
103 }
104 186 push @pairs, [$name, $value];
105 }
106 161 ▶ fail 400, 'undeclared trailers: ' . join(', ', map { shown($_) } sort keys %values)
Branch: 1 branch
BranchTrueFalseTotal
if %values 1 160 161
107 if %values;
108 160 ▶ my @lines = map { "$_->[0]:" . ($_->[1] // $self->_sum->base64) } @pairs;
Condition66%2/3
$_->[1] // $self->_sum->base64
l!l&&r!l&&!r
701020
MC/DC0%0/2
$_->[1] // $self->_sum->base64
$_->[1] $self->_sum->base64
Truth table66%2/3
A // B
A= $_->[1]B= $self->_sum->base64
ABresult
1X1
011
000
109 160 my $trailers = join '', map { "$_\r\n" } @lines;
110  
111 160 my $body;
112 160 ▶ if (!$self->signed) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->signed) { } 68 92 160
elsif (not @lines) { } 30 62 92
113 68 $body = "0\r\n$trailers\r\n";
114 }
115 elsif (!@lines) {
116 30 $body = "0;chunk-signature=@{[ $self->_sign(\'') ]}\r\n\r\n";
117 }
118 else {
119 62 my $sig = $self->_sign(\'');
120 my $string_to_sign = join "\n", 'AWS4-HMAC-SHA256-TRAILER',
121 $self->amzdate, $self->scope, $self->_previous,
122 62 sha256_hex(join '', map { "$_\n" } @lines);
123 62 my $tsig = $self->_mac->($string_to_sign);
124 62 $body = "0;chunk-signature=$sig\r\n${trailers}x-amz-trailer-signature:$tsig\r\n\r\n";
125 }
126 160 $self->_done(1);
127 160 return $body;
128 }
129  
130 # true if the string holds only bytes, even if it is stored as UTF-8; it is
131 # passed by reference, so that big chunks are copied only when they are UTF-8
132 2162 ▶sub _is_bytes ($ref) {
Subroutine _is_bytes: called CC=2 SCAR=6.9
133 2162 ▶ return !utf8::is_utf8($$ref) || utf8::downgrade(my $copy = $$ref, 1);
Condition100%3/3
!utf8::is_utf8($$ref) || utf8::downgrade(my $copy = $$ref, 1)
l!l&&r!l&&!r
215714
MC/DC100%2/2
!utf8::is_utf8($$ref) || utf8::downgrade(my $copy = $$ref, 1)
!utf8::is_utf8($$ref) utf8::downgrade(my $copy = $$ref, 1)
Truth table100%3/3
not(A) || B
A= !utf8::is_utf8($$ref)B= utf8::downgrade(my $copy = $$ref, 1)
ABresult
1X1
011
000
134 }
135  
136 9 1;
« blib/lib/AWS/Signature/V4/X509.pm ↑ Summary blib/lib/AWS/Signature/V4/Checksum.pm »