Language: Java 11 on Android API 31
Rule: Disable access to external entities in XML parsing.
Why: Because not working setFeature with DocumentBuilderFactory on Android.
Using: Enterprise Edition - Version 9.9.4 with sonarqube server
Reproduce: Yes reproduced is simple. All Android platform can report this error
My code simply:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setExpandEntityReferences(false);
factory.setXIncludeAware(false);
factory.setNamespaceAware(true);
DocumentBuilder docBuilder= factory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(xmlStream));
If i using (one rule or all rules) this security command i took ParsingException. I don’t want only using try catch. Because sonar can’t report error but not secured.
factory.setFeature(“http://apache.org/xml/features/disallow-doctype-decl”, true);
factory.setFeature(“http://xml.org/sax/features/external-general-entities”, false);
factory.setFeature(“http://xml.org/sax/features/external-parameter-entities”, false);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Can i take your advice? What is your solution?