Gets or sets a value indicating the maximum allowable number of characters in a document that result from expanding entities.
Assembly: System.Xml (in System.Xml.dll)
'Declaration Public Property MaxCharactersFromEntities As Long 'Usage Dim instance As XmlReaderSettings Dim value As Long value = instance.MaxCharactersFromEntities instance.MaxCharactersFromEntities = value
Property ValueType: System.Int64
The maximum allowable number of characters from expanded entities. The default is 0.
A zero (0) value means no limits on the number of characters that result from expanding entities. A non-zero value specifies the maximum number of characters that can result from expanding entities.
If the reader attempts to read a document that contains entities such that the expanded size will exceed this property, an XmlException will be thrown.
This property allows you to mitigate denial of service attacks where the attacker submits XML documents that attempt to exceed memory limits via expanding entities. By limiting the characters that result from expanded entities, you can detect the attack and recover reliably.
The following code sets this property, and then attempts to parse a document that contains an entity that expands to a size greater than the set limit. In a real world scenario, you would set this limit to a value large enough to handle valid documents, yet small enough to limit the threat from malicious documents.
Dim markup As String = _ "<!DOCTYPE Root [" + Environment.NewLine + _ " <!ENTITY anEntity ""Expands to more than 30 characters"">" + Environment.NewLine + _ " <!ELEMENT Root (#PCDATA)>" + Environment.NewLine + _ "]>" + Environment.NewLine + _ "<Root>Content &anEntity;</Root>" Dim settings As XmlReaderSettings = New XmlReaderSettings() settings.ProhibitDtd = False settings.ValidationType = ValidationType.DTD settings.MaxCharactersFromEntities = 30 Try Dim reader As XmlReader = XmlReader.Create(New StringReader(markup), settings) While (reader.Read()) End While Catch ex As XmlException Console.WriteLine(ex.Message) End Try
This example produces the following output:
There is an error in XML document (MaxCharactersFromEntities, ).
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.