This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 11.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/11.0.x by this push: new 74f69ffaf6 More updates towards a Commons FileUpload 1.6.0 RC/release 74f69ffaf6 is described below commit 74f69ffaf61e54c727603e7e831fe20f0ac5d2a7 Author: Mark Thomas <ma...@apache.org> AuthorDate: Thu Jun 5 14:23:16 2025 +0100 More updates towards a Commons FileUpload 1.6.0 RC/release --- MERGE.txt | 2 +- .../util/http/fileupload/FileUploadBase.java | 38 ++++++++++++++++++-- .../util/http/fileupload/MultipartStream.java | 41 ++++++++++++++++++---- .../http/fileupload/impl/FileItemIteratorImpl.java | 1 + webapps/docs/changelog.xml | 4 +-- 5 files changed, 75 insertions(+), 11 deletions(-) diff --git a/MERGE.txt b/MERGE.txt index 89ba88208a..4e23e5b2cc 100644 --- a/MERGE.txt +++ b/MERGE.txt @@ -46,7 +46,7 @@ Branch: 1.x Sub-tree: src/main/java/org/apache/commons/fileupload The SHA1 ID / tag for the most recent commit to be merged to Tomcat is: -abe5d94b7ef6ff7164b48b5bb3781979ebbafb0e (2025-06-03) +f1028401e3d59bd42aee9ab4d26995991db1aadc (2025-06-05) Note: Tomcat's copy of fileupload also includes classes copied manually from Commons IO. diff --git a/java/org/apache/tomcat/util/http/fileupload/FileUploadBase.java b/java/org/apache/tomcat/util/http/fileupload/FileUploadBase.java index 73e5f1e63e..f1f7c93524 100644 --- a/java/org/apache/tomcat/util/http/fileupload/FileUploadBase.java +++ b/java/org/apache/tomcat/util/http/fileupload/FileUploadBase.java @@ -99,6 +99,13 @@ public abstract class FileUploadBase { */ public static final String MULTIPART_MIXED = "multipart/mixed"; + /** + * Default per part header size limit in bytes. + * + * @since FileUpload 1.6.0 + */ + public static final int DEFAULT_PART_HEADER_SIZE_MAX = 512; + /** * The maximum size permitted for the complete request, as opposed to * {@link #fileSizeMax}. A value of -1 indicates no maximum. @@ -117,6 +124,11 @@ public abstract class FileUploadBase { */ private long fileCountMax = -1; + /** + * The maximum permitted size of the headers provided with a single part in bytes. + */ + private int partHeaderSizeMax = DEFAULT_PART_HEADER_SIZE_MAX; + /** * The content encoding to use when reading part headers. */ @@ -340,6 +352,17 @@ public abstract class FileUploadBase { return headers; } + /** + * Obtain the per part size limit for headers. + * + * @return The maximum size of the headers for a single part in bytes. + * + * @since FileUpload 1.6.0 + */ + public int getPartHeaderSizeMax() { + return partHeaderSizeMax; + } + /** * Returns the progress listener. * @@ -427,8 +450,8 @@ public abstract class FileUploadBase { boolean successful = false; try { final FileItemIterator iter = getItemIterator(ctx); - final FileItemFactory fileItemFactory = Objects.requireNonNull(getFileItemFactory(), - "No FileItemFactory has been set."); + final FileItemFactory fileItemFactory = getFileItemFactory(); + Objects.requireNonNull(fileItemFactory, "getFileItemFactory()"); final byte[] buffer = new byte[Streams.DEFAULT_BUFFER_SIZE]; while (iter.hasNext()) { if (items.size() == fileCountMax) { @@ -510,6 +533,17 @@ public abstract class FileUploadBase { headerEncoding = encoding; } + /** + * Sets the per part size limit for headers. + * + * @param partHeaderSizeMax The maximum size of the headers in bytes. + * + * @since FileUpload 1.6.0 + */ + public void setPartHeaderSizeMax(final int partHeaderSizeMax) { + this.partHeaderSizeMax = partHeaderSizeMax; + } + /** * Sets the progress listener. * diff --git a/java/org/apache/tomcat/util/http/fileupload/MultipartStream.java b/java/org/apache/tomcat/util/http/fileupload/MultipartStream.java index 97c412da66..9f4f69005e 100644 --- a/java/org/apache/tomcat/util/http/fileupload/MultipartStream.java +++ b/java/org/apache/tomcat/util/http/fileupload/MultipartStream.java @@ -23,6 +23,7 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; import org.apache.tomcat.util.http.fileupload.impl.FileUploadIOException; +import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException; import org.apache.tomcat.util.http.fileupload.util.Closeable; import org.apache.tomcat.util.http.fileupload.util.Streams; @@ -481,7 +482,10 @@ public class MultipartStream { /** * The maximum length of {@code header-part} that will be * processed (10 kilobytes = 10240 bytes.). + * + * @deprecated Unused. Replaced by {@link #getPartHeaderSizeMax()}. */ + @Deprecated public static final int HEADER_PART_SIZE_MAX = 10240; /** @@ -594,6 +598,11 @@ public class MultipartStream { */ private final ProgressNotifier notifier; + /** + * The maximum permitted size of the headers provided with a single part in bytes. + */ + private int partHeaderSizeMax = FileUploadBase.DEFAULT_PART_HEADER_SIZE_MAX; + /** * Constructs a {@code MultipartStream} with a custom size buffer. * <p> @@ -725,6 +734,17 @@ public class MultipartStream { return headerEncoding; } + /** + * Obtain the per part size limit for headers. + * + * @return The maximum size of the headers for a single part in bytes. + * + * @since 1.6.0 + */ + public int getPartHeaderSizeMax() { + return partHeaderSizeMax; + } + /** * Creates a new {@link ItemInputStream}. * @return A new instance of {@link ItemInputStream}. @@ -830,8 +850,6 @@ public class MultipartStream { * <p> * Headers are returned verbatim to the input stream, including the trailing {@code CRLF} marker. Parsing is left to * the application. - * <p> - * <strong>TODO</strong> allow limiting maximum header size to protect against abuse. * * @return The {@code header-part} of the current encapsulation. * @@ -854,10 +872,10 @@ public class MultipartStream { throw new MalformedStreamException("Stream ended unexpectedly"); } size++; - if (size > HEADER_PART_SIZE_MAX) { - throw new MalformedStreamException(String.format( - "Header section has more than %s bytes (maybe it is not properly terminated)", - Integer.valueOf(HEADER_PART_SIZE_MAX))); + if (getPartHeaderSizeMax() != -1 && size > getPartHeaderSizeMax()) { + throw new FileUploadIOException(new SizeLimitExceededException( + String.format("Header section has more than %s bytes (maybe it is not properly terminated)", Integer.valueOf(getPartHeaderSizeMax())), + size, getPartHeaderSizeMax())); } if (b == HEADER_SEPARATOR[i]) { i++; @@ -915,6 +933,17 @@ public class MultipartStream { headerEncoding = encoding; } + /** + * Sets the per part size limit for headers. + * + * @param partHeaderSizeMax The maximum size of the headers in bytes. + * + * @since 1.6.0 + */ + public void setPartHeaderSizeMax(final int partHeaderSizeMax) { + this.partHeaderSizeMax = partHeaderSizeMax; + } + /** * Finds the beginning of the first {@code encapsulation}. * diff --git a/java/org/apache/tomcat/util/http/fileupload/impl/FileItemIteratorImpl.java b/java/org/apache/tomcat/util/http/fileupload/impl/FileItemIteratorImpl.java index 29e89f622f..7c8d07596d 100644 --- a/java/org/apache/tomcat/util/http/fileupload/impl/FileItemIteratorImpl.java +++ b/java/org/apache/tomcat/util/http/fileupload/impl/FileItemIteratorImpl.java @@ -198,6 +198,7 @@ public class FileItemIteratorImpl implements FileItemIterator { String.format("The boundary specified in the %s header is too long", FileUploadBase.CONTENT_TYPE), iae); } multiPartStream.setHeaderEncoding(charEncoding); + multiPartStream.setPartHeaderSizeMax(fileUploadBase.getPartHeaderSizeMax()); } public MultipartStream getMultiPartStream() throws FileUploadException, IOException { diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 32b15d48c4..4579221830 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -178,8 +178,8 @@ Update Tomcat Native to 2.0.9. (markt) </update> <update> - Update the internal fork of Apache Commons FileUpload to abe5d94 - (2023-06-03, 1.x-SNAPSHOT). (markt) + Update the internal fork of Apache Commons FileUpload to f102840 + (2023-06-05, 1.x-SNAPSHOT). (markt) </update> <update> Update EasyMock to 5.6.0. (markt) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org