|
@@ -210,6 +210,26 @@ public class CsrfConfigurerTests {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void postWhenCsrfDisabledInLambdaThenRespondsWithOk() throws Exception {
|
|
|
+ this.spring.register(DisableCsrfInLambdaConfig.class, BasicController.class).autowire();
|
|
|
+
|
|
|
+ this.mvc.perform(post("/"))
|
|
|
+ .andExpect(status().isOk());
|
|
|
+ }
|
|
|
+
|
|
|
+ @EnableWebSecurity
|
|
|
+ static class DisableCsrfInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void configure(HttpSecurity http) throws Exception {
|
|
|
+ // @formatter:off
|
|
|
+ http
|
|
|
+ .csrf(AbstractHttpConfigurer::disable);
|
|
|
+ // @formatter:on
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// SEC-2498
|
|
|
@Test
|
|
|
public void loginWhenCsrfDisabledThenRedirectsToPreviousPostRequest() throws Exception {
|
|
@@ -386,6 +406,40 @@ public class CsrfConfigurerTests {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void requireCsrfProtectionMatcherInLambdaWhenRequestDoesNotMatchThenRespondsWithOk() throws Exception {
|
|
|
+ RequireCsrfProtectionMatcherInLambdaConfig.MATCHER = mock(RequestMatcher.class);
|
|
|
+ this.spring.register(RequireCsrfProtectionMatcherInLambdaConfig.class, BasicController.class).autowire();
|
|
|
+ when(RequireCsrfProtectionMatcherInLambdaConfig.MATCHER.matches(any()))
|
|
|
+ .thenReturn(false);
|
|
|
+
|
|
|
+ this.mvc.perform(get("/"))
|
|
|
+ .andExpect(status().isOk());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void requireCsrfProtectionMatcherInLambdaWhenRequestMatchesThenRespondsWithForbidden() throws Exception {
|
|
|
+ RequireCsrfProtectionMatcherInLambdaConfig.MATCHER = mock(RequestMatcher.class);
|
|
|
+ when(RequireCsrfProtectionMatcherInLambdaConfig.MATCHER.matches(any())).thenReturn(true);
|
|
|
+ this.spring.register(RequireCsrfProtectionMatcherInLambdaConfig.class, BasicController.class).autowire();
|
|
|
+
|
|
|
+ this.mvc.perform(get("/"))
|
|
|
+ .andExpect(status().isForbidden());
|
|
|
+ }
|
|
|
+
|
|
|
+ @EnableWebSecurity
|
|
|
+ static class RequireCsrfProtectionMatcherInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
+ static RequestMatcher MATCHER;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void configure(HttpSecurity http) throws Exception {
|
|
|
+ // @formatter:off
|
|
|
+ http
|
|
|
+ .csrf(csrf -> csrf.requireCsrfProtectionMatcher(MATCHER));
|
|
|
+ // @formatter:on
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void getWhenCustomCsrfTokenRepositoryThenRepositoryIsUsed() throws Exception {
|
|
|
CsrfTokenRepositoryConfig.REPO = mock(CsrfTokenRepository.class);
|
|
@@ -454,6 +508,33 @@ public class CsrfConfigurerTests {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void getWhenCustomCsrfTokenRepositoryInLambdaThenRepositoryIsUsed() throws Exception {
|
|
|
+ CsrfTokenRepositoryInLambdaConfig.REPO = mock(CsrfTokenRepository.class);
|
|
|
+ when(CsrfTokenRepositoryInLambdaConfig.REPO.loadToken(any()))
|
|
|
+ .thenReturn(new DefaultCsrfToken("X-CSRF-TOKEN", "_csrf", "token"));
|
|
|
+ this.spring.register(CsrfTokenRepositoryInLambdaConfig.class, BasicController.class).autowire();
|
|
|
+
|
|
|
+ this.mvc.perform(get("/"))
|
|
|
+ .andExpect(status().isOk());
|
|
|
+ verify(CsrfTokenRepositoryInLambdaConfig.REPO).loadToken(any(HttpServletRequest.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @EnableWebSecurity
|
|
|
+ static class CsrfTokenRepositoryInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
+ static CsrfTokenRepository REPO;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void configure(HttpSecurity http) throws Exception {
|
|
|
+ // @formatter:off
|
|
|
+ http
|
|
|
+ .formLogin()
|
|
|
+ .and()
|
|
|
+ .csrf(csrf -> csrf.csrfTokenRepository(REPO));
|
|
|
+ // @formatter:on
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void getWhenCustomAccessDeniedHandlerThenHandlerIsUsed() throws Exception {
|
|
|
AccessDeniedHandlerConfig.DENIED_HANDLER = mock(AccessDeniedHandler.class);
|