SafeMarkupTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Component\Utility\SafeMarkupTest.
  5. */
  6. namespace Drupal\Tests\Component\Utility;
  7. use Drupal\Component\Render\HtmlEscapedText;
  8. use Drupal\Component\Utility\SafeMarkup;
  9. use Drupal\Component\Render\MarkupInterface;
  10. use Drupal\Component\Render\MarkupTrait;
  11. use Drupal\Component\Utility\UrlHelper;
  12. use PHPUnit\Framework\TestCase;
  13. /**
  14. * Tests marking strings as safe.
  15. *
  16. * @group Utility
  17. * @group legacy
  18. * @coversDefaultClass \Drupal\Component\Utility\SafeMarkup
  19. */
  20. class SafeMarkupTest extends TestCase {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function tearDown() {
  25. parent::tearDown();
  26. UrlHelper::setAllowedProtocols(['http', 'https']);
  27. }
  28. /**
  29. * Tests SafeMarkup::isSafe() with different objects.
  30. *
  31. * @covers ::isSafe
  32. * @expectedDeprecation SafeMarkup::isSafe() is scheduled for removal in Drupal 9.0.0. Instead, you should just check if a variable is an instance of \Drupal\Component\Render\MarkupInterface. See https://www.drupal.org/node/2549395.
  33. */
  34. public function testIsSafe() {
  35. $safe_string = $this->getMockBuilder('\Drupal\Component\Render\MarkupInterface')->getMock();
  36. $this->assertTrue(SafeMarkup::isSafe($safe_string));
  37. $string_object = new SafeMarkupTestString('test');
  38. $this->assertFalse(SafeMarkup::isSafe($string_object));
  39. }
  40. /**
  41. * Tests SafeMarkup::checkPlain().
  42. *
  43. * @dataProvider providerCheckPlain
  44. * @covers ::checkPlain
  45. * @expectedDeprecation SafeMarkup::checkPlain() is scheduled for removal in Drupal 9.0.0. Rely on Twig's auto-escaping feature, or use the @link theme_render #plain_text @endlink key when constructing a render array that contains plain text in order to use the renderer's auto-escaping feature. If neither of these are possible, \Drupal\Component\Utility\Html::escape() can be used in places where explicit escaping is needed. See https://www.drupal.org/node/2549395.
  46. *
  47. * @param string $text
  48. * The text to provide to SafeMarkup::checkPlain().
  49. * @param string $expected
  50. * The expected output from the function.
  51. * @param string $message
  52. * The message to provide as output for the test.
  53. */
  54. public function testCheckPlain($text, $expected, $message) {
  55. $result = SafeMarkup::checkPlain($text);
  56. $this->assertInstanceOf(HtmlEscapedText::class, $result);
  57. $this->assertEquals($expected, $result, $message);
  58. }
  59. /**
  60. * Tests Drupal\Component\Render\HtmlEscapedText.
  61. *
  62. * Verifies that the result of SafeMarkup::checkPlain() is the same as using
  63. * HtmlEscapedText directly.
  64. *
  65. * @dataProvider providerCheckPlain
  66. *
  67. * @param string $text
  68. * The text to provide to the HtmlEscapedText constructor.
  69. * @param string $expected
  70. * The expected output from the function.
  71. * @param string $message
  72. * The message to provide as output for the test.
  73. */
  74. public function testHtmlEscapedText($text, $expected, $message) {
  75. $result = new HtmlEscapedText($text);
  76. $this->assertEquals($expected, $result, $message);
  77. }
  78. /**
  79. * Data provider for testCheckPlain() and testHtmlEscapedText().
  80. *
  81. * @see testCheckPlain()
  82. * @see testHtmlEscapedText()
  83. */
  84. public function providerCheckPlain() {
  85. // Checks that invalid multi-byte sequences are escaped.
  86. $tests[] = ["Foo\xC0barbaz", 'Foo�barbaz', 'Escapes invalid sequence "Foo\xC0barbaz"'];
  87. $tests[] = ["\xc2\"", '�&quot;', 'Escapes invalid sequence "\xc2\""'];
  88. $tests[] = ["Fooÿñ", "Fooÿñ", 'Does not escape valid sequence "Fooÿñ"'];
  89. // Checks that special characters are escaped.
  90. $tests[] = [SafeMarkupTestMarkup::create("<script>"), '&lt;script&gt;', 'Escapes &lt;script&gt; even inside an object that implements MarkupInterface.'];
  91. $tests[] = ["<script>", '&lt;script&gt;', 'Escapes &lt;script&gt;'];
  92. $tests[] = ['<>&"\'', '&lt;&gt;&amp;&quot;&#039;', 'Escapes reserved HTML characters.'];
  93. $tests[] = [SafeMarkupTestMarkup::create('<>&"\''), '&lt;&gt;&amp;&quot;&#039;', 'Escapes reserved HTML characters even inside an object that implements MarkupInterface.'];
  94. return $tests;
  95. }
  96. /**
  97. * Tests string formatting with SafeMarkup::format().
  98. *
  99. * @dataProvider providerFormat
  100. * @covers ::format
  101. * @expectedDeprecation SafeMarkup::format() is scheduled for removal in Drupal 9.0.0. Use \Drupal\Component\Render\FormattableMarkup. See https://www.drupal.org/node/2549395.
  102. *
  103. * @param string $string
  104. * The string to run through SafeMarkup::format().
  105. * @param string[] $args
  106. * The arguments to pass into SafeMarkup::format().
  107. * @param string $expected
  108. * The expected result from calling the function.
  109. * @param string $message
  110. * The message to display as output to the test.
  111. * @param bool $expected_is_safe
  112. * Whether the result is expected to be safe for HTML display.
  113. */
  114. public function testFormat($string, array $args, $expected, $message, $expected_is_safe) {
  115. UrlHelper::setAllowedProtocols(['http', 'https', 'mailto']);
  116. $result = SafeMarkup::format($string, $args);
  117. $this->assertEquals($expected, (string) $result, $message);
  118. if ($expected_is_safe) {
  119. $this->assertInstanceOf(MarkupInterface::class, $result);
  120. }
  121. else {
  122. $this->assertNotInstanceOf(MarkupInterface::class, $result);
  123. }
  124. }
  125. /**
  126. * Data provider for testFormat().
  127. *
  128. * @see testFormat()
  129. */
  130. public function providerFormat() {
  131. $tests[] = ['Simple text', [], 'Simple text', 'SafeMarkup::format leaves simple text alone.', TRUE];
  132. $tests[] = ['Escaped text: @value', ['@value' => '<script>'], 'Escaped text: &lt;script&gt;', 'SafeMarkup::format replaces and escapes string.', TRUE];
  133. $tests[] = ['Escaped text: @value', ['@value' => SafeMarkupTestMarkup::create('<span>Safe HTML</span>')], 'Escaped text: <span>Safe HTML</span>', 'SafeMarkup::format does not escape an already safe string.', TRUE];
  134. $tests[] = ['Placeholder text: %value', ['%value' => '<script>'], 'Placeholder text: <em class="placeholder">&lt;script&gt;</em>', 'SafeMarkup::format replaces, escapes and themes string.', TRUE];
  135. $tests[] = ['Placeholder text: %value', ['%value' => SafeMarkupTestMarkup::create('<span>Safe HTML</span>')], 'Placeholder text: <em class="placeholder"><span>Safe HTML</span></em>', 'SafeMarkup::format does not escape an already safe string themed as a placeholder.', TRUE];
  136. $tests['javascript-protocol-url'] = ['Simple text <a href=":url">giraffe</a>', [':url' => 'javascript://example.com?foo&bar'], 'Simple text <a href="//example.com?foo&amp;bar">giraffe</a>', 'Support for filtering bad protocols', TRUE];
  137. $tests['external-url'] = ['Simple text <a href=":url">giraffe</a>', [':url' => 'http://example.com?foo&bar'], 'Simple text <a href="http://example.com?foo&amp;bar">giraffe</a>', 'Support for filtering bad protocols', TRUE];
  138. $tests['relative-url'] = ['Simple text <a href=":url">giraffe</a>', [':url' => '/node/1?foo&bar'], 'Simple text <a href="/node/1?foo&amp;bar">giraffe</a>', 'Support for filtering bad protocols', TRUE];
  139. $tests['fragment-with-special-chars'] = ['Simple text <a href=":url">giraffe</a>', [':url' => 'http://example.com/#&lt;'], 'Simple text <a href="http://example.com/#&amp;lt;">giraffe</a>', 'Support for filtering bad protocols', TRUE];
  140. $tests['mailto-protocol'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => 'mailto:test@example.com'], 'Hey giraffe <a href="mailto:test@example.com">MUUUH</a>', '', TRUE];
  141. $tests['js-with-fromCharCode'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "javascript:alert(String.fromCharCode(88,83,83))"], 'Hey giraffe <a href="alert(String.fromCharCode(88,83,83))">MUUUH</a>', '', TRUE];
  142. // Test some "URL" values that are not RFC 3986 compliant URLs. The result
  143. // of SafeMarkup::format() should still be valid HTML (other than the
  144. // value of the "href" attribute not being a valid URL), and not
  145. // vulnerable to XSS.
  146. $tests['non-url-with-colon'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "llamas: they are not URLs"], 'Hey giraffe <a href=" they are not URLs">MUUUH</a>', '', TRUE];
  147. $tests['non-url-with-html'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "<span>not a url</span>"], 'Hey giraffe <a href="&lt;span&gt;not a url&lt;/span&gt;">MUUUH</a>', '', TRUE];
  148. // Tests non-standard placeholders that will not replace.
  149. $tests['non-standard-placeholder'] = ['Hey hey', ['risky' => "<script>alert('foo');</script>"], 'Hey hey', '', TRUE];
  150. return $tests;
  151. }
  152. }
  153. class SafeMarkupTestString {
  154. protected $string;
  155. public function __construct($string) {
  156. $this->string = $string;
  157. }
  158. public function __toString() {
  159. return $this->string;
  160. }
  161. }
  162. /**
  163. * Marks an object's __toString() method as returning markup.
  164. */
  165. class SafeMarkupTestMarkup implements MarkupInterface {
  166. use MarkupTrait;
  167. }