Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OBPIH-6083 Add custom MultipartResolver #4472

Conversation

drodzewicz
Copy link
Collaborator

No description provided.

@drodzewicz drodzewicz added status: work in progress For issues or pull requests that are in progress but not yet completed warn: do not merge Marks a pull request that is not yet ready to be merged labels Jan 23, 2024
@drodzewicz drodzewicz self-assigned this Jan 23, 2024
@drodzewicz drodzewicz force-pushed the OBPIH-6083-fix-null-pointer-exception-while-handling-upload-document-size-limit-exception branch from c3fbac5 to e84d4b3 Compare January 23, 2024 16:15
@drodzewicz drodzewicz removed the warn: do not merge Marks a pull request that is not yet ready to be merged label Jan 23, 2024
@drodzewicz drodzewicz marked this pull request as ready for review January 23, 2024 16:19
@alannadolny alannadolny self-requested a review January 23, 2024 16:27
@@ -177,7 +177,6 @@ grails:
controllers:
upload:
maxFileSize: 2097152
maxRequestSize: 2097152
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it is not a good decision looking from the security context. Without a maximum request size, someone could send excessively large requests, overwhelming the server. This could lead to performance degradation or temporal server unavailability.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for pointing that out, I had to check/test few things so...
The new CommonsMultipartResolver that I have implemented here receives a parameter maxUploadSize that checks the size of all of the uploaded files as a whole.

The standard multipart resolver (default one) receives parameters maxFileSize maxRequestSize and works a little differently.
the maxFileSize refers to the individual file size and maxRequestSize to the whole request size.

So in the end specifying maxFileSize: 2097152 and maxRequestSize: 2097152 is almost equivalent to maxUploadSize = 2097152

Note that maxRequestSize only sets this limit on multipart request and not all of the API calls, so if we want to secure our API a bit better we need to look for additional property that would handle this validation.

Copy link
Member

Choose a reason for hiding this comment

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

Also, there's probably a default value for maxRequestSize that is much smaller than what we've set it to.

https://guides.grails.org/grails3/grails-upload-file/guide/index.html#:~:text=Grails%203's%20default%20file%20size,to%20allow%2025MB%20files%20uploads.

@drodzewicz drodzewicz added needs attention and removed status: work in progress For issues or pull requests that are in progress but not yet completed labels Jan 23, 2024
} catch (MaxUploadSizeExceededException e) {
Throwable exception = new MultipartMaxSizeException(e)
request.setAttribute("exception", exception)
throw(exception)
Copy link
Member

Choose a reason for hiding this comment

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

Like I said before, I would prefer to either rethrow MaxUploadSizeExceededException or follow the similar approach i.e. returning

Here's an example from a Grails 3 app

https://github.com/UNCake/UNCake/blob/037c4d7c638a61a99a47d0388caf266fe3582faa/UNCake/grails-app/utils/uncake/CustomMultipartResolver.java#L4

If we cannot get it working this way then I'd suggest we go with the command object with contraints apporach for now.

return new DefaultMultipartHttpServletRequest(request, new LinkedMultiValueMap<>(), new LinkedHashMap<>(), new LinkedHashMap<>());

There are other Spring approaches (@ExceptionHandler, @ControllerAdvice) that we might be able to take advantage of but some of them are supported in Spring 5 (we're using Spring 4), so we'll need to wait a bit until we can use them.

import org.apache.commons.io.FileUtils
import org.springframework.web.multipart.MaxUploadSizeExceededException

class MultipartMaxSizeException extends RuntimeException {
Copy link
Member

Choose a reason for hiding this comment

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

Remove this class.

@drodzewicz drodzewicz added status: wont do Issues that we will not be addressing but can be revisited in the future and removed needs attention labels Feb 7, 2024
@awalkowiak
Copy link
Collaborator

As discussed on the tech huddle, iceboxing and closing

@awalkowiak awalkowiak closed this Feb 26, 2024
@awalkowiak awalkowiak deleted the OBPIH-6083-fix-null-pointer-exception-while-handling-upload-document-size-limit-exception branch February 26, 2024 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: wont do Issues that we will not be addressing but can be revisited in the future
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants