Add permissionsPolicy http header

This commit is contained in:
Christophe Gilles
2020-12-04 23:00:09 +01:00
committed by Eleftheria Stein-Kousathana
parent 48ef27b80a
commit 54d3839f63
21 changed files with 1015 additions and 3 deletions

View File

@@ -447,6 +447,44 @@ public class HeadersConfigurerTests {
.withRootCauseInstanceOf(IllegalArgumentException.class);
}
@Test
public void getWhenPermissionsPolicyConfiguredThenPermissionsPolicyHeaderInResponse() throws Exception {
this.spring.register(PermissionsPolicyConfig.class).autowire();
ResultMatcher permissionsPolicy = header().string("Permissions-Policy", "geolocation=(self)");
// @formatter:off
MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
.andExpect(permissionsPolicy)
.andReturn();
// @formatter:on
assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Permissions-Policy");
}
@Test
public void getWhenPermissionsPolicyConfiguredWithStringThenPermissionsPolicyHeaderInResponse() throws Exception {
this.spring.register(PermissionsPolicyStringConfig.class).autowire();
ResultMatcher permissionsPolicy = header().string("Permissions-Policy", "geolocation=(self)");
// @formatter:off
MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
.andExpect(permissionsPolicy)
.andReturn();
// @formatter:on
assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Permissions-Policy");
}
@Test
public void configureWhenPermissionsPolicyEmptyThenException() {
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> this.spring.register(PermissionsPolicyInvalidConfig.class).autowire())
.withRootCauseInstanceOf(IllegalArgumentException.class);
}
@Test
public void configureWhenPermissionsPolicyStringEmptyThenException() {
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> this.spring.register(PermissionsPolicyInvalidStringConfig.class).autowire())
.withRootCauseInstanceOf(IllegalArgumentException.class);
}
@Test
public void getWhenHstsConfiguredWithPreloadThenStrictTransportSecurityHeaderWithPreloadInResponse()
throws Exception {
@@ -1012,6 +1050,68 @@ public class HeadersConfigurerTests {
}
@EnableWebSecurity
static class PermissionsPolicyConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.headers()
.defaultsDisabled()
.permissionsPolicy((permissionsPolicy) -> permissionsPolicy.policy("geolocation=(self)"));
// @formatter:on
}
}
@EnableWebSecurity
static class PermissionsPolicyStringConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.headers()
.defaultsDisabled()
.permissionsPolicy()
.policy("geolocation=(self)");
// @formatter:on
}
}
@EnableWebSecurity
static class PermissionsPolicyInvalidConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.headers()
.defaultsDisabled()
.permissionsPolicy((permissionsPolicy) -> permissionsPolicy.policy(null));
// @formatter:on
}
}
@EnableWebSecurity
static class PermissionsPolicyInvalidStringConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.headers()
.defaultsDisabled()
.permissionsPolicy()
.policy("");
// @formatter:on
}
}
@EnableWebSecurity
static class HstsWithPreloadConfig extends WebSecurityConfigurerAdapter {

View File

@@ -332,6 +332,17 @@ public class HttpHeadersConfigTests {
() -> this.spring.configLocations(this.xml("DefaultsDisabledWithOnlyHeaderValue")).autowire());
}
@Test
public void requestWhenPermissionsPolicyConfiguredWithGeolocationSelfThenGeolocationSelf() throws Exception {
this.spring.configLocations(this.xml("DefaultsDisabledWithPermissionsPolicy")).autowire();
// @formatter:off
this.mvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(excludesDefaults())
.andExpect(header().string("Permissions-Policy", "geolocation=(self)"));
// @formatter:on
}
@Test
public void requestWhenUsingXssProtectionThenDefaultsToModeBlock() throws Exception {
Set<String> excludedHeaders = new HashSet<>(defaultHeaders.keySet());