Comments: Allow the Notes @mention chip markup in comment content (kses)#12503
Comments: Allow the Notes @mention chip markup in comment content (kses)#12503adamsilverstein wants to merge 26 commits into
Conversation
The notes @-mention completer stores a mention as `<a class="wp-note-mention" data-user-id="N" href="...">@name</a>`. The default comment kses allowlist only keeps `href` and `title` on links, so for users without `unfiltered_html` the attributes that make a mention a mention (the chip class and the mentioned user's ID) are stripped on save. Add a 'pre_comment_content' context to wp_kses_allowed_html() that allows `class` and `data-user-id` on links so saved mentions survive sanitization. Both attributes are inert markup. Backports the PHP changes from the Gutenberg mentions PR: WordPress/gutenberg#79604
|
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 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. |
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. |
Allowing class and data-user-id on links in the pre_comment_content context loosened sanitization for every comment, including anonymous front-end comments. Both attributes are CSS/JS selector hooks, so that would let any commenter publish links styled by theme classes or reachable by delegated script handlers. Attach the extended allowlist in wp_filter_comment() only while a note comment's content is being filtered instead. Notes can only be written by logged-in users who can edit the post and never render on the front end, and the sanitization of regular comments is unchanged.
Design review on the Gutenberg side changed the stored mention from a link to a plain span, since a mention marks a person rather than offering navigation. Allow span.class and span.data-user-id instead of the link attributes; the allowance is still attached only while a note comment is filtered.
… attribute The notes mention completer now stores a mention as `<span class="wp-note-mention user-N">@name</span>`, so the kses allowance for note comments shrinks to the single `class` attribute and no longer needs `data-user-id`. Add a test asserting that any other attribute is still stripped from note spans. Trac ticket: https://core.trac.wordpress.org/ticket/65622
1fc1250 to
f74c68b
Compare
Mentions are now inserted as links to the mentioned user's author page - <a class="wp-note-mention user-N" href="..."> - instead of spans, so the note allowance moves from span.class to a.class. Note content now also picks up wp_rel_ugc()'s rel="nofollow ugc" like any other comment link, which the tests cover with a deterministic external href.
|
Sorry, only have time for a surface review.
I think the general API for managing email (or other) notifications would be better for the project than handling each case individually. |
dc57c7f to
e31873f
Compare
Reducing this to the kses only changes which matches the merged PRs. Opening a follow up for the notifications part to dig deeper. |
|
Follow up: #12548 |
Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/wp-includes/kses.php:1207
- The early bailout
str_contains( $unslashed, '<span' )is case-sensitive. A commenter could use<SPAN class="...">so this function returns early and never strips class tokens, leaving an open-endedclassattribute on spans (sinceclassis now allowed through KSES). Removing this check avoids the bypass and matches the intended “always reduce span classes” behavior.
$unslashed = wp_unslash( $content );
if ( ! str_contains( $unslashed, '<span' ) ) {
return $content;
}
…tion An uppercase <SPAN> survives kses with its casing preserved, so the str_contains( ..., '<span' ) early return skipped the class reduction entirely for such tags. Run the tag processor unconditionally and cover the uppercase case with a test.
…rstein/wordpress-develop into add/notes-mention-kses
| * @param array<string, array<string, bool>> $allowed The allowed tags structure for the context. | ||
| * @param string|array<string, array<string, bool>> $context The kses context. |
There was a problem hiding this comment.
@copilot This contradicts with what you previously said in #12503 (comment)
It seems here you are right. Updated in 1bef13b
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/wp-includes/kses.php:1154
- The phpdoc types for
_wp_kses_allow_note_mention_span()don’t match what thewp_kses_allowed_htmlfilter can actually pass.$allowedis not always an “allowed tags structure” (e.g. for theentitiescontext it’s an array of entity-name strings), and$contextis documented as possibly being an array even thoughwp_kses_allowed_html()normalizes array contexts to the string'explicit'before applying the filter. This can mislead static analysis and future maintainers.
Consider loosening these annotations to match the filter contract (array + string).
* @param array<string, array<string, bool>> $allowed The allowed tags structure for the context.
* @param string|array<string, array<string, bool>> $context The kses context.
* @return array<string, array<string, bool>> Modified allowed tags structure.
Partially reverts 659c556
Backports the mention kses allowance from the Gutenberg Notes @mention work:
What
The Notes
@mention completer stores a mention as a non-interactive chip carrying the mentioned user's ID in a class token:This PR allows exactly that markup - and nothing more - through comment kses, via two small always-on filters (hooked in
default-filters.php):_wp_kses_allow_note_mention_span()(new, private) extends thepre_comment_contentallowlist withclassonspanelements._wp_kses_sanitize_note_mention_classes()(new, private) runs onpre_comment_contentat priority 11 - right afterwp_filter_kses- and uses the HTML API (class_list()/remove_class()) to strip every span class token exceptwp-note-mentionanduser-N. It bails whilewp_filter_ksesis not attached, so users withunfiltered_html(filtered throughwp_filter_post_kses, where arbitrary classes are already permitted) are never narrowed below what core allows them.wp_filter_comment()is untouched: no per-comment-type arming or disarming of kses state.Why
This follows the discussion on this PR and on WordPress/gutenberg#80496:
classon note links is a live CSS/JS selector surface in the admin. His data-attribute suggestion turned out to need a kses allowance anyway (data-*is not allowed by default in the comment context), and per @Mamaduka's review, an anchor-based mention breaks in the Link format UI regardless of how the ID is carried - mentions aren't links, they're tokens rendered as chips.classhole a different way: the allowance is unconditional yet reduced to exactly two inert tokens on a semantics-free element, so there is nothing left to scope per comment type and the stateful arm/disarm machinery disappears.Behavior change: all commenters (including anonymous ones) can persist
<span>elements whoseclassis limited towp-note-mentionand/oruser-N. This is inert - the tokens are not styling or scripting hooks outside the notes sidebar, and mention notification parsing (#12548) only processesnote-type comments.Testing
Unit tests in
tests/phpunit/tests/kses.php:test_wp_kses_allowed_html_pre_comment_content_allows_only_the_mention_spanverifies the context gains the span allowance and nothing else beyond the stock$allowedtags.test_note_mention_markup_survives_note_content_sanitization/test_note_mention_markup_survives_regular_comment_content_sanitizationverify the chip survives for both comment types.test_note_mention_span_classes_are_reduced_to_the_mention_tokensverifies junk class tokens are stripped.test_note_mention_class_attribute_removed_when_no_tokens_remainverifies theclassattribute is dropped entirely when no valid tokens remain.test_note_mention_allows_only_class_on_mention_spansverifies every other span attribute (onclick,style,data-*,id) is still stripped.test_class_is_still_stripped_from_links_in_comment_contentverifies links keep the default sanitization.test_note_mention_class_reduction_skipped_when_restrictive_kses_is_inactiveverifies the reduction never narrowsunfiltered_htmlusers.Trac ticket: https://core.trac.wordpress.org/ticket/65622
Proposed commit message