NodeTitleXSSTest.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\Component\Utility\Html;
  4. /**
  5. * Create a node with dangerous tags in its title and test that they are
  6. * escaped.
  7. *
  8. * @group node
  9. */
  10. class NodeTitleXSSTest extends NodeTestBase {
  11. /**
  12. * Tests XSS functionality with a node entity.
  13. */
  14. public function testNodeTitleXSS() {
  15. // Prepare a user to do the stuff.
  16. $web_user = $this->drupalCreateUser(['create page content', 'edit any page content']);
  17. $this->drupalLogin($web_user);
  18. $xss = '<script>alert("xss")</script>';
  19. $title = $xss . $this->randomMachineName();
  20. $edit = [];
  21. $edit['title[0][value]'] = $title;
  22. $this->drupalPostForm('node/add/page', $edit, t('Preview'));
  23. $this->assertNoRaw($xss, 'Harmful tags are escaped when previewing a node.');
  24. $settings = ['title' => $title];
  25. $node = $this->drupalCreateNode($settings);
  26. $this->drupalGet('node/' . $node->id());
  27. // Titles should be escaped.
  28. $this->assertRaw('<title>' . Html::escape($title) . ' | Drupal</title>', 'Title is displayed when viewing a node.');
  29. $this->assertNoRaw($xss, 'Harmful tags are escaped when viewing a node.');
  30. $this->drupalGet('node/' . $node->id() . '/edit');
  31. $this->assertNoRaw($xss, 'Harmful tags are escaped when editing a node.');
  32. }
  33. }