Version 1
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagXssTest.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Ensures that metatags do not allow xss vulnerabilities.
9  *
10  * @group metatag
11  */
12 class MetatagXssTest extends WebTestBase {
13
14   /**
15    * String that causes an alert when page titles aren't filtered for xss.
16    *
17    * @var string
18    */
19   private $xssTitleString = '<script>alert("xss");</script>';
20
21   /**
22    * String that causes an alert when metatags aren't filtered for xss.
23    *
24    * @var string
25    */
26   private $xssString = '"><script>alert("xss");</script><meta "';
27
28   /**
29    * Rendered xss tag that has escaped attribute to avoid xss injection.
30    *
31    * @var string
32    */
33   private $escapedXssTag = '<meta name="abstract" content="&quot;&gt;alert(&quot;xss&quot;);" />';
34
35   /**
36    * String that causes an alert when metatags aren't filtered for xss.
37    *
38    * "Image" meta tags are processed differently to others, so this checks for a
39    * different string.
40    *
41    * @var string
42    */
43   private $xssImageString = '"><script>alert("image xss");</script><meta "';
44
45   /**
46    * Rendered xss tag that has escaped attribute to avoid xss injection.
47    *
48    * @var string
49    */
50   private $escapedXssImageTag = '<link rel="image_src" href="&quot;&gt;alert(&quot;image xss&quot;);" />';
51
52   /**
53    * Administrator user for tests.
54    *
55    * @var \Drupal\user\UserInterface
56    */
57   protected $adminUser;
58
59   /**
60    * {@inheritdoc}
61    */
62   public static $modules = [
63     'node',
64     'views',
65     'system',
66     'field',
67     'field_ui',
68     'token',
69     'metatag',
70   ];
71
72   /**
73    * {@inheritdoc}
74    */
75   protected function setUp() {
76     parent::setUp();
77
78     // Create a user that can manage content types and create content.
79     $admin_permissions = [
80       'administer content types',
81       'administer nodes',
82       'bypass node access',
83       'administer meta tags',
84       'administer site configuration',
85       'access content',
86       'administer content types',
87       'administer nodes',
88       'administer node fields',
89     ];
90
91     // Create and login a with the admin-ish permissions user.
92     $this->adminUser = $this->drupalCreateUser($admin_permissions);
93     $this->drupalLogin($this->adminUser);
94
95     // Set up a content type.
96     $this->drupalCreateContentType(['type' => 'metatag_node', 'name' => 'Test Content Type']);
97
98     // Add a metatag field to the content type.
99     $this->drupalGet('admin/structure/types/manage/metatag_node/fields/add-field');
100     $this->assertResponse(200);
101     $edit = [
102       'label' => 'Metatag',
103       'field_name' => 'metatag_field',
104       'new_storage_type' => 'metatag',
105     ];
106     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
107     $this->drupalPostForm(NULL, [], t('Save field settings'));
108   }
109
110   /**
111    * Verify XSS injected in global config is not rendered.
112    */
113   public function testXssMetatagConfig() {
114     $this->drupalGet('admin/config/search/metatag/global');
115     $values = [
116       'title' => $this->xssTitleString,
117       'abstract' => $this->xssString,
118       'image_src' => $this->xssImageString
119     ];
120     $this->drupalPostForm(NULL, $values, 'Save');
121     $this->assertText('Saved the Global Metatag defaults.');
122     $this->rebuildAll();
123
124     // Load the Views-based front page.
125     $this->drupalGet('node');
126     $this->assertResponse(200);
127     $this->assertText(t('No front page content has been created yet.'));
128
129     // Check for the title tag, which will have the HTML tags removed and then
130     // be lightly HTML encoded.
131     $this->assertEscaped(strip_tags($this->xssTitleString));
132     $this->assertNoRaw($this->xssTitleString);
133
134     // Check for the basic meta tag.
135     $this->assertRaw($this->escapedXssTag);
136     $this->assertNoRaw($this->xssString);
137
138     // Check for the image meta tag.
139     $this->assertRaw($this->escapedXssImageTag);
140     $this->assertNoRaw($this->xssImageString);
141   }
142
143   /**
144    * Verify XSS injected in the entity metatag override field is not rendered.
145    */
146   public function testXssEntityOverride() {
147     $this->drupalGet('node/add/metatag_node');
148     $edit = [
149       'title[0][value]' => $this->randomString(32),
150       'field_metatag_field[0][basic][title]' => $this->xssTitleString,
151       'field_metatag_field[0][basic][abstract]' => $this->xssString,
152       'field_metatag_field[0][advanced][image_src]' => $this->xssImageString,
153     ];
154     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
155
156     // Check for the title tag, which will have the HTML tags removed and then
157     // be lightly HTML encoded.
158     $this->assertEscaped(strip_tags($this->xssTitleString));
159     $this->assertNoRaw($this->xssTitleString);
160
161     // Check for the basic meta tag.
162     $this->assertRaw($this->escapedXssTag);
163     $this->assertNoRaw($this->xssString);
164
165     // Check for the image meta tag.
166     $this->assertRaw($this->escapedXssImageTag);
167     $this->assertNoRaw($this->xssImageString);
168   }
169
170   /**
171    * Verify XSS injected in the entity titles are not rendered.
172    */
173   public function testXssEntityTitle() {
174     $this->drupalGet('node/add/metatag_node');
175     $edit = [
176       'title[0][value]' => $this->xssTitleString,
177       'body[0][value]' => $this->randomString() . ' ' . $this->randomString(),
178     ];
179     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
180
181     // Check for the title tag, which will have the HTML tags removed and then
182     // be lightly HTML encoded.
183     $this->assertEscaped(strip_tags($this->xssTitleString));
184     $this->assertNoRaw($this->xssTitleString);
185   }
186
187   /**
188    * Verify XSS injected in the entity fields are not rendered.
189    */
190   public function testXssEntityBody() {
191     $this->drupalGet('node/add/metatag_node');
192     $edit = [
193       'title[0][value]' => $this->randomString(),
194       'body[0][value]' => $this->xssTitleString,
195     ];
196     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
197
198     // Check the body text.
199     // $this->assertNoTitle($this->xssTitleString);
200     $this->assertNoRaw($this->xssTitleString);
201   }
202
203 }