Skip to content

Commit fa87c1d

Browse files
committed
Backport #1599 in 2.6.x
1 parent 96eb83b commit fa87c1d

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>com.fasterxml.jackson.core</groupId>
1212
<artifactId>jackson-databind</artifactId>
13-
<version>2.6.8-SNAPSHOT</version>
13+
<version>2.6.7.1-SNAPSHOT</version>
1414
<name>jackson-databind</name>
1515
<packaging>bundle</packaging>
1616
<description>General data-binding functionality for Jackson: works on core streaming API</description>

release-notes/VERSION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7-
2.6.8 (if ever released)
7+
2.6.7.1 (11-Jul-2017)
88

99
#1383: Problem with `@JsonCreator` with 1-arg factory-method, implicit param names
10+
#1599: Backport the extra safety checks for polymorphic deserialization
1011

1112
2.6.7 (05-Jun-2016)
1213

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerFactory.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,32 @@ public class BeanDeserializerFactory
4040
private final static Class<?>[] INIT_CAUSE_PARAMS = new Class<?>[] { Throwable.class };
4141

4242
private final static Class<?>[] NO_VIEWS = new Class<?>[0];
43-
43+
44+
/**
45+
* Set of well-known "nasty classes", deserialization of which is considered dangerous
46+
* and should (and is) prevented by default.
47+
*/
48+
private final static Set<String> DEFAULT_NO_DESER_CLASS_NAMES;
49+
static {
50+
Set<String> s = new HashSet<String>();
51+
// Courtesy of [https://github.com/kantega/notsoserial]:
52+
// (and wrt [databind#1599]
53+
s.add("org.apache.commons.collections.functors.InvokerTransformer");
54+
s.add("org.apache.commons.collections.functors.InstantiateTransformer");
55+
s.add("org.apache.commons.collections4.functors.InvokerTransformer");
56+
s.add("org.apache.commons.collections4.functors.InstantiateTransformer");
57+
s.add("org.codehaus.groovy.runtime.ConvertedClosure");
58+
s.add("org.codehaus.groovy.runtime.MethodClosure");
59+
s.add("org.springframework.beans.factory.ObjectFactory");
60+
s.add("com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl");
61+
DEFAULT_NO_DESER_CLASS_NAMES = Collections.unmodifiableSet(s);
62+
}
63+
64+
/**
65+
* Set of class names of types that are never to be deserialized.
66+
*/
67+
private Set<String> _cfgIllegalClassNames = DEFAULT_NO_DESER_CLASS_NAMES;
68+
4469
/*
4570
/**********************************************************
4671
/* Life-cycle
@@ -138,6 +163,8 @@ public JsonDeserializer<Object> createBeanDeserializer(DeserializationContext ct
138163
if (!isPotentialBeanType(type.getRawClass())) {
139164
return null;
140165
}
166+
// For checks like [databind#1599]
167+
checkIllegalTypes(ctxt, type, beanDesc);
141168
// Use generic bean introspection to build deserializer
142169
return buildBeanDeserializer(ctxt, type, beanDesc);
143170
}
@@ -836,4 +863,20 @@ protected boolean isIgnorableType(DeserializationConfig config, BeanDescription
836863
// We default to 'false', i.e. not ignorable
837864
return (status == null) ? false : status.booleanValue();
838865
}
866+
867+
private void checkIllegalTypes(DeserializationContext ctxt, JavaType type,
868+
BeanDescription beanDesc)
869+
throws JsonMappingException
870+
{
871+
// There are certain nasty classes that could cause problems, mostly
872+
// via default typing -- catch them here.
873+
String full = type.getRawClass().getName();
874+
875+
if (_cfgIllegalClassNames.contains(full)) {
876+
String message = String.format("Illegal type (%s) to deserialize: prevented for security reasons",
877+
full);
878+
throw ctxt.mappingException("Invalid type definition for type %s: %s",
879+
beanDesc, message);
880+
}
881+
}
839882
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fasterxml.jackson.databind.interop;
2+
3+
import com.fasterxml.jackson.databind.*;
4+
5+
/**
6+
* Test case(s) to guard against handling of types that are illegal to handle
7+
* due to security constraints.
8+
*/
9+
public class IllegalTypesCheckTest extends BaseMapTest
10+
{
11+
static class Bean1599 {
12+
public int id;
13+
public Object obj;
14+
}
15+
16+
public void testIssue1599() throws Exception
17+
{
18+
final String JSON = aposToQuotes(
19+
"{'id': 124,\n"
20+
+" 'obj':[ 'com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl',\n"
21+
+" {\n"
22+
+" 'transletBytecodes' : [ 'AAIAZQ==' ],\n"
23+
+" 'transletName' : 'a.b',\n"
24+
+" 'outputProperties' : { }\n"
25+
+" }\n"
26+
+" ]\n"
27+
+"}"
28+
);
29+
ObjectMapper mapper = new ObjectMapper();
30+
mapper.enableDefaultTyping();
31+
try {
32+
mapper.readValue(JSON, Bean1599.class);
33+
fail("Should not pass");
34+
} catch (JsonMappingException e) {
35+
verifyException(e, "Illegal type");
36+
verifyException(e, "to deserialize");
37+
verifyException(e, "prevented for security reasons");
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)