cbdb0656cdd2aa52249d18c046a05a31ac88eea4
[yaffs-website] / web / modules / contrib / security_review / src / Checks / TemporaryFiles.php
1 <?php
2
3 namespace Drupal\security_review\Checks;
4
5 use Drupal\security_review\Check;
6 use Drupal\security_review\CheckResult;
7
8 /**
9  * Check for sensitive temporary files like settings.php~.
10  */
11 class TemporaryFiles extends Check {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getNamespace() {
17     return 'Security Review';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   public function getTitle() {
24     return 'Temporary files';
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function run() {
31     $result = CheckResult::SUCCESS;
32     $findings = [];
33
34     // Get list of files from the site directory.
35     $files = [];
36     $site_path = $this->security()->sitePath() . '/';
37     $dir = scandir($site_path);
38     foreach ($dir as $file) {
39       // Set full path to only files.
40       if (!is_dir($file)) {
41         $files[] = $site_path . $file;
42       }
43     }
44     $this->moduleHandler()->alter('security_review_temporary_files', $files);
45
46     // Analyze the files' names.
47     foreach ($files as $path) {
48       $matches = [];
49       if (file_exists($path) && preg_match('/.*(~|\.sw[op]|\.bak|\.orig|\.save)$/', $path, $matches) !== FALSE && !empty($matches)) {
50         // Found a temporary file.
51         $findings[] = $path;
52       }
53     }
54
55     if (!empty($findings)) {
56       $result = CheckResult::FAIL;
57     }
58
59     return $this->createResult($result, $findings);
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function help() {
66     $paragraphs = [];
67     $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.");
68
69     return [
70       '#theme' => 'check_help',
71       '#title' => $this->t('Sensitive temporary files'),
72       '#paragraphs' => $paragraphs,
73     ];
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function evaluate(CheckResult $result) {
80     $findings = $result->findings();
81     if (empty($findings)) {
82       return [];
83     }
84
85     $paragraphs = [];
86     $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.");
87
88     return [
89       '#theme' => 'check_evaluation',
90       '#paragraphs' => $paragraphs,
91       '#items' => $findings,
92     ];
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function evaluatePlain(CheckResult $result) {
99     $findings = $result->findings();
100     if (empty($findings)) {
101       return '';
102     }
103
104     $output = $this->t('Temporary files:') . "\n";
105     foreach ($findings as $file) {
106       $output .= "\t" . $file . "\n";
107     }
108
109     return $output;
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function getMessage($result_const) {
116     switch ($result_const) {
117       case CheckResult::SUCCESS:
118         return $this->t('No sensitive temporary files were found.');
119
120       case CheckResult::FAIL:
121         return $this->t('Sensitive temporary files were found on your files system.');
122
123       default:
124         return $this->t('Unexpected result.');
125     }
126   }
127
128 }