Path: redb-route/redb.Route/Core/LimitedXmlReader.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/LimitedXmlReader.cs ---- using System.Xml; using System.Xml.Schema; namespace redb.Route.Core; /// /// An over another one that checks on every element it reads and /// throws at the first one over a limit. Everything else is the inner reader's: /// the wrapper adds no buffering, so a loader on top builds no more of a hostile document than up to the offending /// element. Line information passes through for LoadOptions.SetLineInfo. /// internal sealed class LimitedXmlReader : XmlReader, IXmlLineInfo, IXmlNamespaceResolver { private const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/"; private readonly XmlReader _inner; private readonly XmlLimits _limits; private long _elements; private long _namespaceDeclarations; internal LimitedXmlReader(XmlReader inner, XmlLimits limits) { _inner = inner; _limits = limits; } /// public override bool Read() { if (!_inner.Read()) return false; if (_inner.NodeType == XmlNodeType.Element) CheckElement(); return true; } private void CheckElement() { var depth = _inner.Depth + 1; if (_limits.MaxDepth is { } maxDepth && depth > maxDepth) throw Over(nameof(XmlLimits.MaxDepth), maxDepth, $"Element '{_inner.Name}' is nested {depth} levels deep"); if (_limits.MaxElements is { } maxElements && ++_elements > maxElements) throw Over(nameof(XmlLimits.MaxElements), maxElements, $"The document has more than {maxElements} elements"); var attributes = _inner.AttributeCount; if (_limits.MaxAttributesPerElement is { } maxAttributes && attributes > maxAttributes) throw Over(nameof(XmlLimits.MaxAttributesPerElement), maxAttributes, $"Element '{_inner.Name}' has {attributes} attributes"); if (attributes == 0 || (_limits.MaxNamespaceDeclarationsPerElement is null && _limits.MaxNamespaceDeclarations is null)) return; var declarations = 0; for (var i = 0; i < attributes; i++) { _inner.MoveToAttribute(i); if (_inner.NamespaceURI == XmlnsNamespace) declarations++; } _inner.MoveToElement(); if (_limits.MaxNamespaceDeclarationsPerElement is { } maxPerElement && declarations > maxPerElement) throw Over(nameof(XmlLimits.MaxNamespaceDeclarationsPerElement), maxPerElement, $"Element '{_inner.Name}' declares {declarations} namespaces"); _namespaceDeclarations += declarations; if (_limits.MaxNamespaceDeclarations is { } maxTotal && _namespaceDeclarations > maxTotal) throw Over(nameof(XmlLimits.MaxNamespaceDeclarations), maxTotal, $"The document declares more than {maxTotal} namespaces"); } private XmlLimitException Over(string limit, long maximum, string what) { var line = _inner as IXmlLineInfo; var (number, position) = line is { } info && info.HasLineInfo() ? (info.LineNumber, info.LinePosition) : (0, 0); return new XmlLimitException(limit, maximum, $"{what}: over the XML limit {limit} = {maximum}.", number, position); } // ── Everything else is the inner reader's ─────────────────────────────── /// public override XmlNodeType NodeType => _inner.NodeType; /// public override string LocalName => _inner.LocalName; /// public override string NamespaceURI => _inner.NamespaceURI; /// public override string Prefix => _inner.Prefix; /// public override string Name => _inner.Name; /// public override bool HasValue => _inner.HasValue; /// public override string Value => _inner.Value; /// public override int Depth => _inner.Depth; /// public override string BaseURI => _inner.BaseURI; /// public override bool IsEmptyElement => _inner.IsEmptyElement; /// public override bool IsDefault => _inner.IsDefault; /// public override char QuoteChar => _inner.QuoteChar; /// public override XmlSpace XmlSpace => _inner.XmlSpace; /// public override string XmlLang => _inner.XmlLang; /// public override IXmlSchemaInfo? SchemaInfo => _inner.SchemaInfo; /// public override Type ValueType => _inner.ValueType; /// public override int AttributeCount => _inner.AttributeCount; /// public override bool EOF => _inner.EOF; /// public override ReadState ReadState => _inner.ReadState; /// public override XmlNameTable NameTable => _inner.NameTable; /// public override XmlReaderSettings? Settings => _inner.Settings; /// public override bool CanResolveEntity => _inner.CanResolveEntity; /// public override bool CanReadBinaryContent => _inner.CanReadBinaryContent; /// public override bool CanReadValueChunk => _inner.CanReadValueChunk; /// public override bool HasAttributes => _inner.HasAttributes; /// public override string? GetAttribute(string name) => _inner.GetAttribute(name); /// public override string? GetAttribute(string name, string? namespaceURI) => _inner.GetAttribute(name, namespaceURI); /// public override string GetAttribute(int i) => _inner.GetAttribute(i); /// public override bool MoveToAttribute(string name) => _inner.MoveToAttribute(name); /// public override bool MoveToAttribute(string name, string? ns) => _inner.MoveToAttribute(name, ns); /// public override void MoveToAttribute(int i) => _inner.MoveToAttribute(i); /// public override bool MoveToFirstAttribute() => _inner.MoveToFirstAttribute(); /// public override bool MoveToNextAttribute() => _inner.MoveToNextAttribute(); /// public override bool MoveToElement() => _inner.MoveToElement(); /// public override bool ReadAttributeValue() => _inner.ReadAttributeValue(); /// public override string? LookupNamespace(string prefix) => _inner.LookupNamespace(prefix); /// public override void ResolveEntity() => _inner.ResolveEntity(); /// public override int ReadValueChunk(char[] buffer, int index, int count) => _inner.ReadValueChunk(buffer, index, count); /// public override void Close() => _inner.Close(); /// protected override void Dispose(bool disposing) { if (disposing) _inner.Dispose(); base.Dispose(disposing); } // ── IXmlLineInfo, IXmlNamespaceResolver ───────────────────────────────── bool IXmlLineInfo.HasLineInfo() => _inner is IXmlLineInfo info && info.HasLineInfo(); int IXmlLineInfo.LineNumber => (_inner as IXmlLineInfo)?.LineNumber ?? 0; int IXmlLineInfo.LinePosition => (_inner as IXmlLineInfo)?.LinePosition ?? 0; IDictionary IXmlNamespaceResolver.GetNamespacesInScope(XmlNamespaceScope scope) => (_inner as IXmlNamespaceResolver)?.GetNamespacesInScope(scope) ?? new Dictionary(); string? IXmlNamespaceResolver.LookupNamespace(string prefix) => _inner.LookupNamespace(prefix); string? IXmlNamespaceResolver.LookupPrefix(string namespaceName) => (_inner as IXmlNamespaceResolver)?.LookupPrefix(namespaceName); }