Skip to content

WW-5873 Enforce struts.multipart.maxSizeOfFiles in both multipart parsers - #2160

Open
shadowe1ite wants to merge 1 commit into
apache:mainfrom
shadowe1ite:fix/multipart-max-size-of-files
Open

shadowe1ite wants to merge 1 commit into
apache:mainfrom
shadowe1ite:fix/multipart-max-size-of-files

Conversation

@shadowe1ite

Copy link
Copy Markdown

Fixes WW-5873

struts.multipart.maxSizeOfFiles wasn't really enforced. JakartaMultiPartRequest never checked it, and JakartaStreamMultiPartRequest only checked it after the whole file was already on disk.

The check now lives in AbstractMultiPartRequest and both parsers use it. The streaming parser stops writing as soon as the remaining budget is used up and deletes the partial file. The old two-arg streamFileToDisk is still there for subclasses.

One small behavior change: a total exactly equal to the limit is now accepted. The streaming parser used to reject it because it compared with >=.

The default parser still lets commons-fileupload parse the whole request first, so what it buffers is still bounded by struts.multipart.maxSize, same as before.

Tests for both parsers (single file over the limit, a second file crossing it, files exactly at the limit) plus one for the streaming parser checking that a 1 MB upload with a 10 byte limit writes no more than 10 bytes. 4 of them fail without the fix; all multipart and upload tests in core pass with it.

@lukaszlenart lukaszlenart left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this, moving the check into AbstractMultiPartRequest and bounding the streamed write is the right shape. A few things before it can go in:

1. A negative maxSizeOfFiles should mean "no limit"

exceedsMaxSizeOfFiles checks fileSize <= maxSizeOfFiles - currentFilesSize, so with struts.multipart.maxSizeOfFiles=-1 every non-empty file is now rejected by JakartaMultiPartRequest, which used to ignore the setting entirely. The other multipart limits already treat a negative value as unlimited (maxFiles and maxParameterCount in AbstractMultiPartRequest), and the upload docs say the max options can be set to unlimited with -1. Please apply the same convention in both parsers (in the stream parser, take the unbounded path) and add a test with -1.

2. Route every write through the three-arg streamFileToDisk and deprecate the two-arg one

When maxSizeOfFiles is set, processFileItemAsFileField calls the three-arg overload directly, so a subclass that overrides streamFileToDisk(FileItemInput, File) silently loses its hook exactly when the limit is configured. Please always call streamFileToDisk(fileItemInput, file, maxBytes) (passing Long.MAX_VALUE when there is no limit), and mark the two-arg overload @Deprecated(since = "7.6.0", forRemoval = true) with a Javadoc pointer to the new one. Its removal will be tracked separately for 8.0.0.

3. Commit headline

The commit is headlined Enforce struts.multipart.maxSizeOfFiles in both multipart parsers; please prefix it with WW-5873, e.g. WW-5873 fix(core): enforce struts.multipart.maxSizeOfFiles in both multipart parsers.

4. Javadoc on addMaxSizeOfFilesError

It's a new protected method and its neighbours carry a Javadoc; a one-liner is enough.

The rest looks good: the early return leaves the remaining part to fileupload2, which closes the current item before moving on, actualSizeOfUploadedFiles keeps its signature after the move, and the tests run against both parsers through AbstractMultiPartRequestTest.

@shadowe1ite
shadowe1ite force-pushed the fix/multipart-max-size-of-files branch from 939169d to 614e9e7 Compare December 3, 2026 08:15
@shadowe1ite

Copy link
Copy Markdown
Author

Thanks for this, moving the check into AbstractMultiPartRequest and bounding the streamed write is the right shape. A few things before it can go in:

1. A negative maxSizeOfFiles should mean "no limit"

exceedsMaxSizeOfFiles checks fileSize <= maxSizeOfFiles - currentFilesSize, so with struts.multipart.maxSizeOfFiles=-1 every non-empty file is now rejected by JakartaMultiPartRequest, which used to ignore the setting entirely. The other multipart limits already treat a negative value as unlimited (maxFiles and maxParameterCount in AbstractMultiPartRequest), and the upload docs say the max options can be set to unlimited with -1. Please apply the same convention in both parsers (in the stream parser, take the unbounded path) and add a test with -1.

2. Route every write through the three-arg streamFileToDisk and deprecate the two-arg one

When maxSizeOfFiles is set, processFileItemAsFileField calls the three-arg overload directly, so a subclass that overrides streamFileToDisk(FileItemInput, File) silently loses its hook exactly when the limit is configured. Please always call streamFileToDisk(fileItemInput, file, maxBytes) (passing Long.MAX_VALUE when there is no limit), and mark the two-arg overload @Deprecated(since = "7.6.0", forRemoval = true) with a Javadoc pointer to the new one. Its removal will be tracked separately for 8.0.0.

3. Commit headline

The commit is headlined Enforce struts.multipart.maxSizeOfFiles in both multipart parsers; please prefix it with WW-5873, e.g. WW-5873 fix(core): enforce struts.multipart.maxSizeOfFiles in both multipart parsers.

4. Javadoc on addMaxSizeOfFilesError

It's a new protected method and its neighbours carry a Javadoc; a one-liner is enough.

The rest looks good: the early return leaves the remaining part to fileupload2, which closes the current item before moving on, actualSizeOfUploadedFiles keeps its signature after the move, and the tests run against both parsers through AbstractMultiPartRequestTest.

@lukaszlenart Thanks for the review! Fixed all four points and added a -1 test for both parsers. All 92 multipart tests pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants