Fixing XXE injection with XmlDocument
The XmlDocument
class has been the de facto XML parser for .NET applications. This XML parser object is often used to load, modify, and delete XML in-memory. It has an XmlResolver
property, which enables the use of external XML resources such as DTDs.
Document Type Definition, most commonly known as DTD, is similar to XML files but holds information about an XML's composition or structure. It can have an ENTITY
element, which can be internal or external. When an XDocument
parses an XML file with a DTD, this XML parser will process it, along with its ENTITY
declarations.
Let's look at some content of an XML file with malicious injected ENTITY
declarations. This is a known classic example of the Billion Laughs attack, which is a Denial-of-Service (DoS) attack that targets XML parsers such as XmlDocument
. Loading this XML will cause your ASP.NET Core web app to crash or become unresponsive:
<?xml version="1.0"?>...