Skip to content
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

police nesting depth in java props parser #397

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public JsonToken nextToken() throws IOException {
if (_readContext == null) { // end of content
return null;
}
_streamReadConstraints.validateNestingDepth(_readContext.getNestingDepth());
}
return _currToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public JPropReadContext(int contextType, JPropReadContext p, JPropNode node)
_index = -1;
_parent = p;
_branchText = node.getValue();
_nestingDepth = p == null ? 0 : p._nestingDepth + 1;
}

public static JPropReadContext create(JPropNode root) {
Expand Down
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();
}
}