Skip to content

Commit

Permalink
Add a new limit for the number of files uploaded per request (#185)
Browse files Browse the repository at this point in the history
Back port
  • Loading branch information
markt-asf committed Jan 3, 2023
1 parent 14ddd9c commit e20c049
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<release version="1.5" date="TBD" description="1.5 Release">
<action issue="FILEUPLOAD-293" dev="jochen" type="fix">DiskFileItem.write(File) had been changed to use FileUtils.moveFile internally, preventing an existing file as the target.</action>
<action dev="markt" type="add">Add a configurable limit (disabled by default) for the number of files to upload per request.</action>
</release>
<release version="1.4" date="2018-12-23" description="1.4 Release">
<action issue="FILEUPLOAD-292" dev="chtompki" type="update">Don't create un-needed resources in FileUploadBase.java</action>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.fileupload;

/**
* This exception is thrown if a request contains more files than the specified
* limit.
*/
public class FileCountLimitExceededException extends FileUploadException {

private static final long serialVersionUID = 6904179610227521789L;

/**
* The limit that was exceeded.
*/
private final long limit;

/**
* Creates a new instance.
*
* @param message The detail message
* @param limit The limit that was exceeded
*/
public FileCountLimitExceededException(final String message, final long limit) {
super(message);
this.limit = limit;
}

/**
* Retrieves the limit that was exceeded.
*
* @return The limit that was exceeded by the request
*/
public long getLimit() {
return limit;
}
}
31 changes: 30 additions & 1 deletion src/main/java/org/apache/commons/fileupload/FileUploadBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ public static boolean isMultipartContent(HttpServletRequest req) {
*/
private long fileSizeMax = -1;

/**
* The maximum permitted number of files that may be uploaded in a single
* request. A value of -1 indicates no maximum.
*/
private long fileCountMax = -1;

/**
* The content encoding to use when reading part headers.
*/
Expand Down Expand Up @@ -241,6 +247,25 @@ public void setFileSizeMax(long fileSizeMax) {
this.fileSizeMax = fileSizeMax;
}

/**
* Returns the maximum number of files allowed in a single request.
*
* @return The maximum number of files allowed in a single request.
*/
public long getFileCountMax() {
return fileCountMax;
}

/**
* Sets the maximum number of files allowed per request.
*
* @param fileCountMax The new limit. {@code -1} means no limit.
*/
public void setFileCountMax(final long fileCountMax) {
this.fileCountMax = fileCountMax;
}


/**
* Retrieves the character encoding used when reading the headers of an
* individual part. When not specified, or <code>null</code>, the request
Expand Down Expand Up @@ -337,7 +362,11 @@ public List<FileItem> parseRequest(RequestContext ctx)
throw new NullPointerException("No FileItemFactory has been set.");
}
while (iter.hasNext()) {
final FileItemStream item = iter.next();
if (items.size() == fileCountMax) {
// The next item will exceed the limit.
throw new FileCountLimitExceededException(ATTACHMENT, getFileCountMax());
}
final FileItemStream item = iter.next();
// Don't use getName() here to prevent an InvalidFileNameException.
final String fileName = ((FileItemIteratorImpl.FileItemStreamImpl) item).name;
FileItem fileItem = fac.createItem(item.getFieldName(), item.getContentType(),
Expand Down

10 comments on commit e20c049

@solomax
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markt-asf Can you please cherry-pick this one into master? :))

@fginer
Copy link

@fginer fginer commented on e20c049 May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That fix has a bug. It's not checking if items are simple fields or files, so if you send more than 10 fields in the form throws the FileCountLimitExceededException.

@markt-asf
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name might not be the best given the circumstances but the behaviour is as intended.

@fginer
Copy link

@fginer fginer commented on e20c049 May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that if you submit a form (with the default specifications) with more than 9 fields, whether or not they are files, the exception is thrown.

@markt-asf
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is that happening? The limit is disabled by default. Might something else be going on?

@fginer
Copy link

@fginer fginer commented on e20c049 May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... it's not file, but field?

@markt-asf
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It limits fields regardless of whether it represents an uploaded file or not.

@fginer
Copy link

@fginer fginer commented on e20c049 May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for the explanation

@rkovarik
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 Looks like I've reported the same here https://issues.apache.org/jira/browse/FILEUPLOAD-351

@fginer
Copy link

@fginer fginer commented on e20c049 Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea @rkovarik , it's the same error.

Please sign in to comment.