@@ -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}
0 commit comments