From 2c1afca9c556b63c1645630b1db8d2934d1f2033 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 13 Nov 2019 15:03:22 +0000 Subject: [PATCH] Reject null form data names ...or skip if there are no values either. Closes gh-22372 --- .../http/converter/FormHttpMessageConverter.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java index fa79ef77aa01..ced2ae1767f9 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java @@ -40,6 +40,7 @@ import org.springframework.http.StreamingHttpOutputMessage; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MimeTypeUtils; import org.springframework.util.MultiValueMap; @@ -415,7 +416,11 @@ else if (mediaType.getCharset() == null) { protected String serializeForm(MultiValueMap formData, Charset charset) { StringBuilder builder = new StringBuilder(); - formData.forEach((name, values) -> + formData.forEach((name, values) -> { + if (name == null) { + Assert.isTrue(CollectionUtils.isEmpty(values), "Null name in form data: " + formData); + return; + } values.forEach(value -> { try { if (builder.length() != 0) { @@ -430,7 +435,8 @@ protected String serializeForm(MultiValueMap formData, Charset c catch (UnsupportedEncodingException ex) { throw new IllegalStateException(ex); } - })); + }); + }); return builder.toString(); }