Skip to content

SigV4a: Add host header only when not already provided #6310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2025
Merged
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
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-9f69c81.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Fixed SigV4a signing to respect pre-existing Host headers to be consistent with existing SigV4 signing behavior."
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_SIGNED_HEADERS;

import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -56,7 +57,7 @@ private CrtUtils() {
* Sanitize an {@link SdkHttpRequest}, in order to prepare it for converting to a CRT request destined to be signed.
* <p>
* Sanitizing includes checking the path is not empty, filtering headers and query parameters that are forbidden in CRT, and
* adding the host header (overriding if already presesnt).
* adding the host header
*/
public static SdkHttpRequest sanitizeRequest(SdkHttpRequest request) {

Expand All @@ -77,11 +78,17 @@ public static SdkHttpRequest sanitizeRequest(SdkHttpRequest request) {
}
});

// Add host, which must be signed. We ignore any pre-existing Host header to match the behavior of the SigV4 signer.
String hostHeader = SdkHttpUtils.isUsingStandardPort(request.protocol(), request.port())
? request.host()
: request.host() + ":" + request.port();
builder.putHeader(HOST, hostHeader);
// Add host header, which must be signed. If the SdkHttpRequest has an associated Host header
// already set, prefer to use that.
Optional<String> existingHostHeader = request.firstMatchingHeader(HOST);
if (existingHostHeader.isPresent()) {
builder.putHeader(HOST, existingHostHeader.get());
} else {
String hostHeader = SdkHttpUtils.isUsingStandardPort(request.protocol(), request.port())
? request.host()
: request.host() + ":" + request.port();
builder.putHeader(HOST, hostHeader);
}

builder.clearQueryParameters();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,26 @@ public void sign_checksumValueProvided_shouldNotOverrideChecksumHeader() {
assertThat(signedRequest.request().firstMatchingHeader("x-amz-checksum-crc32"))
.contains("some value");
}

@Test
public void sign_withProvidedHostHeader_shouldRespectUserHostHeader() {
AwsCredentialsIdentity credentials =
AwsCredentialsIdentity.create("access", "secret");

String hostOverride = "virtual-host.localhost";
SignRequest<AwsCredentialsIdentity> request = generateBasicRequest(
credentials,
httpRequest -> httpRequest.putHeader("Host", hostOverride).port(443),
signRequest -> {

}
);

SignedRequest signedRequest = signer.sign(request);

assertThat(signedRequest.request().firstMatchingHeader("Host")).hasValue(hostOverride);
assertThat(signedRequest.request().firstMatchingHeader("X-Amz-Date")).hasValue("20200803T174823Z");
assertThat(signedRequest.request().firstMatchingHeader("X-Amz-Region-Set")).hasValue("aws-global");
assertThat(signedRequest.request().firstMatchingHeader("Authorization")).isPresent();
}
}
Loading