NAME

Azure::SDK::Storage::Blobs::Uri - build safely encoded Azure Blob request URLs

SYNOPSIS

use Azure::SDK::Storage::Blobs::Uri;

my $uri = Azure::SDK::Storage::Blobs::Uri->from_connection_string($connection);

my $target = $uri->for_blob('phase3-files', 'nested/path with space/åß.txt');

$target->{url};              # http://127.0.0.1:58354/devstoreaccount1/phase3-files/nested/path%20with%20space/%C3%A5%C3%9F.txt
$target->{canonical_path};   # /devstoreaccount1/phase3-files/nested/path%20with%20space/%C3%A5%C3%9F.txt
$target->{canonical_query};  # {}

DESCRIPTION

Builds Blob service URLs from untrusted container and blob names, and returns the canonicalized path and query alongside the encoded URL so that shared-key signing and the request line can never disagree.

Deriving all three from one place is what keeps signing correct, because Shared Key does not treat them uniformly: the canonicalized resource uses the encoded URI path but the decoded query values.

Encoding rules

Path segments are encoded independently. A blob name is split on /, each segment is escaped down to the RFC 3986 unreserved set, and the segments are rejoined with literal / characters. Untrusted text therefore cannot introduce a path separator, a ?, or a # into the request line.

Container names, blob names, and query values are character strings, and are encoded to UTF-8 octets before escaping. This is the same form Azure::SDK::Storage::Blobs::Serializer produces when it parses a listing, so a name or continuation marker read from one response can be fed straight back into the next request. Callers that hold raw bytes (for example a filename read from disk) must decode them before passing them in.

Validation rules

  • The endpoint must be an absolute http/https URI with a host and without userinfo, query, fragment, or relative path segments. These checks run on every construction path, including a bare endpoint handed straight to new, so configuration that never passed through a connection string is not held to weaker rules.

    Percent-encoding in the endpoint path is preserved rather than rejected, decoded, or re-encoded. Only the container and blob names this class appends are encoded.

  • Container names must follow the Azure rule of 3-63 characters of lowercase letters, digits, and single interior hyphens.

  • Blob names are limited to 1024 characters and 254 path segments. Leading, trailing, and repeated / characters are allowed, because Azure stores such names and a client that rejected them could not address them.

  • Blob names must not contain control characters. Azurite answers a container listing containing such a name with HTTP 500 rather than the Encoded="true" element real Azure returns, and control characters would also allow log-line forgery.

  • Blob names must not contain . or .. segments. Because segments are joined with literal /, a relative segment could be resolved by an intermediary and reach a different blob than the caller named. Azure itself accepts these names, so this is a deliberate restriction; lifting it means encoding the dots as %2E, which changes the signed canonical path and needs live verification first.

  • Query parameter names are restricted to alphanumerics, -, _, and ., are lowercased for canonicalization, and are rejected when repeated. undef values are dropped; other values must not contain control characters.

CONSTRUCTOR

new

Named parameter endpoint, required. The absolute blob endpoint. A trailing slash is removed, and the value is validated as described under "Validation rules".

There is deliberately no way to supply the endpoint path. It is always derived from endpoint, so the path that is signed and the path that is requested cannot be made to disagree. See "endpoint_path".

from_connection_string

my $uri = Azure::SDK::Storage::Blobs::Uri->from_connection_string($connection);

Class method. Builds a URI factory from an Azure::SDK::Storage::Blobs::ConnectionString, using only its blob_endpoint.

normalize_endpoint

my ( $endpoint, $path ) = Azure::SDK::Storage::Blobs::Uri->normalize_endpoint($value);

Class method. Validates a blob endpoint and returns it normalized alongside its path component, croaking when the endpoint breaks any rule above. Azure::SDK::Storage::Blobs::ConnectionString uses this so BlobEndpoint and a directly supplied endpoint are validated by the same code.

decode_component

my $value = Azure::SDK::Storage::Blobs::Uri->decode_component($encoded);

Class method. Percent-decodes a single URL component and reassembles the octets into characters, croaking on an incomplete escape or on invalid UTF-8. Azure::SDK::Storage::Blobs::Serializer uses this to decode Encoded="true" blob names, so a name read from a listing is held to the same rules as one read from a URL.

METHODS

endpoint_path

# "/devstoreaccount1"
Azure::SDK::Storage::Blobs::Uri->new(
    endpoint => 'http://127.0.0.1:10000/devstoreaccount1' )->endpoint_path;

# ""
Azure::SDK::Storage::Blobs::Uri->new(
    endpoint => 'https://myacct.blob.core.windows.net' )->endpoint_path;

Read-only. The path component of the endpoint, without a trailing slash, which the shared-key signer prepends to the container and blob segments.

An emulator places the account name in the URL path, so this is /devstoreaccount1; real Azure Storage places it in the host, so this is the empty string. The same code path is therefore correct in both environments, with no path-style heuristic and no configuration.

for_service

my $target = $uri->for_service({ comp => 'list' });

Builds an account-level target.

for_container

my $target = $uri->for_container($container, { restype => 'container' });

Builds a container-level target.

for_blob

my $target = $uri->for_blob($container, $blob);

Builds a blob-level target.

parse

my $parts = $uri->parse($url);

$parts->{container};  # 'phase3-files'
$parts->{blob};       # 'nested/path with space/åß.txt'
$parts->{query};      # { comp => 'list' }

The inverse of for_container and for_blob. Decomposes a URL back into its container name, blob name, and query parameters, percent-decoding each part and reassembling the UTF-8 octets into characters. container and blob are undef when the URL does not reach that level.

This is an instance method rather than a class method because the endpoint is the reference point. The URL must lie under this instance's endpoint, and one that does not is rejected rather than parsed, so a URL from an untrusted source cannot be turned into names that a later request would sign.

Parsing does not re-apply the name validation rules used when building. The service is the authority on what it stores, and a response may legitimately reference names this client would decline to construct.

$uri->parse( $uri->for_blob($c, $b)->{url} ) returns $c and $b unchanged, which is the round-trip property the unit tests assert.

Return value

All three methods return a hash reference:

url

The absolute, fully percent-encoded request URL.

canonical_path

The percent-encoded URI path, including the endpoint path prefix. The shared-key signer prepends /{account} to this value to form the canonicalized resource.

The encoded form is not incidental. Signing the decoded path instead makes every request carrying a non-ASCII blob name fail with HTTP 403, which was confirmed against Azurite 3.35.0 during Step 1 protocol capture.

canonical_query

The normalized query parameters, with lowercased names and percent-decoded UTF-8 octet values. Query values are canonicalized decoded, unlike the path. The octets matter for the same reason: the service hashes the UTF-8 bytes of a decoded continuation marker, so a marker containing å contributes two bytes.

SEE ALSO

Azure::SDK::Storage::Blobs::ConnectionString