Skip to main content
XML has a history of parser attacks: entity expansion (“billion laughs”), external entities (XXE), and documents built to exhaust memory or the stack. XmlEssentials removes the first two by not supporting the features they need, and it bounds the third with limits that are on by default. This page lists the limits, explains what is rejected and why, and says what remains your responsibility. The reader, the document model, and the serializer share one tokenizer, Utf8XmlReader. Everything on this page applies to Utf8XmlReader, XmlDocument, XmlNode.Parse, and XmlSerializer.

DTDs and entities are rejected

XmlEssentials doesn’t process document type definitions (DTDs). A document that contains <!DOCTYPE, or any other <! markup that isn’t a comment or a CDATA section, throws an XmlException with the message DTD is not allowed. The reader stops at the DTD. It doesn’t parse it and doesn’t skip it. That one rule closes two families of attack by construction: There is no setting that turns DTD processing on. With System.Xml, you get this behavior by setting DtdProcessing.Prohibit and a null XmlResolver. Here it is the only behavior. The following sample gives the serializer an entity expansion document and gives the document parser an external entity.
The code produces the following output:
View the complete sample on GitHub The cost is that a legitimate document with a <!DOCTYPE line, such as XHTML or an older configuration file, is rejected too. See XML conformance.

Limits

Every reading API enforces the following limits. A limit property that is 0 uses the default in the table. 0 doesn’t mean “no limit”, except for MaxDocumentByteCount. When the input arrives in blocks, a text node, a CDATA section, an attribute value, a processing instruction or a comment doesn’t have to end before its limit applies. Utf8XmlReader with isFinalBlock set to false, and XmlSerializer.DeserializeAsyncEnumerable, throw as soon as the part that has arrived is longer than the limit, so a sender can’t keep a node open to make you buffer it. The methods that take a whole Stream, such as XmlSerializer.Deserialize(Stream) and XmlDocument.Parse(Stream), read the stream to its end before they parse it. While they read, only MaxDocumentByteCount bounds them. The properties exist on each options type: The writer has no size limits for values, because the values come from your code. Its depth limit also stops the serializer when it writes an object graph that is nested too deeply. A document that exceeds a limit throws an XmlException. The message names the limit and its value: The following sample reads a document that is 65 elements deep with the default limits, and then two documents with limits that the caller lowered.
The code produces the following output:
View the complete sample on GitHub The limits apply to content the serializer skips as well. An element that maps to no property is still read by the tokenizer, so its depth, names, and text are still checked. System.Text.Json has one such limit, MaxDepth, with the same default of 64.

Limit the size of the document

MaxDocumentByteCount is the only limit that is off by default, because the right value depends on your application. Without it, the size of a document is bounded only by what the caller hands the library. Set it whenever you read a stream that someone else controls.
The code produces the following output:
View the complete sample on GitHub When the limit is set, reading from a stream stops as soon as the limit is passed. The rest of the stream isn’t buffered first.

Type names in the document are never loaded

The serializer creates only the types that your code names: the type you pass to Deserialize, the types of its properties, and the derived types you declare with [XmlDerivedType]. A type discriminator in a document, whether it is an element name, an attribute, or xsi:type, is compared with the discriminators you declared. It is never passed to Type.GetType or used to load an assembly. The library contains no call that turns text from a document into a type. A discriminator that doesn’t match a declared one throws an XmlException by default.
Models
The code produces the following output:
View the complete sample on GitHub Two more facts keep the set of types closed:
  • A property of type object is read as XML: an XmlElement, or an XmlNode when XmlSerializerOptions.UnknownTypeHandling is XmlUnknownTypeHandling.XmlNode. The serializer doesn’t choose a type from the document.
  • If you set [XmlPolymorphic(UnknownDerivedTypeHandling = ...)] to a fallback value, an unknown discriminator produces the declared base type. It still doesn’t produce a type that the document chose.
This design matches System.Text.Json, which also requires derived types to be declared. For the attributes and options, see Polymorphism.

Read XML from an untrusted source

Use this list when the document comes from a user, a partner, or the network.
  1. Set MaxDocumentByteCount. Choose a value from the largest document you expect. This is the one limit that is off by default.
  2. Lower the other limits to fit your data. If your documents are never more than 10 levels deep and no value is longer than a few kilobytes, say so in the options. The defaults are ceilings for general use, not a statement about your format.
  3. Start from XmlSerializerOptions.Strict when the format is fixed. The strict preset rejects elements and attributes that map to no property (UnmappedMemberHandling.Disallow), rejects a property that appears twice (AllowDuplicateProperties = false), and rejects comments (ReadCommentHandling = Disallow). The general defaults skip the first, accept the second, and skip the third. See Configure serializer options.
  4. Deserialize into types you wrote for the purpose. Constructors and property setters run with values from the document. Don’t deserialize directly into a type whose setters have side effects.
  5. Validate the values. A well-formed document isn’t a valid one. The library checks XML and the lexical form of each typed value. It doesn’t check XML Schema, ranges, lengths, or business rules. Use required properties for what must be present, and check the rest in your code.
  6. Catch XmlException. Every rejection described on this page is an XmlException. Catch it at the boundary and return an error to the caller.
  7. Pass a CancellationToken to the asynchronous methods. The library has no time limit of its own.
Some exception messages quote text from the document, for example an unknown type discriminator, a declared encoding, or an element name. Treat the message as untrusted input when you log it or show it to a user.

Custom converters

A custom converter runs your code on text from the document, so the guidance above applies to it too.
  • The converter for a type comes from XmlSerializerOptions.Converters or from an [XmlConverter] attribute in your code. A document can’t select or name a converter.
  • A converter that takes a Utf8XmlReader reads through the same tokenizer, so DTD rejection and the limits still apply to what it reads.
  • Don’t use text from the document to choose a type, open a file, build a query, or start a process inside a converter.
  • A converter that calls XmlSerializer again for a nested value should pass the options it was given, so that the caller’s limits stay in force.

Write XML safely

Utf8XmlWriter escapes every value it writes, in text and in attributes, and checks every name. A value that comes from a user can’t close an element or add an attribute. See What the writer escapes. Two APIs turn that protection off: Never pass text from an untrusted source to WriteRawValue, and never use it as an element or attribute name when SkipValidation is true.

What the library doesn’t protect against

  • Total size, unless you set MaxDocumentByteCount. The per-node limits don’t bound the number of nodes. A document made of millions of small elements passes every other limit and produces a large XmlDocument or a large collection.
  • Time. There is no timeout. Reading is linear in the size of the document, so bounding the size bounds the time.
  • Validity. There is no DTD or XML Schema validation.
  • Your converters, constructors, and setters. They run with the document’s values.
  • Confidentiality of the data. The library doesn’t log, doesn’t write files, and doesn’t send telemetry. What your code does with a value or an exception message is up to you.
  • Signatures and encryption. XML Signature and XML Encryption aren’t implemented, and no canonical XML form is produced.

See also

XML conformance

What is read, what is rejected, and every error message.

Configure serializer options

Set limits and the strict preset on XmlSerializerOptions.

Polymorphism

Declare the derived types a document can name.

Thread safety

Which objects you can share between requests.