Opened 4 years ago
Closed 7 weeks ago
#56521 closed defect (bug) (duplicate)
wp_kses wp_kses_hair fails to allow a valueless attribute when is follwed by /
| Reported by: | luistar15 | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Security | Version: | 6.0.2 |
| Severity: | major | Keywords: | has-patch |
| Cc: | Focuses: |
Description
I have created a static gutenberg block for managing forms.
The block generates <input> html elements, for example:
<input name="email" required/>
But after saving the post, the required attribute is striped in the db:
<input name="email"/>
So the visitors are sendig the form ignoring the validation.
It only happens when the attribute is the last one and is followerd by / without spaces:
<input name="email" required> => OK <input name="email" required/> => ERROR <input name="email" required /> => OK
I put togheter a test case:
// theme/functions.php add_action( 'init', function () { $tests = [ '<input name="email" required>', '<input name="email" required/>', '<input name="email" required />', ]; $allowed_html = [ 'input' => [ 'name' => true, 'required' => true, ], ]; header('Content-Type: text/plain; charset=UTF-8', true); foreach ( $tests as $test ) { $sanitized = wp_kses( $test, $allowed_html ); printf( "_in => %s\nout => %s\n\n", $test, $sanitized); } exit; });
I have found where the error could be fixed:
// wp-includes/kses.php:1320 if ( preg_match( '/^\s+/', $attr ) ) { // Valueless.
Replacing the regex /^\s+/ with /^(\s+|\/$)/ fixes the problem.
I'm hopping it can be fixed for the next release, until then I'm manually applying the patch again.
Change History (2)
This ticket was mentioned in PR #3200 on WordPress/wordpress-develop by luistar15.
4 years ago
#1
- Keywords has-patch added
#2
@
7 weeks ago
- Milestone Awaiting Review
- Resolution → duplicate
- Status new → closed
@luistar15 this appears to have been resolved by #63724 when we replaced wp_kses_hair()’s parsing code with the HTML API.
I checked out WordPress 6.9.9 and tested your inputs to find what you reported. However, in WordPress 7.0.0 they all returned the required attribute as expected.
I’m going to mark this as a duplicate of #63724 even though it came first, because that ticket resolved the issue at the root.
As a side note, the mistaken self-closing flag on HTML tags is dangerous for this reason. There are no self-closing tags in HTML (only inside SVG and MathML elements and only specific tags inside of those). Were they combined with unquoted attribute values, the / becomes part of the unquoted value.
For more reliable HTML parsing everywhere, I encourage folks to leave out the /. It is a myth that it’s normative or helpful for making documents more compatible. The normative syntax is <input name="email" required>.
wp_kses_hairfails to parse a valueless attribute at the end of the list.For example:
<input name="email" required/>will strip therequiredattribute.This change fixes it.
https://core.trac.wordpress.org/ticket/56521