diff --git a/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java b/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java index 6fe1ef7c9a47c..7c7534ece2914 100644 --- a/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java +++ b/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java @@ -80,6 +80,7 @@ public class JdkHttpClient implements HttpClient { private final ExecutorService executorService; private final Duration readTimeout; private final Duration connectTimeout; + private final ClientConfig config; JdkHttpClient(ClientConfig config) { Objects.requireNonNull(config, "Client config must be set"); @@ -108,6 +109,7 @@ public class JdkHttpClient implements HttpClient { Credentials credentials = config.credentials(); String info = config.baseUri().getUserInfo(); + if (info != null && !info.trim().isEmpty()) { String[] parts = info.split(":", 2); String username = parts[0]; @@ -121,6 +123,22 @@ protected PasswordAuthentication getPasswordAuthentication() { } }; builder = builder.authenticator(authenticator); + + // Remove credentials from URL + try { + config = + config.baseUri( + new URI( + config.baseUri().getScheme(), + null, + config.baseUri().getHost(), + config.baseUri().getPort(), + config.baseUri().getPath(), + config.baseUri().getQuery(), + config.baseUri().getFragment())); + } catch (URISyntaxException e) { + LOG.log(Level.WARNING, "Could not strip credentials from URI", e); + } } else if (credentials != null) { if (!(credentials instanceof UsernameAndPassword)) { throw new IllegalArgumentException( @@ -153,6 +171,7 @@ protected PasswordAuthentication getPasswordAuthentication() { builder.version(Version.valueOf(version)); } + this.config = config; this.client = builder.build(); } @@ -322,7 +341,7 @@ public WebSocket send(Message message) { throw new WebDriverException(cause); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw new WebDriverException(e.getMessage()); + throw new WebDriverException(e.getMessage(), e); } catch (java.util.concurrent.TimeoutException e) { throw new TimeoutException(e); } finally { @@ -506,6 +525,11 @@ private HttpResponse execute0(HttpRequest req) throws UncheckedIOException { } } + // Package-private method for testing + URI getBaseUri() { + return this.config.baseUri(); + } + @Override public void close() { if (this.client == null) { diff --git a/java/test/org/openqa/selenium/remote/http/jdk/JdkHttpClientTest.java b/java/test/org/openqa/selenium/remote/http/jdk/JdkHttpClientTest.java index f6b5489a1910f..41f1734a248b2 100644 --- a/java/test/org/openqa/selenium/remote/http/jdk/JdkHttpClientTest.java +++ b/java/test/org/openqa/selenium/remote/http/jdk/JdkHttpClientTest.java @@ -17,6 +17,12 @@ package org.openqa.selenium.remote.http.jdk; +import static org.assertj.core.api.Assertions.assertThat; + +import java.net.URI; +import java.net.URISyntaxException; +import org.junit.jupiter.api.Test; +import org.openqa.selenium.remote.http.ClientConfig; import org.openqa.selenium.remote.http.HttpClient; import org.openqa.selenium.remote.internal.HttpClientTestBase; @@ -26,4 +32,49 @@ class JdkHttpClientTest extends HttpClientTestBase { protected HttpClient.Factory createFactory() { return new JdkHttpClient.Factory(); } + + @Test + void shouldStripCredentialsFromUrl() throws URISyntaxException { + URI originalUri = new URI("http://admin:password@localhost:4444/wd/hub"); + ClientConfig config = ClientConfig.defaultConfig().baseUri(originalUri); + + JdkHttpClient client = new JdkHttpClient(config); + + URI modifiedUri = client.getBaseUri(); + + assertThat(modifiedUri.getUserInfo()).isNull(); + assertThat(modifiedUri.getHost()).isEqualTo("localhost"); + assertThat(modifiedUri.getPort()).isEqualTo(4444); + assertThat(modifiedUri.getPath()).isEqualTo("/wd/hub"); + } + + @Test + void shouldHandleUrlWithoutCredentials() throws URISyntaxException { + URI originalUri = new URI("http://localhost:4444/wd/hub"); + ClientConfig config = ClientConfig.defaultConfig().baseUri(originalUri); + + JdkHttpClient client = new JdkHttpClient(config); + + URI modifiedUri = client.getBaseUri(); + + assertThat(modifiedUri).isEqualTo(originalUri); + } + + @Test + void shouldPreserveUrlComponentsExceptCredentials() throws URISyntaxException { + URI originalUri = new URI("https://admin:password@localhost:4444/wd/hub?debug=true#fragment"); + ClientConfig config = ClientConfig.defaultConfig().baseUri(originalUri); + + JdkHttpClient client = new JdkHttpClient(config); + + URI modifiedUri = client.getBaseUri(); + + assertThat(modifiedUri.getScheme()).isEqualTo("https"); + assertThat(modifiedUri.getUserInfo()).isNull(); + assertThat(modifiedUri.getHost()).isEqualTo("localhost"); + assertThat(modifiedUri.getPort()).isEqualTo(4444); + assertThat(modifiedUri.getPath()).isEqualTo("/wd/hub"); + assertThat(modifiedUri.getQuery()).isEqualTo("debug=true"); + assertThat(modifiedUri.getFragment()).isEqualTo("fragment"); + } }