Make WordPress Core

Changeset 60887


Ignore:
Timestamp:
10/01/2025 12:57:19 PM (10 months ago)
Author:
jonsurrell
Message:

HTML API: Ensure non-string HTML input is safely handled.

Prevents an issue where passing null to HTML API constructors could result in runtime errors.

Developed in https://github.com/WordPress/wordpress-develop/pull/9545.

Props kraftbj, jonsurrell, westonruter.
Fixes #63854.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/html-api/class-wp-html-processor.php

    r60647 r60887  
    298298                }
    299299
     300                if ( ! is_string( $html ) ) {
     301                        _doing_it_wrong(
     302                                __METHOD__,
     303                                __( 'The HTML parameter must be a string.' ),
     304                                '6.9.0'
     305                        );
     306                        return null;
     307                }
     308
    300309                $context_processor = static::create_full_parser( "<!DOCTYPE html>{$context}", $encoding );
    301310                if ( null === $context_processor ) {
     
    338347        public static function create_full_parser( $html, $known_definite_encoding = 'UTF-8' ) {
    339348                if ( 'UTF-8' !== $known_definite_encoding ) {
     349                        return null;
     350                }
     351                if ( ! is_string( $html ) ) {
     352                        _doing_it_wrong(
     353                                __METHOD__,
     354                                __( 'The HTML parameter must be a string.' ),
     355                                '6.9.0'
     356                        );
    340357                        return null;
    341358                }
  • trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php

    r60706 r60887  
    835835         */
    836836        public function __construct( $html ) {
     837                if ( ! is_string( $html ) ) {
     838                        _doing_it_wrong(
     839                                __METHOD__,
     840                                __( 'The HTML parameter must be a string.' ),
     841                                '6.9.0'
     842                        );
     843                        $html = '';
     844                }
    837845                $this->html = $html;
    838846        }
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessor.php

    r59467 r60887  
    3535        public function test_warns_that_the_static_creator_methods_should_be_called_instead_of_the_public_constructor() {
    3636                new WP_HTML_Processor( '<p>Light roast.</p>' );
     37        }
     38
     39        /**
     40         * @ticket 63854
     41         *
     42         * @covers ::create_fragment
     43         * @expectedIncorrectUsage WP_HTML_Processor::create_fragment
     44         */
     45        public function test_create_fragment_validates_html_parameter() {
     46                $processor = WP_HTML_Processor::create_fragment( null );
     47                $this->assertNull( $processor );
     48        }
     49
     50        /**
     51         * @ticket 63854
     52         *
     53         * @covers ::create_full_parser
     54         * @expectedIncorrectUsage WP_HTML_Processor::create_full_parser
     55         */
     56        public function test_create_full_parser_validates_html_parameter() {
     57                $processor = WP_HTML_Processor::create_full_parser( null );
     58                $this->assertNull( $processor );
    3759        }
    3860
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

    r60649 r60887  
    7171                        $this->assertFalse( $processor->has_self_closing_flag(), 'Found the self-closing tag when it was absent.' );
    7272                }
     73        }
     74
     75        /**
     76         * @ticket 63854
     77         *
     78         * @covers WP_HTML_Tag_Processor::__construct
     79         * @expectedIncorrectUsage WP_HTML_Tag_Processor::__construct
     80         */
     81        public function test_constructor_validates_html_parameter() {
     82                // Test that passing null triggers _doing_it_wrong and sets HTML to empty string.
     83                $processor = new WP_HTML_Tag_Processor( null );
     84
     85                // Verify that the HTML was set to an empty string.
     86                $this->assertSame( '', $processor->get_updated_html(), 'HTML should be set to empty string when null is passed' );
     87
     88                // Verify that next_token() works without errors (indicating the processor is in a valid state).
     89                $this->assertFalse( $processor->next_token(), 'next_token() should work without errors when HTML is empty string' );
    7390        }
    7491
Note: See TracChangeset for help on using the changeset viewer.