View source
<?php
namespace Drupal\Tests\system\Functional\System;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
class SitesDirectoryHardeningTest extends BrowserTestBase {
use StringTranslationTrait;
protected $defaultTheme = 'stark';
public function testSitesDirectoryHardening() {
$site_path = $this->kernel
->getSitePath();
$settings_file = $this
->settingsFile($site_path);
$this
->assertTrue(drupal_verify_install_file($site_path, FILE_NOT_WRITABLE, 'dir'), new FormattableMarkup('Verified permissions for @file.', [
'@file' => $site_path,
]));
$this
->assertTrue(drupal_verify_install_file($settings_file, FILE_EXIST | FILE_READABLE | FILE_NOT_WRITABLE), new FormattableMarkup('Verified permissions for @file.', [
'@file' => $settings_file,
]));
$this
->makeWritable($site_path);
$this
->checkSystemRequirements();
$this
->assertTrue(drupal_verify_install_file($site_path, FILE_NOT_WRITABLE, 'dir'), new FormattableMarkup('Verified permissions for @file after manual permissions change.', [
'@file' => $site_path,
]));
$this
->assertTrue(drupal_verify_install_file($settings_file, FILE_EXIST | FILE_READABLE | FILE_NOT_WRITABLE), new FormattableMarkup('Verified permissions for @file after manual permissions change.', [
'@file' => $settings_file,
]));
}
public function testSitesDirectoryHardeningConfig() {
$site_path = $this->kernel
->getSitePath();
$settings_file = $this
->settingsFile($site_path);
$settings = Settings::getAll();
$settings['skip_permissions_hardening'] = TRUE;
new Settings($settings);
$this
->assertTrue(Settings::get('skip_permissions_hardening'), 'Able to set skip permissions hardening to true.');
$this
->makeWritable($site_path);
$requirements = $this
->checkSystemRequirements();
$this
->assertEquals(REQUIREMENT_WARNING, $requirements['configuration_files']['severity'], 'Warning severity is properly set.');
$this
->assertEquals('Protection disabled', (string) $requirements['configuration_files']['value']);
$description = strip_tags(\Drupal::service('renderer')
->renderPlain($requirements['configuration_files']['description']));
$this
->assertStringContainsString('settings.php is not protected from modifications and poses a security risk.', $description);
$this
->assertStringContainsString('services.yml is not protected from modifications and poses a security risk.', $description);
$this
->assertDirectoryIsWritable($site_path);
$this
->assertFileIsWritable($settings_file);
$settings = Settings::getAll();
$settings['skip_permissions_hardening'] = FALSE;
new Settings($settings);
$requirements = $this
->checkSystemRequirements();
$this
->assertEquals('Protected', (string) $requirements['configuration_files']['value']);
$this
->assertDirectoryIsNotWritable($site_path);
$this
->assertFileIsNotWritable($settings_file);
}
protected function checkSystemRequirements() {
module_load_install('system');
return system_requirements('runtime');
}
protected function makeWritable($site_path) {
chmod($site_path, 0755);
chmod($this
->settingsFile($site_path), 0644);
}
protected function settingsFile($site_path) {
$settings_file = $site_path . '/settings.php';
return $settings_file;
}
}