View source
<?php
namespace Drupal\Tests\metatag\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class MetatagXssTest extends BrowserTestBase {
use StringTranslationTrait;
private $xssTitleString = '<script>alert("xss");</script>';
private $xssString = '"><script>alert("xss");</script><meta "';
private $escapedXssTag = '<meta name="abstract" content="">alert("xss");" />';
private $xssImageString = '"><script>alert("image xss");</script><meta "';
private $escapedXssImageTag = '<link rel="image_src" href="">alert("image xss");" />';
protected $adminUser;
protected static $modules = [
'node',
'views',
'system',
'field',
'field_ui',
'token',
'metatag',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$admin_permissions = [
'administer content types',
'administer nodes',
'bypass node access',
'administer meta tags',
'administer site configuration',
'access content',
'administer content types',
'administer nodes',
'administer node fields',
];
$this->adminUser = $this
->drupalCreateUser($admin_permissions);
$this
->drupalLogin($this->adminUser);
$this
->drupalCreateContentType([
'type' => 'metatag_node',
'name' => 'Test Content Type',
]);
$this
->drupalGet('admin/structure/types/manage/metatag_node/fields/add-field');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'label' => 'Metatag',
'field_name' => 'metatag_field',
'new_storage_type' => 'metatag',
];
$this
->drupalPostForm(NULL, $edit, $this
->t('Save and continue'));
$this
->drupalPostForm(NULL, [], $this
->t('Save field settings'));
}
public function testXssMetatagConfig() {
$this
->drupalGet('admin/config/search/metatag/global');
$session = $this
->assertSession();
$session
->statusCodeEquals(200);
$values = [
'title' => $this->xssTitleString,
'abstract' => $this->xssString,
'image_src' => $this->xssImageString,
];
$this
->drupalPostForm(NULL, $values, 'Save');
$session
->pageTextContains('Saved the Global Metatag defaults.');
$this
->rebuildAll();
$this
->drupalGet('node');
$session
->statusCodeEquals(200);
$session
->pageTextContains('No front page content has been created yet.');
$session
->assertEscaped(strip_tags($this->xssTitleString));
$session
->responseNotContains($this->xssTitleString);
$session
->responseContains($this->escapedXssTag);
$session
->responseNotContains($this->xssString);
$session
->responseContains($this->escapedXssImageTag);
$session
->responseNotContains($this->xssImageString);
}
public function testXssEntityOverride() {
$save_label = floatval(\Drupal::VERSION) <= 8.300000000000001 ? $this
->t('Save and publish') : $this
->t('Save');
$this
->drupalGet('node/add/metatag_node');
$session = $this
->assertSession();
$session
->statusCodeEquals(200);
$edit = [
'title[0][value]' => $this
->randomString(32),
'field_metatag_field[0][basic][title]' => $this->xssTitleString,
'field_metatag_field[0][basic][abstract]' => $this->xssString,
'field_metatag_field[0][advanced][image_src]' => $this->xssImageString,
];
$this
->drupalPostForm(NULL, $edit, $save_label);
$session
->assertEscaped(strip_tags($this->xssTitleString));
$session
->responseNotContains($this->xssTitleString);
$session
->responseContains($this->escapedXssTag);
$session
->responseNotContains($this->xssString);
$session
->responseContains($this->escapedXssImageTag);
$session
->responseNotContains($this->xssImageString);
}
public function testXssEntityTitle() {
$save_label = floatval(\Drupal::VERSION) <= 8.300000000000001 ? $this
->t('Save and publish') : $this
->t('Save');
$this
->drupalGet('node/add/metatag_node');
$session = $this
->assertSession();
$session
->statusCodeEquals(200);
$edit = [
'title[0][value]' => $this->xssTitleString,
'body[0][value]' => $this
->randomString() . ' ' . $this
->randomString(),
];
$this
->drupalPostForm(NULL, $edit, $save_label);
$session
->assertEscaped(strip_tags($this->xssTitleString));
$session
->responseNotContains($this->xssTitleString);
}
public function testXssEntityBody() {
$save_label = floatval(\Drupal::VERSION) <= 8.300000000000001 ? $this
->t('Save and publish') : $this
->t('Save');
$this
->drupalGet('node/add/metatag_node');
$session = $this
->assertSession();
$session
->statusCodeEquals(200);
$edit = [
'title[0][value]' => $this
->randomString(),
'body[0][value]' => $this->xssTitleString,
];
$this
->drupalPostForm(NULL, $edit, $save_label);
$session
->responseNotContains($this->xssTitleString);
}
}