I have a few SonarQube vulnerabilities and one of them caught my eye.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse(sIn);
} catch (ParserConfigurationException pce) {
log.error("ERROR-pce***************************"+pce.getMessage(),pce);
throw pce;
} catch (SAXException se) {
log.error("ERROR-se**********************"+se.getMessage(),se);
throw se;
} catch (IOException ioe) {
log.error("ERROR-ioe*********************"+ioe.getMessage(),ioe);
throw ioe;
}
As you can see in my code, I have a new DocumentBuilder(); and then I parse this:
InputStream sIn = new ByteArrayInputStream(contenidoXml.getBytes(StandardCharsets.UTF_8));
The Sonar "solution" is to do one of the following things:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// to be compliant, completely disable DOCTYPE declaration:
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
// or completely disable external entities declarations:
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
// or prohibit the use of all protocols by external entities:
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
This is legacy code and I am quite lost here. Could someone explain me the differences between the three solutions and which one is more possible to have zero impact in the code (we have to update a different classes but last time this was deployed SQ didn't even exist in my company).