TemporaryFiles.php in Security Review 8
File
src/Checks/TemporaryFiles.php
View source
<?php
namespace Drupal\security_review\Checks;
use Drupal\security_review\Check;
use Drupal\security_review\CheckResult;
class TemporaryFiles extends Check {
public function getNamespace() {
return 'Security Review';
}
public function getTitle() {
return 'Temporary files';
}
public function run() {
$result = CheckResult::SUCCESS;
$findings = [];
$files = [];
$site_path = $this
->security()
->sitePath() . '/';
$dir = scandir($site_path);
foreach ($dir as $file) {
if (!is_dir($file)) {
$files[] = $site_path . $file;
}
}
$this
->moduleHandler()
->alter('security_review_temporary_files', $files);
foreach ($files as $path) {
$matches = [];
if (file_exists($path) && preg_match('/.*(~|\\.sw[op]|\\.bak|\\.orig|\\.save)$/', $path, $matches) !== FALSE && !empty($matches)) {
$findings[] = $path;
}
}
if (!empty($findings)) {
$result = CheckResult::FAIL;
}
return $this
->createResult($result, $findings);
}
public function help() {
$paragraphs = [];
$paragraphs[] = $this
->t("Some file editors create temporary copies of a file that can be left on the file system. A copy of a sensitive file like Drupal's settings.php may be readable by a malicious user who could use that information to further attack a site.");
return [
'#theme' => 'check_help',
'#title' => $this
->t('Sensitive temporary files'),
'#paragraphs' => $paragraphs,
];
}
public function evaluate(CheckResult $result) {
$findings = $result
->findings();
if (empty($findings)) {
return [];
}
$paragraphs = [];
$paragraphs[] = $this
->t("The following are extraneous files in your Drupal installation that can probably be removed. You should confirm you have saved any of your work in the original files prior to removing these.");
return [
'#theme' => 'check_evaluation',
'#paragraphs' => $paragraphs,
'#items' => $findings,
];
}
public function evaluatePlain(CheckResult $result) {
$findings = $result
->findings();
if (empty($findings)) {
return '';
}
$output = $this
->t('Temporary files:') . "\n";
foreach ($findings as $file) {
$output .= "\t" . $file . "\n";
}
return $output;
}
public function getMessage($result_const) {
switch ($result_const) {
case CheckResult::SUCCESS:
return $this
->t('No sensitive temporary files were found.');
case CheckResult::FAIL:
return $this
->t('Sensitive temporary files were found on your files system.');
default:
return $this
->t('Unexpected result.');
}
}
}