Path: redb-route/tests/redb.Route.Tests/Core/SafeXmlTests.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/tests/redb.Route.Tests/Core/SafeXmlTests.cs
----
using System.Text;
using System.Xml;
using System.Xml.Linq;
using AwesomeAssertions;
using redb.Route.Core;
namespace redb.Route.Tests.Core;
///
/// XML that came from somewhere else is read through , which refuses a document
/// type declaration. The direct loaders expand one: measured on .NET 10, the 452-byte document below
/// becomes 300 000 characters through XmlDocument.Load, XDocument.Load and
/// XDocument.Parse alike, and XmlResolver = null changes nothing, because the expansion
/// runs on internal entities. Scaled up, one small message costs the worker gigabytes and takes every
/// route in the process down with it.
///
public class SafeXmlTests
{
/// Nested internal entities — the "billion laughs" shape, kept small enough to be a test.
private static string BillionLaughs(int levels = 5, int fanOut = 10)
{
var sb = new StringBuilder("");
for (var i = 1; i <= levels; i++)
{
sb.Append($"");
}
return sb.Append($"]>&lol{levels};").ToString();
}
private static byte[] Bytes(string xml) => Encoding.UTF8.GetBytes(xml);
[Fact]
public void The_direct_loaders_do_expand_entities_which_is_why_this_type_exists()
{
var xml = BillionLaughs();
// Not a test of our code: a guard on the premise. If a future runtime starts refusing a DTD
// here, this fails and the next reader of SafeXml learns the reason it was written is gone.
XDocument.Parse(xml).Root!.Value.Length.Should().BeGreaterThan(100_000);
var dom = new XmlDocument { XmlResolver = null };
dom.Load(new MemoryStream(Bytes(xml)));
dom.DocumentElement!.InnerText.Length.Should().BeGreaterThan(100_000);
}
[Fact]
public void Every_loader_refuses_a_document_type_declaration()
{
var xml = BillionLaughs();
var bytes = Bytes(xml);
var loaders = new List<(string Name, Action Act)>
{
("Load(byte[])", () => SafeXml.Load(bytes)),
("Load(Stream)", () => SafeXml.Load(new MemoryStream(bytes))),
("Parse(string)", () => SafeXml.Parse(xml)),
("ParseElement(string)", () => SafeXml.ParseElement(xml)),
("LoadDocument(byte[])", () => SafeXml.LoadDocument(bytes)),
};
foreach (var (name, act) in loaders)
act.Should().Throw($"{name} must refuse a DTD instead of expanding it");
}
[Fact]
public void A_dtd_without_entities_is_refused_just_the_same()
{
// The refusal is of the declaration, not of what it happens to contain: a DTD that looks
// harmless today is still an entity expansion away from not being one.
const string xml = "";
var act = () => SafeXml.Parse(xml);
act.Should().Throw();
}
[Fact]
public void Ordinary_documents_read_exactly_as_before()
{
const string xml = """
- Ящик
""";
var doc = SafeXml.Parse(xml);
var ns = XNamespace.Get("urn:acme");
doc.Root!.Name.Should().Be(ns + "order");
doc.Root.Attribute("id")!.Value.Should().Be("7");
doc.Root.Element(ns + "item")!.Value.Should().Be("Ящик");
doc.Root.Element(ns + "note")!.Value.Should().Be("a < b & c");
SafeXml.Load(Bytes(xml)).Root!.Name.Should().Be(ns + "order");
SafeXml.LoadDocument(Bytes(xml)).DocumentElement!.LocalName.Should().Be("order");
SafeXml.ParseElement("").Name.LocalName.Should().Be("a");
}
[Fact]
public void Line_info_and_whitespace_survive_the_reader()
{
// Two properties other call sites depend on: markup errors are reported by position, and a
// signed document must keep its whitespace or canonicalization fails.
var doc = SafeXml.Parse("\n \n", LoadOptions.SetLineInfo | LoadOptions.PreserveWhitespace);
((IXmlLineInfo)doc.Root!.Element("b")!).LineNumber.Should().Be(2);
doc.Root.ToString(SaveOptions.DisableFormatting).Should().Contain("\n ");
SafeXml.LoadDocument(Bytes(" ")).PreserveWhitespace.Should().BeTrue();
}
[Fact]
public void A_document_over_the_size_bound_is_refused()
{
var big = "" + new string('x', 5_000) + "";
var act = () => SafeXml.Parse(big, maxCharacters: 1_000);
act.Should().Throw("a caller that knows a sane size can cap it");
}
// ── Structure limits ─────────────────────────────────────────────────────
private static string Nested(int levels) =>
string.Concat(Enumerable.Repeat("", levels)) + string.Concat(Enumerable.Repeat("", levels));
private static string Attributes(int count, string prefix = "a") =>
" prefix == "xmlns" ? $"xmlns:p{i}=\"urn:{i}\"" : $"a{i}=\"v\"")) + "/>";
public static TheoryData OverALimit() => new()
{
{ nameof(XmlLimits.MaxDepth), Nested(XmlLimits.DefaultMaxDepth + 1), XmlLimits.Default },
{ nameof(XmlLimits.MaxAttributesPerElement), Attributes(XmlLimits.DefaultMaxAttributesPerElement + 1), XmlLimits.Default },
{ nameof(XmlLimits.MaxNamespaceDeclarationsPerElement), Attributes(XmlLimits.DefaultMaxNamespaceDeclarationsPerElement + 1, "xmlns"), XmlLimits.Default },
{ nameof(XmlLimits.MaxNamespaceDeclarations), "" + string.Concat(Enumerable.Range(0, 11).Select(i => $"")) + "",
XmlLimits.Default with { MaxNamespaceDeclarations = 10 } },
{ nameof(XmlLimits.MaxElements), "" + string.Concat(Enumerable.Repeat("", 10)) + "", XmlLimits.Default with { MaxElements = 10 } },
};
[Theory]
[MemberData(nameof(OverALimit))]
public void Every_loader_refuses_a_document_over_a_limit_and_names_it(string limit, string xml, XmlLimits limits)
{
var bytes = Bytes(xml);
var loaders = new List<(string Name, Action Act)>
{
("Load(byte[])", () => SafeXml.Load(bytes, limits)),
("Load(Stream)", () => SafeXml.Load(new MemoryStream(bytes), limits)),
("Parse(string)", () => SafeXml.Parse(xml, limits)),
("ParseElement(string)", () => SafeXml.ParseElement(xml, limits)),
("LoadDocument(byte[])", () => SafeXml.LoadDocument(bytes, limits)),
("Check(byte[])", () => SafeXml.Check(bytes, limits)),
};
foreach (var (name, act) in loaders)
act.Should().Throw(name).Which.Limit.Should().Be(limit, name);
}
[Fact]
public void The_loaders_without_limits_apply_the_default_ones()
{
// Existing call sites (XPath over a message body, SOAP, route markup) get the structure limits without a change.
var deep = Nested(XmlLimits.DefaultMaxDepth + 1);
((Action)(() => SafeXml.Parse(deep))).Should().Throw().Which.Limit.Should().Be(nameof(XmlLimits.MaxDepth));
((Action)(() => SafeXml.LoadDocument(Bytes(deep)))).Should().Throw();
((Action)(() => SafeXml.Load(Bytes(deep), maxCharacters: 10_000_000))).Should().Throw();
}
[Fact]
public void A_document_at_every_limit_reads()
{
SafeXml.Parse(Nested(XmlLimits.DefaultMaxDepth)).Descendants("x").Should().HaveCount(XmlLimits.DefaultMaxDepth);
SafeXml.Parse(Attributes(XmlLimits.DefaultMaxAttributesPerElement)).Root!.Attributes().Should().HaveCount(XmlLimits.DefaultMaxAttributesPerElement);
SafeXml.Parse(Attributes(XmlLimits.DefaultMaxNamespaceDeclarationsPerElement, "xmlns")).Root.Should().NotBeNull();
SafeXml.Parse("", XmlLimits.Default with { MaxElements = 3, MaxNamespaceDeclarations = 1 }).Root!.Elements().Should().HaveCount(2);
}
[Fact]
public void A_limit_violation_is_an_xml_exception_with_its_position()
{
// Callers that answer malformed XML as the sender's fault (SOAP, AS4) answer this the same way, with the place.
var act = () => SafeXml.Parse("\n \n \n", XmlLimits.Default with { MaxElements = 2 });
var thrown = act.Should().Throw().Which;
thrown.Should().BeAssignableTo();
thrown.LineNumber.Should().Be(3);
thrown.Maximum.Should().Be(2);
thrown.Message.Should().Contain("MaxElements");
}
[Fact]
public void A_deep_document_is_refused_at_the_limit_not_after_reading_it_all()
{
// 200 000 levels: refused at level 257, so the time does not grow with the document.
var watch = System.Diagnostics.Stopwatch.StartNew();
((Action)(() => SafeXml.Parse(Nested(200_000)))).Should().Throw();
watch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(2));
}
[Fact]
public void Line_info_and_whitespace_survive_the_limited_reader()
{
var doc = SafeXml.Parse("\n \n", XmlLimits.Default, LoadOptions.SetLineInfo | LoadOptions.PreserveWhitespace);
((IXmlLineInfo)doc.Root!.Element("b")!).LineNumber.Should().Be(2);
doc.Root.ToString(SaveOptions.DisableFormatting).Should().Contain("\n ");
}
[Fact]
public void A_limit_of_zero_is_refused_as_a_setting()
{
var act = () => SafeXml.Parse("", XmlLimits.Default with { MaxDepth = 0 });
act.Should().Throw().Which.ParamName.Should().Be(nameof(XmlLimits.MaxDepth));
}
}