Skip to content

Commit 3104192

Browse files
committed
Fixed issue with encoded form parameters and added unit test for it
1 parent a0e1404 commit 3104192

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletRequest.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -707,13 +707,8 @@ private Map<String, List<String>> getFormUrlEncodedParametersMap() {
707707
if (!contentType.startsWith(MediaType.APPLICATION_FORM_URLENCODED) || !getMethod().toLowerCase().equals("post")) {
708708
return new HashMap<>();
709709
}
710-
String rawBodyContent;
711-
try {
712-
rawBodyContent = URLDecoder.decode(request.getBody(), DEFAULT_CHARACTER_ENCODING);
713-
} catch (UnsupportedEncodingException e) {
714-
log.warn("Could not decode body content - proceeding as if it was already decoded", e);
715-
rawBodyContent = request.getBody();
716-
}
710+
711+
String rawBodyContent = request.getBody();
717712

718713
Map<String, List<String>> output = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
719714
for (String parameter : rawBodyContent.split(FORM_DATA_SEPARATOR)) {
@@ -725,10 +720,19 @@ private Map<String, List<String>> getFormUrlEncodedParametersMap() {
725720
if (output.containsKey(parameterKeyValue[0])) {
726721
values = output.get(parameterKeyValue[0]);
727722
}
728-
values.add(parameterKeyValue[1]);
729-
output.put(parameterKeyValue[0], values);
723+
values.add(decodeValueIfEncoded(parameterKeyValue[1]));
724+
output.put(decodeValueIfEncoded(parameterKeyValue[0]), values);
730725
}
731726

732727
return output;
733728
}
729+
730+
private String decodeValueIfEncoded(String value) {
731+
try {
732+
return URLDecoder.decode(value, DEFAULT_CHARACTER_ENCODING);
733+
} catch (UnsupportedEncodingException e) {
734+
log.warn("Could not decode body content - proceeding as if it was already decoded", e);
735+
return value;
736+
}
737+
}
734738
}

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletRequestFormTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import org.apache.commons.io.IOUtils;
88
import org.apache.http.HttpEntity;
99
import org.apache.http.client.entity.EntityBuilder;
10+
import org.apache.http.entity.ContentType;
1011
import org.apache.http.entity.mime.MultipartEntityBuilder;
1112
import org.junit.Test;
1213

1314
import javax.servlet.ServletException;
1415
import javax.servlet.http.HttpServletRequest;
1516
import javax.ws.rs.core.HttpHeaders;
17+
import javax.ws.rs.core.MediaType;
1618

1719
import java.io.IOException;
1820
import java.util.Random;
@@ -29,6 +31,8 @@ public class AwsProxyHttpServletRequestFormTest {
2931
private static final String PART_VALUE_2 = "value2";
3032
private static final String FILE_KEY = "file_upload_1";
3133

34+
private static final String ENCODED_VALUE = "test123a%3D1%262@3";
35+
3236
private static final HttpEntity MULTIPART_FORM_DATA = MultipartEntityBuilder.create()
3337
.addTextBody(PART_KEY_1, PART_VALUE_1)
3438
.addTextBody(PART_KEY_2, PART_VALUE_2)
@@ -43,6 +47,24 @@ public class AwsProxyHttpServletRequestFormTest {
4347
.addTextBody(PART_KEY_2, PART_VALUE_2)
4448
.addBinaryBody(FILE_KEY, FILE_BYTES)
4549
.build();
50+
private static final String ENCODED_FORM_ENTITY = PART_KEY_1 + "=" + ENCODED_VALUE + "&" + PART_KEY_2 + "=" + PART_VALUE_2;
51+
52+
@Test
53+
public void postForm_getParam_getEncodedFullValue() {
54+
try {
55+
AwsProxyRequest proxyRequest = new AwsProxyRequestBuilder("/form", "POST")
56+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED)
57+
.body(ENCODED_FORM_ENTITY)
58+
.build();
59+
60+
HttpServletRequest request = new AwsProxyHttpServletRequest(proxyRequest, null, null);
61+
assertNotNull(request.getParts());
62+
assertEquals("test123a=1&2@3", request.getParameter(PART_KEY_1));
63+
} catch (IOException | ServletException e) {
64+
fail(e.getMessage());
65+
}
66+
}
67+
4668
@Test
4769
public void postForm_getParts_parsing() {
4870
try {

0 commit comments

Comments
 (0)