2929import javax .xml .parsers .DocumentBuilderFactory ;
3030import javax .xml .parsers .ParserConfigurationException ;
3131import javax .xml .transform .Transformer ;
32+ import javax .xml .transform .TransformerConfigurationException ;
3233import javax .xml .transform .TransformerException ;
3334import javax .xml .transform .TransformerFactory ;
35+ import javax .xml .transform .TransformerFactoryConfigurationError ;
3436import javax .xml .transform .dom .DOMSource ;
3537import javax .xml .transform .stream .StreamResult ;
3638import java .io .StringWriter ;
@@ -50,6 +52,10 @@ public class Utils {
5052 */
5153 private static final int ENTITY_EXPANSION_LIMIT = 0 ;
5254
55+ //Secured transformer factory implementation
56+ private static String JAVAX_TRANSFORMER_PROP_VAL =
57+ "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl" ;
58+
5359 /**
5460 * Convert Document element to a String object
5561 * @param doc Document element
@@ -127,4 +133,34 @@ public static DocumentBuilderFactory getSecuredDocumentBuilderFactory() {
127133// StreamResult result = new StreamResult(new StringWriter());
128134// transformer.transform(source, result);
129135// }
136+
137+ /**
138+ * Create a secure process enabled TransformerFactory.
139+ *
140+ * @return Secured TransformerFactory which is stricly implemented via
141+ * com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
142+ */
143+ public static TransformerFactory getSecuredTransformerFactory () {
144+
145+ TransformerFactory transformerFactory ;
146+ try {
147+ // Prevent XXE Attack by ensure using the correct factory class to create TrasformerFactory instance.
148+ // This will instruct Java to use the version which supports using ACCESS_EXTERNAL_DTD argument.
149+ transformerFactory = TransformerFactory .newInstance (JAVAX_TRANSFORMER_PROP_VAL , null );
150+ } catch (TransformerFactoryConfigurationError e ) {
151+ logger .error ("Failed to load default TransformerFactory" , e );
152+ // This part uses the default implementation of xalan.
153+ transformerFactory = TransformerFactory .newInstance ();
154+ }
155+
156+ try {
157+ transformerFactory .setFeature (XMLConstants .FEATURE_SECURE_PROCESSING , true );
158+ } catch (TransformerConfigurationException e ) {
159+ logger .error ("Failed to load XML Processor Feature " + XMLConstants .FEATURE_SECURE_PROCESSING +
160+ " for secure-processing." );
161+ }
162+ transformerFactory .setAttribute (XMLConstants .ACCESS_EXTERNAL_DTD , "" );
163+ transformerFactory .setAttribute (XMLConstants .ACCESS_EXTERNAL_STYLESHEET , "" );
164+ return transformerFactory ;
165+ }
130166}
0 commit comments