SafeMarkupKernelTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Drupal\KernelTests\Component\Utility;
  3. use Drupal\Component\Utility\SafeMarkup;
  4. use Drupal\Core\Url;
  5. use Drupal\KernelTests\KernelTestBase;
  6. /**
  7. * Provides a test covering integration of SafeMarkup with other systems.
  8. *
  9. * @group Utility
  10. */
  11. class SafeMarkupKernelTest extends KernelTestBase {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public static $modules = ['system'];
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected function setUp() {
  20. parent::setUp();
  21. $this->container->get('router.builder')->rebuild();
  22. }
  23. /**
  24. * Gets arguments for SafeMarkup::format() based on Url::fromUri() parameters.
  25. *
  26. * @param string $uri
  27. * The URI of the resource.
  28. * @param array $options
  29. * The options to pass to Url::fromUri().
  30. *
  31. * @return array
  32. * Array containing:
  33. * - ':url': A URL string.
  34. */
  35. protected static function getSafeMarkupUriArgs($uri, $options = []) {
  36. $args[':url'] = Url::fromUri($uri, $options)->toString();
  37. return $args;
  38. }
  39. /**
  40. * Tests URL ":placeholders" in SafeMarkup::format().
  41. *
  42. * @dataProvider providerTestSafeMarkupUri
  43. */
  44. public function testSafeMarkupUri($string, $uri, $options, $expected) {
  45. $args = self::getSafeMarkupUriArgs($uri, $options);
  46. $this->assertEquals($expected, SafeMarkup::format($string, $args));
  47. }
  48. /**
  49. * @return array
  50. */
  51. public function providerTestSafeMarkupUri() {
  52. $data = [];
  53. $data['routed-url'] = [
  54. 'Hey giraffe <a href=":url">MUUUH</a>',
  55. 'route:system.admin',
  56. [],
  57. 'Hey giraffe <a href="/admin">MUUUH</a>',
  58. ];
  59. $data['routed-with-query'] = [
  60. 'Hey giraffe <a href=":url">MUUUH</a>',
  61. 'route:system.admin',
  62. ['query' => ['bar' => 'baz#']],
  63. 'Hey giraffe <a href="/admin?bar=baz%23">MUUUH</a>',
  64. ];
  65. $data['routed-with-fragment'] = [
  66. 'Hey giraffe <a href=":url">MUUUH</a>',
  67. 'route:system.admin',
  68. ['fragment' => 'bar&lt;'],
  69. 'Hey giraffe <a href="/admin#bar&amp;lt;">MUUUH</a>',
  70. ];
  71. $data['unrouted-url'] = [
  72. 'Hey giraffe <a href=":url">MUUUH</a>',
  73. 'base://foo',
  74. [],
  75. 'Hey giraffe <a href="/foo">MUUUH</a>',
  76. ];
  77. $data['unrouted-with-query'] = [
  78. 'Hey giraffe <a href=":url">MUUUH</a>',
  79. 'base://foo',
  80. ['query' => ['bar' => 'baz#']],
  81. 'Hey giraffe <a href="/foo?bar=baz%23">MUUUH</a>',
  82. ];
  83. $data['unrouted-with-fragment'] = [
  84. 'Hey giraffe <a href=":url">MUUUH</a>',
  85. 'base://foo',
  86. ['fragment' => 'bar&lt;'],
  87. 'Hey giraffe <a href="/foo#bar&amp;lt;">MUUUH</a>',
  88. ];
  89. $data['mailto-protocol'] = [
  90. 'Hey giraffe <a href=":url">MUUUH</a>',
  91. 'mailto:test@example.com',
  92. [],
  93. 'Hey giraffe <a href="mailto:test@example.com">MUUUH</a>',
  94. ];
  95. return $data;
  96. }
  97. /**
  98. * @dataProvider providerTestSafeMarkupUriWithException
  99. */
  100. public function testSafeMarkupUriWithExceptionUri($string, $uri) {
  101. // Should throw an \InvalidArgumentException, due to Uri::toString().
  102. $this->setExpectedException(\InvalidArgumentException::class);
  103. $args = self::getSafeMarkupUriArgs($uri);
  104. SafeMarkup::format($string, $args);
  105. }
  106. /**
  107. * @return array
  108. */
  109. public function providerTestSafeMarkupUriWithException() {
  110. $data = [];
  111. $data['js-protocol'] = [
  112. 'Hey giraffe <a href=":url">MUUUH</a>',
  113. "javascript:alert('xss')",
  114. ];
  115. $data['js-with-fromCharCode'] = [
  116. 'Hey giraffe <a href=":url">MUUUH</a>',
  117. "javascript:alert(String.fromCharCode(88,83,83))",
  118. ];
  119. $data['non-url-with-colon'] = [
  120. 'Hey giraffe <a href=":url">MUUUH</a>',
  121. "llamas: they are not URLs",
  122. ];
  123. $data['non-url-with-html'] = [
  124. 'Hey giraffe <a href=":url">MUUUH</a>',
  125. '<span>not a url</span>',
  126. ];
  127. return $data;
  128. }
  129. }