Skip to content

Commit cf77cc5

Browse files
committed
Update packaged renamed fork of Commons File Upload
1 parent 0533c84 commit cf77cc5

File tree

8 files changed

+120
-14
lines changed

8 files changed

+120
-14
lines changed

MERGE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ FileUpload
5151
Sub-tree:
5252
src/main/java/org/apache/commons/fileupload2
5353
The SHA1 ID / tag for the most recent commit to be merged to Tomcat is:
54-
aa8eff6f04c939fd99834360415b1ddb2f637cb1 (2022-11-29)
54+
34eb241c051b02eca3b0b1b04f67b3b4e6c3a24d (2023-01-03)
5555

5656
Note: Tomcat's copy of fileupload also includes classes copied manually from
5757
Commons IO.

java/org/apache/catalina/connector/Request.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2872,8 +2872,9 @@ private void parseParts(boolean explicit) {
28722872
}
28732873
}
28742874

2875+
int maxParameterCount = getConnector().getMaxParameterCount();
28752876
Parameters parameters = coyoteRequest.getParameters();
2876-
parameters.setLimit(getConnector().getMaxParameterCount());
2877+
parameters.setLimit(maxParameterCount);
28772878

28782879
boolean success = false;
28792880
try {
@@ -2925,6 +2926,13 @@ private void parseParts(boolean explicit) {
29252926
upload.setFileItemFactory(factory);
29262927
upload.setFileSizeMax(mce.getMaxFileSize());
29272928
upload.setSizeMax(mce.getMaxRequestSize());
2929+
if (maxParameterCount > -1) {
2930+
// There is a limit. The limit for parts needs to be reduced by
2931+
// the number of parameters we have already parsed.
2932+
// Must be under the limit else parsing parameters would have
2933+
// triggered an exception.
2934+
upload.setFileCountMax(maxParameterCount - parameters.size());
2935+
}
29282936

29292937
parts = new ArrayList<>();
29302938
try {

java/org/apache/tomcat/util/http/Parameters.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public void setParseFailedReason(FailReason failReason) {
125125
}
126126

127127

128+
public int size() {
129+
return parameterCount;
130+
}
131+
132+
128133
public void recycle() {
129134
parameterCount = 0;
130135
paramHashValues.clear();

java/org/apache/tomcat/util/http/fileupload/FileUploadBase.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626
import java.util.Objects;
2727

28+
import org.apache.tomcat.util.http.fileupload.impl.FileCountLimitExceededException;
2829
import org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl;
2930
import org.apache.tomcat.util.http.fileupload.impl.FileUploadIOException;
3031
import org.apache.tomcat.util.http.fileupload.impl.IOFileUploadException;
@@ -128,6 +129,12 @@ public static final boolean isMultipartContent(final RequestContext ctx) {
128129
*/
129130
private long fileSizeMax = -1;
130131

132+
/**
133+
* The maximum permitted number of files that may be uploaded in a single
134+
* request. A value of -1 indicates no maximum.
135+
*/
136+
private long fileCountMax = -1;
137+
131138
/**
132139
* The content encoding to use when reading part headers.
133140
*/
@@ -204,6 +211,24 @@ public void setFileSizeMax(final long fileSizeMax) {
204211
this.fileSizeMax = fileSizeMax;
205212
}
206213

214+
/**
215+
* Returns the maximum number of files allowed in a single request.
216+
*
217+
* @return The maximum number of files allowed in a single request.
218+
*/
219+
public long getFileCountMax() {
220+
return fileCountMax;
221+
}
222+
223+
/**
224+
* Sets the maximum number of files allowed per request/
225+
*
226+
* @param fileCountMax The new limit. {@code -1} means no limit.
227+
*/
228+
public void setFileCountMax(long fileCountMax) {
229+
this.fileCountMax = fileCountMax;
230+
}
231+
207232
/**
208233
* Retrieves the character encoding used when reading the headers of an
209234
* individual part. When not specified, or {@code null}, the request
@@ -278,6 +303,10 @@ public List<FileItem> parseRequest(final RequestContext ctx)
278303
"No FileItemFactory has been set.");
279304
final byte[] buffer = new byte[Streams.DEFAULT_BUFFER_SIZE];
280305
while (iter.hasNext()) {
306+
if (items.size() == fileCountMax) {
307+
// The next item will exceed the limit.
308+
throw new FileCountLimitExceededException(ATTACHMENT, getFileCountMax());
309+
}
281310
final FileItemStream item = iter.next();
282311
// Don't use getName() here to prevent an InvalidFileNameException.
283312
final String fileName = item.getName();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.tomcat.util.http.fileupload.impl;
18+
19+
import org.apache.tomcat.util.http.fileupload.FileUploadException;
20+
21+
/**
22+
* This exception is thrown if a request contains more files than the specified
23+
* limit.
24+
*/
25+
public class FileCountLimitExceededException extends FileUploadException {
26+
27+
private static final long serialVersionUID = 2408766352570556046L;
28+
29+
private final long limit;
30+
31+
/**
32+
* Creates a new instance.
33+
*
34+
* @param message The detail message
35+
* @param limit The limit that was exceeded
36+
*/
37+
public FileCountLimitExceededException(final String message, final long limit) {
38+
super(message);
39+
this.limit = limit;
40+
}
41+
42+
/**
43+
* Retrieves the limit that was exceeded.
44+
*
45+
* @return The limit that was exceeded by the request
46+
*/
47+
public long getLimit() {
48+
return limit;
49+
}
50+
}

webapps/docs/changelog.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@
138138
</fix>
139139
</changelog>
140140
</subsection>
141+
<subsection name="Other">
142+
<changelog>
143+
<update>
144+
Update the internal fork of Apache Commons FileUpload to 34eb241
145+
(2023-01-03, 2.0-SNAPSHOT). (markt)
146+
</update>
147+
</changelog>
148+
</subsection>
141149
</section>
142150
<section name="Tomcat 9.0.70 (remm)" rtext="2022-12-05">
143151
<subsection name="Catalina">

webapps/docs/config/ajp.xml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,15 @@
136136
</attribute>
137137

138138
<attribute name="maxParameterCount" required="false">
139-
<p>The maximum number of parameter and value pairs (GET plus POST) which
140-
will be automatically parsed by the container. Parameter and value pairs
141-
beyond this limit will be ignored. A value of less than 0 means no limit.
142-
If not specified, a default of 10000 is used. Note that
143-
<code>FailedRequestFilter</code> <a href="filter.html">filter</a> can be
144-
used to reject requests that hit the limit.</p>
139+
<p>The maximum total number of request parameters (including uploaded
140+
files) obtained from the query string and, for POST requests, the request
141+
body if the content type is
142+
<code>application/x-www-form-urlencoded</code> or
143+
<code>multipart/form-data</code>. Request parameters beyond this limit
144+
will be ignored. A value of less than 0 means no limit. If not specified,
145+
a default of 10000 is used. Note that <code>FailedRequestFilter</code>
146+
<a href="filter.html">filter</a> can be used to reject requests that
147+
exceed the limit.</p>
145148
</attribute>
146149

147150
<attribute name="maxPostSize" required="false">

webapps/docs/config/http.xml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,15 @@
134134
</attribute>
135135

136136
<attribute name="maxParameterCount" required="false">
137-
<p>The maximum number of parameter and value pairs (GET plus POST) which
138-
will be automatically parsed by the container. Parameter and value pairs
139-
beyond this limit will be ignored. A value of less than 0 means no limit.
140-
If not specified, a default of 10000 is used. Note that
141-
<code>FailedRequestFilter</code> <a href="filter.html">filter</a> can be
142-
used to reject requests that hit the limit.</p>
137+
<p>The maximum total number of request parameters (including uploaded
138+
files) obtained from the query string and, for POST requests, the request
139+
body if the content type is
140+
<code>application/x-www-form-urlencoded</code> or
141+
<code>multipart/form-data</code>. Request parameters beyond this limit
142+
will be ignored. A value of less than 0 means no limit. If not specified,
143+
a default of 10000 is used. Note that <code>FailedRequestFilter</code>
144+
<a href="filter.html">filter</a> can be used to reject requests that
145+
exceed the limit.</p>
143146
</attribute>
144147

145148
<attribute name="maxPostSize" required="false">

0 commit comments

Comments
 (0)