From aa38259a7c9531b40cb87ad4c82c9d74fe6914c2 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Fri, 27 Nov 2026 17:37:21 +0100 Subject: [PATCH 1/3] Add XMLLimit-service and matching assertion wrapper --- src/XML/Assert/Assert.php | 4 + src/XML/Assert/SafeXMLTrait.php | 31 ++++ .../Exception/StructuralLimitException.php | 17 +++ src/XML/ExtendableAttributesTrait.php | 1 + src/XML/ExtendableElementTrait.php | 1 + src/XML/Limit/XMLLimit.php | 132 ++++++++++++++++++ src/XML/Limit/XMLLimitConfig.php | 101 ++++++++++++++ 7 files changed, 287 insertions(+) create mode 100644 src/XML/Assert/SafeXMLTrait.php create mode 100644 src/XML/Exception/StructuralLimitException.php create mode 100644 src/XML/Limit/XMLLimit.php create mode 100644 src/XML/Limit/XMLLimitConfig.php diff --git a/src/XML/Assert/Assert.php b/src/XML/Assert/Assert.php index df190ce7..4192b8a5 100644 --- a/src/XML/Assert/Assert.php +++ b/src/XML/Assert/Assert.php @@ -10,6 +10,7 @@ * @package simplesamlphp/xml-common * * @phpcs:ignore Generic.Files.LineLength.TooLong + * @method static void assertSafeXML(\Dom\Element $value, string $message = '', string $exception = ''); * @method static void validAllowedXPathFilter(mixed $value, string[] $allowed_axes, string[] $allowed_functions, string $message = '', string $exception = '') * @method static void validAnyURI(mixed $value, string $message = '', string $exception = '') * @method static void validBase64Binary(mixed $value, string $message = '', string $exception = '') @@ -54,6 +55,7 @@ * @method static void validYear(mixed $value, string $message = '', string $exception = '') * @method static void validYearMonth(mixed $value, string $message = '', string $exception = '') * @phpcs:ignore Generic.Files.LineLength.TooLong + * @method static void nullOrAssertSafeXML(mixed $value, string $message = '', string $exception = ''); * @method static void nullOrValidAllowedXPathFilter(mixed $value, string[] $allowed_axes, string[] $allowed_functions, string $message = '', string $exception = '') * @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidBase64Binary(mixed $value, string $message = '', string $exception = '') @@ -98,6 +100,7 @@ * @method static void nullOrValidYear(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '') * @phpcs:ignore Generic.Files.LineLength.TooLong + * @method static void allAssertSafeXML(\Dom\Element[] $value, string $message = '', string $exception = ''); * @method static void allValidAllowedXPathFilter(mixed $value, string[] $allowed_axes, string[] $allowed_functions, string $message = '', string $exception = '') * @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '') * @method static void allValidBase64Binary(mixed $value, string $message = '', string $exception = '') @@ -176,6 +179,7 @@ class Assert extends BaseAssert use NormalizedStringTrait; use PositiveIntegerTrait; use QNameTrait; + use SafeXMLTrait; use ShortTrait; use StringTrait; use TimeTrait; diff --git a/src/XML/Assert/SafeXMLTrait.php b/src/XML/Assert/SafeXMLTrait.php new file mode 100644 index 00000000..14f93527 --- /dev/null +++ b/src/XML/Assert/SafeXMLTrait.php @@ -0,0 +1,31 @@ +getMessage()); + } + } +} diff --git a/src/XML/Exception/StructuralLimitException.php b/src/XML/Exception/StructuralLimitException.php new file mode 100644 index 00000000..bf91efac --- /dev/null +++ b/src/XML/Exception/StructuralLimitException.php @@ -0,0 +1,17 @@ + */ + private static ?\WeakMap $checked = null; + + + public static function setDefault(?XMLLimitConfig $config): void + { + self::$config = $config; + } + + + public static function getDefault(): XMLLimitConfig + { + return self::$config ?? XMLLimitConfig::getHardDefaults(); + } + + + public static function assertSafeXML( + Dom\Element $root, + ?XMLLimitConfig $config = null, + ): void { + self::$checked ??= new WeakMap(); + + $doc = $root->ownerDocument; + if (isset(self::$checked[$doc])) { + return; // already validated this document + } + + $config ??= self::getDefault(); + + $nodeCount = 0; + $maxObservedDepth = 0; + $maxObservedNsInScope = 0; + $maxNodes = $config->maxNode; + $maxAttributes = $config->maxAttribute; + $maxNsDecls = $config->maxNsDecl; + $maxDepth = $config->maxDepth; + $maxNsInScope = $config->maxNsInScope; + + $walker = function ( + Dom\Node $node, + int $depth, + array $inScopeNs, + ) use ( + &$walker, + &$nodeCount, + &$maxObservedDepth, + &$maxObservedNsInScope, + $maxNodes, + $maxAttributes, + $maxNsDecls, + $maxDepth, + $maxNsInScope, + ): void { + if (++$nodeCount > $maxNodes) { + throw new StructuralLimitException( + sprintf('XML structural limit exceeded: more than %d nodes', $maxNodes), + ); + } + if ($depth > $maxDepth) { + throw new StructuralLimitException( + sprintf('XML structural limit exceeded: nesting depth > %d', $maxDepth), + ); + } + $maxObservedDepth = max($maxObservedDepth, $depth); + + if ($node->nodeType === XML_ELEMENT_NODE) { + /** @var \Dom\Element $node */ + $attrCount = 0; + $nsDeclCount = 0; + $localNs = $inScopeNs; + + foreach ($node->attributes ?? [] as $attr) { + $attrCount++; + if ($attrCount > $maxAttributes) { + throw new StructuralLimitException( + sprintf('XML structural limit exceeded: element has > %d attributes', $maxAttributes), + ); + } + // xmlns or xmlns:prefix + if ($attr->namespaceURI === C::NS_XMLNS || str_starts_with($attr->name, 'xmlns')) { + $nsDeclCount++; + if ($nsDeclCount > $maxNsDecls) { + throw new StructuralLimitException(sprintf( + 'XML structural limit exceeded: > %d namespace declarations on one element', + $maxNsDecls, + )); + } + $prefix = $attr->prefix === 'xmlns' ? $attr->localName : ''; + $localNs[$prefix] = $attr->value; + } + } + + $nsInScope = count($localNs); + if ($nsInScope > $maxNsInScope) { + throw new StructuralLimitException(sprintf( + 'XML structural limit exceeded: > %d namespaces in scope', + $maxNsInScope, + )); + } + $maxObservedNsInScope = max($maxObservedNsInScope, $nsInScope); + + foreach ($node->childNodes as $child) { + $walker($child, $depth + 1, $localNs); + } + } + }; + + $walker($root, 1, []); + + self::$checked[$doc] = true; + } +} diff --git a/src/XML/Limit/XMLLimitConfig.php b/src/XML/Limit/XMLLimitConfig.php new file mode 100644 index 00000000..345cc9cd --- /dev/null +++ b/src/XML/Limit/XMLLimitConfig.php @@ -0,0 +1,101 @@ + Date: Fri, 27 Nov 2026 17:54:18 +0100 Subject: [PATCH 2/3] Deprecate UNBOUNDED_LIMIT constant --- src/XML/Constants.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/XML/Constants.php b/src/XML/Constants.php index df5a8fb1..e0abb040 100644 --- a/src/XML/Constants.php +++ b/src/XML/Constants.php @@ -24,6 +24,8 @@ class Constants /** * The maximum amount of child nodes this library is willing to handle. * By specification, this limit is 150K, but that opens up for denial of service. + * + * @deprecated Use limits defined in XMLLimit instead. Will be removed in a future release */ public const int UNBOUNDED_LIMIT = 10000; } From 7b1191f74696dd707c44f397195e5c5dc9c1ac0b Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Fri, 27 Nov 2026 17:59:05 +0100 Subject: [PATCH 3/3] Replace UNBOUNDED_LIMIT with XMLLimit-defaults --- src/XML/ExtendableAttributesTrait.php | 2 +- src/XML/ExtendableElementTrait.php | 3 +-- tests/bootstrap.php | 4 ++++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/XML/ExtendableAttributesTrait.php b/src/XML/ExtendableAttributesTrait.php index f22c13e0..fe3e986e 100644 --- a/src/XML/ExtendableAttributesTrait.php +++ b/src/XML/ExtendableAttributesTrait.php @@ -181,7 +181,7 @@ protected static function getAttributesNSFromXML( */ protected function setAttributesNS(array $attributes): void { - Assert::maxCount($attributes, C::UNBOUNDED_LIMIT); + Assert::maxCount($attributes, XMLLimit::getDefault()->maxAttribute); Assert::allIsInstanceOf( $attributes, Attribute::class, diff --git a/src/XML/ExtendableElementTrait.php b/src/XML/ExtendableElementTrait.php index 934a1d7f..b9b679d5 100644 --- a/src/XML/ExtendableElementTrait.php +++ b/src/XML/ExtendableElementTrait.php @@ -8,7 +8,6 @@ use RuntimeException; use SimpleSAML\XML\Assert\Assert; use SimpleSAML\XML\Chunk; -use SimpleSAML\XML\Constants as C; use SimpleSAML\XML\Limit\XMLLimit; use SimpleSAML\XML\Registry\ElementRegistry; use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; @@ -121,7 +120,7 @@ protected static function getChildElementsFromXML( */ protected function setElements(array $elements): void { - Assert::maxCount($elements, C::UNBOUNDED_LIMIT); + Assert::maxCount($elements, XMLLimit::getDefault()->maxNode); Assert::allIsInstanceOf($elements, SerializableElementInterface::class); $namespace = $this->getElementNamespace(); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9dd866e7..28a7eeba 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -4,6 +4,8 @@ use SimpleSAML\Test\Container\TestContainer; use SimpleSAML\XML\Container\TestContainerSingleton; +use SimpleSAML\XML\Limit\XMLLimit; +use SimpleSAML\XML\Limit\XMLLimitConfig; use SimpleSAML\XML\Registry\ElementRegistry; $projectRoot = dirname(__DIR__); @@ -14,3 +16,5 @@ $testContainer = new TestContainer(); TestContainerSingleton::setContainer($testContainer); + +XMLLimit::setDefault(XMLLimitConfig::getHardDefaults());