Allowing external entities in untrusted documents to be processed could lay your systems bare to attackers. Imagine if these entities were parsed:
<!ENTITY xxe SYSTEM "file:///etc/passwd" >]><foo>&xxe;</foo> <!ENTITY xxe SYSTEM "http://www.attacker.com/text.txt" >]><foo>&xxe;</foo>
If you must parse untrusted XML, the best way to protect yourself is to use a local, static DTD during parsing and igore any DTD's included in included in the document.
This rule raises an issue when any of the following are used without first disabling external entity processing: JAXP's
DocumentBuilderFactory, SAXParserFactory, Xerces 1 and Xerces 2 StAX's XMLInputFactory and
XMLReaderFactory.
To disable external entity processing for XMLInputFactory, configure one of the properties
XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES or XMLInputFactory.SUPPORT_DTD to false.
To disable external entity processing for SAXParserFactory, XMLReader or DocumentBuilderFactory configure
one of the properties XMLConstants.FEATURE_SECURE_PROCESSING or "http://apache.org/xml/features/disallow-doctype-decl" to
true.
/* Load XML stream and display content */
String maliciousSample = "xxe.xml";
XMLInputFactory factory = XMLInputFactory.newInstance();
try (FileInputStream fis = new FileInputStream(malicousSample)) {
// Load XML stream
XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(fis); // Noncompliant; reader is vulnerable
//...
/* Load XML stream and display content */
String maliciousSample = "xxe.xml";
XMLInputFactory factory = XMLInputFactory.newInstance();
// disable external entities
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
try (FileInputStream fis = new FileInputStream(malicousSample)) {
// Load XML stream
XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(fis);