diff --git a/src/main/java/org/apache/commons/fileupload/MultipartStream.java b/src/main/java/org/apache/commons/fileupload/MultipartStream.java index 325998772..88a76259e 100644 --- a/src/main/java/org/apache/commons/fileupload/MultipartStream.java +++ b/src/main/java/org/apache/commons/fileupload/MultipartStream.java @@ -479,6 +479,16 @@ private void notifyListener() { */ public static final byte DASH = 0x2D; + /** + * The space (SP) ASCII character value. + */ + private static final byte SPACE = 0x20; + + /** + * The horizontal tab (HTAB) ASCII character value. + */ + private static final byte TAB = 0x09; + /** * The maximum length of {@code header-part} that will be * processed (10 kilobytes = 10240 bytes.). @@ -854,6 +864,12 @@ public boolean readBoundary() head += boundaryLength; try { marker[0] = readByte(); + // RFC 2046, section 5.1.1: a delimiter may be followed by transport padding (SP and HTAB). + boolean padded = false; + while (marker[0] == SPACE || marker[0] == TAB) { + padded = true; + marker[0] = readByte(); + } if (marker[0] == LF) { // Work around IE5 Mac bug with input type=image. // Because the boundary delimiter, not including the trailing @@ -865,7 +881,8 @@ public boolean readBoundary() } marker[1] = readByte(); - if (arrayequals(marker, STREAM_TERMINATOR, 2)) { + // Padding is only permitted after the complete delimiter, so it cannot precede the close marker. + if (!padded && arrayequals(marker, STREAM_TERMINATOR, 2)) { nextChunk = false; } else if (arrayequals(marker, FIELD_SEPARATOR, 2)) { nextChunk = true; diff --git a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java index d6b740d61..4adc69706 100644 --- a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java +++ b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java @@ -17,13 +17,19 @@ package org.apache.commons.fileupload; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import org.apache.commons.fileupload.MultipartStream.MalformedStreamException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; @@ -117,4 +123,53 @@ public void testTwoParamConstructor() throws Exception { final MultipartStream ms = new MultipartStream(input, boundary, new MultipartStream.ProgressNotifier(null, contents.length)); assertNotNull(ms); } + + /** + * Parses a body with the boundary {@code ab} and returns the content of each part. + */ + private static List parseParts(final String body) throws IOException { + final byte[] contents = body.getBytes(StandardCharsets.ISO_8859_1); + final MultipartStream ms = new MultipartStream(new ByteArrayInputStream(contents), "ab".getBytes(StandardCharsets.ISO_8859_1), + new MultipartStream.ProgressNotifier(null, contents.length)); + final List parts = new ArrayList<>(); + boolean next = ms.skipPreamble(); + while (next) { + ms.readHeaders(); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + ms.readBodyData(out); + parts.add(out.toString("ISO-8859-1")); + next = ms.readBoundary(); + } + return parts; + } + + private static final String PART_HEADERS = "Content-Disposition: form-data; name=\"a\"\r\n\r\n"; + + @Test + public void testNoTransportPadding() throws Exception { + assertEquals(Arrays.asList("v1", "v2"), parseParts("--ab\r\n" + PART_HEADERS + "v1\r\n--ab\r\n" + PART_HEADERS + "v2\r\n--ab--\r\n")); + } + + /** + * RFC 2046, section 5.1.1: a delimiter may be followed by transport padding, which a receiver must be able to handle. + */ + @Test + public void testTransportPaddingAfterFirstDelimiter() throws Exception { + assertEquals(Arrays.asList("v1"), parseParts("--ab \t\r\n" + PART_HEADERS + "v1\r\n--ab--\r\n")); + } + + @Test + public void testTransportPaddingAfterEncapsulationDelimiter() throws Exception { + assertEquals(Arrays.asList("v1", "v2"), parseParts("--ab\r\n" + PART_HEADERS + "v1\r\n--ab \r\n" + PART_HEADERS + "v2\r\n--ab--\r\n")); + } + + @Test + public void testTransportPaddingAfterCloseDelimiter() throws Exception { + assertEquals(Arrays.asList("v1"), parseParts("--ab\r\n" + PART_HEADERS + "v1\r\n--ab-- \r\n")); + } + + @Test + public void testTransportPaddingBeforeCloseMarker() { + assertThrows(MalformedStreamException.class, () -> parseParts("--ab\r\n" + PART_HEADERS + "v1\r\n--ab --\r\n")); + } }