Enabling DTD Processing
Setting DtdProcessing.Parse which allows external entity expansion.
XML external entity (XXE) vulnerabilities occur when XML parsers process external entities in user-controlled input without proper configuration. In .NET applications using XmlReaderSettings with DtdProcessing.Parse enabled, attackers can exploit XXE to read local files, perform Server-Side Request Forgery (SSRF), or cause denial of service through entity expansion attacks.
using System.Xml;
public XmlDocument LoadXml(string userInput)
{
// VULNERABLE: DTD processing enabled with default resolver
var settings = new XmlReaderSettings
{
DtdProcessing = DtdProcessing.Parse,
XmlResolver = new XmlUrlResolver()
};
// This can process malicious XML with external entities
using (var reader = XmlReader.Create(userInput, settings))
{
var doc = new XmlDocument();
doc.Load(reader);
return doc;
}
}
// Attack payload:
// <!DOCTYPE root [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>
// <root><data>&xxe;</data></root>using System.Xml;
public XmlDocument LoadXml(string userInput)
{
// SECURE: DTD processing disabled and resolver set to null
var settings = new XmlReaderSettings
{
DtdProcessing = DtdProcessing.Prohibit,
XmlResolver = null
};
// Safe XML processing without external entity risks
using (var reader = XmlReader.Create(userInput, settings))
{
var doc = new XmlDocument();
doc.Load(reader);
return doc;
}
}The vulnerable code enables DTD processing with DtdProcessing.Parse and uses XmlUrlResolver, allowing attackers to exploit XXE to read files or perform SSRF. The secure version sets DtdProcessing.Prohibit and XmlResolver = null, preventing external entity processing entirely.
Setting DtdProcessing.Parse which allows external entity expansion.
Sourcery automatically identifies xml external entity (xxe) via xmlreadersettings and many other security issues in your codebase.