Skip to content

Commit d177698

Browse files
committed
Add Referrer-Policy header to default security headers
Closes: gh-13567 Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
1 parent 4ffec6d commit d177698

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/HeadersConfigurer.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
* Strict-Transport-Security: max-age=31536000 ; includeSubDomains
6767
* X-Frame-Options: DENY
6868
* X-XSS-Protection: 0
69+
* Referrer-Policy: no-referrer
6970
* </pre>
7071
*
7172
* @author Rob Winch
@@ -75,6 +76,7 @@
7576
* @author Vedran Pavic
7677
* @author Ankur Pathak
7778
* @author Daniel Garnier-Moiroux
79+
* @author Andrey Litvitski
7880
* @since 3.2
7981
*/
8082
public class HeadersConfigurer<H extends HttpSecurityBuilder<H>>
@@ -266,6 +268,7 @@ public HeadersConfigurer<H> defaultsDisabled() {
266268
this.cacheControl.disable();
267269
this.hsts.disable();
268270
this.frameOptions.disable();
271+
this.referrerPolicy.disable();
269272
return this;
270273
}
271274

@@ -968,6 +971,27 @@ public final class ReferrerPolicyConfig {
968971
private ReferrerPolicyHeaderWriter writer;
969972

970973
private ReferrerPolicyConfig() {
974+
enable();
975+
}
976+
977+
/**
978+
* Disables Referrer Policy
979+
* @return the {@link HeadersConfigurer} for additional configuration
980+
*/
981+
public HeadersConfigurer<H> disable() {
982+
this.writer = null;
983+
return HeadersConfigurer.this;
984+
}
985+
986+
/**
987+
* Ensures the Referrer Policy header is enabled if it is not already.
988+
* @return the {@link ReferrerPolicyConfig} for additional customization
989+
*/
990+
public ReferrerPolicyConfig enable() {
991+
if (this.writer == null) {
992+
this.writer = new ReferrerPolicyHeaderWriter();
993+
}
994+
return this;
971995
}
972996

973997
/**

config/src/test/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfigurationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,13 @@ public void getWhenDefaultFilterChainBeanThenDefaultHeadersInResponse() throws E
141141
.andExpect(header().string(HttpHeaders.EXPIRES, "0"))
142142
.andExpect(header().string(HttpHeaders.PRAGMA, "no-cache"))
143143
.andExpect(header().string(HttpHeaders.X_XSS_PROTECTION, "0"))
144+
.andExpect(header().string(HttpHeaders.REFERRER_POLICY, "no-referrer"))
144145
.andReturn();
145146
// @formatter:on
146147
assertThat(mvcResult.getResponse().getHeaderNames()).containsExactlyInAnyOrder(
147148
HttpHeaders.X_CONTENT_TYPE_OPTIONS, HttpHeaders.X_FRAME_OPTIONS, HttpHeaders.STRICT_TRANSPORT_SECURITY,
148-
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION);
149+
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION,
150+
HttpHeaders.REFERRER_POLICY);
149151
}
150152

151153
@Test

config/src/test/java/org/springframework/security/config/annotation/web/configurers/HeadersConfigurerTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ public void getWhenHeadersConfiguredThenDefaultHeadersInResponse() throws Except
8181
.andExpect(header().string(HttpHeaders.EXPIRES, "0"))
8282
.andExpect(header().string(HttpHeaders.PRAGMA, "no-cache"))
8383
.andExpect(header().string(HttpHeaders.X_XSS_PROTECTION, "0"))
84+
.andExpect(header().string(HttpHeaders.REFERRER_POLICY, "no-referrer"))
8485
.andReturn();
8586
assertThat(mvcResult.getResponse().getHeaderNames()).containsExactlyInAnyOrder(
8687
HttpHeaders.X_CONTENT_TYPE_OPTIONS, HttpHeaders.X_FRAME_OPTIONS, HttpHeaders.STRICT_TRANSPORT_SECURITY,
87-
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION);
88+
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION,
89+
HttpHeaders.REFERRER_POLICY);
8890
}
8991

9092
@Test
@@ -98,10 +100,12 @@ public void getWhenHeadersConfiguredInLambdaThenDefaultHeadersInResponse() throw
98100
.andExpect(header().string(HttpHeaders.EXPIRES, "0"))
99101
.andExpect(header().string(HttpHeaders.PRAGMA, "no-cache"))
100102
.andExpect(header().string(HttpHeaders.X_XSS_PROTECTION, "0"))
103+
.andExpect(header().string(HttpHeaders.REFERRER_POLICY, "no-referrer"))
101104
.andReturn();
102105
assertThat(mvcResult.getResponse().getHeaderNames()).containsExactlyInAnyOrder(
103106
HttpHeaders.X_CONTENT_TYPE_OPTIONS, HttpHeaders.X_FRAME_OPTIONS, HttpHeaders.STRICT_TRANSPORT_SECURITY,
104-
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION);
107+
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION,
108+
HttpHeaders.REFERRER_POLICY);
105109
}
106110

107111
@Test

config/src/test/java/org/springframework/security/config/annotation/web/configurers/NamespaceHttpHeadersTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class NamespaceHttpHeadersTests {
6565
defaultHeaders.put("Expires", "0");
6666
defaultHeaders.put("Pragma", "no-cache");
6767
defaultHeaders.put("X-XSS-Protection", "0");
68+
defaultHeaders.put("Referrer-Policy", "no-referrer");
6869
}
6970
public final SpringTestContext spring = new SpringTestContext(this);
7071

0 commit comments

Comments
 (0)