2424import org .apache .xerces .impl .Constants ;
2525import org .w3c .dom .Document ;
2626
27+ import javax .xml .XMLConstants ;
2728import javax .xml .parsers .DocumentBuilder ;
2829import javax .xml .parsers .DocumentBuilderFactory ;
2930import javax .xml .parsers .ParserConfigurationException ;
3031import javax .xml .transform .Transformer ;
31- import javax .xml .transform .TransformerConfigurationException ;
3232import javax .xml .transform .TransformerException ;
3333import javax .xml .transform .TransformerFactory ;
3434import javax .xml .transform .dom .DOMSource ;
3535import javax .xml .transform .stream .StreamResult ;
3636import java .io .StringWriter ;
3737
38- import static javax .xml .XMLConstants .ACCESS_EXTERNAL_DTD ;
39- import static javax .xml .XMLConstants .ACCESS_EXTERNAL_STYLESHEET ;
40- import static javax .xml .XMLConstants .FEATURE_SECURE_PROCESSING ;
41-
4238/**
4339 *
4440 */
@@ -68,7 +64,7 @@ public static String getStringFromDocument(Document doc) throws TransformerExcep
6864 if (transformerFactoryClassName == null ) {
6965 transformerFactoryClassName = "org.apache.xalan.processor.TransformerFactoryImpl" ;
7066 }
71- TransformerFactory transformerFactory = getSecuredTransformerFactory (transformerFactoryClassName );
67+ TransformerFactory transformerFactory = TransformerFactory . newInstance (transformerFactoryClassName , null );
7268 Transformer transformer = transformerFactory .newTransformer ();
7369 transformer .transform (domSource , result );
7470 return writer .toString ().substring (writer .toString ().indexOf ('>' ) + 1 );
@@ -105,7 +101,7 @@ public static DocumentBuilderFactory getSecuredDocumentBuilderFactory() {
105101 dbf .setFeature (Constants .SAX_FEATURE_PREFIX + Constants .EXTERNAL_GENERAL_ENTITIES_FEATURE , false );
106102 dbf .setFeature (Constants .SAX_FEATURE_PREFIX + Constants .EXTERNAL_PARAMETER_ENTITIES_FEATURE , false );
107103 dbf .setFeature (Constants .XERCES_FEATURE_PREFIX + Constants .LOAD_EXTERNAL_DTD_FEATURE , false );
108- dbf .setFeature (FEATURE_SECURE_PROCESSING , true );
104+ dbf .setFeature (XMLConstants . FEATURE_SECURE_PROCESSING , true );
109105 } catch (ParserConfigurationException e ) {
110106 logger .error (
111107 "Failed to load XML Processor Feature " + Constants .EXTERNAL_GENERAL_ENTITIES_FEATURE + " or " +
@@ -118,29 +114,8 @@ public static DocumentBuilderFactory getSecuredDocumentBuilderFactory() {
118114
119115 return dbf ;
120116 }
121-
122- /**
123- * Create TransformerFactory with the XXE prevention measurements
124- * https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#transformerfactory
125- *
126- * @param transformerFactoryClassName String
127- * @return TransformerFactory
128- */
129- public static TransformerFactory getSecuredTransformerFactory (String transformerFactoryClassName ) {
130- TransformerFactory trfactory = TransformerFactory .
131- newInstance (transformerFactoryClassName , null );
132-
133- try {
134- trfactory .setFeature (FEATURE_SECURE_PROCESSING , true );
135- } catch (TransformerConfigurationException e ) {
136- logger .error ("Failed to load XML Processor " +
137- "Feature http://javax.xml.XMLConstants/feature/secure-processing for secure-processing." );
138- }
139- trfactory .setAttribute (ACCESS_EXTERNAL_DTD , "" );
140- trfactory .setAttribute (ACCESS_EXTERNAL_STYLESHEET , "" );
141- return trfactory ;
142- }
143-
117+
118+
144119// public static Element createElement(String xmlInput) {
145120//
146121// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
0 commit comments