Skip to content

Commit ed27ea7

Browse files
committed
Restrict fallback multipart binding to POST requests with multipart/form-data
Closes gh-26999 See gh-26826
1 parent 128689e commit ed27ea7

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

spring-web/src/main/java/org/springframework/web/bind/ServletRequestDataBinder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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,6 +20,8 @@
2020
import javax.servlet.http.HttpServletRequest;
2121

2222
import org.springframework.beans.MutablePropertyValues;
23+
import org.springframework.http.HttpMethod;
24+
import org.springframework.http.MediaType;
2325
import org.springframework.lang.Nullable;
2426
import org.springframework.util.StringUtils;
2527
import org.springframework.validation.BindException;
@@ -106,9 +108,9 @@ public void bind(ServletRequest request) {
106108
if (multipartRequest != null) {
107109
bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
108110
}
109-
else if (StringUtils.startsWithIgnoreCase(request.getContentType(), "multipart/")) {
111+
else if (StringUtils.startsWithIgnoreCase(request.getContentType(), MediaType.MULTIPART_FORM_DATA_VALUE)) {
110112
HttpServletRequest httpServletRequest = WebUtils.getNativeRequest(request, HttpServletRequest.class);
111-
if (httpServletRequest != null) {
113+
if (httpServletRequest != null && HttpMethod.POST.matches(httpServletRequest.getMethod())) {
112114
StandardServletPartUtils.bindParts(httpServletRequest, mpvs, isBindEmptyMultipartFiles());
113115
}
114116
}

spring-web/src/main/java/org/springframework/web/bind/support/WebRequestDataBinder.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -19,6 +19,9 @@
1919
import javax.servlet.http.HttpServletRequest;
2020

2121
import org.springframework.beans.MutablePropertyValues;
22+
import org.springframework.http.HttpHeaders;
23+
import org.springframework.http.HttpMethod;
24+
import org.springframework.http.MediaType;
2225
import org.springframework.lang.Nullable;
2326
import org.springframework.util.StringUtils;
2427
import org.springframework.validation.BindException;
@@ -107,13 +110,15 @@ public WebRequestDataBinder(@Nullable Object target, String objectName) {
107110
public void bind(WebRequest request) {
108111
MutablePropertyValues mpvs = new MutablePropertyValues(request.getParameterMap());
109112
if (request instanceof NativeWebRequest) {
110-
MultipartRequest multipartRequest = ((NativeWebRequest) request).getNativeRequest(MultipartRequest.class);
113+
NativeWebRequest nativeRequest = (NativeWebRequest) request;
114+
MultipartRequest multipartRequest = nativeRequest.getNativeRequest(MultipartRequest.class);
111115
if (multipartRequest != null) {
112116
bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
113117
}
114-
else if (StringUtils.startsWithIgnoreCase(request.getHeader("Content-Type"), "multipart/")) {
115-
HttpServletRequest servletRequest = ((NativeWebRequest) request).getNativeRequest(HttpServletRequest.class);
116-
if (servletRequest != null) {
118+
else if (StringUtils.startsWithIgnoreCase(
119+
request.getHeader(HttpHeaders.CONTENT_TYPE), MediaType.MULTIPART_FORM_DATA_VALUE)) {
120+
HttpServletRequest servletRequest = nativeRequest.getNativeRequest(HttpServletRequest.class);
121+
if (servletRequest != null && HttpMethod.POST.matches(servletRequest.getMethod())) {
117122
StandardServletPartUtils.bindParts(servletRequest, mpvs, isBindEmptyMultipartFiles());
118123
}
119124
}

spring-web/src/main/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
import org.springframework.beans.BeanUtils;
3939
import org.springframework.beans.TypeMismatchException;
4040
import org.springframework.core.MethodParameter;
41+
import org.springframework.http.HttpHeaders;
42+
import org.springframework.http.HttpMethod;
43+
import org.springframework.http.MediaType;
4144
import org.springframework.lang.Nullable;
4245
import org.springframework.util.Assert;
4346
import org.springframework.util.ObjectUtils;
@@ -349,9 +352,10 @@ public Object resolveConstructorArgument(String paramName, Class<?> paramType, N
349352
return (files.size() == 1 ? files.get(0) : files);
350353
}
351354
}
352-
else if (StringUtils.startsWithIgnoreCase(request.getHeader("Content-Type"), "multipart/")) {
355+
else if (StringUtils.startsWithIgnoreCase(
356+
request.getHeader(HttpHeaders.CONTENT_TYPE), MediaType.MULTIPART_FORM_DATA_VALUE)) {
353357
HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
354-
if (servletRequest != null) {
358+
if (servletRequest != null && HttpMethod.POST.matches(servletRequest.getMethod())) {
355359
List<Part> parts = StandardServletPartUtils.getParts(servletRequest, paramName);
356360
if (!parts.isEmpty()) {
357361
return (parts.size() == 1 ? parts.get(0) : parts);

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,9 +1998,8 @@ void dataClassBindingWithMultipartFile(boolean usePathPatterns) throws Exception
19981998
void dataClassBindingWithServletPart(boolean usePathPatterns) throws Exception {
19991999
initDispatcherServlet(ServletPartDataClassController.class, usePathPatterns);
20002000

2001-
MockHttpServletRequest request = new MockHttpServletRequest();
2001+
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bind");
20022002
request.setContentType("multipart/form-data");
2003-
request.setRequestURI("/bind");
20042003
request.addPart(new MockPart("param1", "value1".getBytes(StandardCharsets.UTF_8)));
20052004
request.addParameter("param2", "true");
20062005
MockHttpServletResponse response = new MockHttpServletResponse();

0 commit comments

Comments
 (0)