Skip to content

Commit c6c234c

Browse files
committed
Add dangerous script escaping tests
1 parent 481dea9 commit c6c234c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/phpunit/tests/html-api/wpHtmlTagProcessorModifiableText.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,39 @@ public static function data_unallowed_modifiable_text_updates() {
499499

500500
);
501501
}
502+
503+
/**
504+
* Ensures that script tag contents are safely updated.
505+
*
506+
* @ticket 62797
507+
*
508+
* @dataProvider data_script_tag_text_updates
509+
*
510+
* @param string $html HTML containing a SCRIPT tag to be modified.
511+
* @param string $update Update containing possibly-compromising text.
512+
* @param string $expected Expected result.
513+
*/
514+
public function test_safely_updates_dangerous_javascript_script_tag_contents( string $html, string $update, string $expected ) {
515+
$processor = new WP_HTML_Tag_Processor( $html );
516+
$this->assertTrue( $processor->next_tag( 'SCRIPT' ) );
517+
$this->assertTrue( $processor->set_modifiable_text( $update ) );
518+
$this->assertSame( $expected, $processor->get_updated_html() );
519+
}
520+
521+
/**
522+
* Data provider.
523+
*
524+
* @return array[]
525+
*/
526+
public static function data_script_tag_text_updates(): array {
527+
return array(
528+
'Simple update' => array( '<script></script>', '{}', '<script>{}</script>' ),
529+
'var script;1<script>0' => array( '<script></script>', 'var script;1<script>0', '<script>var script;1<\u0073cript>0</script>' ),
530+
'1</script>/' => array( '<script></script>', '1</script>/', '<script>1</\u0073cript>/</script>' ),
531+
'var SCRIPT;1<SCRIPT>0' => array( '<script></script>', 'var SCRIPT;1<SCRIPT>0', '<script>var SCRIPT;1<\u0053CRIPT>0</script>' ),
532+
'1</SCRIPT>/' => array( '<script></script>', '1</SCRIPT>/', '<script>1</\u0053CRIPT>/</script>' ),
533+
'"</script>"' => array( '<script></script>', '"</script>"', '<script>"</\u0073cript>"</script>' ),
534+
'"</ScRiPt>"' => array( '<script></script>', '"</ScRiPt>"', '<script>"</\u0053cRiPt>"</script>' ),
535+
);
536+
}
502537
}

0 commit comments

Comments
 (0)