From f407ced87a9a4ca48c43782e8fa54231567b0ec3 Mon Sep 17 00:00:00 2001 From: Adwait Kumar Singh Date: Tue, 10 Jun 2025 13:56:52 -0700 Subject: [PATCH] Add a check in JavaHttpClientTransport to verify if we can add the host header --- client/client-http/build.gradle.kts | 4 ++++ .../java/client/http/JavaHttpClientTransport.java | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/client/client-http/build.gradle.kts b/client/client-http/build.gradle.kts index 7c309068c5..5c39628e0f 100644 --- a/client/client-http/build.gradle.kts +++ b/client/client-http/build.gradle.kts @@ -16,3 +16,7 @@ dependencies { testImplementation(project(":aws:client:aws-client-awsjson")) testImplementation(project(":client:dynamic-client")) } + +tasks.withType { + systemProperty("jdk.httpclient.allowRestrictedHeaders", "host") +} diff --git a/client/client-http/src/main/java/software/amazon/smithy/java/client/http/JavaHttpClientTransport.java b/client/client-http/src/main/java/software/amazon/smithy/java/client/http/JavaHttpClientTransport.java index 36cd2364dc..5e3ef6e8f6 100644 --- a/client/client-http/src/main/java/software/amazon/smithy/java/client/http/JavaHttpClientTransport.java +++ b/client/client-http/src/main/java/software/amazon/smithy/java/client/http/JavaHttpClientTransport.java @@ -5,6 +5,7 @@ package software.amazon.smithy.java.client.http; +import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpConnectTimeoutException; import java.nio.ByteBuffer; @@ -31,6 +32,8 @@ */ public class JavaHttpClientTransport implements ClientTransport { + private static URI DUMMY_URI = URI.create("http://localhost"); + private static final InternalLogger LOGGER = InternalLogger.getLogger(JavaHttpClientTransport.class); private final HttpClient client; @@ -47,6 +50,17 @@ private static void setHostProperties() { } else if (!containsHost(currentValues)) { System.setProperty("jdk.httpclient.allowRestrictedHeaders", currentValues + ",host"); } + try { + java.net.http.HttpRequest.newBuilder() + .uri(DUMMY_URI) + .setHeader("host", "localhost") + .build(); + } catch (IllegalArgumentException e) { + throw new RuntimeException("Unable to add host header. " + + "This means that the HttpClient was initialized before we could allowlist it. " + + "You need to explicitly set allow `host` via the system property `jdk.httpclient.allowRestrictedHeaders`", + e); + } } public JavaHttpClientTransport() {