filterResult.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @group oembed
  4. */
  5. class Tests_Filter_oEmbed_Result extends WP_UnitTestCase {
  6. function test_filter_oembed_result_trusted_malicious_iframe() {
  7. $html = '<p></p><iframe onload="alert(1)"></iframe>';
  8. $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), 'https://www.youtube.com/watch?v=72xdCU__XCk' );
  9. $this->assertSame( $html, $actual );
  10. }
  11. function test_filter_oembed_result_with_untrusted_provider() {
  12. $html = '<p></p><iframe onload="alert(1)" src="http://example.com/sample-page/"></iframe>';
  13. $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), 'http://example.com/sample-page/' );
  14. $matches = array();
  15. preg_match( '|src=".*#\?secret=([\w\d]+)" data-secret="([\w\d]+)"|', $actual, $matches );
  16. $this->assertTrue( isset( $matches[1] ) );
  17. $this->assertTrue( isset( $matches[2] ) );
  18. $this->assertSame( $matches[1], $matches[2] );
  19. }
  20. function test_filter_oembed_result_only_one_iframe_is_allowed() {
  21. $html = '<div><iframe></iframe><iframe></iframe><p></p></div>';
  22. $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
  23. $this->assertSame( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe>', $actual );
  24. }
  25. function test_filter_oembed_result_with_newlines() {
  26. $html = <<<EOD
  27. <script>var = 1;</script>
  28. <iframe></iframe>
  29. <iframe></iframe>
  30. <p></p>
  31. EOD;
  32. $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
  33. $this->assertSame( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe>', $actual );
  34. }
  35. function test_filter_oembed_result_without_iframe() {
  36. $html = '<span>Hello</span><p>World</p>';
  37. $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
  38. $this->assertFalse( $actual );
  39. $html = '<div><p></p></div><script></script>';
  40. $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
  41. $this->assertFalse( $actual );
  42. }
  43. function test_filter_oembed_result_secret_param_available() {
  44. $html = '<iframe src="https://wordpress.org"></iframe>';
  45. $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
  46. $matches = array();
  47. preg_match( '|src="https://wordpress.org#\?secret=([\w\d]+)" data-secret="([\w\d]+)"|', $actual, $matches );
  48. $this->assertTrue( isset( $matches[1] ) );
  49. $this->assertTrue( isset( $matches[2] ) );
  50. $this->assertSame( $matches[1], $matches[2] );
  51. }
  52. function test_filter_oembed_result_wrong_type_provided() {
  53. $actual = wp_filter_oembed_result( 'some string', (object) array( 'type' => 'link' ), '' );
  54. $this->assertSame( 'some string', $actual );
  55. }
  56. function test_filter_oembed_result_invalid_result() {
  57. $this->assertFalse( wp_filter_oembed_result( false, (object) array( 'type' => 'rich' ), '' ) );
  58. $this->assertFalse( wp_filter_oembed_result( '', (object) array( 'type' => 'rich' ), '' ) );
  59. }
  60. function test_filter_oembed_result_blockquote_adds_style_to_iframe() {
  61. $html = '<blockquote></blockquote><iframe></iframe>';
  62. $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
  63. $this->assertSame( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);"></iframe>', $actual );
  64. }
  65. function test_filter_oembed_result_allowed_html() {
  66. $html = '<blockquote class="foo" id="bar"><strong><a href="" target=""></a></strong></blockquote><iframe></iframe>';
  67. $actual = wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' );
  68. $this->assertSame( '<blockquote class="wp-embedded-content"><a href=""></a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);"></iframe>', $actual );
  69. }
  70. public function _data_oembed_test_strings() {
  71. return array(
  72. array(
  73. '<blockquote></blockquote><iframe title=""></iframe>',
  74. '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" title="Hola"></iframe>',
  75. ),
  76. array(
  77. '<blockquote class="foo" id="bar"><strong><a href="" target=""></a></strong></blockquote><iframe width=123></iframe>',
  78. '<blockquote class="wp-embedded-content"><a href=""></a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" title="Hola" width="123"></iframe>',
  79. ),
  80. array(
  81. '<blockquote><iframe width="100"></iframe></blockquote><iframe stitle="aaaa"></iframe>',
  82. '<blockquote class="wp-embedded-content"><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" title="Hola" width="100"></iframe></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" title="Hola"></iframe>',
  83. ),
  84. array(
  85. "<blockquote><iframe title=' width=\"'></iframe></blockquote><iframe title='' height=' title=' width=\"'' heigt='123'\"></iframe>",
  86. '<blockquote class="wp-embedded-content"><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" title=" width=&quot;"></iframe></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" title=" width=&quot;" height=\' title=\' width="\'\' heigt=\'123\'"></iframe>',
  87. ),
  88. );
  89. }
  90. /**
  91. * @dataProvider _data_oembed_test_strings
  92. */
  93. public function test_wp_filter_pre_oembed_custom_result( $html, $expected ) {
  94. $data = (object) array(
  95. 'type' => 'rich',
  96. 'title' => 'Hola',
  97. 'html' => $html,
  98. );
  99. $actual = _wp_oembed_get_object()->data2html( $data, 'https://untrusted.localhost' );
  100. $this->assertSame( $expected, $actual );
  101. }
  102. /**
  103. * @group feed
  104. */
  105. function test_filter_feed_content() {
  106. $html = '<blockquote></blockquote><iframe></iframe>';
  107. $actual = _oembed_filter_feed_content( wp_filter_oembed_result( $html, (object) array( 'type' => 'rich' ), '' ) );
  108. $this->assertSame( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe>', $actual );
  109. }
  110. }