Skip to content

Commit 696692f

Browse files
committed
Do not attempt to decode wildcard content-types as form-data
Prior to this commit, the `DefaultServerWebExchange` would attempt to decode request bodies as form-data or multipart of the request content-type was compatible with the expected media types. If requests are sent with an invalid wildcard content-type such as "*/*" or "multipart/*", we should not attempt to decode here. Fixes gh-34660
1 parent faada70 commit 696692f

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpReques
149149
ServerCodecConfigurer configurer, String logPrefix) {
150150

151151
MediaType contentType = getContentType(request);
152-
if (contentType == null || !contentType.isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED)) {
152+
if (contentType == null || !contentType.isConcrete() || !contentType.isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED)) {
153153
return EMPTY_FORM_DATA;
154154
}
155155

156-
HttpMessageReader<MultiValueMap<String, String>> reader = getReader(configurer, MediaType.APPLICATION_FORM_URLENCODED, FORM_DATA_TYPE);
156+
HttpMessageReader<MultiValueMap<String, String>> reader = getReader(configurer, contentType, FORM_DATA_TYPE);
157157
if (reader == null) {
158158
return Mono.error(new IllegalStateException("No HttpMessageReader for " + contentType));
159159
}
@@ -167,7 +167,7 @@ private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpReques
167167
private Mono<MultiValueMap<String, Part>> initMultipartData(ServerCodecConfigurer configurer, String logPrefix) {
168168

169169
MediaType contentType = getContentType(this.request);
170-
if (contentType == null || !contentType.getType().equalsIgnoreCase("multipart")) {
170+
if (contentType == null || !contentType.isConcrete() || !contentType.getType().equalsIgnoreCase("multipart")) {
171171
return EMPTY_MULTIPART_DATA;
172172
}
173173

spring-web/src/test/java/org/springframework/web/server/adapter/DefaultServerWebExchangeTests.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.http.HttpHeaders;
2222
import org.springframework.http.MediaType;
2323
import org.springframework.http.codec.ServerCodecConfigurer;
24+
import org.springframework.http.codec.multipart.Part;
2425
import org.springframework.util.MultiValueMap;
2526
import org.springframework.web.server.ServerWebExchange;
2627
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver;
@@ -60,14 +61,25 @@ void transformUrlWithMultipleEncoders() {
6061
}
6162

6263
@Test // gh-34660
63-
void useFormDataMessageReaderWhenAllContentType() {
64+
void shouldNotDecodeFormDataWhenContentTypeNotConcrete() {
6465
MockServerHttpRequest request = MockServerHttpRequest
6566
.post("https://example.com")
6667
.header(HttpHeaders.CONTENT_TYPE, MediaType.ALL_VALUE)
6768
.body("project=spring");
6869
ServerWebExchange exchange = createExchange(request);
6970
MultiValueMap<String, String> body = exchange.getFormData().block();
70-
assertThat(body.get("project")).contains("spring");
71+
assertThat(body).isEmpty();
72+
}
73+
74+
@Test // gh-34660
75+
void shouldNotDecodeMultipartWhenContentTypeNotConcrete() {
76+
MockServerHttpRequest request = MockServerHttpRequest
77+
.post("https://example.com")
78+
.header(HttpHeaders.CONTENT_TYPE, "multipart/*")
79+
.body("project=spring");
80+
ServerWebExchange exchange = createExchange(request);
81+
MultiValueMap<String, Part> body = exchange.getMultipartData().block();
82+
assertThat(body).isEmpty();
7183
}
7284

7385

0 commit comments

Comments
 (0)