Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 213 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3353,3 +3353,216 @@ function _wp_enqueue_auto_register_blocks() {
);
}
}

/**
* Returns the allowed SVG tags and attributes for icon sanitization.
*
* @since 7.3.0
*
* @return array<string, array<string, bool>> 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,
'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(
'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 ) {
// Remove markup that wp_kses() would keep, or would turn into stray text.
$svg_element = preg_replace(
array(
'/<\?xml.*?\?>/is',
'/<!--.*?(?:-->|$)/s',
'/<!DOCTYPE[^>\[]*(?:\[.*?\]\s*)?>/is',
'#<(script|style)\b.*?(?:</\1\s*>|$)#is',
),
'',
$svg_content
);

$svg_element = wp_kses(
$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', 'clipPathUnits', 'maskUnits', 'maskContentUnits', 'filterUnits', 'spreadMethod', 'stdDeviation' );
foreach ( $svg_elements as $name ) {
$svg_element = preg_replace( '#(</?)' . strtolower( $name ) . '(?=[\s/>])#', '$1' . $name, $svg_element );
}
foreach ( $svg_attributes as $name ) {
$svg_element = preg_replace( '/(\s)' . strtolower( $name ) . '(?==")/', '$1' . $name, $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 ) {
if ( ! is_string( $path ) ) {
return $path;
}
$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 || ! is_file( $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_element = wp_sanitize_svg_icon( (string) file_get_contents( $full_path ) );
if ( ! str_starts_with( $svg_element, '<svg' ) ) {
_doing_it_wrong(
'wp_resolve_block_icon_path',
sprintf(
/* translators: %s: path to the icon file */
__( 'Icon file is not a valid SVG: %s' ),
$resolved_path
),
'7.3.0'
);
return $path;
}
return $svg_element;
};

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;
}
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down
9 changes: 9 additions & 0 deletions tests/phpunit/data/blocks/icon-file/block.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions tests/phpunit/data/blocks/icon-file/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tests/phpunit/data/blocks/icon-invalid/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "my-plugin/icon-invalid",
"title": "Icon Invalid",
"category": "text",
"icon": "file:./icon.svg",
"textdomain": "my-plugin"
}
1 change: 1 addition & 0 deletions tests/phpunit/data/blocks/icon-invalid/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tests/phpunit/data/blocks/icon-missing/block.json
Original file line number Diff line number Diff line change
@@ -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"
}
13 changes: 13 additions & 0 deletions tests/phpunit/data/blocks/icon-object/block.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions tests/phpunit/data/blocks/icon-object/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading