Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
import org.apache.hc.client5.http.auth.Credentials;
import org.apache.hc.client5.http.auth.CredentialsStore;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.util.Args;

/**
Expand Down Expand Up @@ -95,6 +97,16 @@ private static PasswordAuthentication getSystemCreds(
requestorType);
}

private static boolean isRequestTarget(final AuthScope authScope, final HttpClientContext context) {
final HttpRequest request = context != null ? context.getRequest() : null;
final URIAuthority authority = request != null ? request.getAuthority() : null;
if (authority == null) {
return false;
}
return authority.getHostName().equalsIgnoreCase(authScope.getHost())
&& DefaultSchemePortResolver.INSTANCE.resolve(request.getScheme(), authority) == authScope.getPort();
}

@Override
public Credentials getCredentials(final AuthScope authScope, final HttpContext context) {
Args.notNull(authScope, "Auth scope");
Expand All @@ -110,9 +122,12 @@ public Credentials getCredentials(final AuthScope authScope, final HttpContext c
if (serverCreds != null) {
return new UsernamePasswordCredentials(serverCreds.getUserName(), serverCreds.getPassword());
}
final PasswordAuthentication proxyCreds = getSystemCreds(protocol, authScope, Authenticator.RequestorType.PROXY, clientContext);
if (proxyCreds != null) {
return new UsernamePasswordCredentials(proxyCreds.getUserName(), proxyCreds.getPassword());
// Proxy credentials must never be handed to the origin server the request is addressed to
if (!isRequestTarget(authScope, clientContext)) {
final PasswordAuthentication proxyCreds = getSystemCreds(protocol, authScope, Authenticator.RequestorType.PROXY, clientContext);
if (proxyCreds != null) {
return new UsernamePasswordCredentials(proxyCreds.getUserName(), proxyCreds.getPassword());
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@

final AuthenticatorDelegate authenticatorDelegate = installAuthenticator(AUTH1);

final URL httpRequestUrl = new URL(TARGET_SCHEME1, TARGET_HOST1, TARGET_PORT1, "/");

Check warning on line 97 in httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 21)

[deprecation] URL(String,String,int,String) in URL has been deprecated

Check warning on line 97 in httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java

View workflow job for this annotation

GitHub Actions / build (macos-latest, 21)

[deprecation] URL(String,String,int,String) in URL has been deprecated
final AuthScope authScope = new AuthScope(PROXY_PROTOCOL1, PROXY_HOST1, PROXY_PORT1, PROMPT1, StandardAuthScheme.BASIC);
final HttpClientContext context = HttpClientContext.create();
context.setRequest(new HttpGet(httpRequestUrl.toURI()));
Expand Down Expand Up @@ -129,6 +129,51 @@
Assertions.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
}

@Test
void testSystemCredentialsProviderNoProxyCredentialsForTarget() throws Exception {

final AuthenticatorDelegate authenticatorDelegate = installProxyAuthenticator(AUTH1);

final AuthScope authScope = new AuthScope(TARGET_SCHEME1, TARGET_HOST1, TARGET_PORT1, PROMPT1, StandardAuthScheme.BASIC);
final HttpClientContext context = HttpClientContext.create();
context.setRequest(new HttpGet(TARGET_SCHEME1 + "://" + TARGET_HOST1 + ":" + TARGET_PORT1 + "/"));

final Credentials receivedCredentials =
new SystemDefaultCredentialsProvider().getCredentials(authScope, context);

Mockito.verify(authenticatorDelegate, Mockito.never()).getPasswordAuthentication(ArgumentMatchers.any(),
ArgumentMatchers.any(), ArgumentMatchers.anyInt(), ArgumentMatchers.any(), ArgumentMatchers.any(),
ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.eq(RequestorType.PROXY));
Assertions.assertNull(receivedCredentials);
}

@Test
void testSystemCredentialsProviderProxyCredentialsForProxy() throws Exception {

installProxyAuthenticator(AUTH1);

final AuthScope authScope = new AuthScope(PROXY_PROTOCOL1, PROXY_HOST1, PROXY_PORT1, PROMPT1, StandardAuthScheme.BASIC);
final HttpClientContext context = HttpClientContext.create();
context.setRequest(new HttpGet(TARGET_SCHEME1 + "://" + TARGET_HOST1 + ":" + TARGET_PORT1 + "/"));

final Credentials receivedCredentials =
new SystemDefaultCredentialsProvider().getCredentials(authScope, context);

Assertions.assertNotNull(receivedCredentials);
Assertions.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
}

private AuthenticatorDelegate installProxyAuthenticator(final PasswordAuthentication proxyAuthentication) {
final AuthenticatorDelegate authenticatorDelegate = Mockito.mock(AuthenticatorDelegate.class);
Mockito.when(authenticatorDelegate.getPasswordAuthentication(ArgumentMatchers.any(),
ArgumentMatchers.any(), ArgumentMatchers.anyInt(),
ArgumentMatchers.any(), ArgumentMatchers.any(),
ArgumentMatchers.any(), ArgumentMatchers.any(),
ArgumentMatchers.eq(RequestorType.PROXY))).thenReturn(proxyAuthentication);
Authenticator.setDefault(new DelegatedAuthenticator(authenticatorDelegate));
return authenticatorDelegate;
}

private AuthenticatorDelegate installAuthenticator(final PasswordAuthentication returedAuthentication) {
final AuthenticatorDelegate authenticatorDelegate = Mockito.mock(AuthenticatorDelegate.class);
Mockito.when(authenticatorDelegate.getPasswordAuthentication(ArgumentMatchers.anyString(),
Expand Down
Loading