| 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::Serializer { | |||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use bytes (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use XML::LibXML (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Azure::SDK::Storage::Blobs::BlobItem; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Azure::SDK::Storage::Blobs::BlobPage; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Azure::SDK::Storage::Blobs::Uri; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # Bounds parser memory on a hostile or corrupted body (CWE-770, CWE-409). | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use constant DEFAULT_MAX_BYTES => 8 * 1024 * 1024; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | field $max_bytes :param :reader = DEFAULT_MAX_BYTES; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | field $parser = undef; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 30 | ▶ | ADJUST { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 30 | ▶ | croak 'Azure::SDK::Storage::Blobs::Serializer max_bytes must be a positive integer' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $max_bytes and $max_bytes =~ /\A[0-9]+\z/u and $max_bytes > 0
defined $max_bytes and $max_bytes =~ /\A[0-9]+\z/u
MC/DC33%1/3
defined $max_bytes and $max_bytes =~ /\A[0-9]+\z/u and $max_bytes > 0
defined $max_bytes
$max_bytes =~ /\A[0-9]+\z/u
$max_bytes > 0
Truth table50%2/4
A and B and C
A= defined $max_bytesB= $max_bytes =~ /\A[0-9]+\z/uC= $max_bytes > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | unless defined $max_bytes && $max_bytes =~ /\A[0-9]+\z/ && $max_bytes > 0; | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 29 | $parser = XML::LibXML->new( | |||||||||||||||||||||||||||||||||||||||||
| 25 | # No network fetches, no external DTD, no entity expansion | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # (CWE-611, CWE-776). | ||||||||||||||||||||||||||||||||||||||||||
| 27 | no_network => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | load_ext_dtd => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | expand_entities => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | huge => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | recover => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | no_blanks => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | $parser->set_option( ext_ent_handler => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 35 | croak 'Refusing to resolve an external entity in a Blob response'; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 29 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 21 | ▶ | method parse_blob_list ($xml) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_blob_list: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 21 | my $document = $self->_parse($xml); | |||||||||||||||||||||||||||||||||||||||||
| 41 | 17 | my ($root) = $document->findnodes('/*[local-name()="EnumerationResults"]'); | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 17 | ▶ | croak 'Blob listing response is not an EnumerationResults document' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | unless $root; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 16 | my @items; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 16 | for my $node ( $root->findnodes('.//*[local-name()="Blobs"]/*[local-name()="Blob"]') ) { | |||||||||||||||||||||||||||||||||||||||||
| 48 | 16 | push @items, Azure::SDK::Storage::Blobs::BlobItem->new( | |||||||||||||||||||||||||||||||||||||||||
| 49 | name => _blob_name($node), | ||||||||||||||||||||||||||||||||||||||||||
| 50 | etag => _child_text( $node, 'Etag' ), | ||||||||||||||||||||||||||||||||||||||||||
| 51 | last_modified => _child_text( $node, 'Last-Modified' ), | ||||||||||||||||||||||||||||||||||||||||||
| 52 | content_length => _child_text( $node, 'Content-Length' ), | ||||||||||||||||||||||||||||||||||||||||||
| 53 | content_type => _child_text( $node, 'Content-Type' ), | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 14 | ▶ | return Azure::SDK::Storage::Blobs::BlobPage->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | items => \@items, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | continuation_token => _child_text( $root, 'NextMarker' ), | ||||||||||||||||||||||||||||||||||||||||||
| 60 | container_name => $root->hasAttribute('ContainerName') | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ? $root->getAttribute('ContainerName') | ||||||||||||||||||||||||||||||||||||||||||
| 62 | : undef, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | prefix => _child_text( $root, 'Prefix' ), | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | # Returns undef, not a croak, when the body is not an error envelope: an | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # empty body is a normal outcome the caller handles via the status code. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | ▶ | method parse_error ($xml) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_error: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 9 | ▶ | return undef unless defined $xml && $xml =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $xml and $xml =~ /\S/u
MC/DC50%1/2
defined $xml and $xml =~ /\S/u
defined $xml
$xml =~ /\S/u
Truth table66%2/3
A and B
A= defined $xmlB= $xml =~ /\S/u
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 7 | ▶ | return undef if bytes::length($xml) > $max_bytes; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 6 | ▶ | my $document = eval { $self->_parse($xml) } or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | my ($root) = $document->findnodes('/*[local-name()="Error"]'); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 5 | ▶ | return undef unless $root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 4 | my $code = _child_text( $root, 'Code' ); | |||||||||||||||||||||||||||||||||||||||||
| 79 | 4 | my $message = _child_text( $root, 'Message' ); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | return undef unless defined $code || defined $message; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $code or defined $message
MC/DC0%0/2
defined $code or defined $message
defined $code
defined $message
Truth table33%1/3
A or B
A= defined $codeB= defined $message
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 4 | code => $code, | |||||||||||||||||||||||||||||||||||||||||
| 84 | message => $message, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 27 | ▶ | method _parse ($xml) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=6 SCAR=18.7
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 27 | ▶ | croak 'Blob response body is empty' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $xml and length $xml
MC/DC0%0/2
defined $xml and length $xml
defined $xml
length $xml
Truth table33%1/3
A and B
A= defined $xmlB= length $xml
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | unless defined $xml && length $xml; | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # bytes::length, not length: length counts characters, so a decoded body | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # could carry several times this many octets while appearing to fit. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 27 | ▶ | croak 'Blob response body exceeds ' . $max_bytes . ' bytes' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | if bytes::length($xml) > $max_bytes; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 25 | my $document = $parser->load_xml( string => $xml ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # A Blob response never carries a DTD, so rejecting one outright removes | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # the entity-expansion class rather than relying only on parser options. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 24 | ▶ | croak 'Blob response body must not contain a document type declaration' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $document->internalSubset or defined $document->externalSubset
MC/DC50%1/2
defined $document->internalSubset or defined $document->externalSubset
defined $document->internalSubset
defined $document->externalSubset
Truth table66%2/3
A or B
A= defined $document->internalSubsetB= defined $document->externalSubset
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | if defined $document->internalSubset || defined $document->externalSubset; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 22 | return $document; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | # Decodes through the URI builder's strict decoder, not a local copy: a name | ||||||||||||||||||||||||||||||||||||||||||
| 108 | # read here is fed straight into the next request, so a malformed escape | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # accepted now would be re-encoded into a request for a different blob. | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 16 | ▶ | sub _blob_name ($node) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_blob_name: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 16 | my ($name_node) = $node->findnodes('./*[local-name()="Name"]'); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 16 | ▶ | return undef unless $name_node; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 16 | my $value = $name_node->textContent; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 16 | ▶ | return $value | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$name_node->hasAttribute("Encoded") and lc $name_node->getAttribute("Encoded") eq "true"
MC/DC50%1/2
$name_node->hasAttribute("Encoded") and lc $name_node->getAttribute("Encoded") eq "true"
$name_node->hasAttribute("Encoded")
lc $name_node->getAttribute("Encoded") eq "true"
Truth table66%2/3
A and B
A= $name_node->hasAttribute("Encoded")B= lc $name_node->getAttribute("Encoded") eq "true"
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | unless $name_node->hasAttribute('Encoded') | ||||||||||||||||||||||||||||||||||||||||||
| 117 | && lc $name_node->getAttribute('Encoded') eq 'true'; | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | return Azure::SDK::Storage::Blobs::Uri->decode_component($value); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 92 | ▶ | sub _child_text ( $node, $name ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_child_text: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 92 | my ($child) = $node->findnodes( './/*[local-name()="' . $name . '"]' ); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 92 | ▶ | return undef unless $child; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 68 | my $text = $child->textContent; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 68 | ▶ | return length $text ? $text : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||||||||||||||||