@@ -157,19 +157,13 @@ public function test_update_block_with_value_from_source( $bound_attribute, $blo
157157
158158 public function data_different_get_value_callbacks () {
159159 return array (
160- 'pass arguments to source ' => array (
160+ 'pass arguments to source ' => array (
161161 function ( $ source_args , $ block_instance , $ attribute_name ) {
162162 $ value = $ source_args ['key ' ];
163163 return "The attribute name is ' $ attribute_name' and its binding has argument 'key' with value ' $ value'. " ;
164164 },
165165 "<p class= \"wp-block-paragraph \">The attribute name is 'content' and its binding has argument 'key' with value 'test'.</p> " ,
166166 ),
167- 'unsafe HTML should be sanitized ' => array (
168- function () {
169- return '<script>alert("Unsafe HTML")</script> ' ;
170- },
171- '<p class="wp-block-paragraph">alert("Unsafe HTML")</p> ' ,
172- ),
173167 'symbols and numbers should be rendered correctly ' => array (
174168 function () {
175169 return '$12.50 ' ;
@@ -211,6 +205,35 @@ public function test_different_get_value_callbacks( $get_value_callback, $expect
211205 );
212206 }
213207
208+ /**
209+ * Tests that unsafe HTML returned by the source is sanitized.
210+ *
211+ * @covers ::register_block_bindings_source
212+ */
213+ public function test_unsafe_html_from_source_is_sanitized () {
214+ register_block_bindings_source (
215+ self ::SOURCE_NAME ,
216+ array (
217+ 'label ' => self ::SOURCE_LABEL ,
218+ 'get_value_callback ' => function () {
219+ return '<script>alert("Unsafe HTML")</script> ' ;
220+ },
221+ )
222+ );
223+
224+ $ block_content = <<<HTML
225+ <!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source"}}}} -->
226+ <p>This should not appear</p>
227+ <!-- /wp:paragraph -->
228+ HTML ;
229+ $ parsed_blocks = parse_blocks ( $ block_content );
230+ $ block = new WP_Block ( $ parsed_blocks [0 ] );
231+ $ result = $ block ->render ();
232+
233+ $ processor = new WP_HTML_Tag_Processor ( $ result );
234+ $ this ->assertFalse ( $ processor ->next_tag ( 'SCRIPT ' ), 'The SCRIPT element should be removed. ' );
235+ }
236+
214237 /**
215238 * Tests passing `uses_context` as argument to the source.
216239 *
@@ -500,9 +523,10 @@ public function test_update_list_item_source_image_renders_after_kses() {
500523 $ block_content
501524 );
502525
503- $ this ->assertMatchesRegularExpression (
504- '# <li>Bound <img[^>]* class="wp-image-42"[^>]* src="https://example\ .com/inline-image\ .jpg"[^>]* alt="Inline image"[^>]*/?> </li># ' ,
526+ $ this ->assertEqualHTML (
527+ '<li>Bound <img class="wp-image-42" src="https://example.com/inline-image.jpg" alt="Inline image"> </li> ' ,
505528 $ result ,
529+ '<body> ' ,
506530 'The source image should render in the list item content after sanitization. '
507531 );
508532 }
@@ -519,7 +543,7 @@ public function test_update_list_item_unsafe_source_markup_is_sanitized() {
519543<!-- /wp:list-item -->
520544HTML ;
521545 $ result = $ this ->render_list_item_with_source_value (
522- 'Bound <img src="https://example.com/inline-image.jpg" alt="Inline image" onerror="alert(1)" /> <script>alert("Unsafe HTML")</script> ' ,
546+ 'Bound <img src="https://example.com/inline-image.jpg" alt="Inline image" onerror="alert(1)" /><script>alert("Unsafe HTML")</script> ' ,
523547 $ block_content
524548 );
525549
@@ -533,9 +557,10 @@ public function test_update_list_item_unsafe_source_markup_is_sanitized() {
533557 $ result ,
534558 'Event handler attributes should be stripped from list item source content. '
535559 );
536- $ this ->assertMatchesRegularExpression (
537- '#<img[^>]* src="https://example\ .com/inline-image\ .jpg"[^>]* alt="Inline image"[^>]*/?># ' ,
560+ $ this ->assertEqualHTML (
561+ '<li>Bound <img src="https://example.com/inline-image.jpg" alt="Inline image"></li> ' ,
538562 $ result ,
563+ '<body> ' ,
539564 'Safe image markup should remain after sanitization. '
540565 );
541566 }
0 commit comments