@@ -492,4 +492,39 @@ public static function data_unallowed_modifiable_text_updates() {
492
492
'SCRIPT with </script attributes> ' => array ( '<script language="plaintext">Replace me</script> ' , 'before</script id=sneak>after ' ),
493
493
);
494
494
}
495
+
496
+ /**
497
+ * Ensures that script tag contents are safely updated.
498
+ *
499
+ * @ticket 62797
500
+ *
501
+ * @dataProvider data_script_tag_text_updates
502
+ *
503
+ * @param string $html HTML containing a SCRIPT tag to be modified.
504
+ * @param string $update Update containing possibly-compromising text.
505
+ * @param string $expected Expected result.
506
+ */
507
+ public function test_safely_updates_dangerous_javascript_script_tag_contents ( string $ html , string $ update , string $ expected ) {
508
+ $ processor = new WP_HTML_Tag_Processor ( $ html );
509
+ $ this ->assertTrue ( $ processor ->next_tag ( 'SCRIPT ' ) );
510
+ $ this ->assertTrue ( $ processor ->set_modifiable_text ( $ update ) );
511
+ $ this ->assertSame ( $ expected , $ processor ->get_updated_html () );
512
+ }
513
+
514
+ /**
515
+ * Data provider.
516
+ *
517
+ * @return array[]
518
+ */
519
+ public static function data_script_tag_text_updates (): array {
520
+ return array (
521
+ 'Simple update ' => array ( '<script></script> ' , '{} ' , '<script>{}</script> ' ),
522
+ 'var script;1<script>0 ' => array ( '<script></script> ' , 'var script;1<script>0 ' , '<script>var script;1<\u0073cript>0</script> ' ),
523
+ '1</script>/ ' => array ( '<script></script> ' , '1</script>/ ' , '<script>1</\u0073cript>/</script> ' ),
524
+ 'var SCRIPT;1<SCRIPT>0 ' => array ( '<script></script> ' , 'var SCRIPT;1<SCRIPT>0 ' , '<script>var SCRIPT;1<\u0053CRIPT>0</script> ' ),
525
+ '1</SCRIPT>/ ' => array ( '<script></script> ' , '1</SCRIPT>/ ' , '<script>1</\u0053CRIPT>/</script> ' ),
526
+ '"</script>" ' => array ( '<script></script> ' , '"</script>" ' , '<script>"</\u0073cript>"</script> ' ),
527
+ '"</ScRiPt>" ' => array ( '<script></script> ' , '"</ScRiPt>" ' , '<script>"</\u0053cRiPt>"</script> ' ),
528
+ );
529
+ }
495
530
}
0 commit comments