Skip to content

Commit 7691d25

Browse files
committed
Update implementation to use secure transformer
1 parent 72a6bfe commit 7691d25

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

modules/balana-core/src/main/java/org/wso2/balana/ctx/StatusDetail.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ private String nodeToText(Node node) throws ParsingException {
169169

170170
StringWriter sw = new StringWriter();
171171
try {
172-
TransformerFactory factory = TransformerFactory.newInstance();
173-
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
174-
Transformer transformer = factory.newTransformer();
172+
Transformer transformer = Utils.getSecuredTransformerFactory().newTransformer();
175173
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
176174
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
177175
transformer.transform(new DOMSource(node), new StreamResult(sw));

modules/balana-utils/src/main/java/org/wso2/balana/utils/Utils.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import javax.xml.parsers.DocumentBuilderFactory;
3030
import javax.xml.parsers.ParserConfigurationException;
3131
import javax.xml.transform.Transformer;
32+
import javax.xml.transform.TransformerConfigurationException;
3233
import javax.xml.transform.TransformerException;
3334
import javax.xml.transform.TransformerFactory;
35+
import javax.xml.transform.TransformerFactoryConfigurationError;
3436
import javax.xml.transform.dom.DOMSource;
3537
import javax.xml.transform.stream.StreamResult;
3638
import 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

Comments
 (0)