Skip to content
Merged
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 @@ -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) -> {
Expand All @@ -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");
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -259,6 +262,161 @@ 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 <secret>")
.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 multiHopRedirectBackToOriginalProxyStripsIntermediateProxyAuth() throws Exception {
Request initialRequest =
new Request.Builder()
.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 =
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));

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();
RedirectHandler handler = new RedirectHandler(option, proxySelector);
Response response = handler.intercept(mockChain);

assertNotNull(response);
assertEquals(200, response.code());

ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
verify(mockChain, times(3)).proceed(requestCaptor.capture());
List<Request> 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
void crossHostRedirectWithDirectProxyStripsProxyAuth() throws Exception {
Request original =
Expand Down
Loading