XML External Entities (XXE)

XML external entities in C#

Description

Prohibit DTDs in untrusted XML. DtdProcessing.Parse allows the parser to process DTDs. Its XmlResolver determines whether it can fetch external resources.

Potential impact

External resolution can expose files or send requests to internal services. Entity expansion can exhaust CPU or memory.

Remediation

Set DtdProcessing.Prohibit and XmlResolver = null on XmlReaderSettings. Pass those settings to XmlReader.Create when opening the XML.

Examples

Before

csharp
var settings = new XmlReaderSettings {
    DtdProcessing = DtdProcessing.Parse,
    XmlResolver = new XmlUrlResolver()
};

After

csharp
var settings = new XmlReaderSettings {
    DtdProcessing = DtdProcessing.Prohibit,
    XmlResolver = null
};

The first configuration supplies an XmlUrlResolver that can resolve external references. Both examples build settings objects, so check that the reader uses the second configuration.

References