Skip to content
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
19 changes: 18 additions & 1 deletion src/main/java/org/apache/commons/fileupload/MultipartStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.).
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String> 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"));
}
}
Loading