simplesamlphp /
xml-common
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SimpleSAML\XML; |
||
| 6 | |||
| 7 | use DOMElement; |
||
| 8 | use RuntimeException; |
||
| 9 | use SimpleSAML\XML\Assert\Assert; |
||
| 10 | use SimpleSAML\XML\Chunk; |
||
| 11 | use SimpleSAML\XML\Constants as C; |
||
|
0 ignored issues
–
show
|
|||
| 12 | use SimpleSAML\XML\Registry\ElementRegistry; |
||
| 13 | use SimpleSAML\XMLSchema\XML\Constants\NS; |
||
|
0 ignored issues
–
show
The type
SimpleSAML\XMLSchema\XML\Constants\NS was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 14 | |||
| 15 | use function array_diff; |
||
| 16 | use function array_map; |
||
| 17 | use function array_search; |
||
| 18 | use function defined; |
||
| 19 | use function implode; |
||
| 20 | use function is_array; |
||
| 21 | use function rtrim; |
||
| 22 | use function sprintf; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Trait grouping common functionality for elements implementing the xs:any element. |
||
| 26 | * |
||
| 27 | * @package simplesamlphp/xml-common |
||
| 28 | */ |
||
| 29 | trait ExtendableElementTrait |
||
| 30 | { |
||
| 31 | /** @var \SimpleSAML\XML\SerializableElementInterface[] */ |
||
| 32 | protected array $elements = []; |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Parse an XML document and get the child elements from the specified namespace(s). |
||
| 37 | * The namespace defaults to the XS_ANY_ELT_NAMESPACE constant on the element. |
||
| 38 | * NOTE: In case the namespace is ##any, this method will also return local non-namespaced elements! |
||
| 39 | * |
||
| 40 | * @param \DOMElement $xml |
||
| 41 | * @param string|string[]|null $namespace |
||
| 42 | * @return list<\SimpleSAML\XML\SerializableElementInterface> $elements |
||
| 43 | */ |
||
| 44 | protected static function getChildElementsFromXML( |
||
| 45 | DOMElement $xml, |
||
| 46 | string|array|null $namespace = null, |
||
| 47 | ): array { |
||
| 48 | $namespace = $namespace ?? self::XS_ANY_ELT_NAMESPACE; |
||
| 49 | $exclusionList = self::getElementExclusions(); |
||
| 50 | $registry = ElementRegistry::getInstance(); |
||
| 51 | $elements = []; |
||
| 52 | |||
| 53 | // Validate namespace value |
||
| 54 | if (!is_array($namespace)) { |
||
| 55 | // Must be one of the predefined values |
||
| 56 | Assert::oneOf($namespace, NS::$PREDEFINED); |
||
| 57 | |||
| 58 | foreach ($xml->childNodes as $elt) { |
||
| 59 | if (!($elt instanceof DOMElement)) { |
||
| 60 | continue; |
||
| 61 | } elseif (in_array([$elt->namespaceURI, $elt->localName], $exclusionList, true)) { |
||
| 62 | continue; |
||
| 63 | } elseif ($namespace === NS::OTHER && in_array($elt->namespaceURI, [self::NS, null], true)) { |
||
|
0 ignored issues
–
show
|
|||
| 64 | continue; |
||
| 65 | } elseif ($namespace === NS::TARGETNAMESPACE && $elt->namespaceURI !== self::NS) { |
||
| 66 | continue; |
||
| 67 | } elseif ($namespace === NS::LOCAL && $elt->namespaceURI !== null) { |
||
| 68 | continue; |
||
| 69 | } |
||
| 70 | |||
| 71 | $handler = $registry->getElementHandler($elt->namespaceURI, $elt->localName); |
||
| 72 | $elements[] = ($handler === null) ? Chunk::fromXML($elt) : $handler::fromXML($elt); |
||
| 73 | } |
||
| 74 | } else { |
||
| 75 | // Array must be non-empty and cannot contain ##any or ##other |
||
| 76 | Assert::notEmpty($namespace); |
||
| 77 | Assert::allStringNotEmpty($namespace); |
||
| 78 | Assert::allNotSame($namespace, NS::ANY); |
||
| 79 | Assert::allNotSame($namespace, NS::OTHER); |
||
| 80 | |||
| 81 | // Replace the ##targetedNamespace with the actual namespace |
||
| 82 | if (($key = array_search(NS::TARGETNAMESPACE, $namespace)) !== false) { |
||
| 83 | $namespace[$key] = self::NS; |
||
| 84 | } |
||
| 85 | |||
| 86 | // Replace the ##local with null |
||
| 87 | if (($key = array_search(NS::LOCAL, $namespace)) !== false) { |
||
| 88 | $namespace[$key] = null; |
||
| 89 | } |
||
| 90 | |||
| 91 | foreach ($xml->childNodes as $elt) { |
||
| 92 | if (!($elt instanceof DOMElement)) { |
||
| 93 | continue; |
||
| 94 | } elseif (in_array([$elt->namespaceURI, $elt->localName], $exclusionList, true)) { |
||
| 95 | continue; |
||
| 96 | } elseif (!in_array($elt->namespaceURI, $namespace, true)) { |
||
| 97 | continue; |
||
| 98 | } |
||
| 99 | |||
| 100 | $handler = $registry->getElementHandler($elt->namespaceURI, $elt->localName); |
||
| 101 | $elements[] = ($handler === null) ? Chunk::fromXML($elt) : $handler::fromXML($elt); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return $elements; |
||
| 106 | } |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Set an array with all elements present. |
||
| 111 | * |
||
| 112 | * @param \SimpleSAML\XML\SerializableElementInterface[] $elements |
||
| 113 | */ |
||
| 114 | protected function setElements(array $elements): void |
||
| 115 | { |
||
| 116 | Assert::maxCount($elements, C::UNBOUNDED_LIMIT); |
||
| 117 | Assert::allIsInstanceOf($elements, SerializableElementInterface::class); |
||
| 118 | |||
| 119 | $namespace = $this->getElementNamespace(); |
||
| 120 | // Validate namespace value |
||
| 121 | if (!is_array($namespace)) { |
||
| 122 | // Must be one of the predefined values |
||
| 123 | Assert::oneOf($namespace, NS::$PREDEFINED); |
||
| 124 | } else { |
||
| 125 | // Array must be non-empty and cannot contain ##any or ##other |
||
| 126 | Assert::notEmpty($namespace); |
||
| 127 | Assert::allNotSame($namespace, NS::ANY); |
||
| 128 | Assert::allNotSame($namespace, NS::OTHER); |
||
| 129 | } |
||
| 130 | |||
| 131 | // Get namespaces for all elements |
||
| 132 | /** @var array<\SimpleSAML\XML\AbstractElement|\SimpleSAML\XML\Chunk> $elements */ |
||
| 133 | $actual_namespaces = array_map( |
||
| 134 | /** |
||
| 135 | * @return string|null |
||
| 136 | */ |
||
| 137 | function (AbstractElement|Chunk $elt): ?string { |
||
| 138 | return ($elt instanceof Chunk) ? $elt->getNamespaceURI() : $elt::getNamespaceURI(); |
||
| 139 | }, |
||
| 140 | $elements, |
||
| 141 | ); |
||
| 142 | |||
| 143 | if ($namespace === NS::LOCAL) { |
||
| 144 | // If ##local then all namespaces must be null |
||
| 145 | Assert::allNull($actual_namespaces); |
||
| 146 | } elseif (is_array($namespace)) { |
||
| 147 | // Make a local copy of the property that we can edit |
||
| 148 | $allowed_namespaces = $namespace; |
||
| 149 | |||
| 150 | // Replace the ##targetedNamespace with the actual namespace |
||
| 151 | if (($key = array_search(NS::TARGETNAMESPACE, $allowed_namespaces)) !== false) { |
||
| 152 | $allowed_namespaces[$key] = self::NS; |
||
| 153 | } |
||
| 154 | |||
| 155 | // Replace the ##local with null |
||
| 156 | if (($key = array_search(NS::LOCAL, $allowed_namespaces)) !== false) { |
||
| 157 | $allowed_namespaces[$key] = null; |
||
| 158 | } |
||
| 159 | |||
| 160 | $diff = array_diff($actual_namespaces, $allowed_namespaces); |
||
| 161 | Assert::isEmpty( |
||
| 162 | $diff, |
||
| 163 | sprintf( |
||
| 164 | 'Elements from namespaces [ %s ] are not allowed inside a %s element.', |
||
| 165 | rtrim(implode(', ', $diff)), |
||
| 166 | self::NS, |
||
| 167 | ), |
||
| 168 | ); |
||
| 169 | } elseif ($namespace === NS::OTHER) { |
||
| 170 | // Must be any namespace other than the parent element, excluding elements with no namespace |
||
| 171 | Assert::notInArray(null, $actual_namespaces); |
||
| 172 | Assert::allNotSame($actual_namespaces, self::NS); |
||
| 173 | } elseif ($namespace === NS::TARGETNAMESPACE) { |
||
| 174 | // Must be the same namespace as the one of the parent element |
||
| 175 | Assert::allSame($actual_namespaces, self::NS); |
||
| 176 | } else { |
||
| 177 | // XS_ANY_NS_ANY |
||
| 178 | } |
||
| 179 | |||
| 180 | $exclusionList = self::getElementExclusions(); |
||
| 181 | foreach ($elements as $i => $elt) { |
||
| 182 | if (in_array([$elt->getNamespaceURI(), $elt->getLocalName()], $exclusionList, true)) { |
||
| 183 | unset($elements[$i]); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | $this->elements = $elements; |
||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Get an array with all elements present. |
||
| 193 | * |
||
| 194 | * @return \SimpleSAML\XML\SerializableElementInterface[] |
||
| 195 | */ |
||
| 196 | public function getElements(): array |
||
| 197 | { |
||
| 198 | return $this->elements; |
||
| 199 | } |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * @return string|string[] |
||
| 204 | */ |
||
| 205 | public function getElementNamespace(): array|string |
||
| 206 | { |
||
| 207 | Assert::true( |
||
| 208 | defined('self::XS_ANY_ELT_NAMESPACE'), |
||
| 209 | self::getClassName(self::class) |
||
| 210 | . '::XS_ANY_ELT_NAMESPACE constant must be defined and set to the namespace for the xs:any element.', |
||
| 211 | RuntimeException::class, |
||
| 212 | ); |
||
| 213 | |||
| 214 | return self::XS_ANY_ELT_NAMESPACE; |
||
| 215 | } |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the exclusions list for getChildElementsFromXML. |
||
| 220 | * |
||
| 221 | * @return array{array{string|null, string}}|array{} |
||
|
0 ignored issues
–
show
|
|||
| 222 | */ |
||
| 223 | public static function getElementExclusions(): array |
||
| 224 | { |
||
| 225 | if (defined('self::XS_ANY_ELT_EXCLUSIONS')) { |
||
| 226 | return self::XS_ANY_ELT_EXCLUSIONS; |
||
| 227 | } |
||
| 228 | |||
| 229 | return []; |
||
| 230 | } |
||
| 231 | } |
||
| 232 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths