From ab98a833571551cdb6356c041ffc132441360801 Mon Sep 17 00:00:00 2001 From: mgiannopoulos24 <79588074+mgiannopoulos24@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:42:38 +0200 Subject: [PATCH 1/3] Editor: Support file: paths for block icons in block.json Backport of https://github.com/WordPress/gutenberg/pull/79658 --- src/wp-includes/blocks.php | 164 ++++++++++++++++++ src/wp-includes/default-filters.php | 1 + .../phpunit/data/blocks/icon-file/block.json | 9 + tests/phpunit/data/blocks/icon-file/icon.svg | 1 + .../data/blocks/icon-missing/block.json | 9 + .../data/blocks/icon-object/block.json | 13 ++ .../phpunit/data/blocks/icon-object/icon.svg | 1 + .../tests/blocks/wpResolveBlockIconPath.php | 96 ++++++++++ 8 files changed, 294 insertions(+) create mode 100644 tests/phpunit/data/blocks/icon-file/block.json create mode 100644 tests/phpunit/data/blocks/icon-file/icon.svg create mode 100644 tests/phpunit/data/blocks/icon-missing/block.json create mode 100644 tests/phpunit/data/blocks/icon-object/block.json create mode 100644 tests/phpunit/data/blocks/icon-object/icon.svg create mode 100644 tests/phpunit/tests/blocks/wpResolveBlockIconPath.php diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 309f1db97ac97..6f171ae4db558 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -3353,3 +3353,167 @@ function _wp_enqueue_auto_register_blocks() { ); } } + +/** + * Returns the allowed SVG tags and attributes for icon sanitization. + * + * @since 7.3.0 + * + * @return array> Associative array of allowed SVG elements and their attributes. + */ +function wp_get_allowed_svg_icon_tags() { + // wp_kses() matches lowercase names against these keys. + $svg_attributes = array( + 'xmlns' => true, + 'viewbox' => true, + 'preserveaspectratio' => true, + 'width' => true, + 'height' => true, + 'fill' => true, + 'fill-opacity' => true, + 'stroke' => true, + 'stroke-width' => true, + 'stroke-linecap' => true, + 'stroke-linejoin' => true, + 'opacity' => true, + 'transform' => true, + 'cx' => true, + 'cy' => true, + 'r' => true, + 'rx' => true, + 'ry' => true, + 'x' => true, + 'y' => true, + 'd' => true, + 'clip-rule' => true, + 'fill-rule' => true, + 'points' => true, + 'type' => true, + 'id' => true, + 'offset' => true, + 'stop-color' => true, + 'stop-opacity' => true, + 'gradientunits' => true, + 'gradienttransform' => true, + 'x1' => true, + 'y1' => true, + 'x2' => true, + 'y2' => true, + ); + + $allowed_tags = array( + 'svg' => $svg_attributes, + 'path' => $svg_attributes, + 'circle' => $svg_attributes, + 'rect' => $svg_attributes, + 'line' => $svg_attributes, + 'polyline' => $svg_attributes, + 'polygon' => $svg_attributes, + 'ellipse' => $svg_attributes, + 'g' => $svg_attributes, + 'defs' => $svg_attributes, + 'use' => $svg_attributes, + 'stop' => $svg_attributes, + 'lineargradient' => $svg_attributes, + 'radialgradient' => $svg_attributes, + 'title' => $svg_attributes, + 'desc' => $svg_attributes, + 'symbol' => $svg_attributes, + 'clippath' => $svg_attributes, + 'mask' => $svg_attributes, + 'filter' => $svg_attributes, + 'fegaussianblur' => $svg_attributes, + 'fecolormatrix' => $svg_attributes, + 'feblend' => $svg_attributes, + 'fecomposite' => $svg_attributes, + 'feflood' => $svg_attributes, + 'femerge' => $svg_attributes, + 'femergenode' => $svg_attributes, + 'feoffset' => $svg_attributes, + 'feimage' => $svg_attributes, + ); + + return $allowed_tags; +} + +/** + * Sanitizes SVG content for safe use as a block icon. + * + * Strips dangerous elements and attributes (scripts, event handlers, etc.) + * while preserving valid SVG structure. + * + * @since 7.3.0 + * + * @param string $svg_content Raw SVG content. + * @return string Sanitized SVG content. + */ +function wp_sanitize_svg_icon( $svg_content ) { + $svg_element = wp_kses( + $svg_content, + wp_get_allowed_svg_icon_tags() + ); + + // wp_kses() lowercases names, but SVG names are case-sensitive. + $svg_elements = array( 'linearGradient', 'radialGradient', 'clipPath', 'feGaussianBlur', 'feColorMatrix', 'feBlend', 'feComposite', 'feFlood', 'feMerge', 'feMergeNode', 'feOffset', 'feImage' ); + $svg_attributes = array( 'viewBox', 'preserveAspectRatio', 'gradientUnits', 'gradientTransform' ); + foreach ( $svg_elements as $name ) { + $svg_element = preg_replace( '#(])#', '$1' . $name, $svg_element ); + } + foreach ( $svg_attributes as $name ) { + $svg_element = preg_replace( '/(\s)' . strtolower( $name ) . '(?==")/', '$1' . $name, $svg_element ); + } + + // Remove XML declaration if present. + $svg_element = preg_replace( '/^<\?xml[^>]*\?>\s*/', '', $svg_element ); + + return trim( $svg_element ); +} + +/** + * Resolves file: prefix for the icon in block.json metadata. + * + * Allows block authors to use a file path (e.g., "file:./assets/icon.svg") + * instead of an inline SVG or Dashicon slug for the block icon. + * + * @since 7.3.0 + * + * @param array $metadata Block metadata loaded from block.json. + * @return array Filtered metadata with resolved icon. + */ +function wp_resolve_block_icon_path( $metadata ) { + if ( ! isset( $metadata['icon'] ) || ! isset( $metadata['file'] ) ) { + return $metadata; + } + + $resolve_svg_path = function ( $path ) use ( $metadata ) { + $resolved_path = remove_block_asset_path_prefix( $path ); + if ( $resolved_path === $path ) { + return $path; + } + $full_path = wp_normalize_path( + realpath( dirname( $metadata['file'] ) . '/' . $resolved_path ) + ); + if ( ! $full_path || ! file_exists( $full_path ) ) { + _doing_it_wrong( + 'wp_resolve_block_icon_path', + sprintf( + /* translators: %s: path to the icon file */ + __( 'Icon file not found: %s' ), + $resolved_path + ), + '7.3.0' + ); + return $path; + } + $svg_content = file_get_contents( $full_path ); + return wp_sanitize_svg_icon( $svg_content ); + }; + + if ( is_string( $metadata['icon'] ) ) { + $metadata['icon'] = $resolve_svg_path( $metadata['icon'] ); + } elseif ( is_array( $metadata['icon'] ) && isset( $metadata['icon']['src'] ) ) { + $metadata['icon']['src'] = $resolve_svg_path( $metadata['icon']['src'] ); + } + + return $metadata; +} diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 3c1234f9cfa8e..7bd39973e253c 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -250,6 +250,7 @@ add_filter( 'widget_block_content', 'wp_filter_content_tags', 12 ); // Runs after do_shortcode(). add_filter( 'block_type_metadata', 'wp_migrate_old_typography_shape' ); +add_filter( 'block_type_metadata', 'wp_resolve_block_icon_path' ); add_filter( 'wp_get_custom_css', 'wp_replace_insecure_home_url' ); diff --git a/tests/phpunit/data/blocks/icon-file/block.json b/tests/phpunit/data/blocks/icon-file/block.json new file mode 100644 index 0000000000000..ca09edcdad099 --- /dev/null +++ b/tests/phpunit/data/blocks/icon-file/block.json @@ -0,0 +1,9 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "my-plugin/icon-file", + "title": "Icon File", + "category": "text", + "icon": "file:./icon.svg", + "textdomain": "my-plugin" +} diff --git a/tests/phpunit/data/blocks/icon-file/icon.svg b/tests/phpunit/data/blocks/icon-file/icon.svg new file mode 100644 index 0000000000000..d9c376a3eb029 --- /dev/null +++ b/tests/phpunit/data/blocks/icon-file/icon.svg @@ -0,0 +1 @@ + diff --git a/tests/phpunit/data/blocks/icon-missing/block.json b/tests/phpunit/data/blocks/icon-missing/block.json new file mode 100644 index 0000000000000..4c5004427058f --- /dev/null +++ b/tests/phpunit/data/blocks/icon-missing/block.json @@ -0,0 +1,9 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "my-plugin/icon-missing", + "title": "Icon Missing", + "category": "text", + "icon": "file:./does-not-exist.svg", + "textdomain": "my-plugin" +} diff --git a/tests/phpunit/data/blocks/icon-object/block.json b/tests/phpunit/data/blocks/icon-object/block.json new file mode 100644 index 0000000000000..a2c0453efe680 --- /dev/null +++ b/tests/phpunit/data/blocks/icon-object/block.json @@ -0,0 +1,13 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "my-plugin/icon-object", + "title": "Icon Object", + "category": "text", + "icon": { + "src": "file:./icon.svg", + "background": "#ff0000", + "foreground": "#ffffff" + }, + "textdomain": "my-plugin" +} diff --git a/tests/phpunit/data/blocks/icon-object/icon.svg b/tests/phpunit/data/blocks/icon-object/icon.svg new file mode 100644 index 0000000000000..d9c376a3eb029 --- /dev/null +++ b/tests/phpunit/data/blocks/icon-object/icon.svg @@ -0,0 +1 @@ + diff --git a/tests/phpunit/tests/blocks/wpResolveBlockIconPath.php b/tests/phpunit/tests/blocks/wpResolveBlockIconPath.php new file mode 100644 index 0000000000000..de5b91b677477 --- /dev/null +++ b/tests/phpunit/tests/blocks/wpResolveBlockIconPath.php @@ -0,0 +1,96 @@ +is_registered( $block_name ) ) { + $registry->unregister( $block_name ); + } + } + + parent::tear_down(); + } + + /** + * Tests that a `file:` icon is replaced with the contents of the SVG file. + * + * @covers ::wp_resolve_block_icon_path + */ + public function test_file_icon_is_resolved_to_svg_contents() { + $result = register_block_type_from_metadata( DIR_TESTDATA . '/blocks/icon-file' ); + + $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); + $this->assertStringStartsWith( 'icon, 'The icon was not replaced with the SVG contents.' ); + $this->assertStringContainsString( 'M5 5h14v14H5z', $result->icon, 'The icon does not contain the path from the SVG file.' ); + $this->assertStringContainsString( 'viewBox="0 0 24 24"', $result->icon, 'The icon lost its viewBox.' ); + } + + /** + * Tests that a `file:` icon in object form is resolved while its colors are preserved. + * + * @covers ::wp_resolve_block_icon_path + */ + public function test_file_icon_in_object_form_is_resolved_and_keeps_colors() { + $result = register_block_type_from_metadata( DIR_TESTDATA . '/blocks/icon-object' ); + + $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); + $this->assertIsArray( $result->icon, 'The icon is no longer an array.' ); + $this->assertStringStartsWith( 'icon['src'], 'The icon src was not replaced with the SVG contents.' ); + $this->assertSame( '#ff0000', $result->icon['background'], 'The icon background color was not preserved.' ); + $this->assertSame( '#ffffff', $result->icon['foreground'], 'The icon foreground color was not preserved.' ); + } + + /** + * Tests that an icon that is not a file path is left alone. + * + * @covers ::wp_resolve_block_icon_path + */ + public function test_dashicon_slug_is_left_untouched() { + $result = register_block_type_from_metadata( DIR_TESTDATA . '/blocks/notice' ); + + $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); + $this->assertSame( 'star', $result->icon, 'The dashicon slug was modified.' ); + } + + /** + * Tests that a `file:` icon pointing at a missing file is left alone. + * + * @covers ::wp_resolve_block_icon_path + * @expectedIncorrectUsage wp_resolve_block_icon_path + */ + public function test_missing_icon_file_is_left_untouched() { + $result = register_block_type_from_metadata( DIR_TESTDATA . '/blocks/icon-missing' ); + + $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); + $this->assertSame( 'file:./does-not-exist.svg', $result->icon, 'The unresolvable icon path was modified.' ); + } + /** + * Tests that sanitizing an icon keeps the case of SVG names and removes unsafe markup. + * + * @covers ::wp_sanitize_svg_icon + */ + public function test_sanitized_icon_keeps_svg_name_case() { + $icon = wp_sanitize_svg_icon( '' ); + + $this->assertStringContainsString( 'viewBox="0 0 8 8"', $icon, 'The viewBox attribute lost its case.' ); + $this->assertStringContainsString( 'assertStringContainsString( '', $icon, 'The closing gradient tag lost its case.' ); + $this->assertStringContainsString( 'gradientUnits="userSpaceOnUse"', $icon, 'The gradientUnits attribute lost its case.' ); + $this->assertStringNotContainsString( 'onload', $icon, 'The event handler was not removed.' ); + $this->assertStringNotContainsString( ' Date: Fri, 6 Nov 2026 19:05:47 +0200 Subject: [PATCH 2/3] Tests: Add the Trac ticket to the block icon tests --- tests/phpunit/tests/blocks/wpResolveBlockIconPath.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/phpunit/tests/blocks/wpResolveBlockIconPath.php b/tests/phpunit/tests/blocks/wpResolveBlockIconPath.php index de5b91b677477..1386e91e30f61 100644 --- a/tests/phpunit/tests/blocks/wpResolveBlockIconPath.php +++ b/tests/phpunit/tests/blocks/wpResolveBlockIconPath.php @@ -28,6 +28,7 @@ public function tear_down() { /** * Tests that a `file:` icon is replaced with the contents of the SVG file. * + * @ticket 66564 * @covers ::wp_resolve_block_icon_path */ public function test_file_icon_is_resolved_to_svg_contents() { @@ -42,6 +43,7 @@ public function test_file_icon_is_resolved_to_svg_contents() { /** * Tests that a `file:` icon in object form is resolved while its colors are preserved. * + * @ticket 66564 * @covers ::wp_resolve_block_icon_path */ public function test_file_icon_in_object_form_is_resolved_and_keeps_colors() { @@ -57,6 +59,7 @@ public function test_file_icon_in_object_form_is_resolved_and_keeps_colors() { /** * Tests that an icon that is not a file path is left alone. * + * @ticket 66564 * @covers ::wp_resolve_block_icon_path */ public function test_dashicon_slug_is_left_untouched() { @@ -69,6 +72,7 @@ public function test_dashicon_slug_is_left_untouched() { /** * Tests that a `file:` icon pointing at a missing file is left alone. * + * @ticket 66564 * @covers ::wp_resolve_block_icon_path * @expectedIncorrectUsage wp_resolve_block_icon_path */ @@ -81,6 +85,7 @@ public function test_missing_icon_file_is_left_untouched() { /** * Tests that sanitizing an icon keeps the case of SVG names and removes unsafe markup. * + * @ticket 66564 * @covers ::wp_sanitize_svg_icon */ public function test_sanitized_icon_keeps_svg_name_case() { From ce6a3914a0b2b105a1ddcc614a864b8024c25c3f Mon Sep 17 00:00:00 2001 From: mgiannopoulos24 <79588074+mgiannopoulos24@users.noreply.github.com> Date: Fri, 6 Nov 2026 19:17:09 +0200 Subject: [PATCH 3/3] Blocks: Handle real-world SVG files in block icons --- src/wp-includes/blocks.php | 65 ++++++++++++++++--- .../data/blocks/icon-invalid/block.json | 9 +++ .../phpunit/data/blocks/icon-invalid/icon.svg | 1 + .../tests/blocks/wpResolveBlockIconPath.php | 60 ++++++++++++++++- 4 files changed, 126 insertions(+), 9 deletions(-) create mode 100644 tests/phpunit/data/blocks/icon-invalid/block.json create mode 100644 tests/phpunit/data/blocks/icon-invalid/icon.svg diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 6f171ae4db558..6ab06073d1f47 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -3399,6 +3399,31 @@ function wp_get_allowed_svg_icon_tags() { 'y1' => true, 'x2' => true, 'y2' => true, + 'clip-path' => true, + 'mask' => true, + 'filter' => true, + 'stroke-dasharray' => true, + 'stroke-dashoffset' => true, + 'stroke-miterlimit' => true, + 'stroke-opacity' => true, + 'clippathunits' => true, + 'maskunits' => true, + 'maskcontentunits' => true, + 'filterunits' => true, + 'spreadmethod' => true, + 'fx' => true, + 'fy' => true, + 'stddeviation' => true, + 'in' => true, + 'in2' => true, + 'result' => true, + 'mode' => true, + 'values' => true, + 'operator' => true, + 'flood-color' => true, + 'flood-opacity' => true, + 'dx' => true, + 'dy' => true, ); $allowed_tags = array( @@ -3448,14 +3473,26 @@ function wp_get_allowed_svg_icon_tags() { * @return string Sanitized SVG content. */ function wp_sanitize_svg_icon( $svg_content ) { + // Remove markup that wp_kses() would keep, or would turn into stray text. + $svg_element = preg_replace( + array( + '/<\?xml.*?\?>/is', + '/|$)/s', + '/\[]*(?:\[.*?\]\s*)?>/is', + '#<(script|style)\b.*?(?:|$)#is', + ), + '', + $svg_content + ); + $svg_element = wp_kses( - $svg_content, + $svg_element, wp_get_allowed_svg_icon_tags() ); // wp_kses() lowercases names, but SVG names are case-sensitive. $svg_elements = array( 'linearGradient', 'radialGradient', 'clipPath', 'feGaussianBlur', 'feColorMatrix', 'feBlend', 'feComposite', 'feFlood', 'feMerge', 'feMergeNode', 'feOffset', 'feImage' ); - $svg_attributes = array( 'viewBox', 'preserveAspectRatio', 'gradientUnits', 'gradientTransform' ); + $svg_attributes = array( 'viewBox', 'preserveAspectRatio', 'gradientUnits', 'gradientTransform', 'clipPathUnits', 'maskUnits', 'maskContentUnits', 'filterUnits', 'spreadMethod', 'stdDeviation' ); foreach ( $svg_elements as $name ) { $svg_element = preg_replace( '#(])#', '$1' . $name, $svg_element ); } @@ -3463,9 +3500,6 @@ function wp_sanitize_svg_icon( $svg_content ) { $svg_element = preg_replace( '/(\s)' . strtolower( $name ) . '(?==")/', '$1' . $name, $svg_element ); } - // Remove XML declaration if present. - $svg_element = preg_replace( '/^<\?xml[^>]*\?>\s*/', '', $svg_element ); - return trim( $svg_element ); } @@ -3486,6 +3520,9 @@ function wp_resolve_block_icon_path( $metadata ) { } $resolve_svg_path = function ( $path ) use ( $metadata ) { + if ( ! is_string( $path ) ) { + return $path; + } $resolved_path = remove_block_asset_path_prefix( $path ); if ( $resolved_path === $path ) { return $path; @@ -3493,7 +3530,7 @@ function wp_resolve_block_icon_path( $metadata ) { $full_path = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $resolved_path ) ); - if ( ! $full_path || ! file_exists( $full_path ) ) { + if ( ! $full_path || ! is_file( $full_path ) ) { _doing_it_wrong( 'wp_resolve_block_icon_path', sprintf( @@ -3505,8 +3542,20 @@ function wp_resolve_block_icon_path( $metadata ) { ); return $path; } - $svg_content = file_get_contents( $full_path ); - return wp_sanitize_svg_icon( $svg_content ); + $svg_element = wp_sanitize_svg_icon( (string) file_get_contents( $full_path ) ); + if ( ! str_starts_with( $svg_element, 'is_registered( $block_name ) ) { $registry->unregister( $block_name ); } @@ -98,4 +98,62 @@ public function test_sanitized_icon_keeps_svg_name_case() { $this->assertStringNotContainsString( 'onload', $icon, 'The event handler was not removed.' ); $this->assertStringNotContainsString( ']>' ); + + $this->assertStringStartsWith( 'assertStringNotContainsString( 'Generator', $icon, 'The comment was not removed.' ); + $this->assertStringNotContainsString( 'fill:red', $icon, 'The style contents were not removed.' ); + $this->assertStringNotContainsString( 'x()', $icon, 'The script contents were not removed.' ); + } + + /** + * Tests that common presentation attributes survive sanitizing. + * + * @ticket 66564 + * @covers ::wp_sanitize_svg_icon + */ + public function test_sanitized_icon_keeps_presentation_attributes() { + $icon = wp_sanitize_svg_icon( '' ); + + $this->assertStringContainsString( 'clip-path="url(#a)"', $icon, 'The clip-path attribute was removed.' ); + $this->assertStringContainsString( 'stroke-dasharray="2"', $icon, 'The stroke-dasharray attribute was removed.' ); + $this->assertStringContainsString( 'stroke-miterlimit="10"', $icon, 'The stroke-miterlimit attribute was removed.' ); + $this->assertStringContainsString( 'stdDeviation="1"', $icon, 'The stdDeviation attribute was removed or lost its case.' ); + } + + /** + * Tests that a `file:` icon pointing at a file that is not an SVG is left alone. + * + * @ticket 66564 + * @covers ::wp_resolve_block_icon_path + * @expectedIncorrectUsage wp_resolve_block_icon_path + */ + public function test_invalid_icon_file_is_left_untouched() { + $result = register_block_type_from_metadata( DIR_TESTDATA . '/blocks/icon-invalid' ); + + $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); + $this->assertSame( 'file:./icon.svg', $result->icon, 'The invalid icon was modified.' ); + } + + /** + * Tests that an object icon whose src is not a string is left alone. + * + * @ticket 66564 + * @covers ::wp_resolve_block_icon_path + */ + public function test_non_string_icon_src_is_left_untouched() { + $metadata = array( + 'file' => __FILE__, + 'icon' => array( 'src' => array( 'invalid' ) ), + ); + + $this->assertSame( $metadata, wp_resolve_block_icon_path( $metadata ) ); + } }