SDK/Storage/Blobs/Uri.pm

filter: Statementstmt 100% 122 / 122 Branchbran 76.4% 52 / 68 Conditioncond 58.5% 24 / 41 MC/DCmcdc 34.6% 9 / 26 Subroutinesub 100% 23 / 23 Podpod 100% 7 / 7 totaltotal 82.5% 237 / 287 CC 56 SCAR 19.5
CC
56
Cov
86%
_normalize_endpoint
12.4
_validate_blob_name
10.3
_validate_query
9.8
CRAP 7.0
« SDK/Storage/Queues/Message.pm ↑ Summary SDK/Storage/Tables/Uri.pm »
1 3 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 3 class Azure::SDK::Storage::Blobs::Uri {
6 3 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶ use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 # Azure Blob service limits.
10 3 ▶ use constant MAX_BLOB_NAME_LENGTH => 1024;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶ use constant MAX_BLOB_NAME_SEGMENTS => 254;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 3 field $endpoint :param :reader = undef;
14
15 # Derived, never accepted: a caller-supplied path could sign one resource
16 # while the request line asked for another.
17 3 field $endpoint_path :reader = undef;
18
19 48 ▶ ADJUST {
Subroutine __ANON__: called CC=3 SCAR=13.1
20 48 ▶ croak 'Azure::SDK::Storage::Blobs::Uri requires an endpoint'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $endpoint and length $endpoint 0 48 48
Condition33%1/3
defined $endpoint and length $endpoint
!ll&&!rl&&r
0048
MC/DC0%0/2
defined $endpoint and length $endpoint
defined $endpoint length $endpoint
Truth table33%1/3
A and B
A= defined $endpointB= length $endpoint
ABresult
0X0
100
111
21 unless defined $endpoint && length $endpoint;
22
23 48 ( $endpoint, $endpoint_path ) = _normalize_endpoint($endpoint);
24 }
25
26 18 ▶ sub from_connection_string ( $class, $connection ) {
Subroutine from_connection_string: called CC=1 SCAR=0.0
Pod documented
27 18 return $class->new( endpoint => $connection->blob_endpoint );
28 }
29
30 # Shared with ConnectionString so direct construction, the documented
31 # configuration-independent path, is not the one that skips validation.
32 33 ▶ sub normalize_endpoint ( $class, $value ) {
Subroutine normalize_endpoint: called CC=1 SCAR=0.0
Pod documented
33 33 return _normalize_endpoint($value);
34 }
35
36 # Shared with Serializer so Encoded="true" blob names decode by these rules
37 # rather than a looser copy of them.
38 5 ▶ sub decode_component ( $class, $value ) {
Subroutine decode_component: called CC=1 SCAR=0.0
Pod documented
39 5 return _decode_segment($value);
40 }
41
42 2 ▶ method for_service ( $query = {} ) {
Subroutine for_service: called CC=2 SCAR=6.9
Pod documented
43 2 return $self->_build( [], $query );
44 }
45
46 24 ▶ method for_container ( $container, $query = {} ) {
Subroutine for_container: called CC=2 SCAR=6.9
Pod documented
47 24 return $self->_build( [ _validate_container_name($container) ], $query );
48 }
49
50 41 ▶ method for_blob ( $container, $blob, $query = {} ) {
Subroutine for_blob: called CC=2 SCAR=6.9
Pod documented
51 41 my $name = _validate_container_name($container);
52 41 return $self->_build( [ $name, _validate_blob_name($blob) ], $query );
53 }
54
55 # Instance method on purpose. .NET's BlobUriBuilder and Python's
56 # StorageAccountHostsMixin have to guess whether a URL is path-style or
57 # host-style; knowing the endpoint means comparing instead of guessing, which
58 # also makes a foreign URL a rejection rather than a parse (CWE-918).
59 21 ▶ method parse ($url) {
Subroutine parse: called CC=8 SCAR=21.2
Pod documented
60 21 ▶ croak 'A URL is required'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 21 21
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $url and length $url
defined $url length $url
Truth table33%1/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
61 unless defined $url && length $url;
62 21 ▶ croak 'URL must not contain a fragment'
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /#/u 0 21 21
63 if $url =~ /#/;
64
65 21 my ( $base, $query_string ) = split /\?/, $url, 2;
66
67 21 ▶ croak 'URL does not belong to this endpoint'
Branch: 1 branch
BranchTrueFalseTotal
unless $base eq $endpoint or index($base, $endpoint . '/') == 0 5 16 21
Condition66%2/3
$base eq $endpoint or index($base, $endpoint . '/') == 0
l!l&&r!l&&!r
0165
MC/DC50%1/2
$base eq $endpoint or index($base, $endpoint . '/') == 0
$base eq $endpoint index($base, $endpoint . '/') == 0
Truth table66%2/3
A or B
A= $base eq $endpointB= index($base, $endpoint . '/') == 0
ABresult
1X1
011
000
68 unless $base eq $endpoint || index( $base, $endpoint . '/' ) == 0;
69
70 16 my $path = substr( $base, length $endpoint );
71 16 $path =~ s{\A/}{};
72
73 16 my ( $container, $blob );
74 16 ▶ if ( length $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $path) 15 1 16
75 # Only the first separator is structural.
76 15 my ( $container_part, $blob_part ) = split m{/}, $path, 2;
77 15 $container = _decode_segment($container_part);
78 15 ▶ $blob = join '/', map { _decode_segment($_) } split m{/}, $blob_part, -1
Branch: 1 branch
BranchTrueFalseTotal
if defined $blob_part 13 2 15
79 if defined $blob_part;
80 }
81
82 return {
83 14 container => $container,
84 blob => $blob,
85 query => _parse_query($query_string),
86 };
87 }
88
89 # Returns a hashref with everything both the transport and the shared-key
90 # signer need: url, canonical_path, canonical_query.
91 60 ▶ method _build ( $segments, $query ) {
Subroutine _build: called CC=4 SCAR=13.9
92 60 my %normalized = _validate_query($query);
93
94 # "/" is ordinary in a blob name but a separator in the request line, so
95 # split before encoding.
96 59 my @parts = map { split m{/}, $_, -1 } @{$segments};
97
98 my $suffix = @parts
99 59 ▶ ? '/' . join( '/', map { _encode_segment($_) } @parts )
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 57 2 59
100 : '/';
101
102 # Shared Key canonicalizes the *encoded* path but the *decoded* query.
103 # Signing the decoded path 403s every non-ASCII blob name.
104 59 my $canonical_path = $endpoint_path . $suffix;
105
106 # The service hashes UTF-8 octets, so "å" must contribute two bytes.
107 59 my %canonical_query;
108 59 for my $name ( keys %normalized ) {
109 46 my $value = $normalized{$name};
110 46 utf8::encode($value);
111 46 $canonical_query{$name} = $value;
112 }
113
114 59 my $url = $endpoint . $suffix;
115
116 59 ▶ if (%normalized) {
Branch: 1 branch
BranchTrueFalseTotal
if (%normalized) 18 41 59
117 $url .= '?' . join( '&',
118 18 map { _encode_segment($_) . '=' . _encode_segment( $normalized{$_} ) }
119 sort keys %normalized );
120 }
121
122 return {
123 59 url => $url,
124 canonical_path => $canonical_path,
125 canonical_query => \%canonical_query,
126 };
127 }
128
129 # Escapes everything outside the RFC 3986 unreserved set, so a caller cannot
130 # smuggle a separator or delimiter into the request line (CWE-93, CWE-88).
131 #
132 # Encoding to UTF-8 is unconditional on purpose: Perl's UTF-8 flag does not
133 # distinguish a character string from a byte string, so branching on it would
134 # encode "\x{e5}" as %E5 instead of %C3%A5 and produce an unsigned URL.
135 229 ▶ sub _encode_segment ($value) {
Subroutine _encode_segment: called CC=1 SCAR=0.0
136 229 my $bytes = $value;
137 229 utf8::encode($bytes);
138 229 $bytes =~ s/([^A-Za-z0-9\-\._~])/sprintf( '%%%02X', ord $1 )/ge;
139 229 return $bytes;
140 }
141
142 # The inverse of _encode_segment.
143 58 ▶ sub _decode_segment ($value) {
Subroutine _decode_segment: called CC=3 SCAR=11.0
144 58 ▶ croak 'Value contains a malformed percent-escape'
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /%(?![0-9A-Fa-f]{2})/u 2 56 58
145 if $value =~ /%(?![0-9A-Fa-f]{2})/;
146
147 56 my $bytes = $value;
148 56 $bytes =~ s/%([0-9A-Fa-f]{2})/chr hex $1/ge;
149
150 56 ▶ croak 'Value contains invalid UTF-8'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::decode($bytes) 2 54 56
151 unless utf8::decode($bytes);
152
153 54 return $bytes;
154 }
155
156 # Everything rejected here would otherwise be spliced into a request line: a
157 # query or fragment would precede the container and blob segments, userinfo
158 # would leak credentials into the target, and a relative segment would let
159 # the signed and resolved paths diverge (CWE-88, CWE-22).
160 81 ▶ sub _normalize_endpoint ($value) {
Subroutine _normalize_endpoint: called CC=12 SCAR=25.1
161 81 ▶ croak 'Blob endpoint is required'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 81 81
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
0081
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
162 unless defined $value && length $value;
163
164 81 my $uri = URI->new($value);
165
166 81 ▶ croak 'Blob endpoint must be an absolute http or https URI'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/u 3 78 81
Condition100%3/3
defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/u
!ll&&!rl&&r
1278
MC/DC100%2/2
defined $uri->scheme and $uri->scheme =~ /\Ahttps?\z/u
defined $uri->scheme $uri->scheme =~ /\Ahttps?\z/u
Truth table100%3/3
A and B
A= defined $uri->schemeB= $uri->scheme =~ /\Ahttps?\z/u
ABresult
0X0
100
111
167 unless defined $uri->scheme && $uri->scheme =~ /\Ahttps?\z/;
168 78 ▶ croak 'Blob endpoint must include a host'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri->host and length $uri->host 1 77 78
Condition66%2/3
defined $uri->host and length $uri->host
!ll&&!rl&&r
0177
MC/DC50%1/2
defined $uri->host and length $uri->host
defined $uri->host length $uri->host
Truth table66%2/3
A and B
A= defined $uri->hostB= length $uri->host
ABresult
0X0
100
111
169 unless defined $uri->host && length $uri->host;
170 77 ▶ croak 'Blob endpoint must not include userinfo'
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->userinfo 3 74 77
171 if defined $uri->userinfo;
172 74 ▶ croak 'Blob endpoint must not include a query string'
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->query 3 71 74
173 if defined $uri->query;
174 71 ▶ croak 'Blob endpoint must not include a fragment'
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->fragment 2 69 71
175 if defined $uri->fragment;
176
177 69 ▶ my $path = $uri->path // '';
Condition50%1/2
$uri->path // ""
l!l
690
MC/DC0%0/1
$uri->path // ""
$uri->path
Truth table50%1/2
A
A= $uri->path
Aresult
11
00
178 69 $path =~ s{/+\z}{};
179
180 # Percent-encoding is passed through, matching .NET, Python, and Java,
181 # all of which preserve it byte-exactly in the endpoint prefix. A
182 # relative segment is not: no two reference SDKs agree on what it means,
183 # and .NET silently retargets the account when it sees one.
184 69 ▶ croak 'Blob endpoint path must not contain relative segments'
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[(?:\A|/)\.\.?(?:/|\z)]u 2 67 69
185 if $path =~ m{(?:\A|/)\.\.?(?:/|\z)};
186
187 # authority keeps the port exactly as configured; host_port would append
188 # the scheme default port and change the endpoint the caller supplied.
189 67 my $authority = $uri->scheme . '://' . $uri->authority;
190 67 return ( $authority . $path, $path );
191 }
192
193 14 ▶ sub _parse_query ($query_string) {
Subroutine _parse_query: called CC=7 SCAR=20.0
194 14 my %query;
195 14 ▶ return \%query
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query_string and length $query_string 13 1 14
Condition66%2/3
defined $query_string and length $query_string
!ll&&!rl&&r
1301
MC/DC50%1/2
defined $query_string and length $query_string
defined $query_string length $query_string
Truth table66%2/3
A and B
A= defined $query_stringB= length $query_string
ABresult
0X0
100
111
196 unless defined $query_string && length $query_string;
197
198 1 for my $pair ( split /&/, $query_string, -1 ) {
199 3 ▶ next unless length $pair;
Branch: 1 branch
BranchTrueFalseTotal
unless length $pair 0 3 3
200
201 3 my ( $name, $value ) = split /=/, $pair, 2;
202 3 my $key = lc _decode_segment($name);
203
204 croak "Query parameter '$key' is repeated"
205 3 ▶ if exists $query{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $query{$key} 0 3 3
206
207 3 ▶ $query{$key} = defined $value ? _decode_segment($value) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 3 0 3
208 }
209
210 1 return \%query;
211 }
212
213 65 ▶ sub _validate_container_name ($name) {
Subroutine _validate_container_name: called CC=6 SCAR=18.8
214 65 ▶ croak 'Container name is required'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 65 65
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
0065
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
215 unless defined $name && length $name;
216 65 ▶ croak 'Container name must be 3-63 characters of lowercase letters, '
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/u and length $name >= 3 and length $name <= 63 5 60 65
Condition83%5/6
$name =~ /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/u and length $name >= 3 and length $name <= 63
!ll&&!rl&&r
5060
$name =~ /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/u and length $name >= 3
!ll&&!rl&&r
4160
MC/DC66%2/3
$name =~ /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/u and length $name >= 3 and length $name <= 63
$name =~ /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/u length $name >= 3 length $name <= 63
Truth table75%3/4
A and B and C
A= $name =~ /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/uB= length $name >= 3C= length $name <= 63
ABCresult
0XX0
10X0
1100
1111
217 . 'digits, and single hyphens, starting and ending alphanumerically'
218 unless $name =~ /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/
219 && length($name) >= 3
220 && length($name) <= 63;
221
222 60 return $name;
223 }
224
225 41 ▶ sub _validate_blob_name ($name) {
Subroutine _validate_blob_name: called CC=9 SCAR=23.3
226 41 ▶ croak 'Blob name is required'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 41 41
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
0041
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
227 unless defined $name && length $name;
228 41 ▶ croak 'Blob name exceeds ' . MAX_BLOB_NAME_LENGTH . ' characters'
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 1024 0 41 41
229 if length $name > MAX_BLOB_NAME_LENGTH;
230
231 # Azurite 500s a whole container listing on an XML-illegal name, and
232 # control characters would forge log lines (CWE-117).
233 41 ▶ croak 'Blob name must not contain control characters'
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /[\x00-\x1f\x7f]/u 1 40 41
234 if $name =~ /[\x00-\x1f\x7f]/;
235
236 # Leading, trailing, and repeated "/" are deliberately allowed: .NET
237 # ships TrimBlobNameSlashes because such names occur, and its suite
238 # round-trips "///blob///client///". Rejecting them would strand blobs.
239 40 my @segments = split m{/}, $name, -1;
240 40 ▶ croak 'Blob name exceeds ' . MAX_BLOB_NAME_SEGMENTS . ' path segments'
Branch: 1 branch
BranchTrueFalseTotal
if @segments > 254 0 40 40
241 if @segments > MAX_BLOB_NAME_SEGMENTS;
242
243 # "." and ".." are the exception: joined with a literal "/", an
244 # intermediary could resolve them to a different blob (CWE-22).
245 40 for my $segment (@segments) {
246 81 ▶ croak 'Blob name must not contain "." or ".." path segments'
Branch: 1 branch
BranchTrueFalseTotal
if $segment eq "." or $segment eq ".." 1 80 81
Condition66%2/3
$segment eq "." or $segment eq ".."
l!l&&r!l&&!r
0180
MC/DC50%1/2
$segment eq "." or $segment eq ".."
$segment eq "." $segment eq ".."
Truth table66%2/3
A or B
A= $segment eq "."B= $segment eq ".."
ABresult
1X1
011
000
247 if $segment eq '.' || $segment eq '..';
248 }
249
250 39 return $name;
251 }
252
253 60 ▶ sub _validate_query ($query) {
Subroutine _validate_query: called CC=9 SCAR=22.8
254 60 ▶ return () unless defined $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query 0 60 60
255 60 ▶ croak 'Query parameters must be a hash reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $query eq "HASH" 0 60 60
256 unless ref $query eq 'HASH';
257
258 60 my %normalized;
259 60 for my $name ( keys %{$query} ) {
260 47 ▶ croak 'Query parameter names must be alphanumeric, "-", "_", or "."'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name =~ /\A[A-Za-z0-9_.\-]+\z/u 1 46 47
Condition66%2/3
defined $name and $name =~ /\A[A-Za-z0-9_.\-]+\z/u
!ll&&!rl&&r
0146
MC/DC50%1/2
defined $name and $name =~ /\A[A-Za-z0-9_.\-]+\z/u
defined $name $name =~ /\A[A-Za-z0-9_.\-]+\z/u
Truth table66%2/3
A and B
A= defined $nameB= $name =~ /\A[A-Za-z0-9_.\-]+\z/u
ABresult
0X0
100
111
261 unless defined $name && $name =~ /\A[A-Za-z0-9_.\-]+\z/;
262
263 46 my $key = lc $name;
264 croak "Query parameter '$name' is repeated"
265 46 ▶ if exists $normalized{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $normalized{$key} 0 46 46
266
267 46 my $value = $query->{$name};
268 46 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 46 46
269
270 46 ▶ croak "Query parameter '$name' must not contain control characters"
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[\x00-\x1f\x7f]/u 0 46 46
271 if $value =~ /[\x00-\x1f\x7f]/;
272
273 46 $normalized{$key} = "$value";
274 }
275
276 59 return %normalized;
277 }
278 }
279
280 3 1;
281
282 __END__
« SDK/Storage/Queues/Message.pm ↑ Summary SDK/Storage/Tables/Uri.pm »