Skip to content

Commit

Permalink
Update wp_kses*() functions to accept null as $content parameter (Cla…
Browse files Browse the repository at this point in the history
…ssicPress#1279)

* Fix "kses" functions for null content

* Add unit test

* Fix typo

---------

Co-authored-by: xxsimoxx <simone@gieffeedizioni.it>
  • Loading branch information
xxsimoxx and xxsimoxx authored Dec 21, 2023
1 parent dd8c38b commit 8a30855
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/wp-includes/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,9 @@ function wp_kses_version() {
* @return string Content with fixed HTML tags
*/
function wp_kses_split( $content, $allowed_html, $allowed_protocols ) {
if ( is_null( $content ) ) {
return '';
}
global $pass_allowed_html, $pass_allowed_protocols;

$pass_allowed_html = $allowed_html;
Expand Down Expand Up @@ -1722,6 +1725,9 @@ function wp_kses_bad_protocol( $content, $allowed_protocols ) {
* @return string Filtered content.
*/
function wp_kses_no_null( $content, $options = null ) {
if ( is_null( $content ) ) {
return '';
}
if ( ! isset( $options['slash_zero'] ) ) {
$options = array( 'slash_zero' => 'remove' );
}
Expand All @@ -1746,6 +1752,9 @@ function wp_kses_no_null( $content, $options = null ) {
* @return string Fixed string with quoted slashes.
*/
function wp_kses_stripslashes( $content ) {
if ( is_null( $content ) ) {
return '';
}
return preg_replace( '%\\\\"%', '"', $content );
}

Expand Down Expand Up @@ -1802,6 +1811,9 @@ function wp_kses_html_error( $attr ) {
* @return string Sanitized content.
*/
function wp_kses_bad_protocol_once( $content, $allowed_protocols, $count = 1 ) {
if ( is_null( $content ) ) {
return '';
}
$content = preg_replace( '/(&#0*58(?![;0-9])|&#x0*3a(?![;a-f0-9]))/i', '$1;', $content );
$content2 = preg_split( '/:|&#0*58;|&#x0*3a;|&colon;/i', $content, 2 );

Expand Down Expand Up @@ -1877,6 +1889,9 @@ function wp_kses_bad_protocol_once2( $scheme, $allowed_protocols ) {
* @return string Content with normalized entities.
*/
function wp_kses_normalize_entities( $content, $context = 'html' ) {
if ( is_null( $content ) ) {
return '';
}
// Disarm all entities by converting & to &amp;
$content = str_replace( '&', '&amp;', $content );

Expand Down Expand Up @@ -2028,6 +2043,9 @@ function valid_unicode( $i ) {
* @return string Content after decoded entities.
*/
function wp_kses_decode_entities( $content ) {
if ( is_null( $content ) ) {
return '';
}
$content = preg_replace_callback( '/&#([0-9]+);/', '_wp_kses_decode_entities_chr', $content );
$content = preg_replace_callback( '/&#[Xx]([0-9A-Fa-f]+);/', '_wp_kses_decode_entities_chr_hexdec', $content );

Expand Down
26 changes: 26 additions & 0 deletions tests/phpunit/tests/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -2151,4 +2151,30 @@ public function data_kses_globals_are_defined() {

return $this->text_array_to_dataprovider( $required_kses_globals );
}

/**
* Test that passing a null value as content doesn't
* trigger error and return an empty string
*
* @since CP-2.0
*/
public function test_wp_kses_null_content() {
$result = wp_kses_stripslashes( null, '' );
$this->assertSame( $result, '' );

$result = wp_kses_no_null( null, array() );
$this->assertSame( $result, '' );

$result = wp_kses_split( null, array(), array() );
$this->assertSame( $result, '' );

$result = wp_kses_bad_protocol_once( null, array() );
$this->assertSame( $result, '' );

$result = wp_kses_normalize_entities( null );
$this->assertSame( $result, '' );

$result = wp_kses_decode_entities( null );
$this->assertSame( $result, '' );
}
}

0 comments on commit 8a30855

Please sign in to comment.