From c681ebb8925c929b85a3171600244b216de4cbbe Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Mon, 16 Nov 2026 21:46:19 +0530 Subject: [PATCH 1/3] test(http): add failing test for Proxy-Authorization stripping when proxy changes Signed-off-by: Hardik Kaurani --- .../http/middleware/RedirectHandlerTests.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java index 35d213a3d..066e18957 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java @@ -259,6 +259,52 @@ void crossHostRedirectWithProxyKeepsProxyAuth() throws Exception { result.header("Proxy-Authorization")); // KEPT because proxy is still active (good) } + @Test + void redirectToUrlWithDifferentProxyStripsProxyAuth() throws Exception { + Request original = + new Request.Builder() + .url("http://trusted.example.com/api") + .addHeader("Authorization", "Bearer token") + .addHeader("Cookie", "session=SECRET") + .addHeader("Proxy-Authorization", "Basic ") + .build(); + Response redirect = + new Response.Builder() + .request(original) + .protocol(Protocol.HTTP_1_1) + .code(302) + .message("Found") + .header("Location", "http://other.example.com/api") + .body(ResponseBody.create("", MediaType.parse("text/plain"))) + .build(); + + // Setup proxy selector with different proxies for different hosts + Proxy proxyA = + new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy-a.example.com", 8080)); + Proxy proxyB = + new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy-b.example.com", 8080)); + ProxySelector proxySelector = mock(ProxySelector.class); + when(proxySelector.select( + argThat(uri -> uri != null && "other.example.com".equals(uri.getHost())))) + .thenReturn(Collections.singletonList(proxyB)); + when(proxySelector.select( + argThat(uri -> uri != null && "trusted.example.com".equals(uri.getHost())))) + .thenReturn(Collections.singletonList(proxyA)); + + RedirectHandlerOption option = new RedirectHandlerOption(); + Request result = + new RedirectHandler(option, proxySelector).getRedirect(original, redirect, option); + + assertNotNull(result); + assertEquals("other.example.com", result.url().host()); + assertNull(result.header("Authorization")); // stripped (different host) + assertNull(result.header("Cookie")); // stripped (different host) + assertNull( + result.header( + "Proxy-Authorization")); // stripped because proxy changed from proxyA to + // proxyB + } + @Test void crossHostRedirectWithDirectProxyStripsProxyAuth() throws Exception { Request original = From e1d563fe96eebc71facd7f12742d77dd55453a67 Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Mon, 16 Nov 2026 21:48:09 +0530 Subject: [PATCH 2/3] fix(http): remove Proxy-Authorization on redirect when the proxy changes Signed-off-by: Hardik Kaurani --- .../options/RedirectHandlerOption.java | 15 ++++-- .../http/middleware/RedirectHandlerTests.java | 50 +++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/options/RedirectHandlerOption.java b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/options/RedirectHandlerOption.java index d81e58b5a..66abee0ff 100644 --- a/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/options/RedirectHandlerOption.java +++ b/components/http/okHttp/src/main/java/com/microsoft/kiota/http/middleware/options/RedirectHandlerOption.java @@ -60,7 +60,8 @@ void scrubHeaders( /** * The default implementation for scrubbing sensitive headers during redirects. * This method removes Authorization and Cookie headers when the host, scheme, or port changes, - * and removes Proxy-Authorization headers when no proxy is configured or the proxy is bypassed for the new URL. + * and removes Proxy-Authorization headers when no proxy is configured, the proxy is bypassed for the new URL, + * or the new URL uses a different proxy than the original URL. */ @Nonnull public static final IScrubSensitiveHeaders DEFAULT_SCRUB_SENSITIVE_HEADERS = (requestBuilder, originalUrl, proxyResolver) -> { @@ -82,10 +83,14 @@ void scrubHeaders( requestBuilder.removeHeader("Cookie"); } - // Remove Proxy-Authorization if no proxy is configured or the URL is bypassed - boolean isProxyInactive = - proxyResolver == null || proxyResolver.apply(newUrl) == null; - if (isProxyInactive) { + // Remove Proxy-Authorization if no proxy is configured, the URL is bypassed, or the + // proxy changes + Proxy newProxy = proxyResolver == null ? null : proxyResolver.apply(newUrl); + boolean isProxyInactive = newProxy == null; + boolean isDifferentProxy = + !isProxyInactive + && !Objects.equals(newProxy, proxyResolver.apply(originalUrl)); + if (isProxyInactive || isDifferentProxy) { requestBuilder.removeHeader("Proxy-Authorization"); } }; diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java index 066e18957..77260436b 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java @@ -305,6 +305,56 @@ void redirectToUrlWithDifferentProxyStripsProxyAuth() throws Exception { // proxyB } + @Test + void multiHopRedirectBackToOriginalProxyStripsIntermediateProxyAuth() throws Exception { + Request hop2Request = + new Request.Builder() + .url("http://intermediate.example.com/api") + .addHeader("Authorization", "Bearer token-b") + .addHeader("Cookie", "session=B") + .addHeader("Proxy-Authorization", "Basic ") + .build(); + Response hop2Redirect = + new Response.Builder() + .request(hop2Request) + .protocol(Protocol.HTTP_1_1) + .code(302) + .message("Found") + .header("Location", "http://trusted.example.com/api2") + .body(ResponseBody.create("", MediaType.parse("text/plain"))) + .build(); + + Proxy proxyA = + new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy-a.example.com", 8080)); + Proxy proxyB = + new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy-b.example.com", 8080)); + ProxySelector proxySelector = mock(ProxySelector.class); + when(proxySelector.select( + argThat( + uri -> + uri != null + && "intermediate.example.com" + .equals(uri.getHost())))) + .thenReturn(Collections.singletonList(proxyB)); + when(proxySelector.select( + argThat(uri -> uri != null && "trusted.example.com".equals(uri.getHost())))) + .thenReturn(Collections.singletonList(proxyA)); + + RedirectHandlerOption option = new RedirectHandlerOption(); + Request result = + new RedirectHandler(option, proxySelector) + .getRedirect(hop2Request, hop2Redirect, option); + + assertNotNull(result); + assertEquals("trusted.example.com", result.url().host()); + assertNull(result.header("Authorization")); // stripped (different host) + assertNull(result.header("Cookie")); // stripped (different host) + assertNull( + result.header( + "Proxy-Authorization")); // stripped because proxy changed from proxyB to + // proxyA + } + @Test void crossHostRedirectWithDirectProxyStripsProxyAuth() throws Exception { Request original = From 3176a51510d5dbcf06949f048fb1c2128ddeb05e Mon Sep 17 00:00:00 2001 From: Hardik Kaurani Date: Mon, 16 Nov 2026 22:21:09 +0530 Subject: [PATCH 3/3] test(http): drive intercept through A-B-A redirect loop to verify proxy auth scrubbing Signed-off-by: Hardik Kaurani --- .../http/middleware/RedirectHandlerTests.java | 112 ++++++++++++++---- 1 file changed, 87 insertions(+), 25 deletions(-) diff --git a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java index 77260436b..18543e220 100644 --- a/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java +++ b/components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java @@ -18,9 +18,12 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.ArgumentCaptor; import java.net.*; import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; @SuppressWarnings("resource") public class RedirectHandlerTests { @@ -307,21 +310,12 @@ void redirectToUrlWithDifferentProxyStripsProxyAuth() throws Exception { @Test void multiHopRedirectBackToOriginalProxyStripsIntermediateProxyAuth() throws Exception { - Request hop2Request = + Request initialRequest = new Request.Builder() - .url("http://intermediate.example.com/api") - .addHeader("Authorization", "Bearer token-b") - .addHeader("Cookie", "session=B") - .addHeader("Proxy-Authorization", "Basic ") - .build(); - Response hop2Redirect = - new Response.Builder() - .request(hop2Request) - .protocol(Protocol.HTTP_1_1) - .code(302) - .message("Found") - .header("Location", "http://trusted.example.com/api2") - .body(ResponseBody.create("", MediaType.parse("text/plain"))) + .url("http://trusted.example.com/api") + .addHeader("Authorization", "Bearer token-a") + .addHeader("Cookie", "session=A") + .addHeader("Proxy-Authorization", "Basic proxy-a-secret") .build(); Proxy proxyA = @@ -340,19 +334,87 @@ void multiHopRedirectBackToOriginalProxyStripsIntermediateProxyAuth() throws Exc argThat(uri -> uri != null && "trusted.example.com".equals(uri.getHost())))) .thenReturn(Collections.singletonList(proxyA)); + Interceptor.Chain mockChain = mock(Interceptor.Chain.class); + when(mockChain.request()).thenReturn(initialRequest); + + AtomicInteger hopCount = new AtomicInteger(0); + when(mockChain.proceed(any(Request.class))) + .thenAnswer( + invocation -> { + int hop = hopCount.incrementAndGet(); + Request currentRequest = invocation.getArgument(0); + if (hop == 1) { + return new Response.Builder() + .request(currentRequest) + .protocol(Protocol.HTTP_1_1) + .code(302) + .message("Found") + .header("Location", "http://intermediate.example.com/api") + .body( + ResponseBody.create( + "", MediaType.parse("text/plain"))) + .build(); + } else if (hop == 2) { + // Simulate downstream proxy handler or authenticator attaching + // proxy B's credentials to the request that executed through proxy + // B + Request reqWithProxyB = + currentRequest + .newBuilder() + .header( + "Proxy-Authorization", + "Basic proxy-b-secret") + .build(); + return new Response.Builder() + .request(reqWithProxyB) + .protocol(Protocol.HTTP_1_1) + .code(302) + .message("Found") + .header("Location", "http://trusted.example.com/api2") + .body( + ResponseBody.create( + "", MediaType.parse("text/plain"))) + .build(); + } else { + return new Response.Builder() + .request(currentRequest) + .protocol(Protocol.HTTP_1_1) + .code(200) + .message("OK") + .body( + ResponseBody.create( + "success", MediaType.parse("text/plain"))) + .build(); + } + }); + RedirectHandlerOption option = new RedirectHandlerOption(); - Request result = - new RedirectHandler(option, proxySelector) - .getRedirect(hop2Request, hop2Redirect, option); + RedirectHandler handler = new RedirectHandler(option, proxySelector); + Response response = handler.intercept(mockChain); - assertNotNull(result); - assertEquals("trusted.example.com", result.url().host()); - assertNull(result.header("Authorization")); // stripped (different host) - assertNull(result.header("Cookie")); // stripped (different host) - assertNull( - result.header( - "Proxy-Authorization")); // stripped because proxy changed from proxyB to - // proxyA + assertNotNull(response); + assertEquals(200, response.code()); + + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(Request.class); + verify(mockChain, times(3)).proceed(requestCaptor.capture()); + List capturedRequests = requestCaptor.getAllValues(); + + // 1st request (initial): sent to trusted.example.com with proxy A's credentials + assertEquals("trusted.example.com", capturedRequests.get(0).url().host()); + assertEquals("Basic proxy-a-secret", capturedRequests.get(0).header("Proxy-Authorization")); + + // 2nd request (redirect hop 1): sent to intermediate.example.com; proxy A credentials + // stripped + assertEquals("intermediate.example.com", capturedRequests.get(1).url().host()); + assertNull(capturedRequests.get(1).header("Proxy-Authorization")); + + // 3rd request (redirect hop 2): sent back to trusted.example.com; proxy B credentials + // stripped + assertEquals("trusted.example.com", capturedRequests.get(2).url().host()); + assertEquals("/api2", capturedRequests.get(2).url().encodedPath()); + assertNull(capturedRequests.get(2).header("Proxy-Authorization")); + assertNull(capturedRequests.get(2).header("Authorization")); + assertNull(capturedRequests.get(2).header("Cookie")); } @Test