Skip to content

Commit f407ced

Browse files
committed
Add a check in JavaHttpClientTransport to verify if we can add the host header
1 parent e60a6b2 commit f407ced

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

client/client-http/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ dependencies {
1616
testImplementation(project(":aws:client:aws-client-awsjson"))
1717
testImplementation(project(":client:dynamic-client"))
1818
}
19+
20+
tasks.withType<Test> {
21+
systemProperty("jdk.httpclient.allowRestrictedHeaders", "host")
22+
}

client/client-http/src/main/java/software/amazon/smithy/java/client/http/JavaHttpClientTransport.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package software.amazon.smithy.java.client.http;
77

8+
import java.net.URI;
89
import java.net.http.HttpClient;
910
import java.net.http.HttpConnectTimeoutException;
1011
import java.nio.ByteBuffer;
@@ -31,6 +32,8 @@
3132
*/
3233
public class JavaHttpClientTransport implements ClientTransport<HttpRequest, HttpResponse> {
3334

35+
private static URI DUMMY_URI = URI.create("http://localhost");
36+
3437
private static final InternalLogger LOGGER = InternalLogger.getLogger(JavaHttpClientTransport.class);
3538
private final HttpClient client;
3639

@@ -47,6 +50,17 @@ private static void setHostProperties() {
4750
} else if (!containsHost(currentValues)) {
4851
System.setProperty("jdk.httpclient.allowRestrictedHeaders", currentValues + ",host");
4952
}
53+
try {
54+
java.net.http.HttpRequest.newBuilder()
55+
.uri(DUMMY_URI)
56+
.setHeader("host", "localhost")
57+
.build();
58+
} catch (IllegalArgumentException e) {
59+
throw new RuntimeException("Unable to add host header. " +
60+
"This means that the HttpClient was initialized before we could allowlist it. " +
61+
"You need to explicitly set allow `host` via the system property `jdk.httpclient.allowRestrictedHeaders`",
62+
e);
63+
}
5064
}
5165

5266
public JavaHttpClientTransport() {

0 commit comments

Comments
 (0)