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

Provide a configuration property for configuring Jetty's max form keys #42448

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,11 @@ public static class Jetty {
*/
private DataSize maxHttpFormPostSize = DataSize.ofBytes(200000);

/**
* Maximum number of form keys.
*/
private int maxFormKeys = 1000;

/**
* Time that the connection can be idle before it is closed.
*/
Expand Down Expand Up @@ -1180,6 +1185,14 @@ public void setMaxHttpFormPostSize(DataSize maxHttpFormPostSize) {
this.maxHttpFormPostSize = maxHttpFormPostSize;
}

public int getMaxFormKeys() {
return this.maxFormKeys;
}

public void setMaxFormKeys(int maxFormKeys) {
this.maxFormKeys = maxFormKeys;
}

public Duration getConnectionIdleTimeout() {
return this.connectionIdleTimeout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.server.AbstractConnector;
Expand Down Expand Up @@ -93,7 +94,11 @@ public void customize(ConfigurableJettyWebServerFactory factory) {
map.from(properties::getMaxHttpFormPostSize)
.asInt(DataSize::toBytes)
.when(this::isPositive)
.to((maxHttpFormPostSize) -> customizeMaxHttpFormPostSize(factory, maxHttpFormPostSize));
.to((maxHttpFormPostSize) -> customizeServletContextHandler(factory, contextHandler -> contextHandler.setMaxFormContentSize(maxHttpFormPostSize)));
map.from(properties::getMaxFormKeys)
.when(this::isPositive)
qingbozhang marked this conversation as resolved.
Show resolved Hide resolved
.to((maxFormKeys) -> customizeServletContextHandler(factory, contextHandler -> contextHandler.setMaxFormKeys(maxFormKeys)));

map.from(properties::getConnectionIdleTimeout).to((idleTimeout) -> customizeIdleTimeout(factory, idleTimeout));
map.from(properties::getAccesslog)
.when(ServerProperties.Jetty.Accesslog::isEnabled)
Expand Down Expand Up @@ -122,29 +127,29 @@ private void customizeIdleTimeout(ConfigurableJettyWebServerFactory factory, Dur
});
}

private void customizeMaxHttpFormPostSize(ConfigurableJettyWebServerFactory factory, int maxHttpFormPostSize) {
private void customizeServletContextHandler(ConfigurableJettyWebServerFactory factory, Consumer<ServletContextHandler> customFunc) {
qingbozhang marked this conversation as resolved.
Show resolved Hide resolved
factory.addServerCustomizers(new JettyServerCustomizer() {

@Override
public void customize(Server server) {
setHandlerMaxHttpFormPostSize(server.getHandlers());
acceptCustomizeServletContextHandler(server.getHandlers());
}

private void setHandlerMaxHttpFormPostSize(List<Handler> handlers) {
private void acceptCustomizeServletContextHandler(List<Handler> handlers) {
for (Handler handler : handlers) {
setHandlerMaxHttpFormPostSize(handler);
acceptCustomizeServletContextHandler(handler);
}
}

private void setHandlerMaxHttpFormPostSize(Handler handler) {
private void acceptCustomizeServletContextHandler(Handler handler) {
if (handler instanceof ServletContextHandler contextHandler) {
contextHandler.setMaxFormContentSize(maxHttpFormPostSize);
customFunc.accept(contextHandler);
}
else if (handler instanceof Handler.Wrapper wrapper) {
setHandlerMaxHttpFormPostSize(wrapper.getHandler());
acceptCustomizeServletContextHandler(wrapper.getHandler());
}
else if (handler instanceof Handler.Collection collection) {
setHandlerMaxHttpFormPostSize(collection.getHandlers());
acceptCustomizeServletContextHandler(collection.getHandlers());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,15 @@ void jettyMaxHttpFormPostSizeMatchesDefault() {
.isEqualTo(((ServletContextHandler) server.getHandler()).getMaxFormContentSize());
}

@Test
void jettyMaxFormKeysMatchesDefault() {
JettyServletWebServerFactory jettyFactory = new JettyServletWebServerFactory(0);
JettyWebServer jetty = (JettyWebServer) jettyFactory.getWebServer();
Server server = jetty.getServer();
assertThat(this.properties.getJetty().getMaxFormKeys())
.isEqualTo(((ServletContextHandler) server.getHandler()).getMaxFormKeys());
}

@Test
void undertowMaxHttpPostSizeMatchesDefault() {
assertThat(this.properties.getUndertow().getMaxHttpPostSize().toBytes())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.SynchronousQueue;
import java.util.function.Function;

import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.server.AbstractConnector;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.CustomRequestLog;
Expand Down Expand Up @@ -324,6 +325,23 @@ void customIdleTimeout() {
assertThat(timeouts).containsOnly(60000L);
}

@Test
void customMaxFormKeys() {
bind("server.jetty.max-form-keys=2048");
JettyWebServer server = customizeAndGetServer();
List<Integer> maxFormKeys = getMaxFormKeys(server);
assertThat(maxFormKeys).containsOnly(2048);
}

private List<Integer> getMaxFormKeys(JettyWebServer server) {
server.start();
server.stop();
return server.getServer().getHandlers().stream()
.filter(handler -> handler instanceof ServletContextHandler)
.map(handler -> ((ServletContextHandler) handler).getMaxFormKeys())
.toList();
}

private List<Long> connectorsIdleTimeouts(JettyWebServer server) {
// Start (and directly stop) server to have connectors available
server.start();
Expand Down