Commit e109093b by Mathias Düsterhöft Committed by Spencer Gibb

add config option to not ignore security headers (#1354)

* add config option to not ignore security headers * fix field documentation of ignoreSecurityHeaders * add documenation for zuul.ignoreSecurityHeaders fixes gh-1096
parent 4ba34112
...@@ -1413,6 +1413,8 @@ need to set it unless you want it to be different. N.B. this is new in ...@@ -1413,6 +1413,8 @@ need to set it unless you want it to be different. N.B. this is new in
Spring Cloud Netflix 1.1 (in 1.0 the user had no control over headers Spring Cloud Netflix 1.1 (in 1.0 the user had no control over headers
and all cookies flow in both directions). and all cookies flow in both directions).
=== Ignored Headers
In addition to the per-route sensitive headers, you can set a global In addition to the per-route sensitive headers, you can set a global
value for `zuul.ignoredHeaders` for values that should be discarded value for `zuul.ignoredHeaders` for values that should be discarded
(both request and response) during interactions with downstream (both request and response) during interactions with downstream
...@@ -1421,6 +1423,7 @@ classpath, and otherwise they are initialized to a set of well-known ...@@ -1421,6 +1423,7 @@ classpath, and otherwise they are initialized to a set of well-known
"security" headers (e.g. involving caching) as specified by Spring "security" headers (e.g. involving caching) as specified by Spring
Security. The assumption in this case is that the downstream services Security. The assumption in this case is that the downstream services
might add these headers too, and we want the values from the proxy. might add these headers too, and we want the values from the proxy.
To not discard these well known security headers in case Spring Security is on the classpath you can set `zuul.ignoreSecurityHeaders` to `false`. This can be useful if you disabled the HTTP Security response headers in Spring Security and want the values provided by downstream services
=== The Routes Endpoint === The Routes Endpoint
......
...@@ -42,6 +42,7 @@ import static com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStr ...@@ -42,6 +42,7 @@ import static com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStr
/** /**
* @author Spencer Gibb * @author Spencer Gibb
* @author Dave Syer * @author Dave Syer
* @author Mathias Düsterhöft
*/ */
@Data @Data
@ConfigurationProperties("zuul") @ConfigurationProperties("zuul")
...@@ -50,7 +51,7 @@ public class ZuulProperties { ...@@ -50,7 +51,7 @@ public class ZuulProperties {
/** /**
* Headers that are generally expected to be added by Spring Security, and hence often * Headers that are generally expected to be added by Spring Security, and hence often
* duplicated if the proxy and the backend are secured with Spring. By default they * duplicated if the proxy and the backend are secured with Spring. By default they
* are added to the ignored headers if Spring Security is present. * are added to the ignored headers if Spring Security is present and ignoreSecurityHeaders = true.
*/ */
public static final List<String> SECURITY_HEADERS = Arrays.asList("Pragma", public static final List<String> SECURITY_HEADERS = Arrays.asList("Pragma",
"Cache-Control", "X-Frame-Options", "X-Content-Type-Options", "Cache-Control", "X-Frame-Options", "X-Content-Type-Options",
...@@ -102,6 +103,14 @@ public class ZuulProperties { ...@@ -102,6 +103,14 @@ public class ZuulProperties {
private Set<String> ignoredHeaders = new LinkedHashSet<>(); private Set<String> ignoredHeaders = new LinkedHashSet<>();
/** /**
* SECURITY_HEADERS are added to ignored headers if spring security is on the classpath and ignoreSecurityHeaders = true
* By setting ignoreSecurityHeaders to false we can switch off this default behaviour. This should be used together with
* disabling the default spring security headers
* see https://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html#default-security-headers
*/
private boolean ignoreSecurityHeaders = true;
/**
* Path to install Zuul as a servlet (not part of Spring MVC). The servlet is more * Path to install Zuul as a servlet (not part of Spring MVC). The servlet is more
* memory efficient for requests with large bodies, e.g. file uploads. * memory efficient for requests with large bodies, e.g. file uploads.
*/ */
...@@ -148,7 +157,7 @@ public class ZuulProperties { ...@@ -148,7 +157,7 @@ public class ZuulProperties {
Set<String> ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders); Set<String> ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders);
if (ClassUtils.isPresent( if (ClassUtils.isPresent(
"org.springframework.security.config.annotation.web.WebSecurityConfigurer", "org.springframework.security.config.annotation.web.WebSecurityConfigurer",
null) && Collections.disjoint(ignoredHeaders, SECURITY_HEADERS)) { null) && Collections.disjoint(ignoredHeaders, SECURITY_HEADERS) && ignoreSecurityHeaders) {
// Allow Spring Security in the gateway to control these headers // Allow Spring Security in the gateway to control these headers
ignoredHeaders.addAll(SECURITY_HEADERS); ignoredHeaders.addAll(SECURITY_HEADERS);
} }
......
...@@ -29,6 +29,7 @@ import static org.junit.Assert.assertTrue; ...@@ -29,6 +29,7 @@ import static org.junit.Assert.assertTrue;
/** /**
* @author Dave Syer * @author Dave Syer
* @author Mathias Düsterhöft
*/ */
public class ZuulPropertiesTests { public class ZuulPropertiesTests {
...@@ -46,11 +47,19 @@ public class ZuulPropertiesTests { ...@@ -46,11 +47,19 @@ public class ZuulPropertiesTests {
@Test @Test
public void defaultIgnoredHeaders() { public void defaultIgnoredHeaders() {
assertTrue(this.zuul.isIgnoreSecurityHeaders());
assertTrue(this.zuul.getIgnoredHeaders() assertTrue(this.zuul.getIgnoredHeaders()
.containsAll(ZuulProperties.SECURITY_HEADERS)); .containsAll(ZuulProperties.SECURITY_HEADERS));
} }
@Test @Test
public void securityHeadersNotIgnored() {
zuul.setIgnoreSecurityHeaders(false);
assertTrue(this.zuul.getIgnoredHeaders().isEmpty());
}
@Test
public void addIgnoredHeaders() { public void addIgnoredHeaders() {
this.zuul.setIgnoredHeaders(Collections.singleton("x-foo")); this.zuul.setIgnoredHeaders(Collections.singleton("x-foo"));
assertTrue(this.zuul.getIgnoredHeaders().contains("x-foo")); assertTrue(this.zuul.getIgnoredHeaders().contains("x-foo"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment