-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Add documentation for form limits & improve configuration via context attributes #12560
base: jetty-12.0.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// ======================================================================== | ||
// | ||
|
||
package org.eclipse.jetty.docs.programming.security; | ||
|
||
import org.eclipse.jetty.ee10.servlet.ServletContextHandler; | ||
|
||
public class FormSizeDocs | ||
{ | ||
public void example() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
{ | ||
ServletContextHandler servletContextHandler = new ServletContextHandler(); | ||
int maxSizeInBytes = 1024; | ||
int formKeys = 100; | ||
// tag::formSizeConfig[] | ||
Comment on lines
+23
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include the |
||
servletContextHandler.setMaxFormContentSize(maxSizeInBytes); | ||
servletContextHandler.setMaxFormKeys(formKeys); | ||
// end::formSizeConfig[] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||||
// | ||||||
// ======================================================================== | ||||||
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. | ||||||
// | ||||||
// This program and the accompanying materials are made available under the | ||||||
// terms of the Eclipse Public License v. 2.0 which is available at | ||||||
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||||||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||||||
// | ||||||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||||||
// ======================================================================== | ||||||
// | ||||||
|
||||||
[[configuring-form-size]] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rename it to |
||||||
= Limiting Form Content | ||||||
|
||||||
Form content sent to the server is processed by Jetty into a map of parameters to be used by the web application. | ||||||
This can be vulnerable to denial of service (DOS) attacks since significant memory and CPU can be consumed if a malicious clients sends very large form content or large number of form keys. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Thus, Jetty limits the amount of data and keys that can be in a form posted to Jetty. | ||||||
|
||||||
The default maximum size Jetty permits is 200000 bytes and 1000 keys. | ||||||
You can change this default for a particular webapp or for all webapps on a particular Server instance. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
== Configuring Form Limits for a Webapp | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
To configure the form limits for a single web application, the servlet context handler (or webappContext) instance must be configured using the following methods: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
[,java,indent=0] | ||||||
---- | ||||||
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/security/FormSizeDocs.java[tags=formSizeConfig] | ||||||
---- | ||||||
|
||||||
These methods may be called directly when embedding Jetty, but more commonly are configured from a context XML file or WEB-INF/jetty-web.xml file: | ||||||
|
||||||
[,xml,subs=attributes+] | ||||||
---- | ||||||
<Configure class="org.eclipse.jetty.{ee-current}.webapp.WebAppContext"> | ||||||
|
||||||
... | ||||||
|
||||||
<Set name="maxFormContentSize">200000</Set> | ||||||
<Set name="maxFormKeys">200</Set> | ||||||
</Configure> | ||||||
|
||||||
---- | ||||||
Comment on lines
+33
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be moved to the operations guide. |
||||||
|
||||||
These settings can also be set via the following Context attributes. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not clear what "Context" are you referring to here. You mean context attributes in |
||||||
|
||||||
- `org.eclipse.jetty.server.Request.maxFormKeys` | ||||||
- `org.eclipse.jetty.server.Request.maxFormContentSize` | ||||||
|
||||||
== Configuring Default Form Limits for the Server | ||||||
|
||||||
The default `maxFormKeys` is 1000 and the default `maxFormContentSize` is 200000. | ||||||
|
||||||
However, the following system properties can be set to change the default values of this across every context; `org.eclipse.jetty.server.Request.maxFormKeys` and `org.eclipse.jetty.server.Request.maxFormContentSize`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put them in a bullet list like above. I wonder why we don't look these up as |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -319,6 +319,8 @@ public InvocationType getInvocationType() | |
public void dump(Appendable out, String indent) throws IOException | ||
{ | ||
dumpObjects(out, indent, | ||
Dumpable.named("maxFormKeys ", getMaxFormKeys()), | ||
Dumpable.named("maxFormContentSize ", getMaxFormContentSize()), | ||
Comment on lines
+322
to
+323
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move down in the dump to be after the attributes |
||
new ClassLoaderDump(getClassLoader()), | ||
Dumpable.named("context " + this, getContext()), | ||
Dumpable.named("handler attributes " + this, getContext().getPersistentAttributes()), | ||
|
@@ -2045,6 +2047,44 @@ public void setExtendedListenerTypes(boolean b) | |
{ | ||
_servletContext.setExtendedListenerTypes(b); | ||
} | ||
|
||
@Override | ||
public Object getAttribute(String name) | ||
{ | ||
return switch (name) | ||
{ | ||
case FormFields.MAX_FIELDS_ATTRIBUTE -> getMaxFormKeys(); | ||
case FormFields.MAX_LENGTH_ATTRIBUTE -> getMaxFormContentSize(); | ||
default -> super.getAttribute(name); | ||
}; | ||
} | ||
|
||
@Override | ||
public Object setAttribute(String name, Object attribute) | ||
{ | ||
return switch (name) | ||
{ | ||
case FormFields.MAX_FIELDS_ATTRIBUTE -> | ||
{ | ||
int oldValue = getMaxFormKeys(); | ||
if (attribute == null) | ||
setMaxFormKeys(DEFAULT_MAX_FORM_KEYS); | ||
else | ||
setMaxFormKeys(Integer.parseInt(attribute.toString())); | ||
yield oldValue; | ||
} | ||
case FormFields.MAX_LENGTH_ATTRIBUTE -> | ||
{ | ||
int oldValue = getMaxFormContentSize(); | ||
if (attribute == null) | ||
setMaxFormContentSize(DEFAULT_MAX_FORM_CONTENT_SIZE); | ||
else | ||
setMaxFormContentSize(Integer.parseInt(attribute.toString())); | ||
yield oldValue; | ||
} | ||
default -> super.setAttribute(name, attribute); | ||
}; | ||
} | ||
} | ||
|
||
public class ServletContextApi implements jakarta.servlet.ServletContext | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ | |
import org.eclipse.jetty.util.annotation.ManagedAttribute; | ||
import org.eclipse.jetty.util.annotation.ManagedObject; | ||
import org.eclipse.jetty.util.component.ClassLoaderDump; | ||
import org.eclipse.jetty.util.component.Dumpable; | ||
import org.eclipse.jetty.util.component.DumpableCollection; | ||
import org.eclipse.jetty.util.resource.Resource; | ||
import org.eclipse.jetty.util.resource.ResourceFactory; | ||
|
@@ -987,6 +988,8 @@ else if (getBaseResource() != null) | |
name = String.format("%s@%x", name, hashCode()); | ||
|
||
dumpObjects(out, indent, | ||
Dumpable.named("maxFormKeys ", getMaxFormKeys()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before this line, dump also the environment like in ee9? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than override dump and have to repeat the stuff from the base class, why don't we just add all these Dumpable collections as beans the the context and let the normal dump mechanism dump them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or better yet, add one Dumpable webapp bean, that dumps all these extra details |
||
Dumpable.named("maxFormContentSize ", getMaxFormContentSize()), | ||
new ClassLoaderDump(getClassLoader()), | ||
new DumpableCollection("Systemclasses " + name, systemClasses), | ||
new DumpableCollection("Serverclasses " + name, serverClasses), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rename it to
FormDocs
, as we may use it for something else than just "size".