Skip to content

Commit 48c8cf0

Browse files
committed
Merge pull request #43286 from quaff
* pr/43286: Add support for configuring Tomcat connector's max parameter count Closes gh-43286
2 parents 2505038 + 0d3e024 commit 48c8cf0

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

+21-7
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,11 @@ public static class Tomcat {
513513
*/
514514
private DataSize maxHttpResponseHeaderSize = DataSize.ofKilobytes(8);
515515

516-
public DataSize getMaxHttpFormPostSize() {
517-
return this.maxHttpFormPostSize;
518-
}
519-
520-
public void setMaxHttpFormPostSize(DataSize maxHttpFormPostSize) {
521-
this.maxHttpFormPostSize = maxHttpFormPostSize;
522-
}
516+
/**
517+
* Maximum number of parameters (GET plus POST) that will be automatically parsed
518+
* by the container. A value of less than 0 means no limit.
519+
*/
520+
private int maxParameterCount = 10000;
523521

524522
public Accesslog getAccesslog() {
525523
return this.accesslog;
@@ -669,6 +667,22 @@ public void setMaxHttpResponseHeaderSize(DataSize maxHttpResponseHeaderSize) {
669667
this.maxHttpResponseHeaderSize = maxHttpResponseHeaderSize;
670668
}
671669

670+
public DataSize getMaxHttpFormPostSize() {
671+
return this.maxHttpFormPostSize;
672+
}
673+
674+
public void setMaxHttpFormPostSize(DataSize maxHttpFormPostSize) {
675+
this.maxHttpFormPostSize = maxHttpFormPostSize;
676+
}
677+
678+
public int getMaxParameterCount() {
679+
return this.maxParameterCount;
680+
}
681+
682+
public void setMaxParameterCount(int maxParameterCount) {
683+
this.maxParameterCount = maxParameterCount;
684+
}
685+
672686
/**
673687
* Tomcat access log properties.
674688
*/

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

+6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
119119
.asInt(DataSize::toBytes)
120120
.when((maxHttpFormPostSize) -> maxHttpFormPostSize != 0)
121121
.to((maxHttpFormPostSize) -> customizeMaxHttpFormPostSize(factory, maxHttpFormPostSize));
122+
map.from(properties::getMaxParameterCount)
123+
.to((maxParameterCount) -> customizeMaxParameterCount(factory, maxParameterCount));
122124
map.from(properties::getAccesslog)
123125
.when(ServerProperties.Tomcat.Accesslog::isEnabled)
124126
.to((enabled) -> customizeAccessLog(factory));
@@ -292,6 +294,10 @@ private void customizeMaxHttpFormPostSize(ConfigurableTomcatWebServerFactory fac
292294
factory.addConnectorCustomizers((connector) -> connector.setMaxPostSize(maxHttpFormPostSize));
293295
}
294296

297+
private void customizeMaxParameterCount(ConfigurableTomcatWebServerFactory factory, int maxParameterCount) {
298+
factory.addConnectorCustomizers((connector) -> connector.setMaxParameterCount(maxParameterCount));
299+
}
300+
295301
private void customizeAccessLog(ConfigurableTomcatWebServerFactory factory) {
296302
ServerProperties.Tomcat tomcatProperties = this.serverProperties.getTomcat();
297303
AccessLogValve valve = new AccessLogValve();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ void testCustomizedMimeMapping() {
199199
}
200200

201201
@Test
202-
void testCustomizeUriEncoding() {
202+
void testCustomizeTomcatUriEncoding() {
203203
bind("server.tomcat.uri-encoding", "US-ASCII");
204204
assertThat(this.properties.getTomcat().getUriEncoding()).isEqualTo(StandardCharsets.US_ASCII);
205205
}
@@ -235,17 +235,23 @@ void testCustomizeTomcatKeepAliveTimeoutWithInfinite() {
235235
}
236236

237237
@Test
238-
void customizeMaxKeepAliveRequests() {
238+
void testCustomizeTomcatMaxKeepAliveRequests() {
239239
bind("server.tomcat.max-keep-alive-requests", "200");
240240
assertThat(this.properties.getTomcat().getMaxKeepAliveRequests()).isEqualTo(200);
241241
}
242242

243243
@Test
244-
void customizeMaxKeepAliveRequestsWithInfinite() {
244+
void testCustomizeTomcatMaxKeepAliveRequestsWithInfinite() {
245245
bind("server.tomcat.max-keep-alive-requests", "-1");
246246
assertThat(this.properties.getTomcat().getMaxKeepAliveRequests()).isEqualTo(-1);
247247
}
248248

249+
@Test
250+
void testCustomizeTomcatMaxParameterCount() {
251+
bind("server.tomcat.max-parameter-count", "100");
252+
assertThat(this.properties.getTomcat().getMaxParameterCount()).isEqualTo(100);
253+
}
254+
249255
@Test
250256
void testCustomizeTomcatMinSpareThreads() {
251257
bind("server.tomcat.threads.min-spare", "10");
@@ -379,6 +385,12 @@ void tomcatMaxHttpPostSizeMatchesConnectorDefault() {
379385
.isEqualTo(getDefaultConnector().getMaxPostSize());
380386
}
381387

388+
@Test
389+
void tomcatMaxParameterCountMatchesConnectorDefault() {
390+
assertThat(this.properties.getTomcat().getMaxParameterCount())
391+
.isEqualTo(getDefaultConnector().getMaxParameterCount());
392+
}
393+
382394
@Test
383395
void tomcatBackgroundProcessorDelayMatchesEngineDefault() {
384396
assertThat(this.properties.getTomcat().getBackgroundProcessorDelay())

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ void customMaxHttpRequestHeaderSize() {
194194
.isEqualTo(DataSize.ofMegabytes(10).toBytes()));
195195
}
196196

197+
@Test
198+
void customMaxParameterCount() {
199+
bind("server.tomcat.max-parameter-count=100");
200+
customizeAndRunServer(
201+
(server) -> assertThat(server.getTomcat().getConnector().getMaxParameterCount()).isEqualTo(100));
202+
}
203+
197204
@Test
198205
void customMaxRequestHttpHeaderSizeIgnoredIfNegative() {
199206
bind("server.max-http-request-header-size=-1");

0 commit comments

Comments
 (0)