Skip to content
28 changes: 27 additions & 1 deletion java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ public class JdkHttpClient implements HttpClient {
private final ExecutorService executorService;
private final Duration readTimeout;
private final Duration connectTimeout;
private ClientConfig config;

JdkHttpClient(ClientConfig config) {
Objects.requireNonNull(config, "Client config must be set");

this.config = config;
this.messages = new JdkHttpMessages(config);
this.readTimeout = config.readTimeout();
this.connectTimeout = config.connectionTimeout();
Expand All @@ -108,6 +110,8 @@ public class JdkHttpClient implements HttpClient {

Credentials credentials = config.credentials();
String info = config.baseUri().getUserInfo();
URI baseUri = config.baseUri();

if (info != null && !info.trim().isEmpty()) {
String[] parts = info.split(":", 2);
String username = parts[0];
Expand All @@ -121,6 +125,23 @@ protected PasswordAuthentication getPasswordAuthentication() {
}
};
builder = builder.authenticator(authenticator);

// Remove credentials from URL
try {
this.config =
ClientConfig.defaultConfig()
.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(
Expand Down Expand Up @@ -322,7 +343,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 {
Expand Down Expand Up @@ -506,6 +527,11 @@ private HttpResponse execute0(HttpRequest req) throws UncheckedIOException {
}
}

// Package-private method for testing
URI getBaseUri() {
return config.baseUri();
}

@Override
public void close() {
if (this.client == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
}
}
Loading