We get java:S2755 “Disable access to external entities in XML parsing” reported on TransformerFactory.newInstance() although we pass it to a setDefaultAttributes method, which sets the recommended properties on it to prevent XXE attacks.
Our code looks like this:
public static TransformerFactory newInstance() {
return setDefaultAttributes(TransformerFactory.newInstance());
}
private static TransformerFactory setDefaultAttributes(TransformerFactory factory) {
setOptionalAttribute(factory, XMLConstants.ACCESS_EXTERNAL_DTD, "");
setOptionalAttribute(factory, XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
setOptionalFeature(factory, FeatureKeys.ALLOW_EXTERNAL_FUNCTIONS, false);
return factory;
}
private static void setOptionalAttribute(TransformerFactory factory, String name, Object value) {
try {
factory.setAttribute(name, value);
} catch (IllegalArgumentException e) {
if (logger.isDebugEnabled()) {
logger.debug("{} property not supported by {}", name, factory.getClass().getCanonicalName());
}
}
}
private static void setOptionalFeature(TransformerFactory factory, String name, boolean value) {
try {
factory.setFeature(name, value);
} catch (IllegalArgumentException | TransformerConfigurationException e) {
if (logger.isDebugEnabled()) {
logger.debug("{} feature not supported by {}", name, factory.getClass().getCanonicalName());
}
}
}
Is the SonarQube Java scanner able to not report this false positive?
We use SonarQube Developer Edition Version 8.6 and Java Code Quality and Security plugin: 6.9.0.23563
and investigating. The reason for the optional setting is that we had multiple TransformerFactory implementations deployed, but none supported all these attribute or feature settings. I agree that the debug log message is not wise, so I headed to at least change it to a warning log. However then I found that the TransformerFactory implementation we mainly use just recently released a support for all these settings (