-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Limit the XML nesting depth for CVE-2022-45688 #720
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f566a1d
fix: limit the nesting depth
cleydyr a14cb12
refactor: keep consistence with other tests and tidy up constant
cleydyr 651511f
tests: add new test to verify that an XML having the permitted nestin…
cleydyr eb56704
fix: set default maximum nesting depth as 512
cleydyr 448e204
docs: remove wrong description of parse method
cleydyr 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 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 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 |
---|---|---|
|
@@ -16,6 +16,17 @@ | |
*/ | ||
@SuppressWarnings({""}) | ||
public class XMLParserConfiguration { | ||
/** | ||
* Used to indicate there's no defined limit to the maximum nesting depth when parsing a XML | ||
* document to JSON. | ||
*/ | ||
public static final int UNDEFINED_MAXIMUM_NESTING_DEPTH = -1; | ||
|
||
/** | ||
* The default maximum nesting depth when parsing a XML document to JSON. | ||
*/ | ||
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512; | ||
|
||
/** Original Configuration of the XML Parser. */ | ||
public static final XMLParserConfiguration ORIGINAL | ||
= new XMLParserConfiguration(); | ||
|
@@ -54,6 +65,12 @@ public class XMLParserConfiguration { | |
*/ | ||
private Set<String> forceList; | ||
|
||
/** | ||
* When parsing the XML into JSON, specifies the tags whose values should be converted | ||
* to arrays | ||
*/ | ||
private int maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH; | ||
|
||
/** | ||
* Default parser configuration. Does not keep strings (tries to implicitly convert | ||
* values), and the CDATA Tag Name is "content". | ||
|
@@ -297,4 +314,33 @@ public XMLParserConfiguration withForceList(final Set<String> forceList) { | |
newConfig.forceList = Collections.unmodifiableSet(cloneForceList); | ||
return newConfig; | ||
} | ||
|
||
/** | ||
* The maximum nesting depth that the parser will descend before throwing an exception | ||
* when parsing the XML into JSON. | ||
* @return the maximum nesting depth set for this configuration | ||
*/ | ||
public int getMaxNestingDepth() { | ||
return maxNestingDepth; | ||
} | ||
|
||
/** | ||
* Defines the maximum nesting depth that the parser will descend before throwing an exception | ||
* when parsing the XML into JSON. The default max nesting depth is 512, which means the parser | ||
* will go as deep as the maximum call stack size allows. Using any negative value as a | ||
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 comment is slightly incorrect - now we have a default limit of 512, so the parser won't
|
||
* parameter is equivalent to setting no limit to the nesting depth. | ||
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser | ||
* @return The existing configuration will not be modified. A new configuration is returned. | ||
*/ | ||
public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { | ||
XMLParserConfiguration newConfig = this.clone(); | ||
|
||
if (maxNestingDepth > UNDEFINED_MAXIMUM_NESTING_DEPTH) { | ||
newConfig.maxNestingDepth = maxNestingDepth; | ||
} else { | ||
newConfig.maxNestingDepth = UNDEFINED_MAXIMUM_NESTING_DEPTH; | ||
} | ||
|
||
return newConfig; | ||
} | ||
} |
This file contains 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 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 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
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.
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.
This comment is incorrect (just a duplicate from the previous section) - suggested content: