Merge pull request #13944 from agileware/CIVICRM-1140
[civicrm-core.git] / CRM / Utils / Check / Component / Security.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Utils_Check_Component_Security extends CRM_Utils_Check_Component {
34
35 /**
36 * CMS have a different pattern to their default file path and URL.
37 *
38 * @todo Use Civi::paths instead?
39 */
40 public function getFilePathMarker() {
41 $config = CRM_Core_Config::singleton();
42 switch ($config->userFramework) {
43 case 'Joomla':
44 return '/media/';
45
46 default:
47 return '/files/';
48 }
49 }
50
51 /**
52 * Check if our logfile is directly accessible.
53 *
54 * Per CiviCRM default the logfile sits in a folder which is
55 * web-accessible, and is protected by a default .htaccess
56 * configuration. If server config causes the .htaccess not to
57 * function as intended, there may be information disclosure.
58 *
59 * The debug log may be jam-packed with sensitive data, we don't
60 * want that.
61 *
62 * Being able to be retrieved directly doesn't mean the logfile
63 * is browseable or visible to search engines; it means it can be
64 * requested directly.
65 *
66 * @return array
67 * Array of messages
68 * @see CRM-14091
69 */
70 public function checkLogFileIsNotAccessible() {
71 $messages = array();
72
73 $config = CRM_Core_Config::singleton();
74
75 $log = CRM_Core_Error::createDebugLogger();
76 $log_filename = str_replace('\\', '/', $log->_filename);
77
78 $filePathMarker = $this->getFilePathMarker();
79
80 // Hazard a guess at the URL of the logfile, based on common
81 // CiviCRM layouts.
82 if ($upload_url = explode($filePathMarker, $config->imageUploadURL)) {
83 $url[] = $upload_url[0];
84 if ($log_path = explode($filePathMarker, $log_filename)) {
85 // CRM-17149: check if debug log path includes $filePathMarker
86 if (count($log_path) > 1) {
87 $url[] = $log_path[1];
88 $log_url = implode($filePathMarker, $url);
89 if ($this->fileExists($log_url)) {
90 $docs_url = $this->createDocUrl('checkLogFileIsNotAccessible');
91 $msg = 'The <a href="%1">CiviCRM debug log</a> should not be downloadable.'
92 . '<br />' .
93 '<a href="%2">Read more about this warning</a>';
94 $messages[] = new CRM_Utils_Check_Message(
95 __FUNCTION__,
96 ts($msg, array(1 => $log_url, 2 => $docs_url)),
97 ts('Security Warning'),
98 \Psr\Log\LogLevel::WARNING,
99 'fa-lock'
100 );
101 }
102 }
103 }
104 }
105
106 return $messages;
107 }
108
109 /**
110 * Check if our uploads directory has accessible files.
111 *
112 * We'll test a handful of files randomly. Hazard a guess at the URL
113 * of the uploads dir, based on common CiviCRM layouts. Try and
114 * request the files, and if any are successfully retrieved, warn.
115 *
116 * Being retrievable doesn't mean the files are browseable or visible
117 * to search engines; it only means they can be requested directly.
118 *
119 * @return array
120 * Array of messages
121 * @see CRM-14091
122 *
123 * @todo Test with WordPress, Joomla.
124 */
125 public function checkUploadsAreNotAccessible() {
126 $messages = array();
127
128 $config = CRM_Core_Config::singleton();
129 $privateDirs = array(
130 $config->uploadDir,
131 $config->customFileUploadDir,
132 );
133
134 foreach ($privateDirs as $privateDir) {
135 $heuristicUrl = $this->guessUrl($privateDir);
136 if ($this->isDirAccessible($privateDir, $heuristicUrl)) {
137 $messages[] = new CRM_Utils_Check_Message(
138 __FUNCTION__,
139 ts('Files in the data directory (<a href="%3">%2</a>) should not be downloadable.'
140 . '<br />'
141 . '<a href="%1">Read more about this warning</a>',
142 array(
143 1 => $this->createDocUrl('checkUploadsAreNotAccessible'),
144 2 => $privateDir,
145 3 => $heuristicUrl,
146 )),
147 ts('Private Files Readable'),
148 \Psr\Log\LogLevel::WARNING,
149 'fa-lock'
150 );
151 }
152 }
153
154 return $messages;
155 }
156
157 /**
158 * Check if our uploads or ConfigAndLog directories have browseable
159 * listings.
160 *
161 * Retrieve a listing of files from the local filesystem, and the
162 * corresponding path via HTTP. Then check and see if the local
163 * files are represented in the HTTP result; if so then warn. This
164 * MAY trigger false positives (if you have files named 'a', 'e'
165 * we'll probably match that).
166 *
167 * @return array
168 * Array of messages
169 * @see CRM-14091
170 *
171 * @todo Test with WordPress, Joomla.
172 */
173 public function checkDirectoriesAreNotBrowseable() {
174 $messages = array();
175 $config = CRM_Core_Config::singleton();
176 $publicDirs = array(
177 $config->imageUploadDir => $config->imageUploadURL,
178 );
179
180 // Setup index.html files to prevent browsing
181 foreach ($publicDirs as $publicDir => $publicUrl) {
182 CRM_Utils_File::restrictBrowsing($publicDir);
183 }
184
185 // Test that $publicDir is not browsable
186 foreach ($publicDirs as $publicDir => $publicUrl) {
187 if ($this->isBrowsable($publicDir, $publicUrl)) {
188 $msg = 'Directory <a href="%1">%2</a> should not be browseable via the web.'
189 . '<br />' .
190 '<a href="%3">Read more about this warning</a>';
191 $docs_url = $this->createDocUrl('checkDirectoriesAreNotBrowseable');
192 $messages[] = new CRM_Utils_Check_Message(
193 __FUNCTION__,
194 ts($msg, array(1 => $publicDir, 2 => $publicDir, 3 => $docs_url)),
195 ts('Browseable Directories'),
196 \Psr\Log\LogLevel::ERROR,
197 'fa-lock'
198 );
199 }
200 }
201
202 return $messages;
203 }
204
205
206 /**
207 * Check that some files are not present.
208 *
209 * These files have generally been deleted but Civi source tree but could be
210 * left online if one does a faulty upgrade.
211 *
212 * @return array of messages
213 */
214 public function checkFilesAreNotPresent() {
215 global $civicrm_root;
216
217 $messages = array();
218 $files = array(
219 array(
220 // CRM-16005, upgraded from Civi <= 4.5.6
221 "{$civicrm_root}/packages/dompdf/dompdf.php",
222 \Psr\Log\LogLevel::CRITICAL,
223 ),
224 array(
225 // CRM-16005, Civi >= 4.5.7
226 "{$civicrm_root}/packages/vendor/dompdf/dompdf/dompdf.php",
227 \Psr\Log\LogLevel::CRITICAL,
228 ),
229 array(
230 // CRM-16005, Civi >= 4.6.0
231 "{$civicrm_root}/vendor/dompdf/dompdf/dompdf.php",
232 \Psr\Log\LogLevel::CRITICAL,
233 ),
234 array(
235 // CIVI-SA-2013-001
236 "{$civicrm_root}/packages/OpenFlashChart/php-ofc-library/ofc_upload_image.php",
237 \Psr\Log\LogLevel::CRITICAL,
238 ),
239 array(
240 "{$civicrm_root}/packages/html2text/class.html2text.inc",
241 \Psr\Log\LogLevel::CRITICAL,
242 ),
243 );
244 foreach ($files as $file) {
245 if (file_exists($file[0])) {
246 $messages[] = new CRM_Utils_Check_Message(
247 __FUNCTION__,
248 ts('File \'%1\' presents a security risk and should be deleted.', array(1 => $file[0])),
249 ts('Unsafe Files'),
250 $file[1],
251 'fa-lock'
252 );
253 }
254 }
255 return $messages;
256 }
257
258 /**
259 * Discourage use of remote profile forms.
260 */
261 public function checkRemoteProfile() {
262 $messages = array();
263
264 if (Civi::settings()->get('remote_profile_submissions')) {
265 $messages[] = new CRM_Utils_Check_Message(
266 __FUNCTION__,
267 ts('Warning: External profile support (aka "HTML Snippet" support) is enabled in <a href="%1">system settings</a>. This setting may be prone to abuse. If you must retain it, consider HTTP throttling or other protections.',
268 array(1 => CRM_Utils_System::url('civicrm/admin/setting/misc', 'reset=1'))
269 ),
270 ts('Remote Profiles Enabled'),
271 \Psr\Log\LogLevel::WARNING,
272 'fa-lock'
273 );
274 }
275
276 return $messages;
277 }
278
279
280 /**
281 * Check that the sysadmin has not modified the Cxn
282 * security setup.
283 */
284 public function checkCxnOverrides() {
285 $list = array();
286 if (defined('CIVICRM_CXN_CA') && CIVICRM_CXN_CA !== 'CiviRootCA') {
287 $list[] = 'CIVICRM_CXN_CA';
288 }
289 if (defined('CIVICRM_CXN_APPS_URL') && CIVICRM_CXN_APPS_URL !== \Civi\Cxn\Rpc\Constants::OFFICIAL_APPMETAS_URL) {
290 $list[] = 'CIVICRM_CXN_APPS_URL';
291 }
292
293 $messages = array();
294
295 if (!empty($list)) {
296 $messages[] = new CRM_Utils_Check_Message(
297 __FUNCTION__,
298 ts('The system administrator has disabled security settings (%1). Connections to remote applications are insecure.', array(
299 1 => implode(', ', $list),
300 )),
301 ts('Security Warning'),
302 \Psr\Log\LogLevel::WARNING,
303 'fa-lock'
304 );
305 }
306
307 return $messages;
308 }
309
310 /**
311 * Determine whether $url is a public, browsable listing for $dir
312 *
313 * @param string $dir
314 * Local dir path.
315 * @param string $url
316 * Public URL.
317 * @return bool
318 */
319 public function isBrowsable($dir, $url) {
320 if (empty($dir) || empty($url) || !is_dir($dir)) {
321 return FALSE;
322 }
323
324 $result = FALSE;
325
326 // this could be a new system with no uploads (yet) -- so we'll make a file
327 $file = CRM_Utils_File::createFakeFile($dir);
328
329 if ($file === FALSE) {
330 // Couldn't write the file
331 return FALSE;
332 }
333
334 $content = @file_get_contents("$url");
335 if (stristr($content, $file)) {
336 $result = TRUE;
337 }
338 unlink("$dir/$file");
339
340 return $result;
341 }
342
343 /**
344 * Determine whether $url is a public version of $dir in which files
345 * are remotely accessible.
346 *
347 * @param string $dir
348 * Local dir path.
349 * @param string $url
350 * Public URL.
351 * @return bool
352 */
353 public function isDirAccessible($dir, $url) {
354 $url = rtrim($url, '/');
355 if (empty($dir) || empty($url) || !is_dir($dir)) {
356 return FALSE;
357 }
358
359 $result = FALSE;
360 $file = CRM_Utils_File::createFakeFile($dir, 'delete me');
361
362 if ($file === FALSE) {
363 // Couldn't write the file
364 return FALSE;
365 }
366
367 if ($this->fileExists("$url/$file")) {
368 $content = @file_get_contents("$url/$file");
369 if (preg_match('/delete me/', $content)) {
370 $result = TRUE;
371 }
372 }
373
374 unlink("$dir/$file");
375
376 return $result;
377 }
378
379 /**
380 * @param $topic
381 *
382 * @return string
383 */
384 public function createDocUrl($topic) {
385 return CRM_Utils_System::getWikiBaseURL() . $topic;
386 }
387
388 /**
389 * Make a guess about the URL that corresponds to $targetDir.
390 *
391 * @param string $targetDir
392 * Local path to a directory.
393 * @return string
394 * a guessed URL for $realDir
395 */
396 public function guessUrl($targetDir) {
397 $filePathMarker = $this->getFilePathMarker();
398 $config = CRM_Core_Config::singleton();
399
400 list($heuristicBaseUrl) = explode($filePathMarker, $config->imageUploadURL);
401 list(, $heuristicSuffix) = array_pad(explode($filePathMarker, str_replace('\\', '/', $targetDir)), 2, '');
402 return $heuristicBaseUrl . $filePathMarker . $heuristicSuffix;
403 }
404
405 }