Skip to content

Fix Sigv4a-signed requests to endpoints with non-standard ports. #2725

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 1 commit into from
Sep 20, 2021
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/bugfix-AWSSDKforJavav2-3312616.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "",
"type": "bugfix",
"description": "Update Sigv4a signer to include the port in the Host header, when the port does not match the standard port for the protocol. This allows requests to endpoints with non-standard ports to succeed."
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

package software.amazon.awssdk.authcrt.signer.internal;

import static software.amazon.awssdk.utils.CollectionUtils.isNullOrEmpty;

import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Duration;
Expand All @@ -35,6 +33,7 @@
import software.amazon.awssdk.crt.auth.credentials.Credentials;
import software.amazon.awssdk.http.SdkHttpFullRequest;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.awssdk.utils.http.SdkHttpUtils;

@SdkInternalApi
public class SigningUtils {
Expand Down Expand Up @@ -116,19 +115,19 @@ public static SdkHttpFullRequest sanitizeSdkRequestForCrtSigning(SdkHttpFullRequ

builder.clearHeaders();

// Add host if missing
Map<String, List<String>> headers = request.headers();
if (isNullOrEmpty(headers.get(HOST_HEADER))) {
builder.putHeader(HOST_HEADER, request.host());
}

// Filter headers that will cause signing to fail
for (Map.Entry<String, List<String>> header: headers.entrySet()) {
for (Map.Entry<String, List<String>> header: request.headers().entrySet()) {
if (!FORBIDDEN_HEADERS.contains(header.getKey())) {
builder.putHeader(header.getKey(), header.getValue());
}
}

// 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_HEADER, hostHeader);

builder.clearQueryParameters();

// Filter query parameters that will cause signing to fail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,45 @@ public void setup() {
v4aSigner = AwsCrtV4aSigner.create();
}

@Test
public void hostHeaderExcludesStandardHttpPort() {
SigningTestCase testCase = SignerTestUtils.createBasicHeaderSigningTestCase();
ExecutionAttributes executionAttributes = SignerTestUtils.buildBasicExecutionAttributes(testCase);

SdkHttpFullRequest request = testCase.requestBuilder.protocol("http")
.port(80)
.build();
SdkHttpFullRequest signed = v4aSigner.sign(request, executionAttributes);

assertThat(signed.firstMatchingHeader("Host")).hasValue("demo.us-east-1.amazonaws.com");
}

@Test
public void hostHeaderExcludesStandardHttpsPort() {
SigningTestCase testCase = SignerTestUtils.createBasicHeaderSigningTestCase();
ExecutionAttributes executionAttributes = SignerTestUtils.buildBasicExecutionAttributes(testCase);

SdkHttpFullRequest request = testCase.requestBuilder.protocol("https")
.port(443)
.build();
SdkHttpFullRequest signed = v4aSigner.sign(request, executionAttributes);

assertThat(signed.firstMatchingHeader("Host")).hasValue("demo.us-east-1.amazonaws.com");
}

@Test
public void hostHeaderIncludesNonStandardPorts() {
SigningTestCase testCase = SignerTestUtils.createBasicHeaderSigningTestCase();
ExecutionAttributes executionAttributes = SignerTestUtils.buildBasicExecutionAttributes(testCase);

SdkHttpFullRequest request = testCase.requestBuilder.protocol("http")
.port(443)
.build();
SdkHttpFullRequest signed = v4aSigner.sign(request, executionAttributes);

assertThat(signed.firstMatchingHeader("Host")).hasValue("demo.us-east-1.amazonaws.com:443");
}

@Test
public void testHeaderSigning() {
SigningTestCase testCase = SignerTestUtils.createBasicHeaderSigningTestCase();
Expand Down