SonarQube: 8.9.3.48735
We define the DocumentBuilderFactory as static code and are setting the needed attributes and features. Sonar still reports this as an issue, when the DOC_BUILDER_FACTORY.newDocumentBuilder() is called.
private static final DocumentBuilderFactory DOC_BUILDER_FACTORY;
...
static {
DOC_BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
DOC_BUILDER_FACTORY.setExpandEntityReferences(false);
DOC_BUILDER_FACTORY.setNamespaceAware(true);
DOC_BUILDER_FACTORY.setIgnoringComments(true);
DOC_BUILDER_FACTORY.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
DOC_BUILDER_FACTORY.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
try {
DOC_BUILDER_FACTORY.setFeature(FEATURE_SECURE_PROCESSING, true);
DOC_BUILDER_FACTORY.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
} catch (ParserConfigurationException e) {
throw new IllegalStateException(e);
}
}
...
private String extract(RequestType request) {
...
final Document doc;
try {
final DocumentBuilder builder = DOC_BUILDER_FACTORY.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(iiTypeXML));
doc = builder.parse(is);
} catch (Exception e) {
throw new RuntimeException(e);
}
return doc.getFirstChild().getAttributes().getNamedItem("extension").getTextContent();
}
Do we miss something here? Or is the only solution to mark it as “false positive”?