-
-
Notifications
You must be signed in to change notification settings - Fork 815
Add StreamWriteConstraints with a nesting depth check #1055
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
Merged
cowtowncoder
merged 2 commits into
FasterXML:2.16
from
pjfanning:stream-write-constraints
Jul 8, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
src/main/java/com/fasterxml/jackson/core/StreamWriteConstraints.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package com.fasterxml.jackson.core; | ||
|
||
import com.fasterxml.jackson.core.exc.StreamConstraintsException; | ||
|
||
/** | ||
* The constraints to use for streaming writes: used to guard against problematic | ||
* output by preventing processing of "too big" output constructs (values, | ||
* structures). | ||
* Constraints are registered with {@code TokenStreamFactory} (such as | ||
* {@code JsonFactory}); if nothing explicitly specified, default | ||
* constraints are used. | ||
*<p> | ||
* Currently constrained aspects, with default settings, are: | ||
* <ul> | ||
* <li>Maximum Nesting depth: default 1000 (see {@link #DEFAULT_MAX_DEPTH}) | ||
* </li> | ||
* </ul> | ||
* | ||
* @since 2.16 | ||
*/ | ||
public class StreamWriteConstraints | ||
implements java.io.Serializable | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* Default setting for maximum depth: see {@link Builder#maxNestingDepth(int)} for details. | ||
*/ | ||
public static final int DEFAULT_MAX_DEPTH = 1000; | ||
|
||
protected final int _maxNestingDepth; | ||
|
||
private static final StreamWriteConstraints DEFAULT = | ||
new StreamWriteConstraints(DEFAULT_MAX_DEPTH); | ||
|
||
public static final class Builder { | ||
private int maxNestingDepth; | ||
|
||
/** | ||
* Sets the maximum nesting depth. The depth is a count of objects and arrays that have not | ||
* been closed, `{` and `[` respectively. | ||
* | ||
* @param maxNestingDepth the maximum depth | ||
* | ||
* @return this builder | ||
* @throws IllegalArgumentException if the maxNestingDepth is set to a negative value | ||
*/ | ||
public Builder maxNestingDepth(final int maxNestingDepth) { | ||
if (maxNestingDepth < 0) { | ||
throw new IllegalArgumentException("Cannot set maxNestingDepth to a negative value"); | ||
} | ||
this.maxNestingDepth = maxNestingDepth; | ||
return this; | ||
} | ||
|
||
Builder() { | ||
this(DEFAULT_MAX_DEPTH); | ||
} | ||
|
||
Builder(final int maxNestingDepth) { | ||
this.maxNestingDepth = maxNestingDepth; | ||
} | ||
|
||
Builder(StreamWriteConstraints src) { | ||
maxNestingDepth = src._maxNestingDepth; | ||
} | ||
|
||
public StreamWriteConstraints build() { | ||
return new StreamWriteConstraints(maxNestingDepth); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Life-cycle | ||
/********************************************************************** | ||
*/ | ||
|
||
protected StreamWriteConstraints(final int maxNestingDepth) { | ||
_maxNestingDepth = maxNestingDepth; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static StreamWriteConstraints defaults() { | ||
return DEFAULT; | ||
} | ||
|
||
/** | ||
* @return New {@link Builder} initialized with settings of this constraints | ||
* instance | ||
*/ | ||
public Builder rebuild() { | ||
return new Builder(this); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Accessors | ||
/********************************************************************** | ||
*/ | ||
|
||
/** | ||
* Accessor for maximum depth. | ||
* see {@link Builder#maxNestingDepth(int)} for details. | ||
* | ||
* @return Maximum allowed depth | ||
*/ | ||
public int getMaxNestingDepth() { | ||
return _maxNestingDepth; | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Convenience methods for validation, document limits | ||
/********************************************************************** | ||
*/ | ||
|
||
/** | ||
* Convenience method that can be used to verify that the | ||
* nesting depth does not exceed the maximum specified by this | ||
* constraints object: if it does, a | ||
* {@link StreamConstraintsException} | ||
* is thrown. | ||
* | ||
* @param depth count of unclosed objects and arrays | ||
* | ||
* @throws StreamConstraintsException If depth exceeds maximum | ||
*/ | ||
public void validateNestingDepth(int depth) throws StreamConstraintsException | ||
{ | ||
if (depth > _maxNestingDepth) { | ||
throw _constructException( | ||
"Document nesting depth (%d) exceeds the maximum allowed (%d, from %s)", | ||
depth, _maxNestingDepth, | ||
_constrainRef("getMaxNestingDepth")); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Error reporting | ||
/********************************************************************** | ||
*/ | ||
|
||
// @since 2.16 | ||
protected StreamConstraintsException _constructException(String msgTemplate, Object... args) throws StreamConstraintsException { | ||
throw new StreamConstraintsException(String.format(msgTemplate, args)); | ||
} | ||
|
||
// @since 2.16 | ||
protected String _constrainRef(String method) { | ||
return "`StreamWriteConstraints."+method+"()`"; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.