Allow the host header to be included if addHostHeader is true. Fixes #3171 (#3177)

This commit is contained in:
Ryan Baxter
2018-09-05 20:39:45 -04:00
committed by GitHub
parent dd19bc5ba3
commit 6e8ef02a14
8 changed files with 92 additions and 32 deletions

View File

@@ -141,9 +141,7 @@ public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
@Bean
public ProxyRequestHelper proxyRequestHelper(ZuulProperties zuulProperties) {
ProxyRequestHelper helper = new ProxyRequestHelper();
helper.setIgnoredHeaders(zuulProperties.getIgnoredHeaders());
helper.setTraceRequestBody(zuulProperties.isTraceRequestBody());
ProxyRequestHelper helper = new ProxyRequestHelper(zuulProperties);
return helper;
}
@@ -171,12 +169,10 @@ public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
@Bean
public ProxyRequestHelper proxyRequestHelper(ZuulProperties zuulProperties) {
TraceProxyRequestHelper helper = new TraceProxyRequestHelper();
TraceProxyRequestHelper helper = new TraceProxyRequestHelper(zuulProperties);
if (this.traces != null) {
helper.setTraces(this.traces);
}
helper.setIgnoredHeaders(zuulProperties.getIgnoredHeaders());
helper.setTraceRequestBody(zuulProperties.isTraceRequestBody());
return helper;
}
}

View File

@@ -71,6 +71,18 @@ public class ProxyRequestHelper {
private boolean traceRequestBody = true;
private boolean addHostHeader = false;
@Deprecated
//TODO Remove in 2.1.x
public ProxyRequestHelper() {}
public ProxyRequestHelper(ZuulProperties zuulProperties) {
this.ignoredHeaders.addAll(zuulProperties.getIgnoredHeaders());
this.traceRequestBody = zuulProperties.isTraceRequestBody();
this.addHostHeader = zuulProperties.isAddHostHeader();
}
public void setWhitelistHosts(Set<String> whitelistHosts) {
this.whitelistHosts.addAll(whitelistHosts);
}
@@ -79,10 +91,14 @@ public class ProxyRequestHelper {
this.sensitiveHeaders.addAll(sensitiveHeaders);
}
@Deprecated
//TODO Remove in 2.1.x
public void setIgnoredHeaders(Set<String> ignoredHeaders) {
this.ignoredHeaders.addAll(ignoredHeaders);
}
@Deprecated
//TODO Remove in 2.1.x
public void setTraceRequestBody(boolean traceRequestBody) {
this.traceRequestBody = traceRequestBody;
}
@@ -208,6 +224,9 @@ public class ProxyRequestHelper {
}
switch (name) {
case "host":
if(addHostHeader) {
return true;
}
case "connection":
case "content-length":
case "server":

View File

@@ -48,6 +48,15 @@ import org.springframework.util.StringUtils;
public class TraceProxyRequestHelper extends ProxyRequestHelper {
private HttpTraceRepository traces;
@Deprecated
//TODO Remove in 2.1.x
public TraceProxyRequestHelper(){}
public TraceProxyRequestHelper(ZuulProperties zuulProperties) {
super(zuulProperties);
}
private final HttpExchangeTracer tracer = new HttpExchangeTracer(
Include.defaultIncludes());

View File

@@ -78,6 +78,8 @@ public class RibbonRoutingFilter extends ZuulFilter {
}
}
@Deprecated
//TODO Remove in 2.1.x
public RibbonRoutingFilter(RibbonCommandFactory<?> ribbonCommandFactory) {
this(new ProxyRequestHelper(), ribbonCommandFactory, null);
}

View File

@@ -44,6 +44,7 @@ import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.MockitoAnnotations.initMocks;
@@ -82,7 +83,7 @@ public class ProxyRequestHelperTests {
request.addHeader("multiName", "multiValue2");
RequestContext.getCurrentContext().setRequest(request);
TraceProxyRequestHelper helper = new TraceProxyRequestHelper();
TraceProxyRequestHelper helper = new TraceProxyRequestHelper(new ZuulProperties());
this.traceRepository = new InMemoryHttpTraceRepository();
helper.setTraces(this.traceRepository);
@@ -98,8 +99,9 @@ public class ProxyRequestHelperTests {
public void shouldDebugBodyDisabled() throws Exception {
RequestContext context = RequestContext.getCurrentContext();
ProxyRequestHelper helper = new ProxyRequestHelper();
helper.setTraceRequestBody(false);
ZuulProperties zuulProperties = new ZuulProperties();
zuulProperties.setTraceRequestBody(false);
ProxyRequestHelper helper = new ProxyRequestHelper(zuulProperties);
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false));
}
@@ -111,7 +113,7 @@ public class ProxyRequestHelperTests {
context.setChunkedRequestBody();
context.setRequest(request);
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false));
}
@@ -123,7 +125,7 @@ public class ProxyRequestHelperTests {
context.setZuulEngineRan();
context.setRequest(request);
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false));
}
@@ -135,7 +137,7 @@ public class ProxyRequestHelperTests {
RequestContext context = RequestContext.getCurrentContext();
context.setRequest(request);
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(true));
}
@@ -144,7 +146,7 @@ public class ProxyRequestHelperTests {
public void shouldDebugBodyNullRequest() throws Exception {
RequestContext context = RequestContext.getCurrentContext();
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(true));
}
@@ -156,7 +158,7 @@ public class ProxyRequestHelperTests {
RequestContext context = RequestContext.getCurrentContext();
context.setRequest(request);
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(true));
}
@@ -168,7 +170,7 @@ public class ProxyRequestHelperTests {
RequestContext context = RequestContext.getCurrentContext();
context.setRequest(request);
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false));
}
@@ -180,7 +182,7 @@ public class ProxyRequestHelperTests {
request.addHeader("multiName", "multiValue1");
request.addHeader("multiName", "multiValue2");
TraceProxyRequestHelper helper = new TraceProxyRequestHelper();
TraceProxyRequestHelper helper = new TraceProxyRequestHelper(new ZuulProperties());
helper.setTraces(this.traceRepository);
MultiValueMap<String, String> headers = helper.buildZuulRequestHeaders(request);
@@ -201,7 +203,7 @@ public class ProxyRequestHelperTests {
public void buildZuulRequestHeadersRequestsGzipAndOnlyGzip() {
MockHttpServletRequest request = new MockHttpServletRequest("", "/");
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
MultiValueMap<String, String> headers = helper.buildZuulRequestHeaders(request);
@@ -215,7 +217,7 @@ public class ProxyRequestHelperTests {
MockHttpServletRequest request = new MockHttpServletRequest("", "/");
request.addHeader("content-encoding", "identity");
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
MultiValueMap<String, String> headers = helper.buildZuulRequestHeaders(request);
@@ -229,7 +231,7 @@ public class ProxyRequestHelperTests {
MockHttpServletRequest request = new MockHttpServletRequest("", "/");
request.addHeader("accept-encoding", "identity");
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
MultiValueMap<String, String> headers = helper.buildZuulRequestHeaders(request);
@@ -238,6 +240,29 @@ public class ProxyRequestHelperTests {
assertThat(acceptEncodings, contains("identity"));
}
@Test
public void addHostHeader() {
MockHttpServletRequest request = new MockHttpServletRequest("", "/");
request.addHeader("host", "foo.com");
ZuulProperties zuulProperties = new ZuulProperties();
zuulProperties.setAddHostHeader(true);
ProxyRequestHelper helper = new ProxyRequestHelper(zuulProperties);
MultiValueMap<String, String> headers = helper.buildZuulRequestHeaders(request);
List<String> acceptEncodings = headers.get("host");
assertThat(acceptEncodings, hasSize(1));
assertThat(acceptEncodings, contains("foo.com"));
zuulProperties.setAddHostHeader(false);
helper = new ProxyRequestHelper(zuulProperties);
headers = helper.buildZuulRequestHeaders(request);
acceptEncodings = headers.get("host");
assertNull(acceptEncodings);
}
@Test
public void setResponseLowercase() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");
@@ -247,7 +272,7 @@ public class ProxyRequestHelperTests {
context.setRequest(request);
context.setResponse(response);
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_ENCODING.toLowerCase(), "gzip");
@@ -265,7 +290,7 @@ public class ProxyRequestHelperTests {
context.setRequest(request);
context.setResponse(response);
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, "text/plain");
@@ -287,7 +312,7 @@ public class ProxyRequestHelperTests {
context.setRequest(request);
context.setResponse(response);
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_ENCODING, "gzip");
@@ -302,7 +327,7 @@ public class ProxyRequestHelperTests {
params.add("a", "1234");
params.add("b", "5678");
String queryString = new ProxyRequestHelper().getQueryString(params);
String queryString = new ProxyRequestHelper(new ZuulProperties()).getQueryString(params);
assertThat(queryString, is("?a=1234&b=5678"));
}
@@ -312,7 +337,7 @@ public class ProxyRequestHelperTests {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("wsdl", "");
String queryString = new ProxyRequestHelper().getQueryString(params);
String queryString = new ProxyRequestHelper(new ZuulProperties()).getQueryString(params);
assertThat(queryString, is("?wsdl"));
}
@@ -322,7 +347,7 @@ public class ProxyRequestHelperTests {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("foo", "weird#chars");
String queryString = new ProxyRequestHelper().getQueryString(params);
String queryString = new ProxyRequestHelper(new ZuulProperties()).getQueryString(params);
assertThat(queryString, is("?foo=weird%23chars"));
}
@@ -334,7 +359,7 @@ public class ProxyRequestHelperTests {
params.add("foobar", "bam");
params.add("foo\fbar", "bat"); // form feed is the colon replacement char
String queryString = new ProxyRequestHelper().getQueryString(params);
String queryString = new ProxyRequestHelper(new ZuulProperties()).getQueryString(params);
assertThat(queryString, is("?foo:bar=baz&foobar=bam&foo%0Cbar=bat"));
}
@@ -350,7 +375,7 @@ public class ProxyRequestHelperTests {
context.setRequest(request);
context.set(REQUEST_URI_KEY, decodedURI);
final String requestURI = new ProxyRequestHelper().buildZuulRequestURI(request);
final String requestURI = new ProxyRequestHelper(new ZuulProperties()).buildZuulRequestURI(request);
assertThat(requestURI, equalTo(encodedURI));
}
@@ -364,7 +389,7 @@ public class ProxyRequestHelperTests {
context.setRequest(request);
context.set(REQUEST_URI_KEY, decodedURI);
final String requestURI = new ProxyRequestHelper().buildZuulRequestURI(request);
final String requestURI = new ProxyRequestHelper(new ZuulProperties()).buildZuulRequestURI(request);
assertThat(requestURI, equalTo(encodedURI));
}
@@ -378,7 +403,7 @@ public class ProxyRequestHelperTests {
RequestContext context = RequestContext.getCurrentContext();
context.set(REQUEST_URI_KEY, requestURI);
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
String uri = helper.buildZuulRequestURI(request);
@@ -394,7 +419,7 @@ public class ProxyRequestHelperTests {
RequestContext context = RequestContext.getCurrentContext();
context.set(REQUEST_URI_KEY, requestURI);
ProxyRequestHelper helper = new ProxyRequestHelper();
ProxyRequestHelper helper = new ProxyRequestHelper(new ZuulProperties());
String uri = helper.buildZuulRequestURI(request);

View File

@@ -28,6 +28,7 @@ import com.netflix.zuul.context.RequestContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.cloud.client.discovery.DiscoveryClient;
@@ -35,6 +36,8 @@ import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
import org.springframework.cloud.test.ClassPathExclusions;
import org.springframework.cloud.test.ModifiedClassPathRunner;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.util.MultiValueMap;
@@ -50,6 +53,10 @@ import static org.springframework.cloud.netflix.zuul.filters.support.FilterConst
/**
* @author Dave Syer
*/
@RunWith(ModifiedClassPathRunner.class)
//This is needed for sensitiveHeadersOverrideEmpty, if Spring Security is on the classpath
//then sensitive headers will always be present.
@ClassPathExclusions({"spring-security-*.jar"})
public class PreDecorationFilterTests {
private PreDecorationFilter filter;
@@ -69,6 +76,7 @@ public class PreDecorationFilterTests {
public void init() {
initMocks(this);
this.properties = new ZuulProperties();
this.proxyRequestHelper = new ProxyRequestHelper(properties);
this.routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
this.properties);
this.filter = new PreDecorationFilter(this.routeLocator, "/", this.properties,

View File

@@ -29,6 +29,7 @@ import org.junit.Test;
import org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext;
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -114,7 +115,7 @@ public class RibbonRoutingFilterTests {
private void setupRibbonRoutingFilter() {
RibbonCommandFactory factory = mock(RibbonCommandFactory.class);
filter = new RibbonRoutingFilter(new ProxyRequestHelper(), factory, Collections.<RibbonRequestCustomizer>emptyList());
filter = new RibbonRoutingFilter(new ProxyRequestHelper(new ZuulProperties()), factory, Collections.<RibbonRequestCustomizer>emptyList());
}
private ClientHttpResponse createClientHttpResponseWithNonStatus() {

View File

@@ -652,7 +652,7 @@ public class SimpleHostRoutingFilterTests {
SimpleHostRoutingFilter simpleHostRoutingFilter(ZuulProperties zuulProperties,
ApacheHttpClientConnectionManagerFactory connectionManagerFactory,
ApacheHttpClientFactory clientFactory) {
return new SimpleHostRoutingFilter(new ProxyRequestHelper(), zuulProperties, connectionManagerFactory, clientFactory);
return new SimpleHostRoutingFilter(new ProxyRequestHelper(zuulProperties), zuulProperties, connectionManagerFactory, clientFactory);
}
}