Skip to content

Commit eca8c63

Browse files
replace DocumentBuilder in XmlTextStructureFinder
1 parent 5b1e2e9 commit eca8c63

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

libs/core/src/main/java/org/elasticsearch/core/XmlUtils.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,26 @@ public class XmlUtils {
3232
*
3333
* @throws ParserConfigurationException if one of the features can't be set on the DocumentBuilderFactory
3434
*/
35-
@SuppressForbidden(reason = "This is the only allowed way to construct a DocumentBuilder")
3635
public static DocumentBuilder getHardenedBuilder(String[] schemaFiles) throws ParserConfigurationException {
37-
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
36+
final DocumentBuilderFactory dbf = getHardenedBuilderFactory();
3837
dbf.setNamespaceAware(true);
3938
// Ensure that Schema Validation is enabled for the factory
4039
dbf.setValidating(true);
40+
// This is required, otherwise schema validation causes signature invalidation
41+
dbf.setFeature("http://apache.org/xml/features/validation/schema/normalized-value", false);
42+
// Make sure that URL schema namespaces are not resolved/downloaded from URLs we do not control
43+
dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "file,jar");
44+
dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "file,jar");
45+
// We ship our own xsd files for schema validation since we do not trust anyone else.
46+
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaFiles);
47+
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
48+
documentBuilder.setErrorHandler(new ErrorHandler());
49+
return documentBuilder;
50+
}
51+
52+
@SuppressForbidden(reason = "This is the only allowed way to construct a DocumentBuilder")
53+
public static DocumentBuilderFactory getHardenedBuilderFactory() throws ParserConfigurationException {
54+
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
4155
// Disallow internal and external entity expansion
4256
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
4357
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
@@ -46,11 +60,8 @@ public static DocumentBuilder getHardenedBuilder(String[] schemaFiles) throws Pa
4660
dbf.setFeature("http://xml.org/sax/features/validation", true);
4761
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
4862
dbf.setIgnoringComments(true);
49-
// This is required, otherwise schema validation causes signature invalidation
50-
dbf.setFeature("http://apache.org/xml/features/validation/schema/normalized-value", false);
51-
// Make sure that URL schema namespaces are not resolved/downloaded from URLs we do not control
52-
dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "file,jar");
53-
dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "file,jar");
63+
dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
64+
dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
5465
dbf.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", true);
5566
// Ensure we do not resolve XIncludes. Defaults to false, but set it explicitly to be future-proof
5667
dbf.setXIncludeAware(false);
@@ -61,11 +72,8 @@ public static DocumentBuilder getHardenedBuilder(String[] schemaFiles) throws Pa
6172
dbf.setAttribute("http://apache.org/xml/features/validation/schema", true);
6273
dbf.setAttribute("http://apache.org/xml/features/validation/schema-full-checking", true);
6374
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);
64-
// We ship our own xsd files for schema validation since we do not trust anyone else.
65-
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaFiles);
66-
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
67-
documentBuilder.setErrorHandler(new ErrorHandler());
68-
return documentBuilder;
75+
76+
return dbf;
6977
}
7078

7179
@SuppressForbidden(reason = "This is the only allowed way to construct a Transformer")

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.util.NamedFormatter;
1515
import org.elasticsearch.core.TimeValue;
1616
import org.elasticsearch.core.Tuple;
17+
import org.elasticsearch.core.XmlUtils;
1718
import org.elasticsearch.test.MockLog;
1819
import org.elasticsearch.xpack.core.watcher.watch.ClockMock;
1920
import org.hamcrest.Matchers;
@@ -1501,7 +1502,7 @@ private Encrypter getEncrypter(Tuple<X509Certificate, PrivateKey> keyPair) throw
15011502
}
15021503

15031504
private Response toResponse(String xml) throws SAXException, IOException, ParserConfigurationException {
1504-
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
1505+
final DocumentBuilderFactory dbf = XmlUtils.getHardenedBuilderFactory();
15051506
dbf.setNamespaceAware(true);
15061507
final Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
15071508
return authenticator.buildXmlObject(doc.getDocumentElement(), Response.class);

x-pack/plugin/text-structure/src/main/java/org/elasticsearch/xpack/textstructure/structurefinder/XmlTextStructureFinder.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.elasticsearch.xpack.textstructure.structurefinder;
88

99
import org.elasticsearch.core.Tuple;
10+
import org.elasticsearch.core.XmlUtils;
1011
import org.elasticsearch.xpack.core.textstructure.structurefinder.FieldStats;
1112
import org.elasticsearch.xpack.core.textstructure.structurefinder.TextStructure;
1213
import org.w3c.dom.Document;
@@ -29,7 +30,6 @@
2930
import java.util.TreeMap;
3031
import java.util.regex.Pattern;
3132

32-
import javax.xml.XMLConstants;
3333
import javax.xml.parsers.DocumentBuilder;
3434
import javax.xml.parsers.DocumentBuilderFactory;
3535
import javax.xml.parsers.ParserConfigurationException;
@@ -152,21 +152,9 @@ static XmlTextStructureFinder makeXmlTextStructureFinder(
152152
}
153153

154154
private static DocumentBuilderFactory makeDocBuilderFactory() throws ParserConfigurationException {
155-
156-
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
155+
DocumentBuilderFactory docBuilderFactory = XmlUtils.getHardenedBuilderFactory();
157156
docBuilderFactory.setNamespaceAware(false);
158157
docBuilderFactory.setValidating(false);
159-
docBuilderFactory.setXIncludeAware(false);
160-
docBuilderFactory.setExpandEntityReferences(false);
161-
docBuilderFactory.setIgnoringComments(true);
162-
docBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
163-
docBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
164-
// The next 5 should be irrelevant given the previous 1, but it doesn't hurt to set them just in case
165-
docBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
166-
docBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
167-
docBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
168-
docBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
169-
docBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
170158
return docBuilderFactory;
171159
}
172160

0 commit comments

Comments
 (0)