Repository navigation
Conversation
Preserve character-encoded opening square brackets inside allowed HTML `<code>` elements in `wp_sanitize_html_kses()` by encoding `[` as `&WordPress#91;` in text nodes while `$code_depth > 0`. This prevents shortcode examples saved by the Code block from decoding back into executable shortcodes when posts are filtered for users without `unfiltered_html`. Props therssoftware. See #66673.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
dmsnell
left a comment
There was a problem hiding this comment.
See note in https://core.trac.wordpress.org/ticket/66208#comment:52.
There is a more-significant approach that needs to be taken instead of focusing on this one particular case.
We can leave escaped characters escaped.
See also #10600 for some prior work.
…special-casing code elements. Replace the code-element-specific bracket encoding in `wp_sanitize_html_kses()` with general character-reference preservation and normalization for `#text` tokens using `WP_HTML_Decoder::read_character_reference()`, aligned with the approach in WordPress#10600 and #66208: - Leave non-syntax character references escaped (normalizing missing semicolons, leading zeros, and hex casing). - Unhide ASCII letters, digits, and colons. - Normalize numeric syntax character references (`&`, `"`, `'`, `<`, `>`) to named forms and escape stray `&`, `<`, and `>`. Update `tests/phpunit/tests/kses.php` to cover general character-reference preservation and normalization. Props therssoftware. See #66673.
|
Thanks for the review and pointers to #66208 and #10600, @dmsnell! I've reworked this PR to remove the With this change:
All |
dmsnell
left a comment
There was a problem hiding this comment.
@robelsust I don’t think we just want to copy my work from #10600 and paste it here under your name 🙃
Did you respond to @sirreal’s feedback from there? Also, there’s no need to unhide characters at this point; we’re just preserving behaviors and not enhancing it
| if ( $is_in_text_integration_point && '#text' === $token_type ) { | ||
| if ( '#text' === $token_type ) { | ||
| $text = ''; | ||
| } elseif ( $is_in_text_integration_point ) { |
There was a problem hiding this comment.
what was your reasoning behind making this change?
| $span = $this->bookmarks['text']; | ||
| $raw_text = substr( $this->html, $span->start, $span->length ); | ||
| $this->release_bookmark( 'text' ); | ||
| } |
There was a problem hiding this comment.
there’s a method that already exists on the class doing this
| $output .= "\n{$text}"; | ||
| } else { | ||
| $output .= $text; | ||
| } |
There was a problem hiding this comment.
what’s the reasoning for this change?
…hiding or rewriting. Address review feedback on PR WordPress#15971: - Remove the character-unhiding and entity-rewriting logic copied from WordPress#10600. - Restore the MathML text integration point handling and `$needs_special_newline` logic in `wp_sanitize_html_kses()`. - Add `escape_text_preserving_character_references()` to leave valid HTML character references as written while escaping stray `&`, `<`, `>`, and `\r`. Props therssoftware. See #66673.
|
Thank you for calling that out, @dmsnell — you're completely right, and I apologize for copying the normalization/unhiding logic from #10600 and dropping the integration-point and I've pushed a clean update (
Regarding |
There was a problem hiding this comment.
You’re absolutely right, @robelsust, there’s no “existing method on WP_HTML_Tag_Processor that exposes the raw span without decoding” but I wasn’t talking about the Tag Processor, but rather this class, which is a sub-class of it.
This is probably a fairly good place to scope-limit the work for WordPress 7.2. It doesn’t normalize character references or add missing semicolons, but we can continue the work post-release.
I would like to see us ensure that all risky inputs are properly normalized, so if you are willing I would be happy if you could explore that (all character references are terminated with a semicolon, all numeric character references contain no leading zeros, all hexadecimal character references use upper-case letters for A–F)
| $decoded = WP_HTML_Decoder::read_character_reference( 'data', $text, $next_amp, $token_length ); | ||
| if ( isset( $decoded ) ) { | ||
| $result .= substr( $text, $next_amp, $token_length ); | ||
| $at = $next_amp + $token_length; |
There was a problem hiding this comment.
here we would rather skip over this than make two additional string copies. consider, for example, a text that’s fully normalized with no unescaped & characters and plenty of normative character references.
with this code we end up performing two string copies per character reference but we could instead perform none, returning the original string
| $result .= strtr( substr( $text, $at, $next_amp - $at ), $replacements ); | ||
| } | ||
|
|
||
| $decoded = WP_HTML_Decoder::read_character_reference( 'data', $text, $next_amp, $token_length ); |
There was a problem hiding this comment.
by hard-coding data here we have made this otherwise-useful function unusable for HTML attributes. that is fine for this limited private function, but we should name it explicitly by calling it something like escape_text_node_preserving_character_references()
| } | ||
|
|
||
| return $result; | ||
| } |
There was a problem hiding this comment.
can you move this above sanitize(), and probably above the foreign-element handling helpers? above or below set_attribute() would be good
| 'leading zeros and missing semicolon' => array( | ||
| '<code>[gallery]</code> and <p>[gallery]</p>', | ||
| '<code>[gallery]</code> and <p>[gallery]</p>', | ||
| ), |
There was a problem hiding this comment.
because I think the goal is going to be to normalize these, let’s avoid hard-coding unwanted outputs in the test suite
Description
The Block Editor's Code block escapes
[as[when saving post content so shortcode examples inside<pre class="wp-block-code"><code>...</code></pre>stay as literal text rather than executing viado_shortcode(). Since[64233],wp_sanitize_html_kses()decodes character references in#textnodes viaget_modifiable_text()and only re-encodes\r,<,&, and>, turning[back into[when content is filtered for users withoutunfiltered_html(such as Authors).This PR updates
wp_sanitize_html_kses()insrc/wp-includes/kses.phpto:<code>elements ($code_depth).[) as[in#textnodes while$code_depth > 0, leaving brackets outside allowed<code>elements unchanged.<code>, andwp_kses_post()+do_shortcode()regression tests intests/phpunit/tests/kses.php.Trac ticket: https://core.trac.wordpress.org/ticket/66673
Testing Instructions
unfiltered_htmlcapability (for example, an Author), or run:wp_kses_post()returns<pre class="wp-block-code"><code>[audio src="https://example.com/a.mp3"]</code></pre>, anddo_shortcode()renders the shortcode on the front end.wp_kses_post()preserves[inside<code>, anddo_shortcode()leaves the Code block example unexecuted while still executing shortcodes outside<code>.Automated Checks (PHP 8.4)
./vendor/bin/phpunit tests/phpunit/tests/kses.php:OK (413 tests, 1447 assertions)./vendor/bin/phpunit --group kses,shortcode:Tests: 1032, Assertions: 3010, Skipped: 4.(0 failures)./vendor/bin/phpcs src/wp-includes/kses.php tests/phpunit/tests/kses.php:2 / 2 (100%)passed with 0 errors and 0 warnings./vendor/bin/phpstan(2.2.13) did not match the repository'scomposer.json/phpstan.neon.dist(DisallowedBacktickRule), so PHPStan relies on CI.AI Assistance
AI assistance was used (Antigravity / Gemini) to investigate the regression in
wp_sanitize_html_kses(), implement the$code_depthfix, write unit tests, run local PHPUnit/PHPCS verification, and draft this PR description. All code changes, diffs, and test results were reviewed by the contributor (therssoftware/robelsust).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.