Make WordPress Core

Opened 3 months ago

Last modified 11 days ago

#65795 new defect (bug)

Icons: Improve the SVG sanitizer in WP_Icons_Registry

Reported by: wildworks Owned by:
Priority: normal Milestone: 7.3
Component: Editor Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Core still has no dedicated SVG sanitization function, so the icons registry sanitizes icon content on its own. That sanitizer is incomplete: it strips attributes and elements that are valid SVG markup.

This ticket improves the sanitizer by extracting the root SVG element with WP_HTML_Processor before wp_kses() runs, and by expanding the allow-list to cover SVG-friendly elements and attributes.

Change History (15)

This ticket was mentioned in ​PR #12197 on ​WordPress/wordpress-develop by ​@wildworks.


3 months ago
#1

  • Keywords has-patch has-unit-tests added

## Use of AI Tools

Used for: Initial code skeleton and test suggestions; final implementation and tests were reviewed and edited by me.

​@mciampini commented on ​PR #12197:


3 months ago
#2

@t-hamano with ​https://github.com/WordPress/gutenberg/pull/78808 merged, can you take care of the Core side of things? Ie.

Thank you 🙏

​@retrofox commented on ​PR #12197:


2 months ago
#3

With WordPress/gutenberg#82540 merged, 21 library icons now draw with <rect> or <circle>, and core/sites is among the ones shipping to core, so the shape entries in this allowlist are needed for those icons to render server-side, not only the stroke attributes. The plugin side is fixed in WordPress/gutenberg#82846 (tracked in WordPress/gutenberg#82841), and it is listed against this PR in the backport changelog.

#4 @wildworks
2 months ago

#66112 was marked as a duplicate.

#5 @wildworks
2 months ago

#66101 was marked as a duplicate.

This ticket was mentioned in ​PR #13559 on ​WordPress/wordpress-develop by ​@wildworks.


2 months ago
#6

This PR is a subset of #12197, focusing only on allowing the SVG elements and attributes needed for core icons to render correctly.

The core icons are now stroke-based, but WP_Icons_Registry::sanitize_icon_content() strips attributes such as stroke-width, vector-effect, and style="fill: none", so the icons render incorrectly. This PR extends the allowlist with the same elements and attributes allowed in Gutenberg:

  • Stroke-related attributes and style on all allowed elements
  • fill, fill-rule, and clip-rule on svg
  • clip-rule on path and polygon, and opacity on path
  • rect and circle elements

Related Gutenberg PRs:

test_wp_get_icon_size_null_leaves_dimensions_untouched is also updated, because checking for the width= string now matches the preserved stroke-width attribute.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Porting the Gutenberg changes, writing tests, and drafting this description; reviewed by me.

​@wildworks commented on ​PR #12197:


2 months ago
#7

I created #13559, which extracts some unit tests from this PR, so that I can focus on ensuring the core icon set renders correctly.

We can continue discussing this PR as part of a more comprehensive improvement in the future.

#10 @wildworks
8 weeks ago

In 63652:

Icons: Allow more SVG elements and attributes in the sanitizer.

The icon sanitizer in WP_Icons_Registry stripped valid SVG markup, so icons using fill rules, clip paths, strokes, or shapes such as rect and circle did not render correctly. Expand the allow-list for svg and path to permit that markup, allow the same stroke attributes on polygon, and add rect and circle as newly allowed elements.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/6395

Follow-up to r62748.

Props westonruter, wildworks.
See #65795.

This ticket was mentioned in ​PR #13766 on ​WordPress/wordpress-develop by ​@wildworks.


7 weeks ago
#12

Follow-up to #13559 (r63652).

Use assertEqualHTML() instead of assertSame() in the icons registry tests that compare sanitized icon markup, so they don't fail on changes that don't affect the resulting HTML, such as attribute order or escaping.

Equivalent to ​https://github.com/WordPress/gutenberg/pull/83544.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Porting the Gutenberg change and drafting this PR; reviewed by me.

---
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See ​GitHub Pull Requests for Code Review in the Core Handbook for more details.

🤖 Generated with ​Claude Code

#13 @wildworks
7 weeks ago

In 63960:

Tests: Use assertEqualHTML() in icons registry sanitizer tests.

The tests for the icon sanitizer now check whether the sanitized markup is equivalent HTML, not an exact string. They no longer fail on changes that leave the resulting HTML the same, such as attribute order or escaping.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13766

Follow-up to [63652].

Props dmsnell, westonruter, wildworks.
See #65795.

#15 @wildworks
11 days ago

  • Milestone 7.2 → 7.3

Unfortunately, this won't make it in time for 7.2, so I'd like to punt it to 7.3.

Note: See TracTickets for help on using tickets.