Repository navigation
Conversation
SigV4 canonical headers require runs of sequential spaces inside a value to be collapsed into one, as in the get-header-value-trim case of the SigV4 test suite. The signer only trimmed leading and trailing whitespace, so a signed header such as X-Amz-Meta-Title: "Hello World" produced a signature the service does not agree with.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue #, if available: n/a
Description of changes:
When the v4 signer builds the canonical headers it only trims leading and trailing whitespace from each header value. SigV4 also requires runs of sequential spaces inside the value to be collapsed into a single space. The service canonicalizes the header that way, so any request with a signed header containing two or more consecutive spaces gets a signature the service does not agree with.
A realistic case is S3 user metadata, which is signed under the default header rules: signing a
PutObject(or a presigned URL) withX-Amz-Meta-Title: Hello Worldputsx-amz-meta-title:Hello Worldinto the canonical request, while the service computes it overx-amz-meta-title:Hello World. The same internal signer backssigv4,sigv4a, and the generic client auth schemes inaws-http-auth-schemes, so all of them are affected.The AWS SigV4 test suite has a case for exactly this,
get-header-value-trim(My-Header2: "a b c"is canonicalized asmy-header2:"a b c"). Signing that request with the current code givesSignature=9916079c1024219205257e22df05cd30381e097a4dc1bb02e2f94bc64cd18d53instead of the expectedSignature=acc3ed3afb60bb290fc8d2dd0098b9911fcaa05412b367055dee359757a9c736. Other cases from the same suite, such asget-vanillaandget-header-key-duplicate, already match. For comparison, the aws-sdk-go-v2 v4 signer (StripExcessSpaces) produces the same signature forHello WorldandHello World, and smithy-rs runsget-header-value-trimin its aws-sigv4 tests.The fix collapses runs of ASCII spaces while writing each value into the canonical headers, after the existing
TrimSpace. This matches what aws-sdk-go-v2 does. The header value sent on the wire is not modified, and the change adds no allocations: a value without a double space is written as before after onestrings.Indexcheck.Tests:
TestSignRequest_HeaderValueSequentialSpaces(sigv4) signs theget-header-value-trimrequest from the test suite and checks the expectedAuthorizationheader. It also checks that the header value is left unchanged.TestBuildCanonicalRequest_HeaderValueSequentialSpaces(internal/v4) checks the canonical request for anX-Amz-Meta-Titlevalue with leading, trailing and repeated spaces under the default header rules.Both tests fail on main and pass with this change.
go test ./...passes inaws-http-authandaws-http-auth-schemes. A changelog entry for theaws-http-authmodule is included.Performance (
benchstat, 15 interleaved runs per side, Apple M-series):SignRequest, single spacesSignRequest, double spacesAllocations are unchanged (14 allocs/op for the canonical headers, 100 for a full signature). So the extra scan costs roughly 80 ns per canonical-header build, which did not show up as a significant difference in a full signature.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.