-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
police nesting depth in java props parser (#397)
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
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
54 changes: 54 additions & 0 deletions
54
...rties/src/test/java/com/fasterxml/jackson/dataformat/javaprop/dos/DeepNestParserTest.java
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.fasterxml.jackson.dataformat.javaprop.dos; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.core.StreamReadConstraints; | ||
import com.fasterxml.jackson.core.exc.StreamConstraintsException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsFactory; | ||
import com.fasterxml.jackson.dataformat.javaprop.ModuleTestBase; | ||
|
||
import java.io.IOException; | ||
|
||
public class DeepNestParserTest extends ModuleTestBase { | ||
|
||
public void testDeeplyNestedData() throws IOException { | ||
final int depth = 1500; | ||
final String doc = genDeeplyNestedData(depth); | ||
final ObjectMapper mapper = newPropertiesMapper(); | ||
try (JsonParser jp = mapper.createParser(doc)) { | ||
JsonToken jt; | ||
while ((jt = jp.nextToken()) != null) { | ||
|
||
} | ||
fail("expected StreamConstraintsException"); | ||
} catch (StreamConstraintsException e) { | ||
assertEquals("Depth (1001) exceeds the maximum allowed nesting depth (1000)", e.getMessage()); | ||
} | ||
} | ||
|
||
public void testDeeplyNestedDataWithUnconstrainedMapper() throws IOException { | ||
final int depth = 1500; | ||
final String doc = genDeeplyNestedData(depth); | ||
final JavaPropsFactory factory = JavaPropsFactory.builder() | ||
.streamReadConstraints(StreamReadConstraints.builder().maxNestingDepth(Integer.MAX_VALUE).build()) | ||
.build(); | ||
final ObjectMapper mapper = propertiesMapperBuilder(factory).build(); | ||
try (JsonParser jp = mapper.createParser(doc)) { | ||
JsonToken jt; | ||
while ((jt = jp.nextToken()) != null) { | ||
|
||
} | ||
} | ||
} | ||
|
||
private String genDeeplyNestedData(final int depth) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < depth; i++) { | ||
if (i > 1) sb.append('.'); | ||
sb.append('a'); | ||
} | ||
sb.append("=val"); | ||
return sb.toString(); | ||
} | ||
} |