Yaffs site version 1.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     $this->assertResponse(200);
116     $values = [
117       'title' => $this->xssTitleString,
118       'abstract' => $this->xssString,
119       'image_src' => $this->xssImageString
120     ];
121     $this->drupalPostForm(NULL, $values, 'Save');
122     $this->assertText('Saved the Global Metatag defaults.');
123     $this->rebuildAll();
124
125     // Load the Views-based front page.
126     $this->drupalGet('node');
127     $this->assertResponse(200);
128     $this->assertText(t('No front page content has been created yet.'));
129
130     // Check for the title tag, which will have the HTML tags removed and then
131     // be lightly HTML encoded.
132     $this->assertEscaped(strip_tags($this->xssTitleString));
133     $this->assertNoRaw($this->xssTitleString);
134
135     // Check for the basic meta tag.
136     $this->assertRaw($this->escapedXssTag);
137     $this->assertNoRaw($this->xssString);
138
139     // Check for the image meta tag.
140     $this->assertRaw($this->escapedXssImageTag);
141     $this->assertNoRaw($this->xssImageString);
142   }
143
144   /**
145    * Verify XSS injected in the entity metatag override field is not rendered.
146    */
147   public function testXssEntityOverride() {
148     $this->drupalGet('node/add/metatag_node');
149     $this->assertResponse(200);
150     $edit = [
151       'title[0][value]' => $this->randomString(32),
152       'field_metatag_field[0][basic][title]' => $this->xssTitleString,
153       'field_metatag_field[0][basic][abstract]' => $this->xssString,
154       'field_metatag_field[0][advanced][image_src]' => $this->xssImageString,
155     ];
156     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
157
158     // Check for the title tag, which will have the HTML tags removed and then
159     // be lightly HTML encoded.
160     $this->assertEscaped(strip_tags($this->xssTitleString));
161     $this->assertNoRaw($this->xssTitleString);
162
163     // Check for the basic meta tag.
164     $this->assertRaw($this->escapedXssTag);
165     $this->assertNoRaw($this->xssString);
166
167     // Check for the image meta tag.
168     $this->assertRaw($this->escapedXssImageTag);
169     $this->assertNoRaw($this->xssImageString);
170   }
171
172   /**
173    * Verify XSS injected in the entity titles are not rendered.
174    */
175   public function testXssEntityTitle() {
176     $this->drupalGet('node/add/metatag_node');
177     $this->assertResponse(200);
178     $edit = [
179       'title[0][value]' => $this->xssTitleString,
180       'body[0][value]' => $this->randomString() . ' ' . $this->randomString(),
181     ];
182     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
183
184     // Check for the title tag, which will have the HTML tags removed and then
185     // be lightly HTML encoded.
186     $this->assertEscaped(strip_tags($this->xssTitleString));
187     $this->assertNoRaw($this->xssTitleString);
188   }
189
190   /**
191    * Verify XSS injected in the entity fields are not rendered.
192    */
193   public function testXssEntityBody() {
194     $this->drupalGet('node/add/metatag_node');
195     $this->assertResponse(200);
196     $edit = [
197       'title[0][value]' => $this->randomString(),
198       'body[0][value]' => $this->xssTitleString,
199     ];
200     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
201
202     // Check the body text.
203     // $this->assertNoTitle($this->xssTitleString);
204     $this->assertNoRaw($this->xssTitleString);
205   }
206
207 }