Skip to content

[UNDERTOW-2552] fix: add non-ascii support for big formData value #1723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions core/src/main/java/io/undertow/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.undertow.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand Down Expand Up @@ -74,13 +75,13 @@ public static String readFile(InputStream file) {
*/
public static String readFile(InputStream file, Charset charSet) {
try (BufferedInputStream stream = new BufferedInputStream(file)) {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
StringBuilder builder = new StringBuilder();
int read;
while ((read = stream.read(buff)) != -1) {
builder.append(new String(buff, 0, read, charSet));
result.write(buff, 0, read);
}
return builder.toString();
return result.toString(charSet);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,53 @@ private void logResult(HttpServerExchange exchange, boolean inMemory, String fil
};
}

private static HttpHandler createInMemoryUtf8ReadingHandler(final long fileSizeThreshold, final long maxInvidualFileThreshold, final HttpHandler async) {
return new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
MultiPartParserDefinition multiPartParserDefinition = new MultiPartParserDefinition();
multiPartParserDefinition.setFileSizeThreshold(fileSizeThreshold);
multiPartParserDefinition.setMaxIndividualFileSize(maxInvidualFileThreshold);
multiPartParserDefinition.setDefaultEncoding(StandardCharsets.UTF_8.name());
final FormDataParser parser = FormParserFactory.builder(false)
.addParsers(new FormEncodedDataDefinition(), multiPartParserDefinition)
.build().createParser(exchange);
if (async == null) {
try {
FormData data = parser.parseBlocking();
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
if (data.getFirst("formValue").getValue().equals("myValue")) {
FormData.FormValue file = data.getFirst("file");
if (file.isFileItem()) {
exchange.setStatusCode(StatusCodes.OK);
logResult(exchange, file.getFileItem().isInMemory(), file.getFileName(), file.getValue());
}
}
exchange.endExchange();
} catch (FileTooLargeException e) {
exchange.setStatusCode(StatusCodes.REQUEST_ENTITY_TOO_LARGE);
exchange.endExchange();
} catch (Throwable e) {
e.printStackTrace();
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
} finally {
IoUtils.safeClose(parser);
}
} else {
parser.parse(async);
}
}

private void logResult(HttpServerExchange exchange, boolean inMemory, String fileName, String content) throws IOException {
String res = String.format("in_memory:%s;file_name:%s;hash:%s", inMemory, fileName, DigestUtils.md5Hex(content));
final OutputStream outputStream = exchange.getOutputStream();
outputStream.write(res.getBytes());
outputStream.close();
}
};
}

@Test
public void testFileUploadWithSmallFileSizeThreshold() throws Exception {
DefaultServer.setRootHandler(new BlockingHandler(createInMemoryReadingHandler(10)));
Expand Down Expand Up @@ -294,6 +341,42 @@ public void testFileUploadWithSmallFileSizeThreshold() throws Exception {
}
}

@Test
public void testFileUploadWithSmallFileSizeThresholdAndNonEnglishCharacters() throws Exception {
DefaultServer.setRootHandler(new BlockingHandler(createInMemoryUtf8ReadingHandler(10, -1, null)));
String value = createUtf8PostValue(2000);
TestHttpClient client = new TestHttpClient();
try {

HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8));
entity.addPart("file", new StringBody(value, "text/plain", StandardCharsets.UTF_8));

post.setEntity(entity);
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String resp = HttpClientUtils.readResponse(result);

Map<String, String> parsedResponse = parse(resp);

Assert.assertEquals("false", parsedResponse.get("in_memory"));
Assert.assertEquals(DigestUtils.md5Hex(value), parsedResponse.get("hash"));

} finally {
client.getConnectionManager().shutdown();
}
}

private static String createUtf8PostValue(int size) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {
sb.append("aÁRVÍZTÜKÖRFÚRÓGÉPárvíztükörfúrógép");
}
return sb.toString();
}

@Test
public void testFileUploadWithLargeFileSizeThreshold() throws Exception {
DefaultServer.setRootHandler(new BlockingHandler(createInMemoryReadingHandler(10_000)));
Expand Down
Loading