Skip to content

Commit 43376ae

Browse files
replace constructions of SchemaFactory and Validator
1 parent a040bf2 commit 43376ae

File tree

3 files changed

+41
-10
lines changed
  • libs/core/src/main/java/org/elasticsearch/core
  • x-pack/plugin
    • identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support
    • security/src/main/java/org/elasticsearch/xpack/security/authc/saml

3 files changed

+41
-10
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.elasticsearch.logging.LogManager;
1313
import org.elasticsearch.logging.Logger;
1414
import org.xml.sax.SAXException;
15+
import org.xml.sax.SAXNotRecognizedException;
16+
import org.xml.sax.SAXNotSupportedException;
1517
import org.xml.sax.SAXParseException;
1618

1719
import javax.xml.XMLConstants;
@@ -22,6 +24,9 @@
2224
import javax.xml.transform.TransformerConfigurationException;
2325
import javax.xml.transform.TransformerException;
2426
import javax.xml.transform.TransformerFactory;
27+
import javax.xml.validation.Schema;
28+
import javax.xml.validation.SchemaFactory;
29+
import javax.xml.validation.Validator;
2530

2631
public class XmlUtils {
2732

@@ -49,6 +54,9 @@ public static DocumentBuilder getHardenedBuilder(String[] schemaFiles) throws Pa
4954
return documentBuilder;
5055
}
5156

57+
/**
58+
* Returns a DocumentBuilderFactory pre-configured to be secure
59+
*/
5260
@SuppressForbidden(reason = "This is the only allowed way to construct a DocumentBuilder")
5361
public static DocumentBuilderFactory getHardenedBuilderFactory() throws ParserConfigurationException {
5462
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
@@ -76,6 +84,9 @@ public static DocumentBuilderFactory getHardenedBuilderFactory() throws ParserCo
7684
return dbf;
7785
}
7886

87+
/**
88+
* Constructs a Transformer configured to be secure
89+
*/
7990
@SuppressForbidden(reason = "This is the only allowed way to construct a Transformer")
8091
public static Transformer getHardenedXMLTransformer() throws TransformerConfigurationException {
8192
final TransformerFactory tfactory = TransformerFactory.newInstance();
@@ -88,6 +99,28 @@ public static Transformer getHardenedXMLTransformer() throws TransformerConfigur
8899
return transformer;
89100
}
90101

102+
/**
103+
* Returns a SchemaFactory configured to be secure
104+
*/
105+
@SuppressForbidden(reason = "This is the only allowed way to construct a SchemaFactory")
106+
public static SchemaFactory getHardenedSchemaFactory() throws SAXNotSupportedException, SAXNotRecognizedException {
107+
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
108+
schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
109+
schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
110+
return schemaFactory;
111+
}
112+
113+
/**
114+
* Constructs a Validator configured to be secure
115+
*/
116+
@SuppressForbidden(reason = "This is the only allowed way to construct a Validator")
117+
public static Validator getHardenedValidator(Schema schema) throws SAXNotSupportedException, SAXNotRecognizedException {
118+
Validator validator = schema.newValidator();
119+
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
120+
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
121+
return validator;
122+
}
123+
91124
private static class ErrorHandler implements org.xml.sax.ErrorHandler {
92125
/**
93126
* Enabling schema validation with `setValidating(true)` in our

x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/XmlValidator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
package org.elasticsearch.xpack.idp.saml.support;
99

1010
import org.elasticsearch.core.IOUtils;
11+
import org.elasticsearch.core.XmlUtils;
1112
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
1213
import org.w3c.dom.ls.DOMImplementationLS;
1314
import org.w3c.dom.ls.LSInput;
1415
import org.w3c.dom.ls.LSResourceResolver;
16+
import org.xml.sax.SAXNotRecognizedException;
17+
import org.xml.sax.SAXNotSupportedException;
1518

1619
import java.io.ByteArrayInputStream;
1720
import java.io.IOException;
@@ -20,7 +23,6 @@
2023
import java.util.ArrayList;
2124
import java.util.List;
2225

23-
import javax.xml.XMLConstants;
2426
import javax.xml.transform.stream.StreamSource;
2527
import javax.xml.validation.Schema;
2628
import javax.xml.validation.SchemaFactory;
@@ -34,8 +36,8 @@ public class XmlValidator {
3436
private final SchemaFactory schemaFactory;
3537
private final String xsdName;
3638

37-
public XmlValidator(String xsdName) {
38-
this.schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
39+
public XmlValidator(String xsdName) throws SAXNotSupportedException, SAXNotRecognizedException {
40+
this.schemaFactory = XmlUtils.getHardenedSchemaFactory();
3941
this.xsdName = xsdName;
4042
}
4143

@@ -49,9 +51,7 @@ public void validate(InputStream xml) throws Exception {
4951
try (InputStream xsdStream = loadSchema(xsdName); ResourceResolver resolver = new ResourceResolver()) {
5052
schemaFactory.setResourceResolver(resolver);
5153
Schema schema = schemaFactory.newSchema(new StreamSource(xsdStream));
52-
Validator validator = schema.newValidator();
53-
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
54-
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
54+
Validator validator = XmlUtils.getHardenedValidator(schema);
5555
validator.validate(new StreamSource(xml));
5656
}
5757
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,11 @@ static String describeSamlObject(SAMLObject object) {
207207
}
208208

209209
static void validate(InputStream xml, String xsdName) throws Exception {
210-
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
210+
SchemaFactory schemaFactory = XmlUtils.getHardenedSchemaFactory();
211211
try (InputStream xsdStream = loadSchema(xsdName); ResourceResolver resolver = new ResourceResolver()) {
212212
schemaFactory.setResourceResolver(resolver);
213213
Schema schema = schemaFactory.newSchema(new StreamSource(xsdStream));
214-
Validator validator = schema.newValidator();
215-
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
216-
validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
214+
Validator validator = XmlUtils.getHardenedValidator(schema);
217215
validator.validate(new StreamSource(xml));
218216
}
219217
}

0 commit comments

Comments
 (0)