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.
<!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 is0 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.
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.
Type names in the document are never loaded
The serializer creates only the types that your code names: the type you pass toDeserialize, 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
- A property of type
objectis read as XML: anXmlElement, or anXmlNodewhenXmlSerializerOptions.UnknownTypeHandlingisXmlUnknownTypeHandling.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.
Read XML from an untrusted source
Use this list when the document comes from a user, a partner, or the network.- Set
MaxDocumentByteCount. Choose a value from the largest document you expect. This is the one limit that is off by default. - 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.
- Start from
XmlSerializerOptions.Strictwhen 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. - 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.
- 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.
- Catch
XmlException. Every rejection described on this page is anXmlException. Catch it at the boundary and return an error to the caller. - Pass a
CancellationTokento the asynchronous methods. The library has no time limit of its own.
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.Convertersor from an[XmlConverter]attribute in your code. A document can’t select or name a converter. - A converter that takes a
Utf8XmlReaderreads 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
XmlSerializeragain 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 largeXmlDocumentor 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.