Skip to content

Commit b7ccb63

Browse files
jzheauxrwinch
authored andcommitted
Disable CSRF by Request Matcher
This introduces an evolution on CsrfConfigurer#ignoreAntMatchers, allowing users to specify a RequestMatcher in the circumstance where more than just the path needs to be analyzed to determine whether CsrfFilter should require a token for the request. Simply put, a user can now selectively disable csrf by request matcher in addition to the way it can already be done with ant matchers. Fixes: gh-5477
1 parent ed20edd commit b7ccb63

File tree

2 files changed

+158
-2
lines changed

2 files changed

+158
-2
lines changed

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

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -128,7 +128,7 @@ public CsrfConfigurer<H> requireCsrfProtectionMatcher(
128128
* </p>
129129
*
130130
* <p>
131-
* The following will ensure CSRF protection ignores:
131+
* For example, the following configuration will ensure CSRF protection ignores:
132132
* </p>
133133
* <ul>
134134
* <li>Any GET, HEAD, TRACE, OPTIONS (this is the default)</li>
@@ -150,6 +150,35 @@ public CsrfConfigurer<H> ignoringAntMatchers(String... antPatterns) {
150150
.and();
151151
}
152152

153+
/**
154+
* <p>
155+
* Allows specifying {@link HttpServletRequest}s that should not use CSRF Protection
156+
* even if they match the {@link #requireCsrfProtectionMatcher(RequestMatcher)}.
157+
* </p>
158+
*
159+
* <p>
160+
* For example, the following configuration will ensure CSRF protection ignores:
161+
* </p>
162+
* <ul>
163+
* <li>Any GET, HEAD, TRACE, OPTIONS (this is the default)</li>
164+
* <li>We also explicitly state to ignore any request that has a "X-Requested-With: XMLHttpRequest" header</li>
165+
* </ul>
166+
*
167+
* <pre>
168+
* http
169+
* .csrf()
170+
* .ignoringRequestMatchers(request -> "XMLHttpRequest".equals(request.getHeader("X-Requested-With")))
171+
* .and()
172+
* ...
173+
* </pre>
174+
*
175+
* @since 5.1
176+
*/
177+
public CsrfConfigurer<H> ignoringRequestMatchers(RequestMatcher... requestMatchers) {
178+
return new IgnoreCsrfProtectionRegistry(this.context).requestMatchers(requestMatchers)
179+
.and();
180+
}
181+
153182
@SuppressWarnings("unchecked")
154183
@Override
155184
public void configure(H http) throws Exception {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.annotation.web.configurers;
18+
19+
import org.junit.Rule;
20+
import org.junit.Test;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.http.HttpMethod;
24+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
25+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
26+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
27+
import org.springframework.security.config.test.SpringTestRule;
28+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
29+
import org.springframework.security.web.util.matcher.RequestMatcher;
30+
import org.springframework.test.web.servlet.MockMvc;
31+
import org.springframework.web.bind.annotation.RequestMapping;
32+
import org.springframework.web.bind.annotation.RestController;
33+
34+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
35+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
36+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
37+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
38+
39+
/**
40+
* @author Josh Cummings
41+
*/
42+
public class CsrfConfigurerIgnoringRequestMatchersTests {
43+
44+
@Autowired
45+
MockMvc mvc;
46+
47+
@Rule
48+
public final SpringTestRule spring = new SpringTestRule();
49+
50+
@Test
51+
public void requestWhenIgnoringRequestMatchersThenAugmentedByConfiguredRequestMatcher()
52+
throws Exception {
53+
this.spring.register(IgnoringRequestMatchers.class, BasicController.class).autowire();
54+
55+
this.mvc.perform(get("/path"))
56+
.andExpect(status().isForbidden());
57+
58+
this.mvc.perform(post("/path"))
59+
.andExpect(status().isOk());
60+
}
61+
62+
@EnableWebSecurity
63+
static class IgnoringRequestMatchers extends WebSecurityConfigurerAdapter {
64+
RequestMatcher requestMatcher =
65+
request -> HttpMethod.POST.name().equals(request.getMethod());
66+
67+
@Override
68+
protected void configure(HttpSecurity http) throws Exception {
69+
// @formatter:off
70+
http
71+
.csrf()
72+
.requireCsrfProtectionMatcher(new AntPathRequestMatcher("/path"))
73+
.ignoringRequestMatchers(this.requestMatcher);
74+
// @formatter:on
75+
}
76+
}
77+
78+
@Test
79+
public void requestWhenIgnoringRequestMatcherThenUnionsWithConfiguredIgnoringAntMatchers()
80+
throws Exception {
81+
82+
this.spring.register(IgnoringPathsAndMatchers.class, BasicController.class).autowire();
83+
84+
this.mvc.perform(put("/csrf"))
85+
.andExpect(status().isForbidden());
86+
87+
this.mvc.perform(post("/csrf"))
88+
.andExpect(status().isOk());
89+
90+
this.mvc.perform(put("/no-csrf"))
91+
.andExpect(status().isOk());
92+
}
93+
94+
@EnableWebSecurity
95+
static class IgnoringPathsAndMatchers extends WebSecurityConfigurerAdapter {
96+
RequestMatcher requestMatcher =
97+
request -> HttpMethod.POST.name().equals(request.getMethod());
98+
99+
@Override
100+
protected void configure(HttpSecurity http) throws Exception {
101+
// @formatter:off
102+
http
103+
.csrf()
104+
.ignoringAntMatchers("/no-csrf")
105+
.ignoringRequestMatchers(this.requestMatcher);
106+
// @formatter:on
107+
}
108+
}
109+
110+
@RestController
111+
public static class BasicController {
112+
@RequestMapping("/path")
113+
public String path() {
114+
return "path";
115+
}
116+
117+
@RequestMapping("/csrf")
118+
public String csrf() {
119+
return "csrf";
120+
}
121+
122+
@RequestMapping("/no-csrf")
123+
public String noCsrf() {
124+
return "no-csrf";
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)