Skip to content

Commit

Permalink
More tests and NULL byte transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Sep 20, 2024
1 parent b37b312 commit dd4ff16
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ protected function serialize_token(): string {
return $html;
}

$tag_name = $this->get_tag();
$tag_name = str_replace( "\x00", "\u{FFFD}", $this->get_tag() );
$in_html = 'html' === $this->get_namespace();
$qualified_name = $in_html ? strtolower( $tag_name ) : $this->get_qualified_tag_name();

Expand All @@ -1196,6 +1196,8 @@ protected function serialize_token(): string {
if ( is_string( $value ) ) {
$html .= '="' . htmlspecialchars( $value, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5 ) . '"';
}

$html = str_replace( "\x00", "\u{FFFD}", $html );
}

if ( ! $in_html && $this->has_self_closing_flag() ) {
Expand Down
36 changes: 36 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlProcessor-serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,40 @@ public function data_bogus_comments() {
'XML Processing Instruction look-alike' => array( '<', '?xml foo ', '>' ),
);
}

/**
* Ensures that NULL bytes are properly handled.
*
* @ticket 62036
*
* @dataProvider data_tokens_with_null_bytes
*
* @param string $html_with_nulls HTML token containing NULL bytes in various places.
* @param string $expected_output Expected parse of HTML after handling NULL bytes.
*/
public function test_replaces_null_bytes_appropriately( string $html_with_nulls, string $expected_output ) {
$this->assertSame(
WP_HTML_Processor::normalize( $html_with_nulls ),
$expected_output,
'Should have properly replaced or removed NULL bytes.'
);
}

/**
* Data provider.
*
* @return array[]
*/
public static function data_tokens_with_null_bytes() {
return array(
'Tag name' => array( "<img\x00id=5>", "<img\u{FFFD}id=5></img\u{FFFD}id=5>" ),
'Attribute name' => array( "<img/\x00id=5>", "<img \u{FFFD}id=\"5\">" ),
'Attribute value' => array( "<img id='5\x00'>", "<img id=\"5\u{FFFD}\">" ),
'Body text' => array( "one\x00two", 'onetwo' ),
'Foreign content text' => array( "<svg>one\x00two</svg>", "<svg>one\u{FFFD}two</svg>" ),
'SCRIPT content' => array( "<script>alert(\x00)</script>", "<script>alert(\u{FFFD})</script>" ),
'STYLE content' => array( "<style>\x00 {}</style>", "<style>\u{FFFD} {}</style>" ),
'Comment text' => array( "<!-- \x00 -->", "<!-- \u{FFFD} -->" ),
);
}
}

0 comments on commit dd4ff16

Please sign in to comment.