Skip to content

[java] Fixed issue with embedded authentication in URLs for JdkHttpClient #15071

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

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from
26 changes: 25 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,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");
Expand Down Expand Up @@ -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];
Expand All @@ -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(
Expand Down Expand Up @@ -153,6 +171,7 @@ protected PasswordAuthentication getPasswordAuthentication() {
builder.version(Version.valueOf(version));
}

this.config = config;
this.client = builder.build();
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
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