From f8457ad3a7a45f051c7085d8c759356921c4d051 Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Wed, 25 Nov 2026 20:42:43 +0100 Subject: [PATCH 1/3] Keep the raw URI when a request changes proxy When a retry, redirect or replay is routed through another proxy than the attempt before, a Proxy-Authorization header the request carries was meant for the previous proxy, so the request is copied without it. That copy went through Request.toBuilder(), which always uses the fixing URI encoder. A request built with URL encoding disabled was therefore sent to the new proxy encoded again: GET http://host/a|b?q={x} became GET http://host/a%7Cb?q=%7Bx%7D. Copy a DefaultRequest with the raw encoder instead, as #2393 does when it adds a Range header. The URI of a built request is already encoded, so the raw encoder returns it unchanged. Subclasses and other Request implementations still go through toBuilder(), as they may override what it copies. Claude Code on behalf of Matthias Kurz Co-Authored-By: Claude Opus 5.5 --- .../netty/request/NettyRequestSender.java | 18 ++++++++++++- .../netty/request/ProxyReselectionTest.java | 26 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java index acdf0c8ff..156cf9ac7 100755 --- a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java +++ b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java @@ -46,11 +46,13 @@ import org.asynchttpclient.AsyncHandler; import org.asynchttpclient.AsyncHttpClientConfig; import org.asynchttpclient.AsyncHttpClientState; +import org.asynchttpclient.DefaultRequest; import org.asynchttpclient.ListenableFuture; import org.asynchttpclient.Realm; import org.asynchttpclient.Realm.AuthScheme; import org.asynchttpclient.Request; import org.asynchttpclient.RequestBuilder; +import org.asynchttpclient.RequestBuilderBase; import org.asynchttpclient.LoadBalance; import org.asynchttpclient.StoreCookies; import org.asynchttpclient.exception.FilterException; @@ -253,7 +255,10 @@ private static Request moveToProxy(Request request, NettyResponseFuture futur if (!headers.contains(HttpHeaderNames.PROXY_AUTHORIZATION)) { return request; } - Request withoutProxyAuthorization = request.toBuilder() + // Exact class only: a subclass or another Request implementation may override what toBuilder() copies. + RequestBuilderBase builder = request.getClass() == DefaultRequest.class + ? new PrototypeRequestBuilder(request) : request.toBuilder(); + Request withoutProxyAuthorization = builder .setHeaders(headers.copy().remove(HttpHeaderNames.PROXY_AUTHORIZATION)) .build(); future.setTargetRequest(withoutProxyAuthorization); @@ -1998,4 +2003,15 @@ public void call() { } }); } + + /** + * Copies a request as {@link Request#toBuilder()} does, but keeps its URI as it is: {@code toBuilder()} encodes + * the URI again, which changes one built with URL encoding disabled. + */ + private static final class PrototypeRequestBuilder extends RequestBuilderBase { + + private PrototypeRequestBuilder(Request prototype) { + super(prototype, true, false); + } + } } diff --git a/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java b/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java index 360c6529c..79342c353 100644 --- a/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java +++ b/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java @@ -539,6 +539,32 @@ void aProxyLoginTheRequestCarriesIsNotTunnelledToTheOriginAfterAMove() throws Ex } } + @Test + void aRequestMovedOffAProxyKeepsAUriBuiltWithoutEncoding() throws Exception { + AtomicBoolean down = new AtomicBoolean(); + try (Peer proxyA = new Peer(); Peer proxyB = new Peer().answer(s -> ok("via-b"))) { + proxyA.answer(s -> { + down.set(true); + return DROP; + }); + ProxyServer a = proxyServer("127.0.0.1", proxyA.port()).build(); + ProxyServer b = proxyServer("127.0.0.1", proxyB.port()).build(); + + try (AsyncHttpClient client = asyncHttpClient(client() + .setMaxRequestRetry(1) + .setDisableUrlEncodingForBoundRequests(true) + .setProxyServerSelector(uri -> down.get() ? b : a))) { + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxyA.port() + "/a|b?q={x}") + .setHeader("Proxy-Authorization", ALICE).execute()); + + assertEquals("via-b", response(outcome).getResponseBody()); + assertEquals(proxyA.seen.get(0).requestLine, proxyB.seen.get(0).requestLine, + "the request was encoded again when it moved to proxy B"); + assertNull(proxyB.seen.get(0).proxyAuthorization(), proxyB.seen::toString); + } + } + } + @Test void anAnswerToAnOriginChallengeGoesBackThroughTheSameProxy() throws Exception { AtomicInteger requests = new AtomicInteger(); From 4d7447d7abf106a25767c0810bb7a6200bd1c0e0 Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Wed, 25 Nov 2026 20:46:34 +0100 Subject: [PATCH 2/3] Check that a tunnel is pooled under its proxy The test for a selector that alternates between two proxies only counted CONNECTs. A tunnel entry that followed the selector to the other proxy would still write the request on the channel it holds, so it opens no second tunnel and the test passed, while the channel is then pooled under the other proxy's key. Send a second request through the proxy that opened the tunnel: it has to take that tunnel from the pool. Claude Code on behalf of Matthias Kurz Co-Authored-By: Claude Opus 5.5 --- .../netty/request/ProxyReselectionTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java b/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java index 79342c353..a61d37441 100644 --- a/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java +++ b/client/src/test/java/org/asynchttpclient/netty/request/ProxyReselectionTest.java @@ -322,6 +322,14 @@ void aSelectorThatAlternatesKeepsATunnelOnTheProxyThatOpenedIt() throws Exceptio assertEquals("over-tls", response(outcome).getResponseBody()); assertEquals(1, proxyA.seen.size() + proxyB.seen.size(), () -> "a tunnel was opened again: a=" + proxyA.seen + " b=" + proxyB.seen); + + // Selected through A again, so it takes the tunnel A opened from the pool. + Object next = outcome(client.prepareGet("https://127.0.0.1:" + origin.port() + "/y").execute()); + + assertEquals("over-tls", response(next).getResponseBody()); + assertEquals(1, proxyA.seen.size() + proxyB.seen.size(), + () -> "the tunnel was not pooled under the proxy that opened it: a=" + proxyA.seen + + " b=" + proxyB.seen); } } } From 695d5b5beebc6666719bf48e937ceac0f44962ae Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Wed, 25 Nov 2026 21:08:17 +0100 Subject: [PATCH 3/3] Stop a stale 407 flag from forcing a CONNECT A 407 answered on a kept-alive connection sets connectAllowed, so that its retry sends a CONNECT on the connection it holds. Only a tunnel that opens clears the flag again, so after a 407 on a plain http request it stays set for the rest of the exchange, and a redirect or replay to an https URL then sends its request as a CONNECT too. Since #2408, that attempt keeps the proxy's login after a cross-origin hop, or takes the login of the proxy it moves to, so it finds a tunnel an earlier exchange pooled to the new target. Until #2410 the CONNECT was written into that tunnel, to the origin, with a preemptive Proxy-Authorization and the proxy's custom headers. Since #2410 a CONNECT never takes a pooled channel, so the attempt opens another connection and tunnel to the proxy instead, and the pooled tunnel goes unused. Under a connection limit, that connection can be one too many: with maxConnections 2, a pooled tunnel and the plain http request hold both permits, and the redirect fails with TooManyConnectionsException although the tunnel it could take is idle in the pool. Clear the flag whenever an attempt sends the target, so that only the 407's own retry sees it, and the target takes the pooled tunnel, as it does when no 407 came before. Claude Code on behalf of Matthias Kurz Co-Authored-By: Claude Opus 5.5 --- .../netty/request/NettyRequestSender.java | 5 + .../netty/request/ProxyReselectionTest.java | 118 ++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java index 156cf9ac7..cc3eae562 100755 --- a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java +++ b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java @@ -182,6 +182,11 @@ public ListenableFuture sendRequest(Request request, final AsyncHandler FilterContext filter(FilterContext ctx) { } } + @Test + void aRedirectAfterAProxyChallengeKeepsAnotherProxysLoginOutOfAPooledTunnel() throws Exception { + try (Peer origin = new Peer(serverTls()).answer(s -> s.connect() ? status(405) : ok(s.target())); + Peer proxyA = new Peer(); Peer proxyB = new Peer()) { + proxyA.answer(s -> ALICE.equals(s.proxyAuthorization()) + ? redirect("https://localhost:" + origin.port() + "/landing", false) : challenge("Basic realm=\"a\"")); + proxyB.answer(s -> s.connect() && BOB.equals(s.proxyAuthorization()) ? TUNNEL : status(502)); + ProxyServer a = proxy(proxyA, "alice", "A-secret"); + ProxyServer b = proxyServer("127.0.0.1", proxyB.port()) + .setRealm(basicAuthRealm("bob", "B-secret").setUsePreemptiveAuth(true)).build(); + + try (AsyncHttpClient client = asyncHttpClient(client() + .setProxyServerSelector(uri -> uri.isSecured() ? b : a))) { + // Pools a tunnel to the origin through B. + assertEquals("/warm", response(outcome(client.prepareGet("https://localhost:" + origin.port() + "/warm") + .execute())).getResponseBody()); + + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxyA.port() + "/start").execute()); + + assertTrue(origin.seen.stream().noneMatch(s -> s.connect() || s.proxyAuthorization() != null), + () -> "proxy B's CONNECT went into the tunnel to the origin: " + origin.seen); + assertEquals("/landing", response(outcome).getResponseBody()); + assertTrue(origin.seen.stream().allMatch(s -> s.connection == 1), + () -> "the redirect did not take the pooled tunnel: " + origin.seen); + assertEquals(1, proxyB.seen.size(), proxyB.seen::toString); + } + } + } + + @Test + void aRedirectAfterAProxyChallengeTakesAPooledTunnel() throws Exception { + try (Peer origin = new Peer(serverTls()).answer(s -> s.connect() ? status(405) : ok(s.target())); + Peer proxy = new Peer()) { + proxy.answer(s -> !ALICE.equals(s.proxyAuthorization()) ? challenge("Basic realm=\"p\"") + : s.connect() ? TUNNEL + : redirect("https://localhost:" + origin.port() + "/landing", false)); + + try (AsyncHttpClient client = asyncHttpClient(client().setProxyServer(proxy(proxy, "alice", "A-secret")))) { + assertEquals("/warm", response(outcome(client.prepareGet("https://localhost:" + origin.port() + "/warm") + .execute())).getResponseBody()); + + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxy.port() + "/start").execute()); + + assertTrue(origin.seen.stream().noneMatch(Seen::connect), + () -> "a CONNECT went into the tunnel to the origin: " + origin.seen); + assertEquals("/landing", response(outcome).getResponseBody()); + assertTrue(origin.seen.stream().allMatch(s -> s.connection == 1), + () -> "the redirect did not take the pooled tunnel: " + origin.seen); + assertTrue(proxy.seen.stream().allMatch(s -> s.connection <= 2), + () -> "the redirect opened another connection to the proxy: " + proxy.seen); + } + } + } + + @Test + void aRedirectAfterAProxyChallengeReusesATunnelAtTheConnectionLimit() throws Exception { + try (Peer origin = new Peer(serverTls()).answer(s -> s.connect() ? status(405) : ok(s.target())); + Peer proxy = new Peer()) { + proxy.answer(s -> !ALICE.equals(s.proxyAuthorization()) ? challenge("Basic realm=\"p\"") + : s.connect() ? TUNNEL + : redirect("https://localhost:" + origin.port() + "/landing", false)); + + try (AsyncHttpClient client = asyncHttpClient(client().setMaxConnections(2) + .setProxyServer(proxy(proxy, "alice", "A-secret")))) { + assertEquals("/warm", response(outcome(client.prepareGet("https://localhost:" + origin.port() + "/warm") + .execute())).getResponseBody()); + + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxy.port() + "/start").execute()); + + assertEquals("/landing", response(outcome).getResponseBody()); + assertTrue(origin.seen.stream().allMatch(s -> s.connection == 1), origin.seen::toString); + } + } + } + + @Test + void aRedirectAfterAProxyChallengeKeepsTheProxysHeadersOutOfAPooledTunnel() throws Exception { + try (Peer origin = new Peer(serverTls()).answer(s -> s.connect() ? status(405) : ok(s.target())); + Peer proxy = new Peer()) { + proxy.answer(s -> !ALICE.equals(s.proxyAuthorization()) ? challenge("Basic realm=\"p\"") + : s.connect() ? TUNNEL + : redirect("https://localhost:" + origin.port() + "/landing", false)); + ProxyServer withHeader = proxyServer("127.0.0.1", proxy.port()) + .setRealm(basicAuthRealm("alice", "A-secret")) + .setCustomHeaders(r -> new DefaultHttpHeaders().add("X-Proxy-Token", "proxy-secret")) + .build(); + + try (AsyncHttpClient client = asyncHttpClient(client().setProxyServer(withHeader))) { + assertEquals("/warm", response(outcome(client.prepareGet("https://localhost:" + origin.port() + "/warm") + .execute())).getResponseBody()); + + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxy.port() + "/start").execute()); + + assertTrue(origin.seen.stream().noneMatch(s -> s.headers.containsKey("x-proxy-token")), + () -> "the proxy's header reached the origin: " + origin.seen); + assertEquals("/landing", response(outcome).getResponseBody()); + } + } + } + + @Test + void aRedirectAfterAProxyChallengeOpensATunnelWhenNoneIsPooled() throws Exception { + try (Peer origin = new Peer(serverTls()).answer(s -> s.connect() ? status(405) : ok(s.target())); + Peer proxy = new Peer()) { + proxy.answer(s -> !ALICE.equals(s.proxyAuthorization()) ? challenge("Basic realm=\"p\"") + : s.connect() ? TUNNEL + : redirect("https://localhost:" + origin.port() + "/landing", false)); + + try (AsyncHttpClient client = asyncHttpClient(client().setProxyServer(proxy(proxy, "alice", "A-secret")))) { + Object outcome = outcome(client.prepareGet("http://127.0.0.1:" + proxy.port() + "/start").execute()); + + assertEquals("/landing", response(outcome).getResponseBody(), proxy.seen::toString); + assertTrue(origin.seen.stream().noneMatch(Seen::connect), origin.seen::toString); + } + } + } + @Test void aRedirectFromHttpsToHttpThroughTheSameProxyStillAuthenticatesToIt() throws Exception { try (Peer origin = new Peer(serverTls()); Peer proxy = new Peer()) {