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/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; } 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 @@ +maxAttribute); Assert::allIsInstanceOf( $attributes, Attribute::class, diff --git a/src/XML/ExtendableElementTrait.php b/src/XML/ExtendableElementTrait.php index 30211c2e..b9b679d5 100644 --- a/src/XML/ExtendableElementTrait.php +++ b/src/XML/ExtendableElementTrait.php @@ -8,7 +8,7 @@ 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; use SimpleSAML\XMLSchema\Exception\SchemaViolationException; @@ -120,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/src/XML/Limit/XMLLimit.php b/src/XML/Limit/XMLLimit.php new file mode 100644 index 00000000..8b75c5ac --- /dev/null +++ b/src/XML/Limit/XMLLimit.php @@ -0,0 +1,132 @@ + */ + 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 @@ +