@@ -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" )
0 commit comments