Skip to content

Commit 8d44763

Browse files
committed
Fix non-standard HTTP method for CsrfWebFilter
Closes gh-8452
2 parents 6db514a + 4473dca commit 8d44763

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

web/src/main/java/org/springframework/security/web/server/csrf/CsrfWebFilter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -60,6 +60,7 @@
6060
* </p>
6161
*
6262
* @author Rob Winch
63+
* @author Parikshit Dutta
6364
* @since 5.0
6465
*/
6566
public class CsrfWebFilter implements WebFilter {
@@ -187,7 +188,7 @@ private static class DefaultRequireCsrfProtectionMatcher implements ServerWebExc
187188
@Override
188189
public Mono<MatchResult> matches(ServerWebExchange exchange) {
189190
return Mono.just(exchange.getRequest())
190-
.map(r -> r.getMethod())
191+
.flatMap(r -> Mono.justOrEmpty(r.getMethod()))
191192
.filter(m -> ALLOWED_METHODS.contains(m))
192193
.flatMap(m -> MatchResult.notMatch())
193194
.switchIfEmpty(MatchResult.match());

web/src/test/java/org/springframework/security/web/server/csrf/CsrfWebFilterTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -20,11 +20,14 @@
2020
import org.junit.runner.RunWith;
2121
import org.mockito.Mock;
2222
import org.mockito.junit.MockitoJUnitRunner;
23+
24+
import org.springframework.http.HttpMethod;
2325
import org.springframework.http.HttpStatus;
2426
import org.springframework.http.MediaType;
2527
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
2628
import org.springframework.mock.web.server.MockServerWebExchange;
2729
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
30+
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher.MatchResult;
2831
import org.springframework.test.web.reactive.server.WebTestClient;
2932
import org.springframework.web.bind.annotation.RequestMapping;
3033
import org.springframework.web.bind.annotation.RestController;
@@ -45,6 +48,7 @@
4548

4649
/**
4750
* @author Rob Winch
51+
* @author Parikshit Dutta
4852
* @since 5.0
4953
*/
5054
@RunWith(MockitoJUnitRunner.class)
@@ -183,6 +187,16 @@ public void filterWhenPostAndEstablishedCsrfTokenAndHeaderValidTokenThenContinue
183187
chainResult.assertWasSubscribed();
184188
}
185189

190+
@Test
191+
// gh-8452
192+
public void matchesRequireCsrfProtectionWhenNonStandardHTTPMethodIsUsed() {
193+
HttpMethod customHttpMethod = HttpMethod.resolve("non-standard-http-method");
194+
MockServerWebExchange nonStandardHttpRequest = from(MockServerHttpRequest.method(customHttpMethod, "/"));
195+
196+
ServerWebExchangeMatcher serverWebExchangeMatcher = CsrfWebFilter.DEFAULT_CSRF_MATCHER;
197+
assertThat(serverWebExchangeMatcher.matches(nonStandardHttpRequest).map(MatchResult::isMatch).block()).isTrue();
198+
}
199+
186200
@Test
187201
public void doFilterWhenSkipExchangeInvokedThenSkips() {
188202
PublisherProbe<Void> chainResult = PublisherProbe.empty();

0 commit comments

Comments
 (0)