Skip to content

Commit 045c5dc

Browse files
committed
Detect Jetty 12 "max length exceeded" message in handleParseFailure
Closes gh-31850
1 parent 24f8eac commit 045c5dc

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,13 +112,21 @@ private void parseRequest(HttpServletRequest request) {
112112
}
113113

114114
protected void handleParseFailure(Throwable ex) {
115-
String msg = ex.getMessage();
116-
if (msg != null) {
117-
msg = msg.toLowerCase();
118-
if (msg.contains("size") && msg.contains("exceed")) {
119-
throw new MaxUploadSizeExceededException(-1, ex);
115+
// MaxUploadSizeExceededException ?
116+
Throwable cause = ex;
117+
do {
118+
String msg = cause.getMessage();
119+
if (msg != null) {
120+
msg = msg.toLowerCase();
121+
if (msg.contains("exceed") && (msg.contains("size") || msg.contains("length"))) {
122+
throw new MaxUploadSizeExceededException(-1, ex);
123+
}
120124
}
125+
cause = cause.getCause();
121126
}
127+
while (cause != null);
128+
129+
// General MultipartException
122130
throw new MultipartException("Failed to parse multipart servlet request", ex);
123131
}
124132

spring-web/src/test/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequestTests.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,29 @@
1818

1919
import java.io.IOException;
2020
import java.nio.charset.StandardCharsets;
21+
import java.util.Collection;
2122

23+
import jakarta.servlet.ServletException;
24+
import jakarta.servlet.http.Part;
2225
import org.junit.jupiter.api.Test;
2326

2427
import org.springframework.http.converter.FormHttpMessageConverter;
2528
import org.springframework.util.LinkedMultiValueMap;
2629
import org.springframework.util.MultiValueMap;
30+
import org.springframework.web.multipart.MaxUploadSizeExceededException;
2731
import org.springframework.web.multipart.MultipartFile;
2832
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
2933
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
3034
import org.springframework.web.testfixture.servlet.MockPart;
3135

3236
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3338

3439
/**
3540
* Unit tests for {@link StandardMultipartHttpServletRequest}.
3641
*
3742
* @author Rossen Stoyanchev
43+
* @author Juergen Hoeller
3844
*/
3945
class StandardMultipartHttpServletRequestTests {
4046

@@ -92,6 +98,31 @@ void multipartFileResource() throws IOException {
9298
""".replace("\n", "\r\n"));
9399
}
94100

101+
@Test
102+
void plainSizeExceededServletException() {
103+
ServletException ex = new ServletException("Request size exceeded");
104+
105+
assertThatExceptionOfType(MaxUploadSizeExceededException.class)
106+
.isThrownBy(() -> requestWithException(ex)).withCause(ex);
107+
}
108+
109+
@Test // gh-28759
110+
void jetty94MaxRequestSizeException() {
111+
ServletException ex = new ServletException(new IllegalStateException("Request exceeds maxRequestSize"));
112+
113+
assertThatExceptionOfType(MaxUploadSizeExceededException.class)
114+
.isThrownBy(() -> requestWithException(ex)).withCause(ex);
115+
}
116+
117+
@Test // gh-31850
118+
void jetty12MaxLengthExceededException() {
119+
ServletException ex = new ServletException(new RuntimeException("400: bad multipart",
120+
new IllegalStateException("max length exceeded")));
121+
122+
assertThatExceptionOfType(MaxUploadSizeExceededException.class)
123+
.isThrownBy(() -> requestWithException(ex)).withCause(ex);
124+
}
125+
95126

96127
private static StandardMultipartHttpServletRequest requestWithPart(String name, String disposition, String content) {
97128
MockHttpServletRequest request = new MockHttpServletRequest();
@@ -101,4 +132,14 @@ private static StandardMultipartHttpServletRequest requestWithPart(String name,
101132
return new StandardMultipartHttpServletRequest(request);
102133
}
103134

135+
private static StandardMultipartHttpServletRequest requestWithException(ServletException ex) {
136+
MockHttpServletRequest request = new MockHttpServletRequest() {
137+
@Override
138+
public Collection<Part> getParts() throws ServletException {
139+
throw ex;
140+
}
141+
};
142+
return new StandardMultipartHttpServletRequest(request);
143+
}
144+
104145
}

0 commit comments

Comments
 (0)