More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / modules / contrib / security_review / src / Checks / PrivateFiles.php
1 <?php
2
3 namespace Drupal\security_review\Checks;
4
5 use Drupal\Core\Link;
6 use Drupal\Core\StreamWrapper\PrivateStream;
7 use Drupal\security_review\Check;
8 use Drupal\security_review\CheckResult;
9
10 /**
11  * Checks whether the private files' directory is under the web root.
12  */
13 class PrivateFiles extends Check {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getNamespace() {
19     return 'Security Review';
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   public function getTitle() {
26     return 'Private files';
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function run() {
33     $file_directory_path = PrivateStream::basePath();
34     $visible = TRUE;
35     if (empty($file_directory_path)) {
36       // Private files feature is not enabled.
37       $result = CheckResult::SUCCESS;
38       $visible = FALSE;
39     }
40     elseif (strpos(realpath($file_directory_path), DRUPAL_ROOT) === 0) {
41       // Path begins at root.
42       $result = CheckResult::FAIL;
43     }
44     else {
45       // The private files directory is placed correctly.
46       $result = CheckResult::SUCCESS;
47     }
48     return $this->createResult($result, ['path' => $file_directory_path], $visible);
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function help() {
55     $paragraphs = [];
56     $paragraphs[] = $this->t("If you have Drupal's private files feature enabled you should move the files directory outside of the web server's document root. Drupal will secure access to files that it renders the link to, but if a user knows the actual system path they can circumvent Drupal's private files feature. You can protect against this by specifying a files directory outside of the webserver root.");
57
58     return [
59       '#theme' => 'check_help',
60       '#title' => $this->t('Private files'),
61       '#paragraphs' => $paragraphs,
62     ];
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function evaluate(CheckResult $result) {
69     if ($result->result() != CheckResult::FAIL) {
70       return [];
71     }
72
73     $paragraphs = [];
74     $paragraphs[] = $this->t('Your files directory is not outside of the server root.');
75     $paragraphs[] = Link::createFromRoute(
76       $this->t('Edit the files directory path.'),
77       'system.file_system_settings'
78     );
79
80     return [
81       '#theme' => 'check_evaluation',
82       '#paragraphs' => $paragraphs,
83       '#items' => [],
84     ];
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function evaluatePlain(CheckResult $result) {
91     if ($result->result() != CheckResult::FAIL) {
92       return '';
93     }
94
95     return $this->t('Private files directory: @path', ['@path' => $result->findings()['path']]);
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function getMessage($result_const) {
102     switch ($result_const) {
103       case CheckResult::SUCCESS:
104         return $this->t('Private files directory is outside the web server root.');
105
106       case CheckResult::FAIL:
107         return $this->t('Private files is enabled but the specified directory is not secure outside the web server root.');
108
109       default:
110         return $this->t('Unexpected result.');
111     }
112   }
113
114 }