diff --git a/CHANGES.rst b/CHANGES.rst index 048d163435..397bc8f6ad 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 ~~~~~~~ diff --git a/libcloud/common/aws.py b/libcloud/common/aws.py index 70a395bd58..3cf55b971f 100644 --- a/libcloud/common/aws.py +++ b/libcloud/common/aws.py @@ -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()) ] ) diff --git a/libcloud/test/common/test_aws.py b/libcloud/test/common/test_aws.py index df3f18d400..0d45ede65b 100644 --- a/libcloud/test/common/test_aws.py +++ b/libcloud/test/common/test_aws.py @@ -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 ", @@ -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(