Make WordPress Core

Changeset 55245


Ignore:
Timestamp:
02/07/2023 03:32:43 AM (2 years ago)
Author:
peterwilsoncc
Message:

Formatting: Guard wp_strip_all_tags() against fatal errors.

Check the input of wp_strip_all_tags() before passing it to strip_tags(). This protects against fatal errors introduced in PHP 8, retaining the E_USER_WARNING from PHP 7, and prevents a PHP 8.1 deprecation notice when passing null.

Props chocofc1, costdev, jrf, dd32, audrasjb, peterwilsoncc.
Fixes #56434.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r55209 r55245  
    53965396 */
    53975397function wp_strip_all_tags( $text, $remove_breaks = false ) {
     5398    if ( is_null( $text ) ) {
     5399        return '';
     5400    }
     5401
     5402    if ( ! is_scalar( $text ) ) {
     5403        /*
     5404         * To maintain consistency with pre-PHP 8 error levels,
     5405         * trigger_error() is used to trigger an E_USER_WARNING,
     5406         * rather than _doing_it_wrong(), which triggers an E_USER_NOTICE.
     5407         */
     5408        trigger_error(
     5409            sprintf(
     5410                /* translators: 1: The function name, 2: The argument number, 3: The argument name, 4: The expected type, 5: The provided type. */
     5411                __( 'Warning: %1$s expects parameter %2$s (%3$s) to be a %4$s, %5$s given.' ),
     5412                __FUNCTION__,
     5413                '#1',
     5414                '$text',
     5415                'string',
     5416                gettype( $text )
     5417            ),
     5418            E_USER_WARNING
     5419        );
     5420
     5421        return '';
     5422    }
     5423
    53985424    $text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $text );
    53995425    $text = strip_tags( $text );
  • trunk/tests/phpunit/tests/formatting/wpStripAllTags.php

    r53562 r55245  
    3232        $this->assertSame( 'loremipsum', wp_strip_all_tags( $text ) );
    3333    }
     34
     35    /**
     36     * Tests that `wp_strip_all_tags()` returns an empty string when null is passed.
     37     *
     38     * @ticket 56434
     39     */
     40    public function test_wp_strip_all_tags_should_return_empty_string_for_a_null_arg() {
     41        $this->assertSame( '', wp_strip_all_tags( null ) );
     42    }
     43
     44    /**
     45     * Tests that `wp_strip_all_tags()` triggers a warning and returns
     46     * an empty string when passed a non-string argument.
     47     *
     48     * @ticket 56434
     49     *
     50     * @dataProvider data_wp_strip_all_tags_should_return_empty_string_and_trigger_an_error_for_non_string_arg
     51     *
     52     * @param mixed $non_string A non-string value.
     53     */
     54    public function test_wp_strip_all_tags_should_return_empty_string_and_trigger_an_error_for_non_string_arg( $non_string ) {
     55        $type = gettype( $non_string );
     56        $this->expectError();
     57        $this->expectErrorMessage( "Warning: wp_strip_all_tags expects parameter #1 (\$text) to be a string, $type given." );
     58        $this->assertSame( '', wp_strip_all_tags( $non_string ) );
     59    }
     60
     61    /**
     62     * Data provider for test_wp_strip_all_tags_should_return_empty_string_and_trigger_an_error_for_non_string_arg().
     63     *
     64     * @return array[]
     65     */
     66    public function data_wp_strip_all_tags_should_return_empty_string_and_trigger_an_error_for_non_string_arg() {
     67        return array(
     68            'an empty array'     => array( 'non_string' => array() ),
     69            'a non-empty array'  => array( 'non_string' => array( 'a string' ) ),
     70            'an empty object'    => array( 'non_string' => new stdClass() ),
     71            'a non-empty object' => array( 'non_string' => (object) array( 'howdy' => 'admin' ) ),
     72        );
     73    }
     74
     75    /**
     76     * Tests that `wp_strip_all_tags()` casts scalar values to string.
     77     *
     78     * @ticket 56434
     79     *
     80     * @dataProvider data_wp_strip_all_tags_should_cast_scalar_values_to_string
     81     *
     82     * @param mixed $text A scalar value.
     83     */
     84    public function test_wp_strip_all_tags_should_cast_scalar_values_to_string( $text ) {
     85        $this->assertSame( (string) $text, wp_strip_all_tags( $text ) );
     86    }
     87
     88    /**
     89     * Data provider for test_wp_strip_all_tags_should_cast_scalar_values_to_string()/
     90     *
     91     * @return array[]
     92     */
     93    public function data_wp_strip_all_tags_should_cast_scalar_values_to_string() {
     94        return array(
     95            '(int) 0'      => array( 'text' => 0 ),
     96            '(int) 1'      => array( 'text' => 1 ),
     97            '(int) -1'     => array( 'text' => -1 ),
     98            '(float) 0.0'  => array( 'text' => 0.0 ),
     99            '(float) 1.0'  => array( 'text' => 1.0 ),
     100            '(float) -1.0' => array( 'text' => -1.0 ),
     101            '(bool) false' => array( 'text' => false ),
     102            '(bool) true'  => array( 'text' => true ),
     103        );
     104    }
    34105}
    35106
Note: See TracChangeset for help on using the changeset viewer.