2
2
3
3
import java .io .StringReader ;
4
4
5
+ import javax .xml .parsers .DocumentBuilder ;
5
6
import javax .xml .parsers .DocumentBuilderFactory ;
7
+ import javax .xml .parsers .ParserConfigurationException ;
6
8
7
9
import org .w3c .dom .Document ;
8
10
import org .w3c .dom .Node ;
@@ -20,11 +22,14 @@ public abstract class DOMDeserializer<T> extends FromStringDeserializer<T>
20
22
{
21
23
private static final long serialVersionUID = 1L ;
22
24
23
- private final static DocumentBuilderFactory _parserFactory ;
25
+ private final static DocumentBuilderFactory DEFAULT_PARSER_FACTORY ;
24
26
static {
25
- _parserFactory = DocumentBuilderFactory .newInstance ();
27
+ DocumentBuilderFactory parserFactory = DocumentBuilderFactory .newInstance ();
26
28
// yup, only cave men do XML without recognizing namespaces...
27
- _parserFactory .setNamespaceAware (true );
29
+ parserFactory .setNamespaceAware (true );
30
+ // [databind#1279]: make sure external entities NOT expanded by default
31
+ parserFactory .setExpandEntityReferences (false );
32
+ DEFAULT_PARSER_FACTORY = parserFactory ;
28
33
}
29
34
30
35
protected DOMDeserializer (Class <T > cls ) { super (cls ); }
@@ -34,12 +39,22 @@ public abstract class DOMDeserializer<T> extends FromStringDeserializer<T>
34
39
35
40
protected final Document parse (String value ) throws IllegalArgumentException {
36
41
try {
37
- return _parserFactory . newDocumentBuilder ().parse (new InputSource (new StringReader (value )));
42
+ return documentBuilder ().parse (new InputSource (new StringReader (value )));
38
43
} catch (Exception e ) {
39
44
throw new IllegalArgumentException ("Failed to parse JSON String as XML: " +e .getMessage (), e );
40
45
}
41
46
}
42
47
48
+ /**
49
+ * Overridable factory method used to create {@link DocumentBuilder} for parsing
50
+ * XML as DOM.
51
+ *
52
+ * @since 2.7.6
53
+ */
54
+ protected DocumentBuilder documentBuilder () throws ParserConfigurationException {
55
+ return DEFAULT_PARSER_FACTORY .newDocumentBuilder ();
56
+ }
57
+
43
58
/*
44
59
/**********************************************************
45
60
/* Concrete deserializers
0 commit comments