Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / security_review / src / Checks / PrivateFiles.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\security_review\Checks\PrivateFiles.
6  */
7
8 namespace Drupal\security_review\Checks;
9
10 use Drupal\Core\StreamWrapper\PrivateStream;
11 use Drupal\Core\Url;
12 use Drupal\security_review\Check;
13 use Drupal\security_review\CheckResult;
14
15 /**
16  * Checks whether the private files' directory is under the web root.
17  */
18 class PrivateFiles extends Check {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function getNamespace() {
24     return 'Security Review';
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function getTitle() {
31     return 'Private files';
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function run() {
38     $file_directory_path = PrivateStream::basePath();
39     $visible = TRUE;
40     if (empty($file_directory_path)) {
41       // Private files feature is not enabled.
42       $result = CheckResult::SUCCESS;
43       $visible = FALSE;
44     }
45     elseif (strpos(realpath($file_directory_path), DRUPAL_ROOT) === 0) {
46       // Path begins at root.
47       $result = CheckResult::FAIL;
48     }
49     else {
50       // The private files directory is placed correctly.
51       $result = CheckResult::SUCCESS;
52     }
53     return $this->createResult($result, ['path' => $file_directory_path], $visible);
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function help() {
60     $paragraphs = [];
61     $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.");
62
63     return [
64       '#theme' => 'check_help',
65       '#title' => $this->t('Private files'),
66       '#paragraphs' => $paragraphs,
67     ];
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function evaluate(CheckResult $result) {
74     if ($result->result() != CheckResult::FAIL) {
75       return [];
76     }
77
78     $paragraphs = [];
79     $paragraphs[] = $this->t('Your files directory is not outside of the server root.');
80     $paragraphs[] = $this->l(
81       $this->t('Edit the files directory path.'),
82       Url::fromRoute('system.file_system_settings')
83     );
84
85     return [
86       '#theme' => 'check_evaluation',
87       '#paragraphs' => $paragraphs,
88       '#items' => [],
89     ];
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function evaluatePlain(CheckResult $result) {
96     if ($result->result() != CheckResult::FAIL) {
97       return '';
98     }
99
100     return $this->t('Private files directory: @path', ['@path' => $result->findings()['path']]);
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function getMessage($result_const) {
107     switch ($result_const) {
108       case CheckResult::SUCCESS:
109         return $this->t('Private files directory is outside the web server root.');
110
111       case CheckResult::FAIL:
112         return $this->t('Private files is enabled but the specified directory is not secure outside the web server root.');
113
114       default:
115         return $this->t('Unexpected result.');
116     }
117   }
118
119 }