Skip to content

Commit 353fa40

Browse files
committed
Add test for unclosed script tag
1 parent 632ead6 commit 353fa40

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

tests/phpunit/tests/blocks/editor.php

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* @group blocks
1010
*/
1111
class Tests_Blocks_Editor extends WP_UnitTestCase {
12-
1312
/**
1413
* Sets up each test method.
1514
*/
@@ -631,8 +630,8 @@ function filter_add_preload_paths( $preload_paths, WP_Block_Editor_Context $cont
631630

632631
$after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] );
633632
$this->assertStringContainsString( 'wp.apiFetch.createPreloadingMiddleware', $after );
634-
$this->assertStringContainsString( '"\/wp\/v2\/blocks"', $after );
635-
$this->assertStringContainsString( '"\/wp\/v2\/types"', $after );
633+
$this->assertStringContainsString( '"/wp/v2/blocks"', $after );
634+
$this->assertStringContainsString( '"/wp/v2/types"', $after );
636635
}
637636

638637
/**
@@ -697,28 +696,75 @@ public function data_block_editor_rest_api_preload_adds_missing_leading_slash()
697696
return array(
698697
'a string without a slash' => array(
699698
'preload_paths' => array( 'wp/v2/blocks' ),
700-
'expected' => '\/wp\/v2\/blocks',
699+
'expected' => '/wp/v2/blocks',
701700
),
702701
'a string with a slash' => array(
703702
'preload_paths' => array( '/wp/v2/blocks' ),
704-
'expected' => '\/wp\/v2\/blocks',
703+
'expected' => '/wp/v2/blocks',
705704
),
706705
'a string starting with a question mark' => array(
707706
'preload_paths' => array( '?context=edit' ),
708707
'expected' => '/?context=edit',
709708
),
710709
'an array with a string without a slash' => array(
711710
'preload_paths' => array( array( 'wp/v2/blocks', 'OPTIONS' ) ),
712-
'expected' => '\/wp\/v2\/blocks',
711+
'expected' => '/wp/v2/blocks',
713712
),
714713
'an array with a string with a slash' => array(
715714
'preload_paths' => array( array( '/wp/v2/blocks', 'OPTIONS' ) ),
716-
'expected' => '\/wp\/v2\/blocks',
715+
'expected' => '/wp/v2/blocks',
717716
),
718717
'an array with a string starting with a question mark' => array(
719718
'preload_paths' => array( array( '?context=edit', 'OPTIONS' ) ),
720-
'expected' => '\/?context=edit',
719+
'expected' => '/?context=edit',
721720
),
722721
);
723722
}
723+
724+
/**
725+
* @ticket 62797
726+
*
727+
* @covers ::block_editor_rest_api_preload
728+
*
729+
* Some valid JSON-encoded data is dangerous to embed in HTML without appropriate
730+
* escaping. This test includes prints an example of such data that would prevent
731+
* the enclosing `<script>` from closing on its apparent closer and remain open.
732+
*/
733+
public function test_ensure_preload_data_script_tag_closes() {
734+
add_theme_support( 'html5', array( 'script' ) );
735+
register_rest_route(
736+
'test/v0',
737+
'test-62797',
738+
array(
739+
'methods' => 'GET',
740+
'callback' => function () {
741+
return '<!-- unclosed comment and a script tag <script></script>';
742+
},
743+
'permission_callback' => '__return_true',
744+
)
745+
);
746+
747+
// Prevent a bunch of noisy or unstable data from being included in the test output.
748+
wp_scripts()->registered['wp-api-fetch']->ver = 'test';
749+
wp_scripts()->registered['wp-api-fetch']->extra['after'] = array();
750+
751+
block_editor_rest_api_preload(
752+
array( '/test/v0/test-62797' ),
753+
new WP_Block_Editor_Context()
754+
);
755+
756+
ob_start();
757+
wp_scripts()->do_item( 'wp-api-fetch' );
758+
$output = ob_get_clean();
759+
760+
$baseurl = site_url();
761+
$expected = <<<HTML
762+
<script src="{$baseurl}/wp-includes/js/dist/api-fetch.min.js?ver=test" id="wp-api-fetch-js"></script>
763+
<script id="wp-api-fetch-js-after">
764+
wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( {"/test/v0/test-62797":{"body":["\\u003C!-- unclosed comment and a script tag \\u003Cscript\\u003E\\u003C/script\\u003E"],"headers":{"Allow":"GET"}}} ) );
765+
</script>
766+
767+
HTML;
768+
$this->assertEqualHTML( $expected, $output );
769+
}
724770
}

0 commit comments

Comments
 (0)