Class SafeXml
Reads XML that came from somewhere else. Every loader here goes through an
with and no resolver, which is the only shape in .NET that
refuses a document type declaration.
The direct loaders do not. Measured on .NET 10: a 452-byte document whose DTD declares nested
internal entities expands to 300 000 characters through ,
through and through
alike — LINQ to XML is no safer than the old DOM here. Setting XmlResolver = null does not
help either: it closes external entities, while the expansion is driven by internal ones (the
"billion laughs" shape of an XXE attack). One small message can therefore cost the worker
gigabytes, which is a denial of service against every route sharing the process.
A DTD is refused rather than expanded with a budget: SOAP forbids a document type declaration in
an envelope outright, and a payload that needs one is rare enough to be an explicit decision
rather than a default.
Every loader also checks the structure of the document while it reads (): depth,
attributes and namespace declarations per element, and where the caller asks, size, element count and
namespace declarations in the whole document. A document over a limit is refused with
before it is built, so whatever reads it next gets a bounded input. The loaders
without an argument apply , with maxCharacters as
the size bound.
public static class SafeXml
Inheritance
Methods
Check(byte[], XmlLimits)
Reads xml to the end without building anything, and throws
public static void Check(byte[] xml, XmlLimits limits)
CreateReader(Stream, XmlLimits)
A reader over stream that refuses a DTD and checks limits.
public static XmlReader CreateReader(Stream stream, XmlLimits limits)
CreateReader(TextReader, XmlLimits)
A reader over text that refuses a DTD and checks limits.
public static XmlReader CreateReader(TextReader text, XmlLimits limits)
Load(byte[], LoadOptions, long?)
Loads a document from bytes, within and maxCharacters.
public static XDocument Load(byte[] xml, LoadOptions options = LoadOptions.None, long? maxCharacters = null)
Load(byte[], XmlLimits, LoadOptions)
Loads a document from bytes within limits.
public static XDocument Load(byte[] xml, XmlLimits limits, LoadOptions options = LoadOptions.None)
Load(Stream, LoadOptions, long?)
Loads a document from a stream, within and maxCharacters.
public static XDocument Load(Stream stream, LoadOptions options = LoadOptions.None, long? maxCharacters = null)
Load(Stream, XmlLimits, LoadOptions)
Loads a document from a stream within limits.
public static XDocument Load(Stream stream, XmlLimits limits, LoadOptions options = LoadOptions.None)
LoadDocument(byte[], bool, long?)
Loads an , the shape System.Security.Cryptography.Xml needs for signatures and
public static XmlDocument LoadDocument(byte[] xml, bool preserveWhitespace = true, long? maxCharacters = null)
LoadDocument(byte[], XmlLimits, bool)
Loads an from bytes within limits; whitespace as in the other overload.
public static XmlDocument LoadDocument(byte[] xml, XmlLimits limits, bool preserveWhitespace = true)
LoadDocument(Stream, XmlLimits, bool)
Loads an from a stream within limits; whitespace as in the other overload.
public static XmlDocument LoadDocument(Stream stream, XmlLimits limits, bool preserveWhitespace = true)
LoadFile(string, LoadOptions, long?)
Loads a document from a file path, within and maxCharacters.
public static XDocument LoadFile(string path, LoadOptions options = LoadOptions.None, long? maxCharacters = null)
LoadFile(string, XmlLimits, LoadOptions)
Loads a document from a file path within limits.
public static XDocument LoadFile(string path, XmlLimits limits, LoadOptions options = LoadOptions.None)
Parse(string, LoadOptions, long?)
Parses a document from text, within and maxCharacters.
public static XDocument Parse(string xml, LoadOptions options = LoadOptions.None, long? maxCharacters = null)
Parse(string, XmlLimits, LoadOptions)
Parses a document from text within limits.
public static XDocument Parse(string xml, XmlLimits limits, LoadOptions options = LoadOptions.None)
ParseElement(string, LoadOptions, long?)
Parses a single element from text, within and maxCharacters.
public static XElement ParseElement(string xml, LoadOptions options = LoadOptions.None, long? maxCharacters = null)
ParseElement(string, XmlLimits, LoadOptions)
Parses a single element from text within limits.
public static XElement ParseElement(string xml, XmlLimits limits, LoadOptions options = LoadOptions.None)
Settings(long?)
Reader settings that refuse a DTD and never resolve anything external. They do not check the structure: a
public static XmlReaderSettings Settings(long? maxCharacters = null)