Path: redb-route/redb.Route/Core/XmlLimits.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Core/XmlLimits.cs
----
namespace redb.Route.Core;
///
/// Structure limits for XML read through : how deep, how wide and how large a document may be.
/// The reader checks them while it reads, so a document over a limit is refused before it is built, and whatever
/// comes after the read (canonicalization, signature checks, XPath, XSLT, a deserializer) only ever sees a bounded
/// document, whatever version of the XML libraries the application runs on.
///
/// A null limit is not checked. bounds the structure only, generously enough that no ordinary
/// document meets it; a receiver that knows the shape of what it reads (an AS4 envelope) sets tighter ones.
///
///
public sealed record XmlLimits
{
/// Default : 256 levels of nested elements.
public const int DefaultMaxDepth = 256;
/// Default : 1024, namespace declarations included.
public const int DefaultMaxAttributesPerElement = 1024;
/// Default : 256.
public const int DefaultMaxNamespaceDeclarationsPerElement = 256;
///
/// The limits every loader applies when the caller names none: depth, attributes and
/// namespace declarations per element. Size and element count are not bounded here; the caller that knows a sane
/// size passes one.
///
public static XmlLimits Default { get; } = new();
///
/// Nesting depth of elements, the root counting as 1. Deep nesting is what recursive processing of a document
/// (canonicalization, XPath, a serializer) pays for with its stack. Default .
///
public int? MaxDepth { get; init; } = DefaultMaxDepth;
///
/// Attributes on one element, namespace declarations included. Default .
///
public int? MaxAttributesPerElement { get; init; } = DefaultMaxAttributesPerElement;
///
/// Namespace declarations (xmlns, xmlns:p) on one element: canonicalization works through the
/// namespaces in scope of every element. Default .
///
public int? MaxNamespaceDeclarationsPerElement { get; init; } = DefaultMaxNamespaceDeclarationsPerElement;
/// Namespace declarations in the whole document. Default: not bounded.
public long? MaxNamespaceDeclarations { get; init; }
/// Elements in the whole document. Default: not bounded.
public long? MaxElements { get; init; }
///
/// Characters in the whole document, after entity references are resolved ().
/// Default: not bounded.
///
public long? MaxCharacters { get; init; }
/// Throws when a limit is set to zero or less: such a limit would refuse every document.
public void Validate()
{
Positive(MaxDepth, nameof(MaxDepth));
Positive(MaxAttributesPerElement, nameof(MaxAttributesPerElement));
Positive(MaxNamespaceDeclarationsPerElement, nameof(MaxNamespaceDeclarationsPerElement));
Positive(MaxNamespaceDeclarations, nameof(MaxNamespaceDeclarations));
Positive(MaxElements, nameof(MaxElements));
Positive(MaxCharacters, nameof(MaxCharacters));
}
private static void Positive(long? value, string name)
{
if (value is <= 0)
throw new ArgumentOutOfRangeException(name, value, $"XML limit {name} must be positive when set.");
}
}