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

Detect Jetty 12 "max length exceeded" message for MaxUploadSizeExceededException #31850

Closed
Aliaksie opened this issue Dec 15, 2023 · 9 comments
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: feedback-provided Feedback has been provided type: enhancement A general enhancement
Milestone

Comments

@Aliaksie
Copy link

Aliaksie commented Dec 15, 2023

Affects:

  • org.springframework.boot -> 3.2.0
  • org.springframework -> 6.1.1
  • spring-boot-starter-jetty -> 3.2.0
  • jetty -> 12.x

StandardMultipartHttpServletRequest contains the following code:

	protected void handleParseFailure(Throwable ex) {
		String msg = ex.getMessage();
		if (msg != null) {
			msg = msg.toLowerCase();
			if (msg.contains("size") && msg.contains("exceed")) {
				throw new MaxUploadSizeExceededException(-1, ex);
			}
		}
		throw new MultipartException("Failed to parse multipart servlet request", ex);
	}

That seems like it doesn't handle correctly the case for MaxUploadSizeExceededException for jetty server due to fact that original error from the Servlet API will be throw new ServletException(new BadMessageException("bad multipart", cause));, and the error from jetty-http for multipart will be throw new IllegalStateException("max length exceeded: %d".formatted(max));.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 15, 2023
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Dec 15, 2023
@sbrannen sbrannen changed the title Inconsistent handling MaxUploadSizeExceededException Inconsistent handling of MaxUploadSizeExceededException Dec 15, 2023
@sdeleuze sdeleuze self-assigned this Dec 15, 2023
@sdeleuze
Copy link
Contributor

Could you please provide a reproducer as an attached archive or a link to a repository?

@sdeleuze sdeleuze added the status: waiting-for-feedback We need additional information before we can continue label Dec 15, 2023
@Aliaksie
Copy link
Author

Aliaksie commented Dec 15, 2023

Could you please provide a reproducer as an attached archive or a link to a repository?

@sdeleuze sure please have a look there

there i've added code that you could use later for fix

@ExceptionHandler({MultipartException.class})
        public ResponseEntity<ErrorResponse> handleMultipartException(final HttpServletRequest request,
                                                                      final MultipartException e) {
            final Throwable rootCause = e.getRootCause();
            String msg = Optional.ofNullable(rootCause).map(Throwable::getMessage).orElse(null);
            if (msg == null) {
                return this.error(HttpStatus.BAD_REQUEST, request, e.getMessage());
            }

            msg = msg.toLowerCase();
            if (hasLengthError(msg) && msg.contains("exceed")) {
                return tooLarge(request, new MaxUploadSizeExceededException(-1, rootCause));
            }

            return this.error(HttpStatus.BAD_REQUEST, request, e.getMessage());
        }

        private static boolean hasLengthError(final String msg) {
            return msg.contains("size") || msg.contains("max length");
        }

Note: Be aware that test results may overlap with other client-related issues: "The connection was closed BEFORE response."

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Dec 15, 2023
@jhoeller
Copy link
Contributor

So if StandardMultipartHttpServletRequest.handleParseFailure checked for the keyword "length" in addition to "size", would that be sufficient? Or do we have an additional level of cause nesting to consider there as well?

@jhoeller jhoeller changed the title Inconsistent handling of MaxUploadSizeExceededException Detect Jetty 12 "max length exceeded" message for MaxUploadSizeExceededException Dec 18, 2023
@jhoeller jhoeller added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 18, 2023
@jhoeller jhoeller added this to the 6.1.3 milestone Dec 18, 2023
@Aliaksie
Copy link
Author

So if StandardMultipartHttpServletRequest.handleParseFailure checked for the keyword "length" in addition to "size", would that be sufficient? Or do we have an additional level of cause nesting to consider there as well?

in my understanding both should be taken into account, as I mentioned, in Jetty 12.x the nesting of the error and the error msg itself have changed... you can also see it from my screenshot below

image

@jhoeller
Copy link
Contributor

Indeed, thanks for sharing that screenshot which illustrates it nicely!

@jhoeller jhoeller self-assigned this Dec 18, 2023
@jhoeller
Copy link
Contributor

Related: #28759 for Jetty 9.4's exception message.

@jhoeller
Copy link
Contributor

Pushed a revision now for inclusion in the 6.1.3 release. It would be great if you could give the upcoming 6.1.x snapshot a try!

@Aliaksie
Copy link
Author

Aliaksie commented Dec 18, 2023

Pushed a revision now for inclusion in the 6.1.3 release. It would be great if you could give the upcoming 6.1.x snapshot a try!

yes it works with 6.1.3-SNAPSHOT ! demo using snapshot updated.

image

small note: maybe it would be nice if it were like throw new MaxUploadSizeExceededException(-1, cause); but the way it is now is also good.

thanks!

@jhoeller
Copy link
Contributor

Thanks for the immediate check!

I was wondering whether to throw the top-level exception or the relevant cause as well but ultimately settled with the top-level exception since that may contain further details, even if it was a specific cause that led to us to identify an upload size failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: feedback-provided Feedback has been provided type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants