3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
33 class CRM_Utils_Check_Component_Security
extends CRM_Utils_Check_Component
{
36 * CMS have a different pattern to their default file path and URL.
38 * @todo Use Civi::paths instead?
40 public function getFilePathMarker() {
41 $config = CRM_Core_Config
::singleton();
42 switch ($config->userFramework
) {
52 * Check if our logfile is directly accessible.
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.
59 * The debug log may be jam-packed with sensitive data, we don't
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
70 public function checkLogFileIsNotAccessible() {
73 $config = CRM_Core_Config
::singleton();
75 $log = CRM_Core_Error
::createDebugLogger();
76 $log_filename = str_replace('\\', '/', $log->_filename
);
78 $filePathMarker = $this->getFilePathMarker();
80 // Hazard a guess at the URL of the logfile, based on common
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 $headers = @get_headers
($log_url);
90 if (stripos($headers[0], '200')) {
91 $docs_url = $this->createDocUrl('checkLogFileIsNotAccessible');
92 $msg = 'The <a href="%1">CiviCRM debug log</a> should not be downloadable.'
94 '<a href="%2">Read more about this warning</a>';
95 $messages[] = new CRM_Utils_Check_Message(
97 ts($msg, array(1 => $log_url, 2 => $docs_url)),
98 ts('Security Warning'),
99 \Psr\Log\LogLevel
::WARNING
,
111 * Check if our uploads directory has accessible files.
113 * We'll test a handful of files randomly. Hazard a guess at the URL
114 * of the uploads dir, based on common CiviCRM layouts. Try and
115 * request the files, and if any are successfully retrieved, warn.
117 * Being retrievable doesn't mean the files are browseable or visible
118 * to search engines; it only means they can be requested directly.
124 * @todo Test with WordPress, Joomla.
126 public function checkUploadsAreNotAccessible() {
129 $config = CRM_Core_Config
::singleton();
130 $privateDirs = array(
132 $config->customFileUploadDir
,
135 foreach ($privateDirs as $privateDir) {
136 $heuristicUrl = $this->guessUrl($privateDir);
137 if ($this->isDirAccessible($privateDir, $heuristicUrl)) {
138 $messages[] = new CRM_Utils_Check_Message(
140 ts('Files in the data directory (<a href="%3">%2</a>) should not be downloadable.'
142 . '<a href="%1">Read more about this warning</a>',
144 1 => $this->createDocUrl('checkUploadsAreNotAccessible'),
148 ts('Private Files Readable'),
149 \Psr\Log\LogLevel
::WARNING
,
159 * Check if our uploads or ConfigAndLog directories have browseable
162 * Retrieve a listing of files from the local filesystem, and the
163 * corresponding path via HTTP. Then check and see if the local
164 * files are represented in the HTTP result; if so then warn. This
165 * MAY trigger false positives (if you have files named 'a', 'e'
166 * we'll probably match that).
172 * @todo Test with WordPress, Joomla.
174 public function checkDirectoriesAreNotBrowseable() {
176 $config = CRM_Core_Config
::singleton();
178 $config->imageUploadDir
=> $config->imageUploadURL
,
181 // Setup index.html files to prevent browsing
182 foreach ($publicDirs as $publicDir => $publicUrl) {
183 CRM_Utils_File
::restrictBrowsing($publicDir);
186 // Test that $publicDir is not browsable
187 foreach ($publicDirs as $publicDir => $publicUrl) {
188 if ($this->isBrowsable($publicDir, $publicUrl)) {
189 $msg = 'Directory <a href="%1">%2</a> should not be browseable via the web.'
191 '<a href="%3">Read more about this warning</a>';
192 $docs_url = $this->createDocUrl('checkDirectoriesAreNotBrowseable');
193 $messages[] = new CRM_Utils_Check_Message(
195 ts($msg, array(1 => $publicDir, 2 => $publicDir, 3 => $docs_url)),
196 ts('Browseable Directories'),
197 \Psr\Log\LogLevel
::ERROR
,
208 * Check that some files are not present.
210 * These files have generally been deleted but Civi source tree but could be
211 * left online if one does a faulty upgrade.
213 * @return array of messages
215 public function checkFilesAreNotPresent() {
216 global $civicrm_root;
221 // CRM-16005, upgraded from Civi <= 4.5.6
222 "{$civicrm_root}/packages/dompdf/dompdf.php",
223 \Psr\Log\LogLevel
::CRITICAL
,
226 // CRM-16005, Civi >= 4.5.7
227 "{$civicrm_root}/packages/vendor/dompdf/dompdf/dompdf.php",
228 \Psr\Log\LogLevel
::CRITICAL
,
231 // CRM-16005, Civi >= 4.6.0
232 "{$civicrm_root}/vendor/dompdf/dompdf/dompdf.php",
233 \Psr\Log\LogLevel
::CRITICAL
,
237 "{$civicrm_root}/packages/OpenFlashChart/php-ofc-library/ofc_upload_image.php",
238 \Psr\Log\LogLevel
::CRITICAL
,
241 "{$civicrm_root}/packages/html2text/class.html2text.inc",
242 \Psr\Log\LogLevel
::CRITICAL
,
245 foreach ($files as $file) {
246 if (file_exists($file[0])) {
247 $messages[] = new CRM_Utils_Check_Message(
249 ts('File \'%1\' presents a security risk and should be deleted.', array(1 => $file[0])),
260 * Discourage use of remote profile forms.
262 public function checkRemoteProfile() {
265 if (Civi
::settings()->get('remote_profile_submissions')) {
266 $messages[] = new CRM_Utils_Check_Message(
268 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.',
269 array(1 => CRM_Utils_System
::url('civicrm/admin/setting/misc', 'reset=1'))
271 ts('Remote Profiles Enabled'),
272 \Psr\Log\LogLevel
::WARNING
,
282 * Check that the sysadmin has not modified the Cxn
285 public function checkCxnOverrides() {
287 if (defined('CIVICRM_CXN_CA') && CIVICRM_CXN_CA
!== 'CiviRootCA') {
288 $list[] = 'CIVICRM_CXN_CA';
290 if (defined('CIVICRM_CXN_APPS_URL') && CIVICRM_CXN_APPS_URL
!== \Civi\Cxn\Rpc\Constants
::OFFICIAL_APPMETAS_URL
) {
291 $list[] = 'CIVICRM_CXN_APPS_URL';
297 $messages[] = new CRM_Utils_Check_Message(
299 ts('The system administrator has disabled security settings (%1). Connections to remote applications are insecure.', array(
300 1 => implode(', ', $list),
302 ts('Security Warning'),
303 \Psr\Log\LogLevel
::WARNING
,
312 * Determine whether $url is a public, browsable listing for $dir
320 public function isBrowsable($dir, $url) {
321 if (empty($dir) ||
empty($url) ||
!is_dir($dir)) {
327 // this could be a new system with no uploads (yet) -- so we'll make a file
328 $file = CRM_Utils_File
::createFakeFile($dir);
330 if ($file === FALSE) {
331 // Couldn't write the file
335 $content = @file_get_contents
("$url");
336 if (stristr($content, $file)) {
339 unlink("$dir/$file");
345 * Determine whether $url is a public version of $dir in which files
346 * are remotely accessible.
354 public function isDirAccessible($dir, $url) {
355 $url = rtrim($url, '/');
356 if (empty($dir) ||
empty($url) ||
!is_dir($dir)) {
361 $file = CRM_Utils_File
::createFakeFile($dir, 'delete me');
363 if ($file === FALSE) {
364 // Couldn't write the file
368 $headers = @get_headers
("$url/$file");
369 if (stripos($headers[0], '200')) {
370 $content = @file_get_contents
("$url/$file");
371 if (preg_match('/delete me/', $content)) {
376 unlink("$dir/$file");
386 public function createDocUrl($topic) {
387 return CRM_Utils_System
::getWikiBaseURL() . $topic;
391 * Make a guess about the URL that corresponds to $targetDir.
393 * @param string $targetDir
394 * Local path to a directory.
396 * a guessed URL for $realDir
398 public function guessUrl($targetDir) {
399 $filePathMarker = $this->getFilePathMarker();
400 $config = CRM_Core_Config
::singleton();
402 list($heuristicBaseUrl) = explode($filePathMarker, $config->imageUploadURL
);
403 list(, $heuristicSuffix) = array_pad(explode($filePathMarker, str_replace('\\', '/', $targetDir)), 2, '');
404 return $heuristicBaseUrl . $filePathMarker . $heuristicSuffix;