Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ Common
(#2229)
[0z5a - @0z5a]

- [AWS] Collapse sequential whitespace inside header values when building the
SigV4 canonical request, as the AWS spec and SDKs do. Before, uploading an S3
object with user metadata that contains two spaces or a tab in a value failed
with ``SignatureDoesNotMatch``.
(#2236)
[0z5a - @0z5a]

Compute
~~~~~~~

Expand Down
4 changes: 3 additions & 1 deletion libcloud/common/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ def _get_canonical_headers(self, headers):
return (
"\n".join(
[
":".join([k.lower(), str(v).strip()])
# SigV4 "Trimall": strip the value and collapse sequential
# whitespace into a single space, as AWS and other SDKs do.
":".join([k.lower(), " ".join(str(v).split())])
for k, v in sorted(headers.items(), key=lambda k: k[0].lower())
]
)
Expand Down
17 changes: 13 additions & 4 deletions libcloud/test/common/test_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,6 @@ def test_get_canonical_headers_lowercases_headers_names(self):
)

def test_get_canonical_headers_trims_header_values(self):
# TODO: according to AWS spec (and RFC 2616 Section 4.2.) excess whitespace
# from inside non-quoted strings should be stripped. Now we only strip the
# start and end of the string. See
# http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
headers = {
"accept-encoding": " gzip,deflate",
"user-agent": "libcloud/0.17.0 ",
Expand All @@ -215,6 +211,19 @@ def test_get_canonical_headers_trims_header_values(self):
"accept-encoding:gzip,deflate\n" "user-agent:libcloud/0.17.0\n",
)

def test_get_canonical_headers_collapses_sequential_whitespace(self):
# SigV4 "Trimall": sequential spaces inside a value become one space.
# Expected values match botocore's SigV4Auth canonical headers.
headers = {
"x-amz-meta-a": "a b",
"x-amz-meta-b": " a \t b ",
"x-amz-meta-c": "a b c",
}
self.assertEqual(
self.signer._get_canonical_headers(headers),
"x-amz-meta-a:a b\n" "x-amz-meta-b:a b\n" "x-amz-meta-c:a b c\n",
)

def test_get_request_params_joins_params_sorted_lexically(self):
self.assertEqual(
self.signer._get_request_params(
Expand Down