Skip to content

Commit 4929735

Browse files
committed
Fix #1279
1 parent 255c08d commit 4929735

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

release-notes/VERSION

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.7.6 (not yet released)
8+
9+
#1279: Ensure DOM parsing defaults to not expanding external entities
10+
711
2.7.5 (11-Jun-2016)
812

913
#1098: DeserializationFeature.FAIL_ON_INVALID_SUBTYPE does not work with

src/main/java/com/fasterxml/jackson/databind/ext/DOMDeserializer.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.io.StringReader;
44

5+
import javax.xml.parsers.DocumentBuilder;
56
import javax.xml.parsers.DocumentBuilderFactory;
7+
import javax.xml.parsers.ParserConfigurationException;
68

79
import org.w3c.dom.Document;
810
import org.w3c.dom.Node;
@@ -20,11 +22,14 @@ public abstract class DOMDeserializer<T> extends FromStringDeserializer<T>
2022
{
2123
private static final long serialVersionUID = 1L;
2224

23-
private final static DocumentBuilderFactory _parserFactory;
25+
private final static DocumentBuilderFactory DEFAULT_PARSER_FACTORY;
2426
static {
25-
_parserFactory = DocumentBuilderFactory.newInstance();
27+
DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
2628
// 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;
2833
}
2934

3035
protected DOMDeserializer(Class<T> cls) { super(cls); }
@@ -34,12 +39,22 @@ public abstract class DOMDeserializer<T> extends FromStringDeserializer<T>
3439

3540
protected final Document parse(String value) throws IllegalArgumentException {
3641
try {
37-
return _parserFactory.newDocumentBuilder().parse(new InputSource(new StringReader(value)));
42+
return documentBuilder().parse(new InputSource(new StringReader(value)));
3843
} catch (Exception e) {
3944
throw new IllegalArgumentException("Failed to parse JSON String as XML: "+e.getMessage(), e);
4045
}
4146
}
4247

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+
4358
/*
4459
/**********************************************************
4560
/* Concrete deserializers

0 commit comments

Comments
 (0)