BlockXssTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace Drupal\Tests\block\Functional;
  3. use Drupal\block_content\Entity\BlockContent;
  4. use Drupal\block_content\Entity\BlockContentType;
  5. use Drupal\Core\Url;
  6. use Drupal\system\Entity\Menu;
  7. use Drupal\Tests\BrowserTestBase;
  8. use Drupal\views\Entity\View;
  9. /**
  10. * Tests that the block module properly escapes block descriptions.
  11. *
  12. * @group block
  13. */
  14. class BlockXssTest extends BrowserTestBase {
  15. /**
  16. * Modules to install.
  17. *
  18. * @var array
  19. */
  20. public static $modules = ['block', 'block_content', 'menu_ui', 'views'];
  21. /**
  22. * Tests that nothing is escaped other than the blocks explicitly tested.
  23. */
  24. public function testNoUnexpectedEscaping() {
  25. $this->drupalLogin($this->drupalCreateUser(['administer blocks', 'access administration pages']));
  26. $this->drupalGet(Url::fromRoute('block.admin_display'));
  27. $this->clickLink('Place block');
  28. $this->assertNoEscaped('<');
  29. }
  30. /**
  31. * Tests XSS in title.
  32. */
  33. public function testXssInTitle() {
  34. $this->container->get('module_installer')->install(['block_test']);
  35. $this->drupalPlaceBlock('test_xss_title', ['label' => '<script>alert("XSS label");</script>']);
  36. \Drupal::state()->set('block_test.content', $this->randomMachineName());
  37. $this->drupalGet('');
  38. $this->assertNoRaw('<script>alert("XSS label");</script>', 'The block title was properly sanitized when rendered.');
  39. $this->drupalLogin($this->drupalCreateUser(['administer blocks', 'access administration pages']));
  40. $default_theme = $this->config('system.theme')->get('default');
  41. $this->drupalGet('admin/structure/block/list/' . $default_theme);
  42. $this->assertNoRaw("<script>alert('XSS subject');</script>", 'The block title was properly sanitized in Block Plugin UI Admin page.');
  43. }
  44. /**
  45. * Tests XSS in category.
  46. */
  47. public function testXssInCategory() {
  48. $this->container->get('module_installer')->install(['block_test']);
  49. $this->drupalPlaceBlock('test_xss_title');
  50. $this->drupalLogin($this->drupalCreateUser(['administer blocks', 'access administration pages']));
  51. $this->drupalGet(Url::fromRoute('block.admin_display'));
  52. $this->clickLink('Place block');
  53. $this->assertNoRaw("<script>alert('XSS category');</script>");
  54. }
  55. /**
  56. * Tests various modules that provide blocks for XSS.
  57. */
  58. public function testBlockXss() {
  59. $this->drupalLogin($this->rootUser);
  60. $this->doViewTest();
  61. $this->doMenuTest();
  62. $this->doBlockContentTest();
  63. $this->drupalGet(Url::fromRoute('block.admin_display'));
  64. $this->clickLink('Place block');
  65. $this->assertNoRaw('&amp;lt;', 'The page does not have double escaped HTML tags.');
  66. }
  67. /**
  68. * Tests XSS coming from View block labels.
  69. */
  70. protected function doViewTest() {
  71. // Create a View without a custom label for its block Display. The
  72. // admin_label of the block then becomes just the View's label.
  73. $view = View::create([
  74. 'id' => $this->randomMachineName(),
  75. 'label' => '<script>alert("view1");</script>',
  76. ]);
  77. $view->addDisplay('block');
  78. $view->save();
  79. // Create a View with a custom label for its block Display. The
  80. // admin_label of the block then becomes the View's label combined with
  81. // the Display's label.
  82. $view = View::create([
  83. 'id' => $this->randomMachineName(),
  84. 'label' => '<script>alert("view2");</script>',
  85. ]);
  86. $view->addDisplay('block', 'Fish & chips');
  87. $view->save();
  88. $this->drupalGet(Url::fromRoute('block.admin_display'));
  89. $this->clickLink('Place block');
  90. // \Drupal\views\Plugin\Derivative\ViewsBlock::getDerivativeDefinitions()
  91. // has a different code path for an admin label based only on the View
  92. // label versus one based on both the View label and the Display label.
  93. // Ensure that this test is covering both code paths by asserting the
  94. // absence of a ":" for the first View and the presence of a ":" for the
  95. // second one. Note that the second assertion is redundant with the one
  96. // further down which also checks for the Display label, but is included
  97. // here for clarity.
  98. $this->assertNoEscaped('<script>alert("view1");</script>:');
  99. $this->assertEscaped('<script>alert("view2");</script>:');
  100. // Assert that the blocks have their admin labels escaped and
  101. // don't appear anywhere unescaped.
  102. $this->assertEscaped('<script>alert("view1");</script>');
  103. $this->assertNoRaw('<script>alert("view1");</script>');
  104. $this->assertEscaped('<script>alert("view2");</script>: Fish & chips');
  105. $this->assertNoRaw('<script>alert("view2");</script>');
  106. $this->assertNoRaw('Fish & chips');
  107. // Assert the Display label doesn't appear anywhere double escaped.
  108. $this->assertNoRaw('Fish & chips');
  109. $this->assertNoRaw('Fish &amp;amp; chips');
  110. }
  111. /**
  112. * Tests XSS coming from Menu block labels.
  113. */
  114. protected function doMenuTest() {
  115. Menu::create([
  116. 'id' => $this->randomMachineName(),
  117. 'label' => '<script>alert("menu");</script>',
  118. ])->save();
  119. $this->drupalGet(Url::fromRoute('block.admin_display'));
  120. $this->clickLink('Place block');
  121. $this->assertEscaped('<script>alert("menu");</script>');
  122. $this->assertNoRaw('<script>alert("menu");</script>');
  123. }
  124. /**
  125. * Tests XSS coming from Block Content block info.
  126. */
  127. protected function doBlockContentTest() {
  128. BlockContentType::create([
  129. 'id' => 'basic',
  130. 'label' => 'basic',
  131. 'revision' => TRUE,
  132. ])->save();
  133. BlockContent::create([
  134. 'type' => 'basic',
  135. 'info' => '<script>alert("block_content");</script>',
  136. ])->save();
  137. $this->drupalGet(Url::fromRoute('block.admin_display'));
  138. $this->clickLink('Place block');
  139. $this->assertEscaped('<script>alert("block_content");</script>');
  140. $this->assertNoRaw('<script>alert("block_content");</script>');
  141. }
  142. }