Skip to content

KSES: Allow SVG-specific presentation attributes in safe_style_css#79172

Open
t-hamano wants to merge 2 commits into
trunkfrom
svg-kses-filter
Open

KSES: Allow SVG-specific presentation attributes in safe_style_css#79172
t-hamano wants to merge 2 commits into
trunkfrom
svg-kses-filter

Conversation

@t-hamano

@t-hamano t-hamano commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

See #78808 (comment)

What?

Adds a safe_style_css filter that allowlists SVG-specific presentation attributes so they are preserved when SVG markup is sanitized with wp_kses().

Why?

wp_kses() runs the style attribute through safecss_filter_attr(), which only keeps an allowlist of CSS properties. SVG presentation attributes are not in that list, so inline styles get stripped during sanitization.

This issue may become more prominent once stroke-based icons are implemented by #78808 or custom icons are allowed to be registered by #77260.

How?

Adds lib/compat/wordpress-7.1/kses.php with a single safe_style_css filter that appends the SVG presentation attributes. The list is based on the SVG presentation attributes documented on MDN.

Testing Instructions

add_action( 'admin_notices', function () {
	$svg = '<svg style="fill: none; stroke: red; stroke-width: 2;" viewbox="0 0 24 24">'
		. '<path style="fill-rule: evenodd; vector-effect: non-scaling-stroke;" d="M0 0h24"/></svg>';
	$allowed = array(
		'svg'  => array( 'style' => true, 'viewbox' => true ),
		'path' => array( 'style' => true, 'd' => true ),
	);
	echo '<div class="notice notice-info"><p><code>' . esc_html( wp_kses( $svg, $allowed ) ) . '</code></p></div>';
} );

Confirm that the styles are not removed when using the code above.

Use of AI Tools

This PR was authored with the assistance of Claude Code. All changes were reviewed by the author.

Icon SVG markup uses inline styles such as `style="fill: none"`, but
`wp_kses()` strips them because `safecss_filter_attr()` only keeps an
allowlist of CSS properties that does not include SVG presentation
attributes. Add a single `safe_style_css` filter that allowlists the
SVG-specific presentation attributes so they survive sanitization,
replacing the per-call workaround in the icons registry.

Co-Authored-By: Claude <noreply@anthropic.com>
@t-hamano t-hamano self-assigned this Jun 13, 2026
@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Feature] Icons Related to Icon registration API and Icon REST API labels Jun 13, 2026
@github-project-automation github-project-automation Bot moved this to 🔎 Needs Review in WordPress 7.1 Editor Tasks Jun 13, 2026
@t-hamano t-hamano marked this pull request as ready for review June 13, 2026 14:08
@t-hamano t-hamano requested a review from spacedmonkey as a code owner June 13, 2026 14:08
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: t-hamano <wildworks@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Flaky tests detected in 9f9b1b3.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/27469026092
📝 Reported issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Icons Related to Icon registration API and Icon REST API [Type] Bug An existing feature does not function as intended

Projects

Status: 🔎 Needs Review

Development

Successfully merging this pull request may close these issues.

1 participant