Skip to content

Commit 4b2b478

Browse files
committed
Tests: Fix Block Bindings unsafe HTML test.
Assert that the output has no SCRIPT tag, without depending on how KSES removes it. Developed in: #14092 Follow-up to r64233. Props jonsurrell, mamaduka, andrewserong. See #66208. git-svn-id: https://develop.svn.wordpress.org/trunk@64355 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 84b4736 commit 4b2b478

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

‎tests/phpunit/tests/block-bindings/render.php‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,13 @@ public function test_update_block_with_value_from_source( $bound_attribute, $blo
182182

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'];
188188
return "The attribute name is '$attribute_name' and its binding has argument 'key' with value '$value'.";
189189
},
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 () {
200194
return '$12.50';
@@ -242,6 +236,38 @@ public function test_different_get_value_callbacks( $get_value_callback, $expect
242236
);
243237
}
244238

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 -->
262+
HTML;
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.' );
269+
}
270+
245271
/**
246272
* Tests passing `uses_context` as argument to the source.
247273
*

0 commit comments

Comments
 (0)