Skip to content

Commit

Permalink
Fix #3582
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 24, 2022
1 parent 8513512 commit 0631835
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project: jackson-databind
#3565: `Arrays.asList()` value deserialization has changed from mutable to
immutable in 2.13
(reported by JonasWilms@github)
#3582: Add check in `BeanDeserializer._deserializeFromArray()` to prevent
use of deeply nested arrays

2.13.3 (14-May-2022)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,10 @@ public enum DeserializationFeature implements ConfigFeature
* values to the corresponding value type. This is basically the opposite of the {@link #ACCEPT_SINGLE_VALUE_AS_ARRAY}
* feature. If more than one value is found in the array, a JsonMappingException is thrown.
* <p>
* NOTE: only <b>single</b> wrapper Array is allowed: if multiple attempted, exception
* will be thrown.
*
* Feature is disabled by default
* Feature is disabled by default.
* @since 2.4
*/
UNWRAP_SINGLE_VALUE_ARRAYS(false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.deser.impl.*;
import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId.Referring;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.IgnorePropertiesUtil;
import com.fasterxml.jackson.databind.util.NameTransformer;
import com.fasterxml.jackson.databind.util.TokenBuffer;
Expand Down Expand Up @@ -628,6 +629,15 @@ protected Object _deserializeFromArray(JsonParser p, DeserializationContext ctxt
return ctxt.handleUnexpectedToken(getValueType(ctxt), JsonToken.START_ARRAY, p, null);
}
if (unwrap) {
// 23-Aug-2022, tatu: To prevent unbounded nested arrays, we better
// check there is NOT another START_ARRAY lurking there..
if (p.nextToken() == JsonToken.START_ARRAY) {
JavaType targetType = getValueType(ctxt);
return ctxt.handleUnexpectedToken(targetType, JsonToken.START_ARRAY, p,
"Cannot deserialize value of type %s from deeply-nested JSON Array: only single wrapper allowed with `%s`",
ClassUtil.getTypeDescription(targetType),
"DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS");
}
final Object value = deserialize(p, ctxt);
if (p.nextToken() != JsonToken.END_ARRAY) {
handleMissingEndArrayForSingle(p, ctxt);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.fasterxml.jackson.databind.deser.dos;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;

public class DeepArrayWrappingForDeser3582Test extends BaseMapTest
{
// 23-Aug-2022, tatu: Before fix, fails with 5000
// (but passes with 2000)
// private final static int TOO_DEEP_NESTING = 4999;
private final static int TOO_DEEP_NESTING = 999;
// 23-Aug-2022, tatu: Before fix, failed with 5000
private final static int TOO_DEEP_NESTING = 9999;

private final ObjectMapper MAPPER = jsonMapperBuilder()
.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
Expand All @@ -16,8 +15,14 @@ public class DeepArrayWrappingForDeser3582Test extends BaseMapTest
public void testArrayWrapping() throws Exception
{
final String doc = _nestedDoc(TOO_DEEP_NESTING, "[ ", "] ", "{}");
Point p = MAPPER.readValue(doc, Point.class);
assertNotNull(p);
try {
MAPPER.readValue(doc, Point.class);
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "Cannot deserialize");
verifyException(e, "nested JSON Array");
verifyException(e, "only single");
}
}

private String _nestedDoc(int nesting, String open, String close, String content) {
Expand Down

0 comments on commit 0631835

Please # to comment.