I got vulnerabilities flaws from the scan report for Java code, did some research, and found this recommendation to resolve such issues:
Improper Restriction of XML External Entity Reference (CWE ID 611)
This is the code including the fix for the XXE Attack issue:
public static String convertNodeToString(Node node) {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
try {
tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
transformer = tf.newTransformer();
// below code to remove XML declaration
// transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(writer));
String output = writer.getBuffer().toString();
return output;
} catch (TransformerException e) {
e.printStackTrace();
}
return null;
}
The good thing is that JUnit testing was a success, but, when I deployed the code on a running instance, I got this error:
java.lang.IllegalArgumentException: Not supported: http://javax.xml.XMLConstants/property/accessExternalDTD
As per my experience, this is because the running instance uses some dependencies which caused such a conflict and resulted in this error.
Following is part of the stack trace form the console:
java.lang.IllegalArgumentException: Not supported: http://javax.xml.XMLConstants/property/accessExternalDTD at org.apache.xalan.processor.TransformerFactoryImpl.setAttribute(TransformerFactoryImpl.java:571)
How I can find which dependency is causing the such error? Is there anything I can do to resolve such an error? I am also suspecting that I missed including a dependency. Please help me solve this issue.
Edit 1:
I did further research and I think this happens because of this reference in the java.exe command used to launch the actual instance:
java.exe -Xbootclasspath/p:../lib/xalan.jar;../lib/xercesImpl.jar;...
Now, I need to find out how I can overcome this issue. I came across some articles proposing to ensure the creation of the factory instance using the correct package. I think the above code ends up using the wrong package.
The question now is how to use java code to ensure using the correct package to create the TransformerFactory
instance.
Edit 2:
The first answer helped me make some progress. I found that the classpath of the deployed instance has a reference to org.apache.xalan.processor.TransformerFactoryImpl
in xalan.jar
which seems it is used by TransformerFactory.newInstance()
to create the transformer factory. I think the question is how I can make the needed changes to ensure using the proper class to create the transformer.
Edit 3:
I followed the recommendation here and added this code:
TransformerFactory factory = TransformerFactory.newInstance();
factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
The error was resolved in the running instance, but, the scan tool is still reporting this vulnerability flaw XXE Attack
. According to this article, this happens because an outdated XML processor is present on the classpath (e.g. Xerces, Xalan) which is exactly my case.
I think I came across an article recommending changing some system properties that will indicate the factory to create the transformer instance using the correct class. I am trying to find this article now.
I appreciate your help.