1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import org .junit .runner .RunWith ;
21
21
import org .mockito .Mock ;
22
22
import org .mockito .junit .MockitoJUnitRunner ;
23
+
24
+ import org .springframework .http .HttpMethod ;
23
25
import org .springframework .http .HttpStatus ;
24
26
import org .springframework .http .MediaType ;
25
27
import org .springframework .mock .http .server .reactive .MockServerHttpRequest ;
26
28
import org .springframework .mock .web .server .MockServerWebExchange ;
29
+ import org .springframework .security .web .server .util .matcher .ServerWebExchangeMatcher ;
30
+ import org .springframework .security .web .server .util .matcher .ServerWebExchangeMatcher .MatchResult ;
27
31
import org .springframework .web .server .WebFilterChain ;
28
32
import org .springframework .web .server .WebSession ;
29
33
import reactor .core .publisher .Mono ;
33
37
import static org .assertj .core .api .AssertionsForInterfaceTypes .assertThat ;
34
38
import static org .mockito .ArgumentMatchers .any ;
35
39
import static org .mockito .Mockito .when ;
40
+ import static org .springframework .mock .web .server .MockServerWebExchange .from ;
36
41
37
42
/**
38
43
* @author Rob Winch
44
+ * @author Parikshit Dutta
39
45
* @since 5.0
40
46
*/
41
47
@ RunWith (MockitoJUnitRunner .class )
@@ -49,10 +55,10 @@ public class CsrfWebFilterTests {
49
55
50
56
private CsrfWebFilter csrfFilter = new CsrfWebFilter ();
51
57
52
- private MockServerWebExchange get = MockServerWebExchange . from (
58
+ private MockServerWebExchange get = from (
53
59
MockServerHttpRequest .get ("/" ));
54
60
55
- private MockServerWebExchange post = MockServerWebExchange . from (
61
+ private MockServerWebExchange post = from (
56
62
MockServerHttpRequest .post ("/" ));
57
63
58
64
@ Test
@@ -104,7 +110,7 @@ public void filterWhenPostAndEstablishedCsrfTokenAndRequestParamInvalidTokenThen
104
110
this .csrfFilter .setCsrfTokenRepository (this .repository );
105
111
when (this .repository .loadToken (any ()))
106
112
.thenReturn (Mono .just (this .token ));
107
- this .post = MockServerWebExchange . from (MockServerHttpRequest .post ("/" )
113
+ this .post = from (MockServerHttpRequest .post ("/" )
108
114
.body (this .token .getParameterName () + "=" +this .token .getToken ()+"INVALID" ));
109
115
110
116
Mono <Void > result = this .csrfFilter .filter (this .post , this .chain );
@@ -125,7 +131,7 @@ public void filterWhenPostAndEstablishedCsrfTokenAndRequestParamValidTokenThenCo
125
131
.thenReturn (Mono .just (this .token ));
126
132
when (this .repository .generateToken (any ()))
127
133
.thenReturn (Mono .just (this .token ));
128
- this .post = MockServerWebExchange . from (MockServerHttpRequest .post ("/" )
134
+ this .post = from (MockServerHttpRequest .post ("/" )
129
135
.contentType (MediaType .APPLICATION_FORM_URLENCODED )
130
136
.body (this .token .getParameterName () + "=" +this .token .getToken ()));
131
137
@@ -142,7 +148,7 @@ public void filterWhenPostAndEstablishedCsrfTokenAndHeaderInvalidTokenThenCsrfEx
142
148
this .csrfFilter .setCsrfTokenRepository (this .repository );
143
149
when (this .repository .loadToken (any ()))
144
150
.thenReturn (Mono .just (this .token ));
145
- this .post = MockServerWebExchange . from (MockServerHttpRequest .post ("/" )
151
+ this .post = from (MockServerHttpRequest .post ("/" )
146
152
.header (this .token .getHeaderName (), this .token .getToken ()+"INVALID" ));
147
153
148
154
Mono <Void > result = this .csrfFilter .filter (this .post , this .chain );
@@ -163,7 +169,7 @@ public void filterWhenPostAndEstablishedCsrfTokenAndHeaderValidTokenThenContinue
163
169
.thenReturn (Mono .just (this .token ));
164
170
when (this .repository .generateToken (any ()))
165
171
.thenReturn (Mono .just (this .token ));
166
- this .post = MockServerWebExchange . from (MockServerHttpRequest .post ("/" )
172
+ this .post = from (MockServerHttpRequest .post ("/" )
167
173
.header (this .token .getHeaderName (), this .token .getToken ()));
168
174
169
175
Mono <Void > result = this .csrfFilter .filter (this .post , this .chain );
@@ -173,4 +179,14 @@ public void filterWhenPostAndEstablishedCsrfTokenAndHeaderValidTokenThenContinue
173
179
174
180
chainResult .assertWasSubscribed ();
175
181
}
182
+
183
+ @ Test
184
+ // gh-8452
185
+ public void matchesRequireCsrfProtectionWhenNonStandardHTTPMethodIsUsed () {
186
+ HttpMethod customHttpMethod = HttpMethod .resolve ("non-standard-http-method" );
187
+ MockServerWebExchange nonStandardHttpRequest = from (MockServerHttpRequest .method (customHttpMethod , "/" ));
188
+
189
+ ServerWebExchangeMatcher serverWebExchangeMatcher = CsrfWebFilter .DEFAULT_CSRF_MATCHER ;
190
+ assertThat (serverWebExchangeMatcher .matches (nonStandardHttpRequest ).map (MatchResult ::isMatch ).block ()).isTrue ();
191
+ }
176
192
}
0 commit comments