Skip to content

[AWS] Collapse sequential whitespace in SigV4 canonical header values - #2236

Open
0z5a wants to merge 1 commit into
apache:trunkfrom
0z5a:fix-sigv4-trimall-header-values
Open

0z5a wants to merge 1 commit into
apache:trunkfrom
0z5a:fix-sigv4-trimall-header-values

Conversation

@0z5a

@0z5a 0z5a commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Description

AWSRequestSignerAlgorithmV4._get_canonical_headers() only strips leading and trailing whitespace from header values. The SigV4 spec ("Trimall") also turns sequential whitespace inside a value into a single space. botocore does this, and so does at least one S3-compatible server (MinIO). So when a signed header value has two spaces or a tab in it, libcloud signs a different canonical request than the server computes, and the request is rejected with SignatureDoesNotMatch. There is an old TODO about this in test_aws.py.

Since the V4 signer signs every request header, the easiest way to hit this is S3 user metadata:

driver.upload_object_via_stream(iter([b"x"]), container, "key",
                                extra={"meta_data": {"desc": "a  b"}})
# InvalidCredsError: ... <Code>SignatureDoesNotMatch</Code> ...

How I found it

I compared the canonical request libcloud builds with the one botocore builds (S3SigV4Auth.canonical_request) for the same request. They only differed when a header value had sequential spaces or a tab:

value      libcloud              botocore
'a b'      x-amz-meta-desc:a b   x-amz-meta-desc:a b
'a  b'     x-amz-meta-desc:a  b  x-amz-meta-desc:a b
'a\tb'     x-amz-meta-desc:a\tb  x-amz-meta-desc:a b

Then I checked against a real server (a local MinIO, through the MinIO driver). On trunk, uploads with "a b" or "a\tb" in metadata fail with SignatureDoesNotMatch, while "a b" works. botocore uploads all three values to the same server fine. With this change all three uploads succeed through libcloud too.

Where this can show up in practice

Any S3 (or S3-compatible) upload where metadata comes from user input or file attributes, e.g. a description or an original filename with a double space or a tab. Other V4-signed requests with such header values are affected the same way.

Change

Header values are now built with " ".join(str(v).split()), which matches botocore. I added a test with expected values taken from botocore's canonical headers (it fails on trunk and passes with the fix), removed the old TODO comment, and added a CHANGES.rst entry. libcloud/test/common/test_aws.py, test_s3.py, test_minio.py and test_ec2.py pass locally (1298 passed, 1 skipped).

The old TODO mentioned an exception for quoted strings. botocore collapses whitespace inside quotes too ('"a b"' becomes "a b"), and MinIO accepts that signature, so I didn't add an exception.

Status

  • done, ready for review

Checklist (tick everything that applies)

  • Code linting (required, can be done after the PR checks)
  • Documentation
  • Tests
  • ICLA (required for bigger changes)

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

The SigV4 canonical request "Trimall" step turns sequential whitespace
inside a header value into a single space. libcloud only stripped the
ends of the value, so a signed header such as S3 user metadata with two
spaces or a tab produced a different signature than the server computed
and the request failed with SignatureDoesNotMatch.

Join the whitespace-separated parts with a single space, as botocore
does, and add a test with expected values taken from botocore.

Signed-off-by: Dezhen Lu <dezhen.lu@student.uni-tuebingen.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant