1212import org .elasticsearch .logging .LogManager ;
1313import org .elasticsearch .logging .Logger ;
1414import org .xml .sax .SAXException ;
15+ import org .xml .sax .SAXNotRecognizedException ;
16+ import org .xml .sax .SAXNotSupportedException ;
1517import org .xml .sax .SAXParseException ;
1618
1719import javax .xml .XMLConstants ;
2224import javax .xml .transform .TransformerConfigurationException ;
2325import javax .xml .transform .TransformerException ;
2426import javax .xml .transform .TransformerFactory ;
27+ import javax .xml .validation .Schema ;
28+ import javax .xml .validation .SchemaFactory ;
29+ import javax .xml .validation .Validator ;
2530
2631public 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
0 commit comments