Skip to content

aws-http-auth: collapse sequential spaces in signed header values - #753

Open
0z5a wants to merge 1 commit into
aws:mainfrom
0z5a:fix/sigv4-collapse-header-spaces
Open

0z5a wants to merge 1 commit into
aws:mainfrom
0z5a:fix/sigv4-collapse-header-spaces

Conversation

@0z5a

@0z5a 0z5a commented Nov 16, 2026

Copy link
Copy Markdown

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) with X-Amz-Meta-Title: Hello World puts x-amz-meta-title:Hello World into the canonical request, while the service computes it over x-amz-meta-title:Hello World. The same internal signer backs sigv4, sigv4a, and the generic client auth schemes in aws-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 as my-header2:"a b c"). Signing that request with the current code gives Signature=9916079c1024219205257e22df05cd30381e097a4dc1bb02e2f94bc64cd18d53 instead of the expected Signature=acc3ed3afb60bb290fc8d2dd0098b9911fcaa05412b367055dee359757a9c736. Other cases from the same suite, such as get-vanilla and get-header-key-duplicate, already match. For comparison, the aws-sdk-go-v2 v4 signer (StripExcessSpaces) produces the same signature for Hello World and Hello World, and smithy-rs runs get-header-value-trim in 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 one strings.Index check.

Tests:

  • TestSignRequest_HeaderValueSequentialSpaces (sigv4) signs the get-header-value-trim request from the test suite and checks the expected Authorization header. It also checks that the header value is left unchanged.
  • TestBuildCanonicalRequest_HeaderValueSequentialSpaces (internal/v4) checks the canonical request for an X-Amz-Meta-Title value with leading, trailing and repeated spaces under the default header rules.

Both tests fail on main and pass with this change. go test ./... passes in aws-http-auth and aws-http-auth-schemes. A changelog entry for the aws-http-auth module is included.

Performance (benchstat, 15 interleaved runs per side, Apple M-series):

benchmark main this change
canonical headers, single spaces 1.004 µs 1.082 µs +7.8%, p=0.100
canonical headers, double spaces 1.031 µs 1.116 µs +8.2%, p=0.010
full SignRequest, single spaces 6.891 µs 7.189 µs p=0.744 (not significant)
full SignRequest, double spaces 6.991 µs 6.769 µs p=0.285 (not significant)

Allocations 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.

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.
@0z5a
0z5a requested review from a team as code owners November 16, 2026 00:27
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