Skip to content

Add Referrer-Policy header to default security headers #17606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -66,6 +66,7 @@
* Strict-Transport-Security: max-age=31536000 ; includeSubDomains
* X-Frame-Options: DENY
* X-XSS-Protection: 0
* Referrer-Policy: no-referrer
* </pre>
*
* @author Rob Winch
Expand All @@ -75,6 +76,7 @@
* @author Vedran Pavic
* @author Ankur Pathak
* @author Daniel Garnier-Moiroux
* @author Andrey Litvitski
* @since 3.2
*/
public class HeadersConfigurer<H extends HttpSecurityBuilder<H>>
Expand Down Expand Up @@ -266,6 +268,7 @@ public HeadersConfigurer<H> defaultsDisabled() {
this.cacheControl.disable();
this.hsts.disable();
this.frameOptions.disable();
this.referrerPolicy.disable();
return this;
}

Expand Down Expand Up @@ -968,6 +971,27 @@ public final class ReferrerPolicyConfig {
private ReferrerPolicyHeaderWriter writer;

private ReferrerPolicyConfig() {
enable();
}

/**
* Disables Referrer Policy
* @return the {@link HeadersConfigurer} for additional configuration
*/
public HeadersConfigurer<H> disable() {
this.writer = null;
return HeadersConfigurer.this;
}

/**
* Ensures the Referrer Policy header is enabled if it is not already.
* @return the {@link ReferrerPolicyConfig} for additional customization
*/
public ReferrerPolicyConfig enable() {
if (this.writer == null) {
this.writer = new ReferrerPolicyHeaderWriter();
}
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,13 @@ public void getWhenDefaultFilterChainBeanThenDefaultHeadersInResponse() throws E
.andExpect(header().string(HttpHeaders.EXPIRES, "0"))
.andExpect(header().string(HttpHeaders.PRAGMA, "no-cache"))
.andExpect(header().string(HttpHeaders.X_XSS_PROTECTION, "0"))
.andExpect(header().string(HttpHeaders.REFERRER_POLICY, "no-referrer"))
.andReturn();
// @formatter:on
assertThat(mvcResult.getResponse().getHeaderNames()).containsExactlyInAnyOrder(
HttpHeaders.X_CONTENT_TYPE_OPTIONS, HttpHeaders.X_FRAME_OPTIONS, HttpHeaders.STRICT_TRANSPORT_SECURITY,
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION);
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION,
HttpHeaders.REFERRER_POLICY);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ public void getWhenHeadersConfiguredThenDefaultHeadersInResponse() throws Except
.andExpect(header().string(HttpHeaders.EXPIRES, "0"))
.andExpect(header().string(HttpHeaders.PRAGMA, "no-cache"))
.andExpect(header().string(HttpHeaders.X_XSS_PROTECTION, "0"))
.andExpect(header().string(HttpHeaders.REFERRER_POLICY, "no-referrer"))
.andReturn();
assertThat(mvcResult.getResponse().getHeaderNames()).containsExactlyInAnyOrder(
HttpHeaders.X_CONTENT_TYPE_OPTIONS, HttpHeaders.X_FRAME_OPTIONS, HttpHeaders.STRICT_TRANSPORT_SECURITY,
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION);
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION,
HttpHeaders.REFERRER_POLICY);
}

@Test
Expand All @@ -98,10 +100,12 @@ public void getWhenHeadersConfiguredInLambdaThenDefaultHeadersInResponse() throw
.andExpect(header().string(HttpHeaders.EXPIRES, "0"))
.andExpect(header().string(HttpHeaders.PRAGMA, "no-cache"))
.andExpect(header().string(HttpHeaders.X_XSS_PROTECTION, "0"))
.andExpect(header().string(HttpHeaders.REFERRER_POLICY, "no-referrer"))
.andReturn();
assertThat(mvcResult.getResponse().getHeaderNames()).containsExactlyInAnyOrder(
HttpHeaders.X_CONTENT_TYPE_OPTIONS, HttpHeaders.X_FRAME_OPTIONS, HttpHeaders.STRICT_TRANSPORT_SECURITY,
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION);
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION,
HttpHeaders.REFERRER_POLICY);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class NamespaceHttpHeadersTests {
defaultHeaders.put("Expires", "0");
defaultHeaders.put("Pragma", "no-cache");
defaultHeaders.put("X-XSS-Protection", "0");
defaultHeaders.put("Referrer-Policy", "no-referrer");
}
public final SpringTestContext spring = new SpringTestContext(this);

Expand Down