|
@@ -77,19 +77,6 @@ public class HeadersConfigurerTests {
|
|
|
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION);
|
|
|
}
|
|
|
|
|
|
- @EnableWebSecurity
|
|
|
- static class HeadersConfig extends WebSecurityConfigurerAdapter {
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void configure(HttpSecurity http) throws Exception {
|
|
|
- // @formatter:off
|
|
|
- http
|
|
|
- .headers();
|
|
|
- // @formatter:on
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
@Test
|
|
|
public void getWhenHeadersConfiguredInLambdaThenDefaultHeadersInResponse() throws Exception {
|
|
|
this.spring.register(HeadersInLambdaConfig.class).autowire();
|
|
@@ -108,27 +95,373 @@ public class HeadersConfigurerTests {
|
|
|
HttpHeaders.CACHE_CONTROL, HttpHeaders.EXPIRES, HttpHeaders.PRAGMA, HttpHeaders.X_XSS_PROTECTION);
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void getWhenHeaderDefaultsDisabledAndContentTypeConfiguredThenOnlyContentTypeHeaderInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(ContentTypeOptionsConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/"))
|
|
|
+ .andExpect(header().string(HttpHeaders.X_CONTENT_TYPE_OPTIONS, "nosniff")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.X_CONTENT_TYPE_OPTIONS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenOnlyContentTypeConfiguredInLambdaThenOnlyContentTypeHeaderInResponse() throws Exception {
|
|
|
+ this.spring.register(ContentTypeOptionsInLambdaConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/"))
|
|
|
+ .andExpect(header().string(HttpHeaders.X_CONTENT_TYPE_OPTIONS, "nosniff")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.X_CONTENT_TYPE_OPTIONS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHeaderDefaultsDisabledAndFrameOptionsConfiguredThenOnlyFrameOptionsHeaderInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(FrameOptionsConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/"))
|
|
|
+ .andExpect(header().string(HttpHeaders.X_FRAME_OPTIONS, XFrameOptionsMode.DENY.name())).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.X_FRAME_OPTIONS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHeaderDefaultsDisabledAndHstsConfiguredThenOnlyStrictTransportSecurityHeaderInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(HstsConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(
|
|
|
+ header().string(HttpHeaders.STRICT_TRANSPORT_SECURITY, "max-age=31536000 ; includeSubDomains"))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.STRICT_TRANSPORT_SECURITY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHeaderDefaultsDisabledAndCacheControlConfiguredThenCacheControlAndExpiresAndPragmaHeadersInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(CacheControlConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate"))
|
|
|
+ .andExpect(header().string(HttpHeaders.EXPIRES, "0"))
|
|
|
+ .andExpect(header().string(HttpHeaders.PRAGMA, "no-cache")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactlyInAnyOrder(HttpHeaders.CACHE_CONTROL,
|
|
|
+ HttpHeaders.EXPIRES, HttpHeaders.PRAGMA);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenOnlyCacheControlConfiguredInLambdaThenCacheControlAndExpiresAndPragmaHeadersInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(CacheControlInLambdaConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate"))
|
|
|
+ .andExpect(header().string(HttpHeaders.EXPIRES, "0"))
|
|
|
+ .andExpect(header().string(HttpHeaders.PRAGMA, "no-cache")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactlyInAnyOrder(HttpHeaders.CACHE_CONTROL,
|
|
|
+ HttpHeaders.EXPIRES, HttpHeaders.PRAGMA);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHeaderDefaultsDisabledAndXssProtectionConfiguredThenOnlyXssProtectionHeaderInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(XssProtectionConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.X_XSS_PROTECTION, "1; mode=block")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.X_XSS_PROTECTION);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenOnlyXssProtectionConfiguredInLambdaThenOnlyXssProtectionHeaderInResponse() throws Exception {
|
|
|
+ this.spring.register(XssProtectionInLambdaConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.X_XSS_PROTECTION, "1; mode=block")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.X_XSS_PROTECTION);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenFrameOptionsSameOriginConfiguredThenFrameOptionsHeaderHasValueSameOrigin() throws Exception {
|
|
|
+ this.spring.register(HeadersCustomSameOriginConfig.class).autowire();
|
|
|
+
|
|
|
+ this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.X_FRAME_OPTIONS, XFrameOptionsMode.SAMEORIGIN.name()))
|
|
|
+ .andReturn();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenFrameOptionsSameOriginConfiguredInLambdaThenFrameOptionsHeaderHasValueSameOrigin()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(HeadersCustomSameOriginInLambdaConfig.class).autowire();
|
|
|
+
|
|
|
+ this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.X_FRAME_OPTIONS, XFrameOptionsMode.SAMEORIGIN.name()))
|
|
|
+ .andReturn();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHeaderDefaultsDisabledAndPublicHpkpWithNoPinThenNoHeadersInResponse() throws Exception {
|
|
|
+ this.spring.register(HpkpConfigNoPins.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenSecureRequestAndHpkpWithPinThenPublicKeyPinsReportOnlyHeaderInResponse() throws Exception {
|
|
|
+ this.spring.register(HpkpConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
+ "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\""))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenInsecureRequestHeaderDefaultsDisabledAndHpkpWithPinThenNoHeadersInResponse() throws Exception {
|
|
|
+ this.spring.register(HpkpConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHpkpWithMultiplePinsThenPublicKeyPinsReportOnlyHeaderWithMultiplePinsInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(HpkpConfigWithPins.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header().string(
|
|
|
+ HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
+ "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; pin-sha256=\"E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=\""))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHpkpWithCustomAgeThenPublicKeyPinsReportOnlyHeaderWithCustomAgeInResponse() throws Exception {
|
|
|
+ this.spring.register(HpkpConfigCustomAge.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
+ "max-age=604800 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\""))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHpkpWithReportOnlyFalseThenPublicKeyPinsHeaderInResponse() throws Exception {
|
|
|
+ this.spring.register(HpkpConfigTerminateConnection.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.PUBLIC_KEY_PINS,
|
|
|
+ "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\""))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHpkpIncludeSubdomainThenPublicKeyPinsReportOnlyHeaderWithIncludeSubDomainsInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(HpkpConfigIncludeSubDomains.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header().string(
|
|
|
+ HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
+ "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; includeSubDomains"))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHpkpWithReportUriThenPublicKeyPinsReportOnlyHeaderWithReportUriInResponse() throws Exception {
|
|
|
+ this.spring.register(HpkpConfigWithReportURI.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header().string(
|
|
|
+ HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
+ "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; report-uri=\"https://example.net/pkp-report\""))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHpkpWithReportUriAsStringThenPublicKeyPinsReportOnlyHeaderWithReportUriInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(HpkpConfigWithReportURIAsString.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header().string(
|
|
|
+ HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
+ "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; report-uri=\"https://example.net/pkp-report\""))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHpkpWithReportUriInLambdaThenPublicKeyPinsReportOnlyHeaderWithReportUriInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(HpkpWithReportUriInLambdaConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header().string(
|
|
|
+ HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
+ "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; report-uri=\"https://example.net/pkp-report\""))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenContentSecurityPolicyConfiguredThenContentSecurityPolicyHeaderInResponse() throws Exception {
|
|
|
+ this.spring.register(ContentSecurityPolicyDefaultConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.CONTENT_SECURITY_POLICY, "default-src 'self'")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenContentSecurityPolicyWithReportOnlyThenContentSecurityPolicyReportOnlyHeaderInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(ContentSecurityPolicyReportOnlyConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.CONTENT_SECURITY_POLICY_REPORT_ONLY,
|
|
|
+ "default-src 'self'; script-src trustedscripts.example.com"))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames())
|
|
|
+ .containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY_REPORT_ONLY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenContentSecurityPolicyWithReportOnlyInLambdaThenContentSecurityPolicyReportOnlyHeaderInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(ContentSecurityPolicyReportOnlyInLambdaConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.CONTENT_SECURITY_POLICY_REPORT_ONLY,
|
|
|
+ "default-src 'self'; script-src trustedscripts.example.com"))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames())
|
|
|
+ .containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY_REPORT_ONLY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void configureWhenContentSecurityPolicyEmptyThenException() {
|
|
|
+ assertThatThrownBy(() -> this.spring.register(ContentSecurityPolicyInvalidConfig.class).autowire())
|
|
|
+ .isInstanceOf(BeanCreationException.class).hasRootCauseInstanceOf(IllegalArgumentException.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void configureWhenContentSecurityPolicyEmptyInLambdaThenException() {
|
|
|
+ assertThatThrownBy(() -> this.spring.register(ContentSecurityPolicyInvalidInLambdaConfig.class).autowire())
|
|
|
+ .isInstanceOf(BeanCreationException.class).hasRootCauseInstanceOf(IllegalArgumentException.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void configureWhenContentSecurityPolicyNoPolicyDirectivesInLambdaThenDefaultHeaderValue() throws Exception {
|
|
|
+ this.spring.register(ContentSecurityPolicyNoDirectivesInLambdaConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string(HttpHeaders.CONTENT_SECURITY_POLICY, "default-src 'self'")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenReferrerPolicyConfiguredThenReferrerPolicyHeaderInResponse() throws Exception {
|
|
|
+ this.spring.register(ReferrerPolicyDefaultConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string("Referrer-Policy", ReferrerPolicy.NO_REFERRER.getPolicy())).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Referrer-Policy");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenReferrerPolicyInLambdaThenReferrerPolicyHeaderInResponse() throws Exception {
|
|
|
+ this.spring.register(ReferrerPolicyDefaultInLambdaConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string("Referrer-Policy", ReferrerPolicy.NO_REFERRER.getPolicy())).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Referrer-Policy");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenReferrerPolicyConfiguredWithCustomValueThenReferrerPolicyHeaderWithCustomValueInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(ReferrerPolicyCustomConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string("Referrer-Policy", ReferrerPolicy.SAME_ORIGIN.getPolicy())).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Referrer-Policy");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenReferrerPolicyConfiguredWithCustomValueInLambdaThenCustomValueInResponse() throws Exception {
|
|
|
+ this.spring.register(ReferrerPolicyCustomInLambdaConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string("Referrer-Policy", ReferrerPolicy.SAME_ORIGIN.getPolicy())).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Referrer-Policy");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenFeaturePolicyConfiguredThenFeaturePolicyHeaderInResponse() throws Exception {
|
|
|
+ this.spring.register(FeaturePolicyConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
+ .andExpect(header().string("Feature-Policy", "geolocation 'self'")).andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Feature-Policy");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void configureWhenFeaturePolicyEmptyThenException() {
|
|
|
+ assertThatThrownBy(() -> this.spring.register(FeaturePolicyInvalidConfig.class).autowire())
|
|
|
+ .isInstanceOf(BeanCreationException.class).hasRootCauseInstanceOf(IllegalArgumentException.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHstsConfiguredWithPreloadThenStrictTransportSecurityHeaderWithPreloadInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(HstsWithPreloadConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header()
|
|
|
+ .string(HttpHeaders.STRICT_TRANSPORT_SECURITY, "max-age=31536000 ; includeSubDomains ; preload"))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.STRICT_TRANSPORT_SECURITY);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getWhenHstsConfiguredWithPreloadInLambdaThenStrictTransportSecurityHeaderWithPreloadInResponse()
|
|
|
+ throws Exception {
|
|
|
+ this.spring.register(HstsWithPreloadInLambdaConfig.class).autowire();
|
|
|
+
|
|
|
+ MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header()
|
|
|
+ .string(HttpHeaders.STRICT_TRANSPORT_SECURITY, "max-age=31536000 ; includeSubDomains ; preload"))
|
|
|
+ .andReturn();
|
|
|
+ assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.STRICT_TRANSPORT_SECURITY);
|
|
|
+ }
|
|
|
+
|
|
|
@EnableWebSecurity
|
|
|
- static class HeadersInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
+ static class HeadersConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
|
@Override
|
|
|
protected void configure(HttpSecurity http) throws Exception {
|
|
|
// @formatter:off
|
|
|
http
|
|
|
- .headers(withDefaults());
|
|
|
+ .headers();
|
|
|
// @formatter:on
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHeaderDefaultsDisabledAndContentTypeConfiguredThenOnlyContentTypeHeaderInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(ContentTypeOptionsConfig.class).autowire();
|
|
|
+ @EnableWebSecurity
|
|
|
+ static class HeadersInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void configure(HttpSecurity http) throws Exception {
|
|
|
+ // @formatter:off
|
|
|
+ http
|
|
|
+ .headers(withDefaults());
|
|
|
+ // @formatter:on
|
|
|
+ }
|
|
|
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/"))
|
|
|
- .andExpect(header().string(HttpHeaders.X_CONTENT_TYPE_OPTIONS, "nosniff")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.X_CONTENT_TYPE_OPTIONS);
|
|
|
}
|
|
|
|
|
|
@EnableWebSecurity
|
|
@@ -146,15 +479,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenOnlyContentTypeConfiguredInLambdaThenOnlyContentTypeHeaderInResponse() throws Exception {
|
|
|
- this.spring.register(ContentTypeOptionsInLambdaConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/"))
|
|
|
- .andExpect(header().string(HttpHeaders.X_CONTENT_TYPE_OPTIONS, "nosniff")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.X_CONTENT_TYPE_OPTIONS);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ContentTypeOptionsInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -172,16 +496,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHeaderDefaultsDisabledAndFrameOptionsConfiguredThenOnlyFrameOptionsHeaderInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(FrameOptionsConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/"))
|
|
|
- .andExpect(header().string(HttpHeaders.X_FRAME_OPTIONS, XFrameOptionsMode.DENY.name())).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.X_FRAME_OPTIONS);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class FrameOptionsConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -197,18 +511,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHeaderDefaultsDisabledAndHstsConfiguredThenOnlyStrictTransportSecurityHeaderInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(HstsConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(
|
|
|
- header().string(HttpHeaders.STRICT_TRANSPORT_SECURITY, "max-age=31536000 ; includeSubDomains"))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.STRICT_TRANSPORT_SECURITY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HstsConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -224,19 +526,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHeaderDefaultsDisabledAndCacheControlConfiguredThenCacheControlAndExpiresAndPragmaHeadersInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(CacheControlConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate"))
|
|
|
- .andExpect(header().string(HttpHeaders.EXPIRES, "0"))
|
|
|
- .andExpect(header().string(HttpHeaders.PRAGMA, "no-cache")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactlyInAnyOrder(HttpHeaders.CACHE_CONTROL,
|
|
|
- HttpHeaders.EXPIRES, HttpHeaders.PRAGMA);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class CacheControlConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -252,19 +541,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenOnlyCacheControlConfiguredInLambdaThenCacheControlAndExpiresAndPragmaHeadersInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(CacheControlInLambdaConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate"))
|
|
|
- .andExpect(header().string(HttpHeaders.EXPIRES, "0"))
|
|
|
- .andExpect(header().string(HttpHeaders.PRAGMA, "no-cache")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactlyInAnyOrder(HttpHeaders.CACHE_CONTROL,
|
|
|
- HttpHeaders.EXPIRES, HttpHeaders.PRAGMA);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class CacheControlInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -282,16 +558,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHeaderDefaultsDisabledAndXssProtectionConfiguredThenOnlyXssProtectionHeaderInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(XssProtectionConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.X_XSS_PROTECTION, "1; mode=block")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.X_XSS_PROTECTION);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class XssProtectionConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -307,15 +573,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenOnlyXssProtectionConfiguredInLambdaThenOnlyXssProtectionHeaderInResponse() throws Exception {
|
|
|
- this.spring.register(XssProtectionInLambdaConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.X_XSS_PROTECTION, "1; mode=block")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.X_XSS_PROTECTION);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class XssProtectionInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -325,21 +582,12 @@ public class HeadersConfigurerTests {
|
|
|
http
|
|
|
.headers(headers ->
|
|
|
headers
|
|
|
- .defaultsDisabled()
|
|
|
- .xssProtection(withDefaults())
|
|
|
- );
|
|
|
- // @formatter:on
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getWhenFrameOptionsSameOriginConfiguredThenFrameOptionsHeaderHasValueSameOrigin() throws Exception {
|
|
|
- this.spring.register(HeadersCustomSameOriginConfig.class).autowire();
|
|
|
+ .defaultsDisabled()
|
|
|
+ .xssProtection(withDefaults())
|
|
|
+ );
|
|
|
+ // @formatter:on
|
|
|
+ }
|
|
|
|
|
|
- this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.X_FRAME_OPTIONS, XFrameOptionsMode.SAMEORIGIN.name()))
|
|
|
- .andReturn();
|
|
|
}
|
|
|
|
|
|
@EnableWebSecurity
|
|
@@ -356,16 +604,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenFrameOptionsSameOriginConfiguredInLambdaThenFrameOptionsHeaderHasValueSameOrigin()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(HeadersCustomSameOriginInLambdaConfig.class).autowire();
|
|
|
-
|
|
|
- this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.X_FRAME_OPTIONS, XFrameOptionsMode.SAMEORIGIN.name()))
|
|
|
- .andReturn();
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HeadersCustomSameOriginInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -382,14 +620,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHeaderDefaultsDisabledAndPublicHpkpWithNoPinThenNoHeadersInResponse() throws Exception {
|
|
|
- this.spring.register(HpkpConfigNoPins.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).isEmpty();
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HpkpConfigNoPins extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -405,25 +635,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenSecureRequestAndHpkpWithPinThenPublicKeyPinsReportOnlyHeaderInResponse() throws Exception {
|
|
|
- this.spring.register(HpkpConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
- "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\""))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getWhenInsecureRequestHeaderDefaultsDisabledAndHpkpWithPinThenNoHeadersInResponse() throws Exception {
|
|
|
- this.spring.register(HpkpConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).isEmpty();
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HpkpConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -440,18 +651,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHpkpWithMultiplePinsThenPublicKeyPinsReportOnlyHeaderWithMultiplePinsInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(HpkpConfigWithPins.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header().string(
|
|
|
- HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
- "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; pin-sha256=\"E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=\""))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HpkpConfigWithPins extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -472,17 +671,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHpkpWithCustomAgeThenPublicKeyPinsReportOnlyHeaderWithCustomAgeInResponse() throws Exception {
|
|
|
- this.spring.register(HpkpConfigCustomAge.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
- "max-age=604800 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\""))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HpkpConfigCustomAge extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -500,17 +688,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHpkpWithReportOnlyFalseThenPublicKeyPinsHeaderInResponse() throws Exception {
|
|
|
- this.spring.register(HpkpConfigTerminateConnection.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.PUBLIC_KEY_PINS,
|
|
|
- "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\""))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HpkpConfigTerminateConnection extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -528,18 +705,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHpkpIncludeSubdomainThenPublicKeyPinsReportOnlyHeaderWithIncludeSubDomainsInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(HpkpConfigIncludeSubDomains.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header().string(
|
|
|
- HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
- "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; includeSubDomains"))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HpkpConfigIncludeSubDomains extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -557,17 +722,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHpkpWithReportUriThenPublicKeyPinsReportOnlyHeaderWithReportUriInResponse() throws Exception {
|
|
|
- this.spring.register(HpkpConfigWithReportURI.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header().string(
|
|
|
- HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
- "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; report-uri=\"https://example.net/pkp-report\""))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HpkpConfigWithReportURI extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -585,18 +739,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHpkpWithReportUriAsStringThenPublicKeyPinsReportOnlyHeaderWithReportUriInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(HpkpConfigWithReportURIAsString.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header().string(
|
|
|
- HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
- "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; report-uri=\"https://example.net/pkp-report\""))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HpkpConfigWithReportURIAsString extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -614,18 +756,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHpkpWithReportUriInLambdaThenPublicKeyPinsReportOnlyHeaderWithReportUriInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(HpkpWithReportUriInLambdaConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header().string(
|
|
|
- HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY,
|
|
|
- "max-age=5184000 ; pin-sha256=\"d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=\" ; report-uri=\"https://example.net/pkp-report\""))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.PUBLIC_KEY_PINS_REPORT_ONLY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HpkpWithReportUriInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -647,15 +777,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenContentSecurityPolicyConfiguredThenContentSecurityPolicyHeaderInResponse() throws Exception {
|
|
|
- this.spring.register(ContentSecurityPolicyDefaultConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.CONTENT_SECURITY_POLICY, "default-src 'self'")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ContentSecurityPolicyDefaultConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -671,19 +792,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenContentSecurityPolicyWithReportOnlyThenContentSecurityPolicyReportOnlyHeaderInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(ContentSecurityPolicyReportOnlyConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.CONTENT_SECURITY_POLICY_REPORT_ONLY,
|
|
|
- "default-src 'self'; script-src trustedscripts.example.com"))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames())
|
|
|
- .containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY_REPORT_ONLY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ContentSecurityPolicyReportOnlyConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -700,19 +808,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenContentSecurityPolicyWithReportOnlyInLambdaThenContentSecurityPolicyReportOnlyHeaderInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(ContentSecurityPolicyReportOnlyInLambdaConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.CONTENT_SECURITY_POLICY_REPORT_ONLY,
|
|
|
- "default-src 'self'; script-src trustedscripts.example.com"))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames())
|
|
|
- .containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY_REPORT_ONLY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ContentSecurityPolicyReportOnlyInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -734,12 +829,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void configureWhenContentSecurityPolicyEmptyThenException() {
|
|
|
- assertThatThrownBy(() -> this.spring.register(ContentSecurityPolicyInvalidConfig.class).autowire())
|
|
|
- .isInstanceOf(BeanCreationException.class).hasRootCauseInstanceOf(IllegalArgumentException.class);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ContentSecurityPolicyInvalidConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -755,12 +844,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void configureWhenContentSecurityPolicyEmptyInLambdaThenException() {
|
|
|
- assertThatThrownBy(() -> this.spring.register(ContentSecurityPolicyInvalidInLambdaConfig.class).autowire())
|
|
|
- .isInstanceOf(BeanCreationException.class).hasRootCauseInstanceOf(IllegalArgumentException.class);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ContentSecurityPolicyInvalidInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -780,15 +863,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void configureWhenContentSecurityPolicyNoPolicyDirectivesInLambdaThenDefaultHeaderValue() throws Exception {
|
|
|
- this.spring.register(ContentSecurityPolicyNoDirectivesInLambdaConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string(HttpHeaders.CONTENT_SECURITY_POLICY, "default-src 'self'")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.CONTENT_SECURITY_POLICY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ContentSecurityPolicyNoDirectivesInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -806,15 +880,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenReferrerPolicyConfiguredThenReferrerPolicyHeaderInResponse() throws Exception {
|
|
|
- this.spring.register(ReferrerPolicyDefaultConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string("Referrer-Policy", ReferrerPolicy.NO_REFERRER.getPolicy())).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Referrer-Policy");
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ReferrerPolicyDefaultConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -830,15 +895,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenReferrerPolicyInLambdaThenReferrerPolicyHeaderInResponse() throws Exception {
|
|
|
- this.spring.register(ReferrerPolicyDefaultInLambdaConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string("Referrer-Policy", ReferrerPolicy.NO_REFERRER.getPolicy())).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Referrer-Policy");
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ReferrerPolicyDefaultInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -856,16 +912,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenReferrerPolicyConfiguredWithCustomValueThenReferrerPolicyHeaderWithCustomValueInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(ReferrerPolicyCustomConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string("Referrer-Policy", ReferrerPolicy.SAME_ORIGIN.getPolicy())).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Referrer-Policy");
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ReferrerPolicyCustomConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -881,15 +927,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenReferrerPolicyConfiguredWithCustomValueInLambdaThenCustomValueInResponse() throws Exception {
|
|
|
- this.spring.register(ReferrerPolicyCustomInLambdaConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string("Referrer-Policy", ReferrerPolicy.SAME_ORIGIN.getPolicy())).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Referrer-Policy");
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class ReferrerPolicyCustomInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -909,15 +946,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenFeaturePolicyConfiguredThenFeaturePolicyHeaderInResponse() throws Exception {
|
|
|
- this.spring.register(FeaturePolicyConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true))
|
|
|
- .andExpect(header().string("Feature-Policy", "geolocation 'self'")).andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly("Feature-Policy");
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class FeaturePolicyConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -933,12 +961,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void configureWhenFeaturePolicyEmptyThenException() {
|
|
|
- assertThatThrownBy(() -> this.spring.register(FeaturePolicyInvalidConfig.class).autowire())
|
|
|
- .isInstanceOf(BeanCreationException.class).hasRootCauseInstanceOf(IllegalArgumentException.class);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class FeaturePolicyInvalidConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -954,17 +976,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHstsConfiguredWithPreloadThenStrictTransportSecurityHeaderWithPreloadInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(HstsWithPreloadConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header()
|
|
|
- .string(HttpHeaders.STRICT_TRANSPORT_SECURITY, "max-age=31536000 ; includeSubDomains ; preload"))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.STRICT_TRANSPORT_SECURITY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HstsWithPreloadConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
@@ -981,17 +992,6 @@ public class HeadersConfigurerTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void getWhenHstsConfiguredWithPreloadInLambdaThenStrictTransportSecurityHeaderWithPreloadInResponse()
|
|
|
- throws Exception {
|
|
|
- this.spring.register(HstsWithPreloadInLambdaConfig.class).autowire();
|
|
|
-
|
|
|
- MvcResult mvcResult = this.mvc.perform(get("/").secure(true)).andExpect(header()
|
|
|
- .string(HttpHeaders.STRICT_TRANSPORT_SECURITY, "max-age=31536000 ; includeSubDomains ; preload"))
|
|
|
- .andReturn();
|
|
|
- assertThat(mvcResult.getResponse().getHeaderNames()).containsExactly(HttpHeaders.STRICT_TRANSPORT_SECURITY);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class HstsWithPreloadInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|