Skip to content

Commit cf69cdf

Browse files
ZhivkoDelchevrwinch
authored andcommitted
Reverse content type check
When MultipartFormData is enabled currently the CsrfWebFilter compares the content-type header against MULTIPART_FORM_DATA MediaType which leads to NullPointerExecption when there is no content-type header. This commit reverse the check to compare the MULTIPART_FORM_DATA MediaType against the content-type which contains null check and avoids the exception. closes gh-11204 Closes gh-11205
1 parent f359051 commit cf69cdf

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private Mono<String> tokenFromMultipartData(ServerWebExchange exchange, CsrfToke
151151
ServerHttpRequest request = exchange.getRequest();
152152
HttpHeaders headers = request.getHeaders();
153153
MediaType contentType = headers.getContentType();
154-
if (!contentType.includes(MediaType.MULTIPART_FORM_DATA)) {
154+
if (!MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
155155
return Mono.empty();
156156
}
157157
return exchange.getMultipartData().map((d) -> d.getFirst(expected.getParameterName())).cast(FormFieldPart.class)

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

+11
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ public void filterWhenMultipartFormDataAndEnabledThenGranted() {
189189
.expectStatus().is2xxSuccessful();
190190
}
191191

192+
@Test
193+
public void filterWhenPostAndMultipartFormDataEnabledAndNoBodyProvided() {
194+
this.csrfFilter.setCsrfTokenRepository(this.repository);
195+
this.csrfFilter.setTokenFromMultipartDataEnabled(true);
196+
given(this.repository.loadToken(any())).willReturn(Mono.just(this.token));
197+
given(this.repository.generateToken(any())).willReturn(Mono.just(this.token));
198+
WebTestClient client = WebTestClient.bindToController(new OkController()).webFilter(this.csrfFilter).build();
199+
client.post().uri("/").header(this.token.getHeaderName(), this.token.getToken()).exchange().expectStatus()
200+
.is2xxSuccessful();
201+
}
202+
192203
@Test
193204
public void filterWhenFormDataAndEnabledThenGranted() {
194205
this.csrfFilter.setCsrfTokenRepository(this.repository);

0 commit comments

Comments
 (0)