metatag.xss.test 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Tests Metatag module to ensure there are no XSS scripting vulnerabilities.
  4. */
  5. class MetatagCoreXSSTest extends MetatagTestHelper {
  6. /**
  7. * String that causes an alert when page titles aren't filtered for xss.
  8. *
  9. * @var string
  10. */
  11. private $xssTitleString = '<script>alert("xss");</script>';
  12. /**
  13. * String that causes an alert when metatags aren't filtered for xss.
  14. *
  15. * @var string
  16. */
  17. private $xssString = '"><script>alert("xss");</script><meta "';
  18. /**
  19. * Rendered xss tag that has escaped attribute to avoid xss injection.
  20. *
  21. * @var string
  22. */
  23. private $escapedXssTag = '<meta name="abstract" content="&quot;&gt;alert(&quot;xss&quot;);" />';
  24. /**
  25. * String that causes an alert when metatags aren't filtered for xss.
  26. *
  27. * "Image" meta tags are processed differently to others, so this checks for a
  28. * different string.
  29. *
  30. * @var string
  31. */
  32. private $xssImageString = '/"><script>alert("image xss");</script><meta "';
  33. /**
  34. * Rendered xss tag that has escaped attribute to avoid xss injection.
  35. *
  36. * @var string
  37. */
  38. private $escapedXssImageTag = '<link rel="image_src" href="/&quot;&gt;alert(&quot;image%20xss&quot;);" />';
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function getInfo() {
  43. return array(
  44. 'name' => 'Metatag core tests for XSS.',
  45. 'description' => 'Test Metatag for XSS vulnerabilities.',
  46. 'group' => 'Metatag',
  47. );
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. function setUp(array $modules = array()) {
  53. parent::setUp($modules);
  54. $content_type = 'page';
  55. // Create an admin user and log them in.
  56. $perms = array(
  57. // Needed for the content type.
  58. 'create ' . $content_type . ' content',
  59. 'delete any ' . $content_type . ' content',
  60. 'edit any ' . $content_type . ' content',
  61. // This permission is required in order to create new revisions.
  62. 'administer nodes',
  63. );
  64. $this->adminUser = $this->createAdminUser($perms);
  65. $this->drupalLogin($this->adminUser);
  66. }
  67. /**
  68. * Verify XSS injected in global Node config is not rendered.
  69. */
  70. function testXssMetatagConfig() {
  71. // Submit the form with some example XSS values.
  72. $this->drupalGet('admin/config/search/metatags/config/global');
  73. $this->assertResponse(200);
  74. $edit = array(
  75. 'metatags[und][title][value]' => $this->xssTitleString,
  76. 'metatags[und][abstract][value]' => $this->xssString,
  77. 'metatags[und][image_src][value]' => $this->xssImageString,
  78. );
  79. $this->drupalPost(NULL, $edit, t('Save'));
  80. $this->assertResponse(200);
  81. // Use front page to test.
  82. $this->drupalGet('<front>');
  83. // Verify title is clean.
  84. $this->assertNoTitle($this->xssTitleString);
  85. $this->assertNoRaw($this->xssTitleString);
  86. // Verify the abstract is clean.
  87. $this->assertRaw($this->escapedXssTag);
  88. $this->assertNoRaw($this->xssString);
  89. // Verify the image_src is clean.
  90. $this->assertRaw($this->escapedXssImageTag);
  91. $this->assertNoRaw($this->xssImageString);
  92. }
  93. /**
  94. * Verify XSS injected in the entity metatag override field is not rendered.
  95. */
  96. public function testXssEntityOverride() {
  97. $title = 'Test Page';
  98. // Load a page node.
  99. $this->drupalGet('node/add/page');
  100. $this->assertResponse(200);
  101. // Submit the node with some example XSS values.
  102. $edit = array(
  103. 'title' => $title,
  104. 'metatags[und][title][value]' => $this->xssTitleString,
  105. 'metatags[und][description][value]' => $this->xssString,
  106. 'metatags[und][abstract][value]' => $this->xssString,
  107. );
  108. $this->drupalPost(NULL, $edit, t('Save'));
  109. // Verify the page saved.
  110. $this->assertResponse(200);
  111. $this->assertText(t('Basic page @title has been created.', array('@title' => $title)));
  112. // Verify title is not the injected string and thus cleaned.
  113. $this->assertNoTitle($this->xssTitleString);
  114. $this->assertNoRaw($this->xssTitleString);
  115. // Verify the description and abstract are clean.
  116. $this->assertRaw($this->escapedXssTag);
  117. $this->assertNoRaw($this->xssString);
  118. }
  119. /**
  120. * Verify XSS injected in the entity titles are not rendered.
  121. */
  122. public function testXssEntityTitle() {
  123. // Load a page node.
  124. $this->drupalGet('node/add/page');
  125. $this->assertResponse(200);
  126. // Submit the node with some example XSS values.
  127. $edit = array(
  128. 'title' => $this->xssTitleString,
  129. 'body[und][0][value]' => 'hello world',
  130. );
  131. $this->drupalPost(NULL, $edit, t('Save'));
  132. // Verify the page saved.
  133. $this->assertResponse(200);
  134. $this->assertText(t('has been created.'));
  135. // Verify title is not the injected string and thus cleaned.
  136. $this->assertNoRaw($this->xssTitleString);
  137. }
  138. /**
  139. * Verify XSS injected in the body field is not rendered.
  140. */
  141. public function testXssEntityBody() {
  142. $title = 'Hello World';
  143. // Load a page node.
  144. $this->drupalGet('node/add/page');
  145. $this->assertResponse(200);
  146. // Submit the node with a test body value.
  147. $edit = array(
  148. 'title' => $title,
  149. 'body[und][0][value]' => $this->xssString,
  150. );
  151. $this->drupalPost(NULL, $edit, t('Save'));
  152. // Verify the page saved.
  153. $this->assertResponse(200);
  154. $this->assertText(t('Basic page @title has been created.', array('@title' => $title)));
  155. // Verify body field is clean.
  156. $this->assertNoRaw($this->xssString);
  157. }
  158. }