| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace SimpleSAML\XML; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use DOMDocument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use DOMElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use DOMXPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SimpleSAML\XML\Assert\Assert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SimpleSAML\XML\Exception\IOException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SimpleSAML\XML\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SimpleSAML\XML\Exception\UnparseableXMLException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use function count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function file_get_contents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use function func_num_args; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use function libxml_clear_errors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use function libxml_set_external_entity_loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use function libxml_use_internal_errors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use function sprintf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use function strpos; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use function substr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @package simplesamlphp/xml-common | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | final class DOMDocumentFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var non-negative-int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * TODO: Add LIBXML_NO_XXE to the defaults when PHP 8.4.0 + libxml 2.13.0 become generally available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public const DEFAULT_OPTIONS = \LIBXML_COMPACT | \LIBXML_NONET | \LIBXML_NSCLEAN; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param string $xml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param non-negative-int $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return \DOMDocument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public static function fromString( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         string $xml, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         int $options = self::DEFAULT_OPTIONS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     ): DOMDocument { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         libxml_set_external_entity_loader(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         Assert::notWhitespaceOnly($xml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         Assert::notRegex( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $xml, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             '/<(\s*)!(\s*)DOCTYPE/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             'Dangerous XML detected, DOCTYPE nodes are not allowed in the XML body', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             RuntimeException::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $internalErrors = libxml_use_internal_errors(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         libxml_clear_errors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         // If LIBXML_NO_XXE is available and option not set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (func_num_args() === 1 && defined('LIBXML_NO_XXE')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $options |= \LIBXML_NO_XXE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $domDocument = self::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $loaded = $domDocument->loadXML($xml, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         libxml_use_internal_errors($internalErrors); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (!$loaded) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $error = libxml_get_last_error(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             libxml_clear_errors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             throw new UnparseableXMLException($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         libxml_clear_errors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         foreach ($domDocument->childNodes as $child) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             Assert::false( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $child->nodeType === \XML_DOCUMENT_TYPE_NODE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 'Dangerous XML detected, DOCTYPE nodes are not allowed in the XML body', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 RuntimeException::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return $domDocument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param string $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param non-negative-int $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return \DOMDocument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public static function fromFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         string $file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         int $options = self::DEFAULT_OPTIONS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     ): DOMDocument { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         error_clear_last(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $xml = @file_get_contents($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if ($xml === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $e = error_get_last(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $error = $e['message'] ?? "Check that the file exists and can be read."; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             throw new IOException("File '$file' was not loaded;  $error"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         Assert::notWhitespaceOnly($xml, sprintf('File "%s" does not have content', $file), RuntimeException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return (func_num_args() < 2) ? static::fromString($xml) : static::fromString($xml, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param string $version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param string $encoding | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @return \DOMDocument | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     public static function create(string $version = '1.0', string $encoding = 'UTF-8'): DOMDocument | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         return new DOMDocument($version, $encoding); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public static function normalizeDocument(DOMDocument $doc): DOMDocument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         // Get the root element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $root = $doc->documentElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         // Collect all xmlns attributes from the document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $xpath = new DOMXPath($doc); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $xmlnsAttributes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         // Register all namespaces to ensure XPath can handle them | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         foreach ($xpath->query('//namespace::*') as $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $name = $node->nodeName === 'xmlns' ? 'xmlns' : $node->nodeName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             $xmlnsAttributes[$name] = $node->nodeValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         // If no xmlns attributes found, return early with debug info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         if (empty($xmlnsAttributes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             return $doc->saveXML(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         // Remove xmlns attributes from all elements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $nodes = $xpath->query('//*[namespace::*]'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         foreach ($nodes as $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             $attributesToRemove = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             foreach ($node->attributes as $attr) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 if (strpos($attr->nodeName, 'xmlns') === 0 || $attr->nodeName === 'xmlns') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                     $attributesToRemove[] = $attr->nodeName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             foreach ($attributesToRemove as $attrName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                 $node->removeAttribute($attrName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         // Add all collected xmlns attributes to the root element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         foreach ($xmlnsAttributes as $name => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             $root->setAttribute($name, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         // Return the normalized XML | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         return static::fromString($root->ownerDocument->saveXML()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |  |