-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tag Processor: throw when supplied unacceptible attribute names. #44431
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c284114
Tag Processor: throw when supplied unacceptible attribute names.
dmsnell 65e4695
Update code comments
dmsnell 504e84f
Split string into multi-line, move tests into isolated runner
dmsnell ff9da88
Test for valid attributes
adamziel a69ebf7
Lint
adamziel 1619cb1
Update phpunit/html/WP_HTML_Tag_Processor_Isolated_Test.php
adamziel f8b1704
Lint
adamziel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
/** | ||
* Unit tests covering WP_HTML_Tag_Processor functionality. | ||
* | ||
* @package WordPress | ||
* @subpackage HTML | ||
*/ | ||
|
||
if ( ! function_exists( 'esc_attr' ) ) { | ||
function esc_attr( $s ) { | ||
return str_replace( '"', '"', $s ); | ||
} | ||
} | ||
|
||
if ( ! class_exists( 'WP_UnitTestCase' ) ) { | ||
abstract class WP_UnitTestCase extends \PHPUnit\Framework\TestCase {} | ||
} | ||
|
||
require_once __DIR__ . '/../../lib/experimental/html/index.php'; | ||
|
||
/** | ||
* Runs tests in isolated PHP process for verifying behaviors | ||
* that depend on the `WP_DEBUG` constant value, if set. | ||
* | ||
* @group html | ||
* | ||
* @coversDefaultClass WP_HTML_Tag_Processor | ||
*/ | ||
class WP_HTML_Tag_Processor_Isolated_Test extends WP_UnitTestCase { | ||
// phpcs:disable WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase | ||
protected $runTestInSeparateProcess = true; | ||
|
||
/** | ||
* Attribute names with invalid characters should be rejected. | ||
* | ||
* When WP_DEBUG is set we want to throw an error to alert a | ||
* developer that they are sending invalid attribute names. | ||
* | ||
* @dataProvider data_invalid_attribute_names | ||
* @covers set_attribute | ||
*/ | ||
public function test_set_attribute_throw_when_given_invalid_attribute_names_in_debug_mode( $attribute_name ) { | ||
define( 'WP_DEBUG', true ); | ||
$p = new WP_HTML_Tag_Processor( '<span></span>' ); | ||
|
||
$this->expectException( Exception::class ); | ||
|
||
$p->next_tag(); | ||
$p->set_attribute( $attribute_name, 'test' ); | ||
|
||
$this->assertEquals( '<span></span>', (string) $p ); | ||
} | ||
|
||
/** | ||
* Attribute names with invalid characters should be rejected. | ||
* | ||
* When WP_DEBUG isn't set we want to quietly fail to set the | ||
* invalid attribute to avoid breaking the HTML and to do so | ||
* without breaking the entire page. | ||
* | ||
* @dataProvider data_invalid_attribute_names | ||
* @covers set_attribute | ||
*/ | ||
public function test_set_attribute_silently_fails_when_given_invalid_attribute_names_outside_of_debug_mode( $attribute_name ) { | ||
$p = new WP_HTML_Tag_Processor( '<span></span>' ); | ||
|
||
$p->next_tag(); | ||
$p->set_attribute( $attribute_name, 'test' ); | ||
|
||
$this->assertEquals( '<span></span>', (string) $p ); | ||
} | ||
|
||
/** | ||
* Data provider with invalid HTML attribute names. | ||
* | ||
* @return array { | ||
* @type string $attribute_name Text considered invalid for HTML attribute names. | ||
* } | ||
*/ | ||
public function data_invalid_attribute_names() { | ||
return array( | ||
'controls_null' => array( "i\x00d" ), | ||
'controls_newline' => array( "\nbroken-expectations" ), | ||
'space' => array( 'aria label' ), | ||
'double-quote' => array( '"id"' ), | ||
'single-quote' => array( "'id'" ), | ||
'greater-than' => array( 'sneaky>script' ), | ||
'solidus' => array( 'data/test-id' ), | ||
'equals' => array( 'checked=checked' ), | ||
'noncharacters_1' => array( html_entity_decode( 'anything' ) ), | ||
'noncharacters_2' => array( html_entity_decode( 'test' ) ), | ||
'noncharacters_3' => array( html_entity_decode( 'test' ) ), | ||
'noncharacters_4' => array( html_entity_decode( 'test' ) ), | ||
'noncharacters_5' => array( html_entity_decode( '' ) ), | ||
'wp_no_lt' => array( 'id<script' ), | ||
'wp_no_amp' => array( 'class<script' ), | ||
); | ||
} | ||
|
||
/** | ||
* Attribute names with only valid characters should not be rejected. | ||
* | ||
* > Attributes have a name and a value. Attribute names must | ||
* > consist of one or more characters other than controls, | ||
* > U+0020 SPACE, U+0022 ("), U+0027 ('), U+003E (>), | ||
* > U+002F (/), U+003D (=), and noncharacters. | ||
* | ||
* @see https://html.spec.whatwg.org/#attributes-2 | ||
* | ||
* @dataProvider data_valid_attribute_names | ||
* @covers set_attribute | ||
*/ | ||
public function test_set_attribute_does_not_reject_valid_attribute_names( $attribute_name ) { | ||
define( 'WP_DEBUG', true ); | ||
$p = new WP_HTML_Tag_Processor( '<span></span>' ); | ||
|
||
$p->next_tag(); | ||
$p->set_attribute( $attribute_name, 'test' ); | ||
|
||
$this->assertEquals( "<span $attribute_name=\"test\"></span>", (string) $p ); | ||
} | ||
|
||
/** | ||
* Data provider with valid HTML attribute names. | ||
* | ||
* @return array { | ||
* @type string $attribute_name Text considered valid for HTML attribute names. | ||
* } | ||
*/ | ||
public function data_valid_attribute_names() { | ||
return array( | ||
'ascii_letters' => array( 'abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ' ), | ||
'ascii_numbers' => array( '0123456789' ), | ||
'symbols' => array( '!@#$%^*()[]{};:\\||,.?`~£§±' ), | ||
'emoji' => array( '❌' ), | ||
'utf8_diacritics' => array( 'ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆĞÍÌÎÏİŇÑÓÖÒÔÕØŘŔŠŞŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇğíìîïıňñóöòôõøðřŕšşťúůüùûýÿžþÞĐđßÆa' ), | ||
'hebrew_accents' => array( html_entity_decode( '֝a' ) ), | ||
// See https://arxiv.org/abs/2111.00169. | ||
'rtl_magic' => array( html_entity_decode( '⁧⁦abc⁩⁦def⁩⁩' ) ), | ||
// Only a single unicode "noncharacter" should be rejected. Specific byte segments used in the "noncharacter" sequence are valid. | ||
'noncharacter_segments' => array( "\xFF\xFE" ), | ||
); | ||
} | ||
|
||
} |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return new WP_Error perhaps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should return an object as devs won't check the return value anyway.
The special case in the debug mode looks like a reasonable approach. I'm curious whether there is any standardized way of logging those issues.
Maybe we could call
_doing_it_wrong
. We use that in several places when registering blocks or similar stuff for the block editor, for example:https://github.com/WordPress/wordpress-develop/blob/2b1febd20d77898eb81439a688ea5597da00172a/src/wp-includes/class-wp-block-type-registry.php#L55L62
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the big point about this patch that I'm unsure of. my only goal is to quickly bail if someone submits the wrong attribute, as I don't think it's likely people will send user-input attribute names here. if they do do that then I don't want it to crash the full render.
I'll ask around and see if anyone else has experience with this kind of failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, think this is the best approach here. If a plugin allows user input for attribute names (very unlikely as usually these are not random), it will have to make sure the input is valid, or the attribute will be skipped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think there isn't one, unfortunately. Has been discussed few times afaik but a decision on how to implement it was not reached. Thinking it is time for this to be added to core. Perhaps another constant:
WP_DEV_MODE
or similar, then be much bolder about throwing errors and exceptions (with backtrace?) and writing in logs.Using
WP_DEBUG
seems proper here imho.doing_it_wrong
doesn't seem as good because it is targeted more at developers that try to use some function/method improperly (when there are better ways).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@azaozz, great feedback! Thank you so much for clarifying where given options fit best.