` (HTML inside * SVG), links to `javascript:` or other sites, external images, CSS * `@import` / `url()` to other sites, comments, and processing * instructions. Files declaring entities (`\[]*(\[[^\]]*\])?\s*>/i', '', $svg ); $previous = libxml_use_internal_errors( true ); $dom = new DOMDocument(); $loaded = $dom->loadXML( $svg, LIBXML_NONET | LIBXML_COMPACT | LIBXML_PARSEHUGE ); libxml_clear_errors(); libxml_use_internal_errors( $previous ); $root = $dom->documentElement; if ( ! $loaded || ! $root instanceof DOMElement || 'svg' !== strtolower( $root->localName ) ) { return null; } self::clean_children( $dom ); self::clean_element( $root ); // Make sure the root declares the SVG namespace, so browsers // render it as SVG. if ( ! $root->hasAttribute( 'xmlns' ) && self::SVG_NS !== $root->namespaceURI ) { $root->setAttribute( 'xmlns', self::SVG_NS ); } $clean = $dom->saveXML( $root ); return is_string( $clean ) && '' !== $clean ? '' . "\n" . $clean . "\n" : null; } /** * Remove comments, processing instructions, and disallowed elements * below a node, recursively. * * @param DOMNode $node Node. * @return void */ private static function clean_children( DOMNode $node ): void { // Iterate over a copy: removing nodes while walking a live list // would skip siblings. foreach ( iterator_to_array( $node->childNodes ) as $child ) { if ( XML_COMMENT_NODE === $child->nodeType || XML_PI_NODE === $child->nodeType ) { $node->removeChild( $child ); continue; } if ( ! $child instanceof DOMElement ) { continue; } if ( ! self::element_allowed( $child ) ) { $node->removeChild( $child ); continue; } self::clean_element( $child ); } } /** * Whether an element is on the allowlist. * * @param DOMElement $element Element. * @return bool */ private static function element_allowed( DOMElement $element ): bool { $ns = (string) $element->namespaceURI; if ( '' !== $ns && self::SVG_NS !== $ns ) { return false; } return in_array( strtolower( $element->localName ), self::ELEMENTS, true ); } /** * Clean one allowed element's attributes, then its children. * * @param DOMElement $element Element. * @return void */ private static function clean_element( DOMElement $element ): void { $tag = strtolower( $element->localName ); foreach ( iterator_to_array( $element->attributes ) as $attribute ) { if ( ! $attribute instanceof DOMAttr ) { continue; } $name = strtolower( $attribute->localName ); $ns = (string) $attribute->namespaceURI; $value = $attribute->value; $keep = in_array( $ns, self::ATTRIBUTE_NS, true ) && in_array( $name, self::ATTRIBUTES, true ) && ! str_starts_with( $name, 'on' ); if ( $keep && 'href' === $name ) { $keep = self::href_allowed( $tag, $value ); } if ( $keep && 'style' === $name ) { $value = self::clean_css( $value ); $attribute->value = $value; } // Presentation attributes may reference other files: url(…). if ( $keep && 'style' !== $name && str_contains( strtolower( $value ), 'url(' ) ) { $keep = self::urls_local( $value ); } if ( ! $keep ) { $element->removeAttributeNode( $attribute ); } } // Namespace declarations other than SVG / xlink (e.g. Inkscape's) // can only be removed by their prefix. $xpath = new \DOMXPath( $element->ownerDocument ); $declarations = $xpath->query( 'namespace::*', $element ); foreach ( false !== $declarations ? $declarations : array() as $declaration ) { $prefix = (string) $declaration->prefix; if ( '' !== $prefix && 'xml' !== $prefix && 'xlink' !== $prefix && ! in_array( $declaration->nodeValue, array( self::SVG_NS, 'http://www.w3.org/1999/xlink' ), true ) ) { $element->removeAttributeNS( (string) $declaration->nodeValue, $prefix ); } } if ( 'style' === $tag ) { $element->textContent = self::clean_css( $element->textContent ); return; } self::clean_children( $element ); } /** * Whether a link is safe: in-document references everywhere, web and * mail links on ``, and embedded raster images on ``. * * @param string $tag Element name. * @param string $value Link. * @return bool */ private static function href_allowed( string $tag, string $value ): bool { // Browsers ignore whitespace and control characters inside URL // schemes ("java\nscript:"), so compare without them. $value = strtolower( (string) preg_replace( '/[\x00-\x20]+/', '', $value ) ); if ( '' === $value || str_starts_with( $value, '#' ) ) { return true; } if ( 'a' === $tag ) { return (bool) preg_match( '#^(https?:|mailto:|/)#', $value ); } if ( 'image' === $tag ) { return (bool) preg_match( '#^data:image/(png|jpe?g|gif|webp);#', $value ); } return false; } /** * Whether every url(…) in a value points inside the document or at an * embedded raster image. * * @param string $value Value. * @return bool */ private static function urls_local( string $value ): bool { preg_match_all( '/url\(\s*([\'"]?)(.*?)\1\s*\)/is', $value, $matches ); foreach ( $matches[2] as $url ) { if ( ! self::href_allowed( 'image', (string) $url ) ) { return false; } } return true; } /** * The UTF-8 character for a CSS escape's hex code point; empty for an * invalid one (0, a surrogate, or beyond U+10FFFF). * * @param string $hex Hex digits, e.g. "40". * @return string */ private static function code_point( string $hex ): string { $entity = '&#x' . $hex . ';'; $char = html_entity_decode( $entity, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); return ( $entity === $char || "\u{FFFD}" === $char ) ? '' : $char; } /** * Clean CSS (a `style` attribute or `