|
24 | 24 | import java.util.Map; |
25 | 25 | import java.util.Objects; |
26 | 26 |
|
| 27 | +import org.apache.tomcat.util.http.fileupload.impl.FileCountLimitExceededException; |
27 | 28 | import org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl; |
28 | 29 | import org.apache.tomcat.util.http.fileupload.impl.FileUploadIOException; |
29 | 30 | import org.apache.tomcat.util.http.fileupload.impl.IOFileUploadException; |
@@ -103,6 +104,12 @@ public abstract class FileUploadBase { |
103 | 104 | */ |
104 | 105 | private long fileSizeMax = -1; |
105 | 106 |
|
| 107 | + /** |
| 108 | + * The maximum permitted number of files that may be uploaded in a single |
| 109 | + * request. A value of -1 indicates no maximum. |
| 110 | + */ |
| 111 | + private long fileCountMax = -1; |
| 112 | + |
106 | 113 | /** |
107 | 114 | * The content encoding to use when reading part headers. |
108 | 115 | */ |
@@ -179,6 +186,24 @@ public void setFileSizeMax(final long fileSizeMax) { |
179 | 186 | this.fileSizeMax = fileSizeMax; |
180 | 187 | } |
181 | 188 |
|
| 189 | + /** |
| 190 | + * Returns the maximum number of files allowed in a single request. |
| 191 | + * |
| 192 | + * @return The maximum number of files allowed in a single request. |
| 193 | + */ |
| 194 | + public long getFileCountMax() { |
| 195 | + return fileCountMax; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Sets the maximum number of files allowed per request/ |
| 200 | + * |
| 201 | + * @param fileCountMax The new limit. {@code -1} means no limit. |
| 202 | + */ |
| 203 | + public void setFileCountMax(long fileCountMax) { |
| 204 | + this.fileCountMax = fileCountMax; |
| 205 | + } |
| 206 | + |
182 | 207 | /** |
183 | 208 | * Retrieves the character encoding used when reading the headers of an |
184 | 209 | * individual part. When not specified, or {@code null}, the request |
@@ -253,6 +278,10 @@ public List<FileItem> parseRequest(final RequestContext ctx) |
253 | 278 | "No FileItemFactory has been set."); |
254 | 279 | final byte[] buffer = new byte[Streams.DEFAULT_BUFFER_SIZE]; |
255 | 280 | while (iter.hasNext()) { |
| 281 | + if (items.size() == fileCountMax) { |
| 282 | + // The next item will exceed the limit. |
| 283 | + throw new FileCountLimitExceededException(ATTACHMENT, getFileCountMax()); |
| 284 | + } |
256 | 285 | final FileItemStream item = iter.next(); |
257 | 286 | // Don't use getName() here to prevent an InvalidFileNameException. |
258 | 287 | final String fileName = item.getName(); |
|
0 commit comments