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