Skip to content

Commit f805a8c

Browse files
committed
Do not treat a 407 received through a tunnel as a proxy challenge
Once the CONNECT tunnel is established a 407 comes from the origin. It was evaluated against the proxy auth exchange, which failed with a NullPointerException or IllegalStateException. The response is now returned to the caller as is.
1 parent 9e45136 commit f805a8c

4 files changed

Lines changed: 225 additions & 8 deletions

File tree

‎httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncProtocolExec.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ public AsyncDataConsumer handleResponse(
217217
if (needAuthentication(
218218
targetAuthExchange,
219219
proxyAuthExchange,
220-
proxy != null ? proxy : target,
220+
// A 407 received through a tunnel comes from the origin, not from the proxy
221+
route.isTunnelled() ? null : proxy,
221222
target,
222223
pathPrefix,
223224
response,
@@ -319,11 +320,11 @@ private boolean needAuthentication(
319320
}
320321
}
321322

322-
final boolean proxyAuthRequested = authenticator.isChallenged(
323+
final boolean proxyAuthRequested = proxy != null && authenticator.isChallenged(
323324
proxy, ChallengeType.PROXY, response, proxyAuthExchange, context);
324-
final boolean proxyMutualAuthRequired = authenticator.isChallengeExpected(proxyAuthExchange);
325+
final boolean proxyMutualAuthRequired = proxy != null && authenticator.isChallengeExpected(proxyAuthExchange);
325326

326-
if (authCacheKeeper != null) {
327+
if (proxy != null && authCacheKeeper != null) {
327328
if (proxyAuthRequested) {
328329
authCacheKeeper.updateOnChallenge(proxy, null, proxyAuthExchange, context);
329330
} else {

‎httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/ProtocolExec.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ public ClassicHttpResponse execute(
207207
if (needAuthentication(
208208
targetAuthExchange,
209209
proxyAuthExchange,
210-
proxy != null ? proxy : target,
210+
// A 407 received through a tunnel comes from the origin, not from the proxy
211+
route.isTunnelled() ? null : proxy,
211212
target,
212213
pathPrefix,
213214
response,
@@ -280,11 +281,11 @@ private boolean needAuthentication(
280281
}
281282
}
282283

283-
final boolean proxyAuthRequested = authenticator.isChallenged(
284+
final boolean proxyAuthRequested = proxy != null && authenticator.isChallenged(
284285
proxy, ChallengeType.PROXY, response, proxyAuthExchange, context);
285-
final boolean proxyMutualAuthRequired = authenticator.isChallengeExpected(proxyAuthExchange);
286+
final boolean proxyMutualAuthRequired = proxy != null && authenticator.isChallengeExpected(proxyAuthExchange);
286287

287-
if (authCacheKeeper != null) {
288+
if (proxy != null && authCacheKeeper != null) {
288289
if (proxyAuthRequested) {
289290
authCacheKeeper.updateOnChallenge(proxy, null, proxyAuthExchange, context);
290291
} else {

‎httpclient5/src/test/java/org/apache/hc/client5/http/impl/async/TestAsyncProtocolExec.java‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@
2727
package org.apache.hc.client5.http.impl.async;
2828

2929
import java.io.IOException;
30+
import java.util.ArrayList;
3031
import java.util.Collections;
32+
import java.util.List;
3133
import java.util.concurrent.atomic.AtomicInteger;
3234

3335
import org.apache.hc.client5.http.AuthenticationStrategy;
3436
import org.apache.hc.client5.http.HttpRoute;
37+
import org.apache.hc.client5.http.RouteInfo;
3538
import org.apache.hc.client5.http.async.AsyncExecCallback;
3639
import org.apache.hc.client5.http.async.AsyncExecChain;
3740
import org.apache.hc.client5.http.async.AsyncExecRuntime;
3841
import org.apache.hc.client5.http.auth.AuthExchange;
3942
import org.apache.hc.client5.http.auth.AuthScope;
4043
import org.apache.hc.client5.http.auth.ChallengeType;
4144
import org.apache.hc.client5.http.auth.StandardAuthScheme;
45+
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
4246
import org.apache.hc.client5.http.impl.auth.BasicScheme;
4347
import org.apache.hc.client5.http.impl.auth.CredentialsProviderBuilder;
4448
import org.apache.hc.client5.http.protocol.HttpClientContext;
@@ -157,4 +161,111 @@ public void failed(final Exception cause) {
157161
final AuthExchange authExchange = context.getAuthExchange(target);
158162
Assertions.assertEquals("/blah/", authExchange.getPathPrefix());
159163
}
164+
165+
private static final class RecordingCallback implements AsyncExecCallback {
166+
167+
private final List<Integer> codes = new ArrayList<>();
168+
private final AtomicInteger completed = new AtomicInteger(0);
169+
170+
@Override
171+
public AsyncDataConsumer handleResponse(final HttpResponse response, final EntityDetails entityDetails) {
172+
codes.add(response.getCode());
173+
return Mockito.mock(AsyncDataConsumer.class);
174+
}
175+
176+
@Override
177+
public void handleInformationResponse(final HttpResponse response) {
178+
}
179+
180+
@Override
181+
public void completed() {
182+
completed.incrementAndGet();
183+
}
184+
185+
@Override
186+
public void failed(final Exception cause) {
187+
Assertions.fail(cause);
188+
}
189+
190+
}
191+
192+
private void stubChain(final AtomicInteger proceedCount, final List<HttpRequest> requests, final int... codes)
193+
throws Exception {
194+
Mockito.doAnswer(invocation -> {
195+
requests.add(invocation.getArgument(0, HttpRequest.class));
196+
final AsyncExecCallback cb = invocation.getArgument(3, AsyncExecCallback.class);
197+
final int code = codes[Math.min(proceedCount.getAndIncrement(), codes.length - 1)];
198+
final HttpResponse resp = new BasicHttpResponse(code, "Huh?");
199+
if (code == 407) {
200+
resp.setHeader(HttpHeaders.PROXY_AUTHENTICATE, StandardAuthScheme.BASIC + " realm=test");
201+
}
202+
cb.handleResponse(resp, (EntityDetails) null);
203+
cb.completed();
204+
return null;
205+
}).when(chain).proceed(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
206+
}
207+
208+
@Test
209+
void testProxyChallengeThroughTunnelWithAuthCacheIsReturnedToCaller() throws Exception {
210+
final AsyncProtocolExec cachingExec = new AsyncProtocolExec(targetAuthStrategy, proxyAuthStrategy, null, false);
211+
final HttpHost httpsTarget = new HttpHost("https", "foo", 443);
212+
final HttpHost proxy = new HttpHost("http", "proxy", 8080);
213+
final HttpRoute route = new HttpRoute(httpsTarget, null, proxy, true,
214+
RouteInfo.TunnelType.TUNNELLED, RouteInfo.LayerType.LAYERED);
215+
final BasicAuthCache authCache = new BasicAuthCache();
216+
final HttpClientContext context = HttpClientContext.create();
217+
context.setAuthCache(authCache);
218+
context.setCredentialsProvider(CredentialsProviderBuilder.create()
219+
.add(new AuthScope(proxy), "proxyuser", "proxypass".toCharArray())
220+
.build());
221+
Mockito.when(execRuntime.isEndpointConnected()).thenReturn(true);
222+
final AtomicInteger proceedCount = new AtomicInteger(0);
223+
final List<HttpRequest> requests = new ArrayList<>();
224+
stubChain(proceedCount, requests, 407);
225+
226+
final HttpRequest request = BasicRequestBuilder.get("https://foo/test").build();
227+
final AsyncExecChain.Scope scope = new AsyncExecChain.Scope("test", route, request,
228+
Mockito.mock(CancellableDependency.class), context, execRuntime, null, new AtomicInteger(1));
229+
final RecordingCallback callback = new RecordingCallback();
230+
231+
cachingExec.execute(request, (AsyncEntityProducer) null, scope, chain, callback);
232+
233+
Assertions.assertEquals(Collections.singletonList(407), callback.codes);
234+
Assertions.assertEquals(1, callback.completed.get());
235+
Assertions.assertEquals(1, proceedCount.get());
236+
Assertions.assertNull(requests.get(0).getFirstHeader(HttpHeaders.PROXY_AUTHORIZATION));
237+
Mockito.verifyNoInteractions(proxyAuthStrategy);
238+
Assertions.assertEquals(AuthExchange.State.UNCHALLENGED, context.getAuthExchange(proxy).getState());
239+
Assertions.assertNull(authCache.get(proxy));
240+
}
241+
242+
@Test
243+
void testProxyChallengeOnPlainRouteStillAuthenticates() throws Exception {
244+
final HttpHost proxy = new HttpHost("http", "proxy", 8080);
245+
final HttpRoute route = new HttpRoute(target, null, proxy, false);
246+
final HttpClientContext context = HttpClientContext.create();
247+
context.setCredentialsProvider(CredentialsProviderBuilder.create()
248+
.add(new AuthScope(proxy), "proxyuser", "proxypass".toCharArray())
249+
.build());
250+
Mockito.when(proxyAuthStrategy.select(
251+
Mockito.eq(ChallengeType.PROXY),
252+
Mockito.any(),
253+
Mockito.<HttpClientContext>any()))
254+
.thenReturn(Collections.singletonList(new BasicScheme()));
255+
Mockito.when(execRuntime.isEndpointConnected()).thenReturn(true);
256+
final AtomicInteger proceedCount = new AtomicInteger(0);
257+
final List<HttpRequest> requests = new ArrayList<>();
258+
stubChain(proceedCount, requests, 407, 200);
259+
260+
final HttpRequest request = BasicRequestBuilder.get("http://foo/test").build();
261+
final AsyncExecChain.Scope scope = new AsyncExecChain.Scope("test", route, request,
262+
Mockito.mock(CancellableDependency.class), context, execRuntime, null, new AtomicInteger(1));
263+
final RecordingCallback callback = new RecordingCallback();
264+
265+
protocolExec.execute(request, (AsyncEntityProducer) null, scope, chain, callback);
266+
267+
Assertions.assertEquals(2, proceedCount.get());
268+
Assertions.assertEquals(Collections.singletonList(200), callback.codes);
269+
Assertions.assertNotNull(requests.get(1).getFirstHeader(HttpHeaders.PROXY_AUTHORIZATION));
270+
}
160271
}

‎httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestProtocolExec.java‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import org.apache.hc.client5.http.AuthenticationStrategy;
3636
import org.apache.hc.client5.http.HttpRoute;
37+
import org.apache.hc.client5.http.RouteInfo;
3738
import org.apache.hc.client5.http.auth.AuthExchange;
3839
import org.apache.hc.client5.http.auth.AuthScope;
3940
import org.apache.hc.client5.http.auth.ChallengeType;
@@ -43,6 +44,7 @@
4344
import org.apache.hc.client5.http.classic.methods.HttpGet;
4445
import org.apache.hc.client5.http.classic.methods.HttpPost;
4546
import org.apache.hc.client5.http.entity.EntityBuilder;
47+
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
4648
import org.apache.hc.client5.http.impl.auth.BasicScheme;
4749
import org.apache.hc.client5.http.impl.auth.CredentialsProviderBuilder;
4850
import org.apache.hc.client5.http.protocol.HttpClientContext;
@@ -58,6 +60,7 @@
5860
import org.junit.jupiter.api.BeforeEach;
5961
import org.junit.jupiter.api.Test;
6062
import org.junit.jupiter.api.extension.ExtendWith;
63+
import org.mockito.ArgumentCaptor;
6164
import org.mockito.Mock;
6265
import org.mockito.Mockito;
6366
import org.mockito.junit.jupiter.MockitoExtension;
@@ -295,4 +298,105 @@ void testAuthExchangePathPrefixRestoredAfterChallenge() throws Exception {
295298
final AuthExchange authExchange = context.getAuthExchange(target);
296299
Assertions.assertEquals("/blah/", authExchange.getPathPrefix());
297300
}
301+
302+
@Test
303+
void testProxyChallengeThroughTunnelIsReturnedToCaller() throws Exception {
304+
final HttpHost httpsTarget = new HttpHost("https", "foo", 443);
305+
final HttpHost proxy = new HttpHost("http", "proxy", 8080);
306+
final HttpRoute route = new HttpRoute(httpsTarget, null, proxy, true,
307+
RouteInfo.TunnelType.TUNNELLED, RouteInfo.LayerType.LAYERED);
308+
final HttpClientContext context = HttpClientContext.create();
309+
context.setCredentialsProvider(CredentialsProviderBuilder.create()
310+
.add(new AuthScope(proxy), "proxyuser", "proxypass".toCharArray())
311+
.build());
312+
final ClassicHttpRequest request = new HttpGet("https://foo/test");
313+
final ClassicHttpResponse response = new BasicClassicHttpResponse(407, "Huh?");
314+
response.setHeader(HttpHeaders.PROXY_AUTHENTICATE, StandardAuthScheme.BASIC + " realm=test");
315+
316+
Mockito.when(chain.proceed(Mockito.same(request), Mockito.any())).thenReturn(response);
317+
318+
final ExecChain.Scope scope = new ExecChain.Scope("test", route, request, execRuntime, context);
319+
final ClassicHttpResponse finalResponse = protocolExec.execute(request, scope, chain);
320+
Assertions.assertEquals(407, finalResponse.getCode());
321+
Mockito.verify(chain, Mockito.times(1)).proceed(request, scope);
322+
Mockito.verifyNoInteractions(proxyAuthStrategy);
323+
Assertions.assertNull(context.getAuthExchange(proxy).getAuthScheme());
324+
}
325+
326+
@Test
327+
void testProxyChallengeWithoutProxyIsReturnedToCaller() throws Exception {
328+
final HttpRoute route = new HttpRoute(target);
329+
final HttpClientContext context = HttpClientContext.create();
330+
context.setCredentialsProvider(CredentialsProviderBuilder.create()
331+
.add(new AuthScope(target), "user", "pass".toCharArray())
332+
.build());
333+
final ClassicHttpRequest request = new HttpGet("http://foo/test");
334+
final ClassicHttpResponse response = new BasicClassicHttpResponse(407, "Huh?");
335+
response.setHeader(HttpHeaders.PROXY_AUTHENTICATE, StandardAuthScheme.BASIC + " realm=test");
336+
337+
Mockito.when(chain.proceed(Mockito.same(request), Mockito.any())).thenReturn(response);
338+
339+
final ExecChain.Scope scope = new ExecChain.Scope("test", route, request, execRuntime, context);
340+
final ClassicHttpResponse finalResponse = protocolExec.execute(request, scope, chain);
341+
Assertions.assertEquals(407, finalResponse.getCode());
342+
Mockito.verify(chain, Mockito.times(1)).proceed(request, scope);
343+
Mockito.verifyNoInteractions(proxyAuthStrategy);
344+
}
345+
346+
@Test
347+
void testProxyChallengeThroughTunnelWithAuthCacheIsReturnedToCaller() throws Exception {
348+
final ProtocolExec cachingExec = new ProtocolExec(targetAuthStrategy, proxyAuthStrategy, null, false);
349+
final HttpHost httpsTarget = new HttpHost("https", "foo", 443);
350+
final HttpHost proxy = new HttpHost("http", "proxy", 8080);
351+
final HttpRoute route = new HttpRoute(httpsTarget, null, proxy, true,
352+
RouteInfo.TunnelType.TUNNELLED, RouteInfo.LayerType.LAYERED);
353+
final BasicAuthCache authCache = new BasicAuthCache();
354+
final HttpClientContext context = HttpClientContext.create();
355+
context.setAuthCache(authCache);
356+
context.setCredentialsProvider(CredentialsProviderBuilder.create()
357+
.add(new AuthScope(proxy), "proxyuser", "proxypass".toCharArray())
358+
.build());
359+
final ClassicHttpRequest request = new HttpGet("https://foo/test");
360+
final ClassicHttpResponse response = new BasicClassicHttpResponse(407, "Huh?");
361+
response.setHeader(HttpHeaders.PROXY_AUTHENTICATE, StandardAuthScheme.BASIC + " realm=test");
362+
363+
Mockito.when(chain.proceed(Mockito.same(request), Mockito.any())).thenReturn(response);
364+
365+
final ExecChain.Scope scope = new ExecChain.Scope("test", route, request, execRuntime, context);
366+
final ClassicHttpResponse finalResponse = cachingExec.execute(request, scope, chain);
367+
Assertions.assertSame(response, finalResponse);
368+
Mockito.verify(chain, Mockito.times(1)).proceed(request, scope);
369+
Mockito.verifyNoInteractions(proxyAuthStrategy);
370+
Assertions.assertNull(request.getFirstHeader(HttpHeaders.PROXY_AUTHORIZATION));
371+
Assertions.assertEquals(AuthExchange.State.UNCHALLENGED, context.getAuthExchange(proxy).getState());
372+
Assertions.assertNull(authCache.get(proxy));
373+
}
374+
375+
@Test
376+
void testProxyChallengeOnPlainRouteStillAuthenticates() throws Exception {
377+
final HttpHost proxy = new HttpHost("http", "proxy", 8080);
378+
final HttpRoute route = new HttpRoute(target, null, proxy, false);
379+
final HttpClientContext context = HttpClientContext.create();
380+
context.setCredentialsProvider(CredentialsProviderBuilder.create()
381+
.add(new AuthScope(proxy), "proxyuser", "proxypass".toCharArray())
382+
.build());
383+
final ClassicHttpRequest request = new HttpGet("http://foo/test");
384+
final ClassicHttpResponse response1 = new BasicClassicHttpResponse(407, "Huh?");
385+
response1.setHeader(HttpHeaders.PROXY_AUTHENTICATE, StandardAuthScheme.BASIC + " realm=test");
386+
final ClassicHttpResponse response2 = new BasicClassicHttpResponse(200, "OK");
387+
388+
Mockito.when(chain.proceed(Mockito.any(), Mockito.any())).thenReturn(response1, response2);
389+
Mockito.when(proxyAuthStrategy.select(
390+
Mockito.eq(ChallengeType.PROXY),
391+
Mockito.any(),
392+
Mockito.<HttpClientContext>any())).thenReturn(Collections.singletonList(new BasicScheme()));
393+
Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true);
394+
395+
final ExecChain.Scope scope = new ExecChain.Scope("test", route, request, execRuntime, context);
396+
final ClassicHttpResponse finalResponse = protocolExec.execute(request, scope, chain);
397+
Assertions.assertEquals(200, finalResponse.getCode());
398+
final ArgumentCaptor<ClassicHttpRequest> captor = ArgumentCaptor.forClass(ClassicHttpRequest.class);
399+
Mockito.verify(chain, Mockito.times(2)).proceed(captor.capture(), Mockito.same(scope));
400+
Assertions.assertNotNull(captor.getAllValues().get(1).getFirstHeader(HttpHeaders.PROXY_AUTHORIZATION));
401+
}
298402
}

0 commit comments

Comments
 (0)