Skip to content

Commit b37cbf9

Browse files
committed
HTML API: Replace null-bytes in class_list class names
As part of an audit of HTML API CSS behaviors, this patch resolves an issue with how the HTML API reports class names containing the NULL byte. NULL bytes should be replaced by the Unicode replacement character, U+FFFD, but previously weren't. This patch performs that replacement. Developed in #7187 Discussed in https://core.trac.wordpress.org/ticket/61531 Follow-up to [56703]. Props dmsnell, jonsurrell. See #61531. git-svn-id: https://develop.svn.wordpress.org/trunk@58969 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 54c35ce commit b37cbf9

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/wp-includes/html-api/class-wp-html-tag-processor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ public function class_list() {
11601160
*
11611161
* @see https://www.w3.org/TR/CSS2/syndata.html#x1
11621162
*/
1163-
$name = strtolower( substr( $class, $at, $length ) );
1163+
$name = str_replace( "\x00", "\u{FFFD}", strtolower( substr( $class, $at, $length ) ) );
11641164
$at += $length;
11651165

11661166
/*

tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,35 @@ public function test_class_list_visits_unique_class_names_only_once() {
22362236
$this->assertSame( array( 'one' ), $found_classes, 'Visited multiple copies of the same class name when it should have skipped the duplicates.' );
22372237
}
22382238

2239+
/**
2240+
* Ensures that null bytes are replaced with the replacement character (U+FFFD) in class_list.
2241+
*
2242+
* @ticket 61531
2243+
*
2244+
* @covers WP_HTML_Tag_Processor::class_list
2245+
*/
2246+
public function test_class_list_null_bytes_replaced() {
2247+
$processor = new WP_HTML_Tag_Processor( "<div class='a \0 b\0 \0c\0'>" );
2248+
$processor->next_tag();
2249+
2250+
$found_classes = iterator_to_array( $processor->class_list() );
2251+
2252+
$this->assertSame( array( 'a', "\u{FFFD}", "b\u{FFFD}", "\u{FFFD}c\u{FFFD}" ), $found_classes );
2253+
}
2254+
2255+
/**
2256+
* Ensures that the tag processor matches class names with null bytes correctly.
2257+
*
2258+
* @ticket 61531
2259+
*
2260+
* @covers WP_HTML_Tag_Processor::has_class
2261+
*/
2262+
public function test_has_class_null_byte_class_name() {
2263+
$processor = new WP_HTML_Tag_Processor( "<div class='null-byte-\0-there'>" );
2264+
$processor->next_tag();
2265+
$this->assertTrue( $processor->has_class( 'null-byte-�-there' ) );
2266+
}
2267+
22392268
/**
22402269
* @ticket 59209
22412270
*

0 commit comments

Comments
 (0)