Make WordPress Core

Changeset 64355


Ignore:
Timestamp:
10/15/2026 08:08:11 AM (5 weeks ago)
Author:
jonsurrell
Message:

Tests: Fix Block Bindings unsafe HTML test.

Assert that the output has no SCRIPT tag, without depending on how KSES removes it.

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

Follow-up to r64233.

Props jonsurrell, mamaduka, andrewserong.
See #66208.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-bindings/render.php

    r64233 r64355  
    183183        public function data_different_get_value_callbacks() {
    184184                return array(
    185                         'pass arguments to source'        => array(
     185                        'pass arguments to source' => array(
    186186                                function ( $source_args, $block_instance, $attribute_name ) {
    187187                                        $value = $source_args['key'];
    … …  
    190190                                "<p class=\"wp-block-paragraph\">The attribute name is 'content' and its binding has argument 'key' with value 'test'.</p>",
    191191                        ),
    192                         'unsafe HTML should be sanitized' => array(
    193                                 function () {
    194                                         return '<script>alert("Unsafe HTML")</script>';
    195                                 },
    196                                 '<p class="wp-block-paragraph"></p>',
    197                         ),
    198192                        'symbols and numbers should be rendered correctly' => array(
    199193                                function () {
    … …  
    241235                        'The block content should be updated with the value returned by the source.'
    242236                );
     237        }
     238
     239        /**
     240         * Tests that unsafe HTML returned by the source is sanitized.
     241         *
     242         * @ticket 60651
     243         * @ticket 66208
     244         *
     245         * @covers ::register_block_bindings_source
     246         */
     247        public function test_unsafe_html_from_source_is_sanitized() {
     248                register_block_bindings_source(
     249                        self::SOURCE_NAME,
     250                        array(
     251                                'label'              => self::SOURCE_LABEL,
     252                                'get_value_callback' => function () {
     253                                        return '<script>alert("Unsafe HTML")</script>';
     254                                },
     255                        )
     256                );
     257
     258                $block_content = <<<HTML
     259<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source"}}}} -->
     260<p>This should not appear</p>
     261<!-- /wp:paragraph -->
     262HTML;
     263                $parsed_blocks = parse_blocks( $block_content );
     264                $block         = new WP_Block( $parsed_blocks[0] );
     265                $result        = $block->render();
     266
     267                $processor = new WP_HTML_Tag_Processor( $result );
     268                $this->assertFalse( $processor->next_tag( 'SCRIPT' ), 'The SCRIPT element should be removed.' );
    243269        }
    244270
Note: See TracChangeset for help on using the changeset viewer.