Skip to content

Commit

Permalink
Fix CsrfWebFilter error message when expected CSRF not found
Browse files Browse the repository at this point in the history
Closes gh-9337
  • Loading branch information
rwinch committed Jan 12, 2021
1 parent 429caea commit 61b75bb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static void skipExchange(ServerWebExchange exchange) {
private Mono<Void> validateToken(ServerWebExchange exchange) {
return this.csrfTokenRepository.loadToken(exchange)
.switchIfEmpty(Mono
.defer(() -> Mono.error(new CsrfException("CSRF Token has been associated to this client"))))
.defer(() -> Mono.error(new CsrfException("An expected CSRF token cannot be found"))))
.filterWhen((expected) -> containsValidCsrfToken(exchange, expected))
.switchIfEmpty(Mono.defer(() -> Mono.error(new CsrfException("Invalid CSRF Token")))).then();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class CsrfWebFilterTests {

private MockServerWebExchange get = MockServerWebExchange.from(MockServerHttpRequest.get("/"));

private ServerWebExchange post = MockServerWebExchange.from(MockServerHttpRequest.post("/"));
private MockServerWebExchange post = MockServerWebExchange.from(MockServerHttpRequest.post("/"));

@Test
public void filterWhenGetThenSessionNotCreatedAndChainContinues() {
Expand All @@ -91,6 +91,8 @@ public void filterWhenPostAndEstablishedCsrfTokenAndRequestMissingTokenThenCsrfE
Mono<Void> result = this.csrfFilter.filter(this.post, this.chain);
StepVerifier.create(result).verifyComplete();
assertThat(this.post.getResponse().getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
StepVerifier.create(this.post.getResponse().getBodyAsString())
.assertNext(b -> assertThat(b).contains("An expected CSRF token cannot be found"));
}

@Test
Expand Down

0 comments on commit 61b75bb

Please sign in to comment.