Skip to content

Commit

Permalink
Propagate original request user-agent in proxy CONNECT requests (#1742)
Browse files Browse the repository at this point in the history
* Makes sure custom user-agent is propagated down to CONNECT requests when a proxy is in the way, ensuring all outgoing requests bear the correct user agent.

* Add a test for custom user agent with proxy
  • Loading branch information
TomGranot authored Jan 5, 2021
1 parent 7770c8b commit d24ee6a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public NettyRequest newNettyRequest(Request request, boolean performConnectReque
if (connect) {
// assign proxy-auth as configured on request
headers.set(PROXY_AUTHORIZATION, request.getHeaders().getAll(PROXY_AUTHORIZATION));
headers.set(USER_AGENT, request.getHeaders().getAll(USER_AGENT));

} else {
// assign headers as configured on request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static io.netty.handler.codec.http.HttpHeaderNames.PROXY_AUTHENTICATE;
import static io.netty.handler.codec.http.HttpHeaderNames.PROXY_AUTHORIZATION;
import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static org.asynchttpclient.Dsl.*;
import static org.asynchttpclient.test.TestUtils.addHttpConnector;
import static org.asynchttpclient.test.TestUtils.addHttpsConnector;
import static org.asynchttpclient.config.AsyncHttpClientConfigDefaults.*;

/**
* Test that validates that when having an HTTP proxy and trying to access an HTTPS through the proxy the proxy credentials should be passed during the CONNECT request.
* Test that validates that when having an HTTP proxy and trying to access an HTTPS
* through the proxy the proxy credentials and a custom user-agent (if set) should be passed during the CONNECT request.
*/
public class BasicHttpProxyToHttpsTest {

private static final Logger LOGGER = LoggerFactory.getLogger(BasicHttpProxyToHttpsTest.class);
private static final String CUSTOM_USER_AGENT = "custom-user-agent";

private int httpPort;
private int proxyPort;
Expand All @@ -66,13 +68,24 @@ public void setUpGlobal() throws Exception {
ConnectHandler connectHandler = new ConnectHandler() {

@Override
// This proxy receives a CONNECT request from the client before making the real request for the target host.
protected boolean handleAuthentication(HttpServletRequest request, HttpServletResponse response, String address) {

// If the userAgent of the CONNECT request is the same as the default userAgent,
// then the custom userAgent was not properly propagated and the test should fail.
String userAgent = request.getHeader(USER_AGENT.toString());
if(userAgent.equals(defaultUserAgent())) {
return false;
}

// If the authentication failed, the test should also fail.
String authorization = request.getHeader(PROXY_AUTHORIZATION.toString());
if (authorization == null) {
response.setStatus(HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
response.setHeader(PROXY_AUTHENTICATE.toString(), "Basic realm=\"Fake Realm\"");
return false;
} else if (authorization.equals("Basic am9obmRvZTpwYXNz")) {
}
else if (authorization.equals("Basic am9obmRvZTpwYXNz")) {
return true;
}
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
Expand All @@ -98,6 +111,7 @@ public void nonPreemptiveProxyAuthWithHttpsTarget() throws IOException, Interrup
String targetUrl = "https://localhost:" + httpPort + "/foo/bar";
Request request = get(targetUrl)
.setProxyServer(proxyServer("127.0.0.1", proxyPort).setRealm(realm(AuthScheme.BASIC, "johndoe", "pass")))
.setHeader("user-agent", CUSTOM_USER_AGENT)
// .setRealm(realm(AuthScheme.BASIC, "user", "passwd"))
.build();
Future<Response> responseFuture = client.executeRequest(request);
Expand All @@ -107,4 +121,4 @@ public void nonPreemptiveProxyAuthWithHttpsTarget() throws IOException, Interrup
Assert.assertEquals("/foo/bar", response.getHeader("X-pathInfo"));
}
}
}
}

0 comments on commit d24ee6a

Please sign in to comment.