-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Throw JsonMappingException for deeply nested JSON (#2816, CVE-2020-36518) #3416
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f19edd2
Throw a checked JsonMappingException instead of an unchecked StackOve…
TaylorSMarks e7c34bc
Track depth of object being deserialized and abort if it's too deep (…
TaylorSMarks a6e8fe3
Increase the maximum depth from 256 to 1000. Update the unit tests to…
TaylorSMarks 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
70 changes: 70 additions & 0 deletions
70
src/test/java/com/fasterxml/jackson/databind/deser/DeepNestingUntypedDeserTest.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,70 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
|
||
import com.fasterxml.jackson.core.JsonParseException; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DeepNestingUntypedDeserTest extends BaseMapTest | ||
{ | ||
// 28-Mar-2021, tatu: Currently 3000 fails for untyped/Object, | ||
// 4000 for untyped/Array | ||
private final static int TOO_DEEP_NESTING = 4000; | ||
private final static int NOT_TOO_DEEP = 1000; | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
public void testTooDeepUntypedWithArray() throws Exception | ||
{ | ||
final String doc = _nestedDoc(TOO_DEEP_NESTING, "[ ", "] "); | ||
try { | ||
MAPPER.readValue(doc, Object.class); | ||
fail("Should have thrown an exception."); | ||
} catch (JsonParseException jpe) { | ||
assertTrue(jpe.getMessage().startsWith("JSON is too deeply nested.")); | ||
} | ||
} | ||
|
||
public void testUntypedWithArray() throws Exception | ||
{ | ||
final String doc = _nestedDoc(NOT_TOO_DEEP, "[ ", "] "); | ||
Object ob = MAPPER.readValue(doc, Object.class); | ||
assertTrue(ob instanceof List<?>); | ||
} | ||
|
||
public void testTooDeepUntypedWithObject() throws Exception | ||
{ | ||
final String doc = "{"+_nestedDoc(TOO_DEEP_NESTING, "\"x\":{", "} ") + "}"; | ||
try { | ||
MAPPER.readValue(doc, Object.class); | ||
fail("Should have thrown an exception."); | ||
} catch (JsonParseException jpe) { | ||
assertTrue(jpe.getMessage().startsWith("JSON is too deeply nested.")); | ||
} | ||
} | ||
|
||
public void testUntypedWithObject() throws Exception | ||
{ | ||
final String doc = "{"+_nestedDoc(NOT_TOO_DEEP, "\"x\":{", "} ") + "}"; | ||
Object ob = MAPPER.readValue(doc, Object.class); | ||
assertTrue(ob instanceof Map<?, ?>); | ||
} | ||
|
||
private String _nestedDoc(int nesting, String open, String close) { | ||
StringBuilder sb = new StringBuilder(nesting * (open.length() + close.length())); | ||
for (int i = 0; i < nesting; ++i) { | ||
sb.append(open); | ||
if ((i & 31) == 0) { | ||
sb.append("\n"); | ||
} | ||
} | ||
for (int i = 0; i < nesting; ++i) { | ||
sb.append(close); | ||
if ((i & 31) == 0) { | ||
sb.append("\n"); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
} |
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.
@cowtowncoder @TaylorSMarks would it make sense to add a public static method to UntypedObjectDeserializer.java that allows users to set a different limit (but still with 1000 default)?