Skip to content

Commit

Permalink
fix: fix ServletFileUpload header encoding (#20480)
Browse files Browse the repository at this point in the history
This change will set ServletFileUpload's header encoding always to UTF-8 in StreamReceiverHandler when request character encoding is null. This ensures that system's default character encoding is not applied when parsing filename of the uploaded file, unless request's character encoding is set otherwise.
Only for setups without multipart config for servlet.

Fixes: #20417
  • Loading branch information
tltv committed Nov 19, 2024
1 parent 8f7483c commit 0173714
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,22 @@ protected Collection<Part> getParts(VaadinRequest request)

protected FileItemInputIterator getItemIterator(VaadinRequest request)
throws FileUploadException, IOException {
JakartaServletFileUpload upload = createServletFileUpload(request);
return upload.getItemIterator((HttpServletRequest) request);
}

// protected for testing purposes only
protected JakartaServletFileUpload createServletFileUpload(VaadinRequest request) {
JakartaServletFileUpload upload = new JakartaServletFileUpload();
upload.setSizeMax(requestSizeMax);
upload.setFileSizeMax(fileSizeMax);
upload.setFileCountMax(fileCountMax);
return upload.getItemIterator((HttpServletRequest) request);
if (request.getCharacterEncoding() == null) {
// Request body's file upload headers are expected to be encoded in
// UTF-8 if not explicitly set otherwise in the request.
upload.setHeaderCharset(StandardCharsets.UTF_8);
}
return upload;
}

public void setRequestSizeMax(long requestSizeMax) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Optional;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -87,6 +88,7 @@ public class StreamReceiverHandlerTest {
private List<Part> parts;

private boolean isGetContentLengthLongCalled;
private String requestCharacterEncoding;

@Before
public void setup() throws Exception {
Expand Down Expand Up @@ -181,6 +183,11 @@ public long getContentLengthLong() {
isGetContentLengthLongCalled = true;
return 0;
}

@Override
public String getCharacterEncoding() {
return requestCharacterEncoding;
}
};
}

Expand Down Expand Up @@ -291,6 +298,27 @@ public void doHandleMultipartFileUpload_noPart_uploadFailed_responseStatusIs500_
Assert.assertTrue(isGetContentLengthLongCalled);
}

@Test
public void createServletFileUpload_useUTF8HeaderCharacterEncodingWhenRequestCharEncodingIsNotSet() {
ServletFileUpload servletFileUpload = handler
.createServletFileUpload(request);
Assert.assertNotNull(servletFileUpload);
Assert.assertEquals(
"Header encoding should be UTF-8 when request character encoding is null",
"UTF-8", servletFileUpload.getHeaderEncoding());
}

@Test
public void createServletFileUpload_dontSetHeaderCharEncodingWhenRequestCharEncodingIsSet() {
requestCharacterEncoding = "ASCII";
ServletFileUpload servletFileUpload = handler
.createServletFileUpload(request);
Assert.assertNotNull(servletFileUpload);
Assert.assertNull(
"Header encoding should not be set by Flow when request character encoding is set",
servletFileUpload.getHeaderEncoding());
}

@Test
public void doHandleMultipartFileUpload_hasParts_uploadFailed_responseStatusIs500()
throws IOException {
Expand Down

0 comments on commit 0173714

Please sign in to comment.