XML External Entities attacks benefit from an XML feature to build documents dynamically at the time of processing. An XML entity allows to include data dynamically from a given resource. External entities allow an XML document to include data from an external URI. Unless configured to do otherwise, external entities force the XML parser to access the resource specified by the URI, e.g., a file on the local machine or on a remote systems. This behavior exposes the application to XML External Entity (XXE) attacks, which can be used to perform denial of service of the local system, gain unauthorized access to files on the local machine, scan remote machines, and perform denial of service of remote systems.
The following XML document shows an example of an XXE attack:
------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY >
<!ENTITY xxe SYSTEM "file:///c:/winnt/win.ini" >]>&xxe;
This example could disclose the contents of the C:\winnt\win.ini system file, if the XML parser attempts to substitute the entity with the contents of the file.
Recommendation:
----------------------------------------------------------
An XML parser should be configured securely so that it does not allow external entities as part of an incoming XML document.
The best way to prevent XXE attacks is to disable XML entity resolution by disabling inline DTD setting DtdProcessing to DtdProcessing.Prohibit or by disabling XML Entity resolution setting the XmlReaderSettings.XmlResolver property to null:
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Prohibit;
settings.XmlResolver = null;
XmlReader reader = XmlReader.Create(stream, settings);
If external entities must be processed in your application you should create a custom XmlResolver with the following features:
1. Set a request timeout to prevent infinite delay attacks
2. Limit the amount of data that it will retrieve
3. Restrict the XmlResolver from retrieving resources on the local host
Note:
------------------------
set XMLConstants.FEATURE_SECURE_PROCESSING on the TransformerFactory class. Additionaly you can point to to xalan and javas own implementation of TrasnformerFactoryImpl implementation class to research more for any solution for your problem.
3