*/ private const CUSTOM_ALLOWED = array( 'svg' => array( 'class', 'xmlns', 'width', 'height', 'viewbox', 'aria-hidden', 'role', 'focusable' ), 'path' => array( 'fill', 'fill-rule', 'd', 'transform' ), 'polygon' => array( 'fill', 'fill-rule', 'points', 'transform', 'focusable' ), ); /** * Strictly validates administrator-provided SVG before persistence. * * @param string $svg SVG markup. * @return string|WP_Error Normalized SVG or an error. */ public function sanitize_custom( $svg ) { if ( ! is_string( $svg ) || '' === trim( $svg ) ) { return new WP_Error( 'icon_library_svg_empty', __( 'The SVG file is empty.', 'aculect-icon-library' ) ); } if ( self::MAX_FILE_SIZE < strlen( $svg ) ) { return new WP_Error( 'icon_library_svg_too_large', __( 'SVG files must be 64 KB or smaller.', 'aculect-icon-library' ) ); } // Reject any DOCTYPE/ENTITY declaration outright rather than relying solely on // libxml's default (since PHP 5.4/8+) refusal to expand external entities; a // future PHP or libxml version change should not silently reopen an XXE path here. if ( false !== stripos( $svg, 'loadXML( $svg, LIBXML_NONET | LIBXML_NOBLANKS ); libxml_clear_errors(); libxml_use_internal_errors( $previous ); if ( ! $loaded || ! $document->documentElement instanceof DOMElement || 'svg' !== strtolower( $document->documentElement->tagName ) ) { return new WP_Error( 'icon_library_svg_invalid', __( 'The file is not a valid SVG document.', 'aculect-icon-library' ) ); } // Processing instructions (e.g. an xml-stylesheet declaration) can reference // external resources outside the tag/attribute allow-list below, so they are // blocked here rather than left to be caught by that allow-list. $xpath = new \DOMXPath( $document ); $processing_instructions = $xpath->query( '//processing-instruction()' ); if ( $processing_instructions && $processing_instructions->length > 0 ) { return new WP_Error( 'icon_library_svg_declaration', __( 'SVG processing instructions are not allowed.', 'aculect-icon-library' ) ); } $namespace = $document->documentElement->namespaceURI; if ( $namespace && 'http://www.w3.org/2000/svg' !== $namespace ) { return new WP_Error( 'icon_library_svg_namespace', __( 'The SVG namespace is not supported.', 'aculect-icon-library' ) ); } $document = ( new CustomSvgNormalizer() )->normalize( $document ); if ( is_wp_error( $document ) ) { return $document; } $geometry_count = 0; foreach ( $document->getElementsByTagName( '*' ) as $element ) { $tag = strtolower( $element->localName ? $element->localName : $element->tagName ); if ( ! isset( self::CUSTOM_ALLOWED[ $tag ] ) ) { /* translators: %s: SVG element name. */ return new WP_Error( 'icon_library_svg_element', sprintf( __( 'Unsupported SVG element: %s.', 'aculect-icon-library' ), $tag ) ); } if ( 'path' === $tag || 'polygon' === $tag ) { ++$geometry_count; } foreach ( iterator_to_array( $element->attributes ) as $attribute ) { $name = strtolower( $attribute->localName ? $attribute->localName : $attribute->name ); if ( ! in_array( $name, self::CUSTOM_ALLOWED[ $tag ], true ) ) { /* translators: 1: SVG attribute name, 2: SVG element name. */ return new WP_Error( 'icon_library_svg_attribute', sprintf( __( 'Unsupported SVG attribute: %1$s on %2$s.', 'aculect-icon-library' ), $name, $tag ) ); } if ( $attribute->namespaceURI && 'http://www.w3.org/2000/xmlns/' !== $attribute->namespaceURI ) { return new WP_Error( 'icon_library_svg_namespace', __( 'Namespaced SVG attributes are not supported.', 'aculect-icon-library' ) ); } if ( false !== strpos( $attribute->value, '\\' ) || 1 === preg_match( '/(?:url\s*\(|javascript:|data:|https?:|\/\/)/i', $attribute->value ) ) { return new WP_Error( 'icon_library_svg_reference', __( 'External or executable SVG references are not allowed.', 'aculect-icon-library' ) ); } } } if ( 0 === $geometry_count ) { return new WP_Error( 'icon_library_svg_geometry', __( 'The SVG contains no supported visible geometry.', 'aculect-icon-library' ) ); } $normalized = $document->saveXML( $document->documentElement ); if ( ! is_string( $normalized ) || '' === trim( $normalized ) ) { return new WP_Error( 'icon_library_svg_invalid', __( 'The SVG could not be normalized.', 'aculect-icon-library' ) ); } if ( self::MAX_FILE_SIZE < strlen( $normalized ) ) { return new WP_Error( 'icon_library_svg_too_large', __( 'The normalized SVG must be 64 KB or smaller.', 'aculect-icon-library' ) ); } return trim( $normalized ); } /** * Sanitizes SVG markup. * * @param string $svg SVG markup. * @return string */ public function sanitize( $svg ) { if ( ! is_string( $svg ) || '' === trim( $svg ) ) { return ''; } $svg = preg_replace( '/<\?xml.*?\?>/is', '', $svg ); $svg = preg_replace( '/<\?.*?\?>/s', '', $svg ); $svg = preg_replace( '//is', '', $svg ); $svg = preg_replace( '//s', '', $svg ); $sanitized = wp_kses( $svg, self::get_allowed_svg_tags() ); if ( ! is_string( $sanitized ) || false === stripos( $sanitized, ' true, 'clip-rule' => true, 'cx' => true, 'cy' => true, 'd' => true, 'fill' => true, 'fill-rule' => true, 'height' => true, 'opacity' => true, 'points' => true, 'r' => true, 'rx' => true, 'ry' => true, 'stroke' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, 'stroke-width' => true, 'transform' => true, 'viewbox' => true, 'width' => true, 'x' => true, 'x1' => true, 'x2' => true, 'y' => true, 'y1' => true, 'y2' => true, ); return array( 'svg' => array( 'aria-hidden' => true, 'class' => true, 'clip-rule' => true, 'fill' => true, 'fill-rule' => true, 'focusable' => true, 'height' => true, 'role' => true, 'stroke' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, 'stroke-width' => true, 'viewbox' => true, 'width' => true, 'xmlns' => true, ), 'g' => $common_shape_attributes, 'path' => $common_shape_attributes, 'circle' => $common_shape_attributes, 'ellipse' => $common_shape_attributes, 'line' => $common_shape_attributes, 'polygon' => $common_shape_attributes, 'polyline' => $common_shape_attributes, 'rect' => $common_shape_attributes, ); } }