Skip to content

Commit f2c445d

Browse files
committed
Fix #1735
1 parent ce7d1c9 commit f2c445d

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/ClassNameIdResolver.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ protected JavaType _typeFromId(String id, DatabindContext ctxt) throws IOExcepti
5151
TypeFactory tf = ctxt.getTypeFactory();
5252
if (id.indexOf('<') > 0) {
5353
// note: may want to try combining with specialization (esp for EnumMap)?
54-
return tf.constructFromCanonical(id);
54+
// 17-Aug-2017, tatu: As per [databind#1735] need to ensure assignment
55+
// compatibility -- needed later anyway, and not doing so may open
56+
// security issues.
57+
JavaType t = tf.constructFromCanonical(id);
58+
if (!t.isTypeOrSubTypeOf(_baseType.getRawClass())) {
59+
// Probably cleaner to have a method in `TypeFactory` but can't add in patch
60+
throw new IllegalArgumentException(String.format(
61+
"Class %s not subtype of %s", t.getRawClass().getName(), _baseType));
62+
}
63+
return t;
5564
}
5665
Class<?> cls;
5766
try {

src/test/java/com/fasterxml/jackson/databind/BaseTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,9 @@ protected void verifyException(Throwable e, String... matches)
364364
return;
365365
}
366366
}
367-
fail("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
367+
fail("Expected an exception with one of substrings ("
368+
+Arrays.asList(matches)+"): got one (of type "+e.getClass().getName()
369+
+") with message \""+msg+"\"");
368370
}
369371

370372
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fasterxml.jackson.databind.jsontype;
2+
3+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
4+
5+
import com.fasterxml.jackson.databind.*;
6+
7+
// for [databind#1735]:
8+
public class GenericTypeId1735Test extends BaseMapTest
9+
{
10+
static class Wrapper1735 {
11+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "type")
12+
public Payload1735 w;
13+
}
14+
15+
static class Payload1735 {
16+
public void setValue(String str) { }
17+
}
18+
19+
static class Nefarious1735 {
20+
public Nefarious1735() {
21+
throw new Error("Never call this constructor");
22+
}
23+
24+
public void setValue(String str) {
25+
throw new Error("Never call this setter");
26+
}
27+
}
28+
29+
/*
30+
/**********************************************************
31+
/* Unit tests
32+
/**********************************************************
33+
*/
34+
35+
private final ObjectMapper MAPPER = objectMapper();
36+
37+
private final static String NEF_CLASS = Nefarious1735.class.getName();
38+
39+
// Existing checks should kick in fine
40+
public void testSimpleTypeCheck1735() throws Exception
41+
{
42+
try {
43+
MAPPER.readValue(aposToQuotes(
44+
"{'w':{'type':'"+NEF_CLASS+"'}}"),
45+
Wrapper1735.class);
46+
fail("Should not pass");
47+
} catch (JsonMappingException e) {
48+
verifyException(e, "not subtype of");
49+
}
50+
}
51+
52+
// but this was not being verified early enough
53+
public void testNestedTypeCheck1735() throws Exception
54+
{
55+
try {
56+
MAPPER.readValue(aposToQuotes(
57+
"{'w':{'type':'java.util.HashMap<java.lang.String,java.lang.String>'}}"),
58+
Wrapper1735.class);
59+
fail("Should not pass");
60+
} catch (JsonMappingException e) {
61+
verifyException(e, "not subtype of");
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)