You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2025/09/02 07:22:12 UTC

(commons-fileupload) branch 1.x updated: Better exception if a multipart/mixed part has no boundary token defined

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git


The following commit(s) were added to refs/heads/1.x by this push:
     new 37252b18 Better exception if a multipart/mixed part has no boundary token defined
37252b18 is described below

commit 37252b1864e5af979904f034611bdea6916d4a27
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Tue Sep 2 08:22:07 2025 +0100

    Better exception if a multipart/mixed part has no boundary token defined
---
 src/changes/changes.xml                            |  1 +
 .../apache/commons/fileupload/FileUploadBase.java  |  4 +++
 .../apache/commons/fileupload/FileUploadTest.java  | 33 ++++++++++++++++++++++
 3 files changed, 38 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6557bf23..318982f3 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -43,6 +43,7 @@ The <action> type attribute can be add,update,fix,remove.
   <body>
     <release version="1.6.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
       <!-- FIX -->
+      <action type="fix" dev="markt" due-to="Coverity Scan">Better exception type and message if a multipart/mixed part is presented without a boundary defined</action>
       <!-- ADD -->
       <!-- UPDATE -->
       <action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 84 to 85.</action>
diff --git a/src/main/java/org/apache/commons/fileupload/FileUploadBase.java b/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
index 9448ead0..c9918f11 100644
--- a/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
+++ b/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
@@ -32,6 +32,7 @@ import java.util.Objects;
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.commons.fileupload.MultipartStream.IllegalBoundaryException;
 import org.apache.commons.fileupload.MultipartStream.ItemInputStream;
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
 import org.apache.commons.fileupload.servlet.ServletRequestContext;
@@ -395,6 +396,9 @@ public abstract class FileUploadBase {
                             currentFieldName = fieldName;
                             // Multiple files associated with this field name
                             final byte[] subBoundary = getBoundary(subContentType);
+                            if (subBoundary == null) {
+                                throw new IllegalBoundaryException("The request was rejected because no boundary token was defined for a multipart/mixed part");
+                            }
                             multi.setBoundary(subBoundary);
                             skipPreamble = true;
                             continue;
diff --git a/src/test/java/org/apache/commons/fileupload/FileUploadTest.java b/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
index 80d6d2a1..f1ec8d4e 100644
--- a/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
+++ b/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
@@ -29,6 +29,7 @@ import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
 
+import org.apache.commons.fileupload.MultipartStream.IllegalBoundaryException;
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 import org.apache.commons.fileupload.portlet.PortletFileUploadTest;
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
@@ -399,6 +400,38 @@ public class FileUploadTest {
         assertEquals("fieldValue2", field2.getString());
     }
 
+    /**
+     * Test upload that uses multipart/mixed without defining a boundary.
+     */
+    @Test
+    public void testMultipartMixedNoBoundary() {
+        final String contentType = "multipart/form-data; boundary=AaB03x";
+        final String request =
+            "--AaB03x\r\n" +
+            "content-disposition: form-data; name=\"field1\"\r\n" +
+            "\r\n" +
+            "Joe Blow\r\n" +
+            "--AaB03x\r\n" +
+            "content-disposition: form-data; name=\"pics\"\r\n" +
+            "Content-type: multipart/mixed\r\n" +
+            "\r\n" +
+            "--BbC04y\r\n" +
+            "Content-disposition: attachment; filename=\"file1.txt\"\r\n" +
+            "Content-Type: text/plain\r\n" +
+            "\r\n" +
+            "... contents of file1.txt ...\r\n" +
+            "--BbC04y\r\n" +
+            "Content-disposition: attachment; filename=\"file2.gif\"\r\n" +
+            "Content-type: image/gif\r\n" +
+            "Content-Transfer-Encoding: binary\r\n" +
+            "\r\n" +
+            "...contents of file2.gif...\r\n" +
+            "--BbC04y--\r\n" +
+            "--AaB03x--";
+        Exception e = assertThrows(FileUploadException.class, () -> Util.parseUpload(upload, request.getBytes("US-ASCII"), contentType));
+        assertEquals(IllegalBoundaryException.class, e.getCause().getClass());
+    }
+
     /**
      * Test for multipart/related without any content-disposition Header.
      * This kind of Content-Type is commonly used by SOAP-Requests with Attachments (MTOM)