Commit | Line | Data |
---|---|---|
349b394e CB |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
349b394e | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
349b394e CB |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
349b394e CB |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
349b394e | 32 | */ |
3a0d0bbd | 33 | class CRM_Utils_Check_Component_Security extends CRM_Utils_Check_Component { |
349b394e | 34 | |
5c58b447 CB |
35 | /** |
36 | * CMS have a different pattern to their default file path and URL. | |
37 | * | |
3a0d0bbd | 38 | * @todo Use Civi::paths instead? |
5c58b447 | 39 | */ |
a996bf82 | 40 | public function getFilePathMarker() { |
5c58b447 CB |
41 | $config = CRM_Core_Config::singleton(); |
42 | switch ($config->userFramework) { | |
43 | case 'Joomla': | |
44 | return '/media/'; | |
e7292422 | 45 | |
5c58b447 CB |
46 | default: |
47 | return '/files/'; | |
48 | } | |
49 | } | |
50 | ||
349b394e CB |
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 | * | |
a6c01b45 | 66 | * @return array |
16b10e64 | 67 | * Array of messages |
349b394e CB |
68 | * @see CRM-14091 |
69 | */ | |
23d89616 | 70 | public function checkLogFileIsNotAccessible() { |
e7d3e318 TO |
71 | $messages = array(); |
72 | ||
349b394e CB |
73 | $config = CRM_Core_Config::singleton(); |
74 | ||
75 | $log = CRM_Core_Error::createDebugLogger(); | |
b35868f5 | 76 | $log_filename = str_replace('\\', '/', $log->_filename); |
349b394e | 77 | |
a996bf82 | 78 | $filePathMarker = $this->getFilePathMarker(); |
5c58b447 | 79 | |
349b394e CB |
80 | // Hazard a guess at the URL of the logfile, based on common |
81 | // CiviCRM layouts. | |
5c58b447 CB |
82 | if ($upload_url = explode($filePathMarker, $config->imageUploadURL)) { |
83 | $url[] = $upload_url[0]; | |
84 | if ($log_path = explode($filePathMarker, $log_filename)) { | |
4de448d7 | 85 | // CRM-17149: check if debug log path includes $filePathMarker |
7332c9bd | 86 | if (count($log_path) > 1) { |
4de448d7 NG |
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.' | |
93 | . '<br />' . | |
94 | '<a href="%2">Read more about this warning</a>'; | |
95 | $messages[] = new CRM_Utils_Check_Message( | |
165aab59 | 96 | __FUNCTION__, |
4de448d7 | 97 | ts($msg, array(1 => $log_url, 2 => $docs_url)), |
165aab59 CW |
98 | ts('Security Warning'), |
99 | \Psr\Log\LogLevel::WARNING, | |
100 | 'fa-lock' | |
4de448d7 NG |
101 | ); |
102 | } | |
349b394e | 103 | } |
5c58b447 | 104 | } |
349b394e | 105 | } |
e7d3e318 TO |
106 | |
107 | return $messages; | |
349b394e CB |
108 | } |
109 | ||
110 | /** | |
111 | * Check if our uploads directory has accessible files. | |
112 | * | |
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. | |
116 | * | |
117 | * Being retrievable doesn't mean the files are browseable or visible | |
118 | * to search engines; it only means they can be requested directly. | |
119 | * | |
a6c01b45 | 120 | * @return array |
16b10e64 | 121 | * Array of messages |
349b394e | 122 | * @see CRM-14091 |
5c58b447 | 123 | * |
50bfb460 | 124 | * @todo Test with WordPress, Joomla. |
349b394e | 125 | */ |
23d89616 | 126 | public function checkUploadsAreNotAccessible() { |
e7d3e318 TO |
127 | $messages = array(); |
128 | ||
349b394e | 129 | $config = CRM_Core_Config::singleton(); |
a738c74c TO |
130 | $privateDirs = array( |
131 | $config->uploadDir, | |
132 | $config->customFileUploadDir, | |
133 | ); | |
5c58b447 | 134 | |
a738c74c TO |
135 | foreach ($privateDirs as $privateDir) { |
136 | $heuristicUrl = $this->guessUrl($privateDir); | |
137 | if ($this->isDirAccessible($privateDir, $heuristicUrl)) { | |
138 | $messages[] = new CRM_Utils_Check_Message( | |
165aab59 | 139 | __FUNCTION__, |
a738c74c | 140 | ts('Files in the data directory (<a href="%3">%2</a>) should not be downloadable.' |
353ffa53 TO |
141 | . '<br />' |
142 | . '<a href="%1">Read more about this warning</a>', | |
a738c74c TO |
143 | array( |
144 | 1 => $this->createDocUrl('checkUploadsAreNotAccessible'), | |
145 | 2 => $privateDir, | |
146 | 3 => $heuristicUrl, | |
147 | )), | |
1b366958 | 148 | ts('Private Files Readable'), |
165aab59 CW |
149 | \Psr\Log\LogLevel::WARNING, |
150 | 'fa-lock' | |
a738c74c | 151 | ); |
5c58b447 | 152 | } |
349b394e | 153 | } |
e7d3e318 TO |
154 | |
155 | return $messages; | |
349b394e CB |
156 | } |
157 | ||
158 | /** | |
159 | * Check if our uploads or ConfigAndLog directories have browseable | |
160 | * listings. | |
161 | * | |
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). | |
167 | * | |
a6c01b45 | 168 | * @return array |
16b10e64 | 169 | * Array of messages |
349b394e | 170 | * @see CRM-14091 |
5c58b447 | 171 | * |
50bfb460 | 172 | * @todo Test with WordPress, Joomla. |
349b394e | 173 | */ |
23d89616 | 174 | public function checkDirectoriesAreNotBrowseable() { |
e7d3e318 | 175 | $messages = array(); |
349b394e | 176 | $config = CRM_Core_Config::singleton(); |
af5201d4 TO |
177 | $publicDirs = array( |
178 | $config->imageUploadDir => $config->imageUploadURL, | |
5c58b447 | 179 | ); |
af5201d4 TO |
180 | |
181 | // Setup index.html files to prevent browsing | |
182 | foreach ($publicDirs as $publicDir => $publicUrl) { | |
183 | CRM_Utils_File::restrictBrowsing($publicDir); | |
184 | } | |
185 | ||
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.' | |
190 | . '<br />' . | |
191 | '<a href="%3">Read more about this warning</a>'; | |
192 | $docs_url = $this->createDocUrl('checkDirectoriesAreNotBrowseable'); | |
a2600a6d | 193 | $messages[] = new CRM_Utils_Check_Message( |
165aab59 | 194 | __FUNCTION__, |
1248c859 | 195 | ts($msg, array(1 => $publicDir, 2 => $publicDir, 3 => $docs_url)), |
1b366958 | 196 | ts('Browseable Directories'), |
165aab59 CW |
197 | \Psr\Log\LogLevel::ERROR, |
198 | 'fa-lock' | |
a2600a6d | 199 | ); |
5c58b447 | 200 | } |
349b394e | 201 | } |
e7d3e318 TO |
202 | |
203 | return $messages; | |
349b394e CB |
204 | } |
205 | ||
44d3eb82 NG |
206 | |
207 | /** | |
47b04697 TO |
208 | * Check that some files are not present. |
209 | * | |
210 | * These files have generally been deleted but Civi source tree but could be | |
211 | * left online if one does a faulty upgrade. | |
44d3eb82 NG |
212 | * |
213 | * @return array of messages | |
214 | */ | |
215 | public function checkFilesAreNotPresent() { | |
216 | global $civicrm_root; | |
217 | ||
218 | $messages = array(); | |
219 | $files = array( | |
7df9b5d5 | 220 | array( |
4d7bf881 CW |
221 | // CRM-16005, upgraded from Civi <= 4.5.6 |
222 | "{$civicrm_root}/packages/dompdf/dompdf.php", | |
7df9b5d5 AH |
223 | \Psr\Log\LogLevel::CRITICAL, |
224 | ), | |
225 | array( | |
4d7bf881 CW |
226 | // CRM-16005, Civi >= 4.5.7 |
227 | "{$civicrm_root}/packages/vendor/dompdf/dompdf/dompdf.php", | |
7df9b5d5 AH |
228 | \Psr\Log\LogLevel::CRITICAL, |
229 | ), | |
230 | array( | |
4d7bf881 CW |
231 | // CRM-16005, Civi >= 4.6.0 |
232 | "{$civicrm_root}/vendor/dompdf/dompdf/dompdf.php", | |
7df9b5d5 AH |
233 | \Psr\Log\LogLevel::CRITICAL, |
234 | ), | |
235 | array( | |
4d7bf881 CW |
236 | // CIVI-SA-2013-001 |
237 | "{$civicrm_root}/packages/OpenFlashChart/php-ofc-library/ofc_upload_image.php", | |
7df9b5d5 AH |
238 | \Psr\Log\LogLevel::CRITICAL, |
239 | ), | |
240 | array( | |
241 | "{$civicrm_root}/packages/html2text/class.html2text.inc", | |
242 | \Psr\Log\LogLevel::CRITICAL, | |
243 | ), | |
44d3eb82 NG |
244 | ); |
245 | foreach ($files as $file) { | |
7df9b5d5 | 246 | if (file_exists($file[0])) { |
44d3eb82 | 247 | $messages[] = new CRM_Utils_Check_Message( |
165aab59 | 248 | __FUNCTION__, |
50208d1e | 249 | ts('File \'%1\' presents a security risk and should be deleted.', array(1 => $file[0])), |
1b366958 | 250 | ts('Unsafe Files'), |
165aab59 CW |
251 | $file[1], |
252 | 'fa-lock' | |
44d3eb82 NG |
253 | ); |
254 | } | |
255 | } | |
256 | return $messages; | |
257 | } | |
258 | ||
098de400 TO |
259 | /** |
260 | * Check that the sysadmin has not modified the Cxn | |
261 | * security setup. | |
262 | */ | |
263 | public function checkCxnOverrides() { | |
264 | $list = array(); | |
265 | if (defined('CIVICRM_CXN_CA') && CIVICRM_CXN_CA !== 'CiviRootCA') { | |
266 | $list[] = 'CIVICRM_CXN_CA'; | |
267 | } | |
098de400 TO |
268 | if (defined('CIVICRM_CXN_APPS_URL') && CIVICRM_CXN_APPS_URL !== \Civi\Cxn\Rpc\Constants::OFFICIAL_APPMETAS_URL) { |
269 | $list[] = 'CIVICRM_CXN_APPS_URL'; | |
270 | } | |
271 | ||
272 | $messages = array(); | |
273 | ||
274 | if (!empty($list)) { | |
275 | $messages[] = new CRM_Utils_Check_Message( | |
165aab59 | 276 | __FUNCTION__, |
098de400 TO |
277 | ts('The system administrator has disabled security settings (%1). Connections to remote applications are insecure.', array( |
278 | 1 => implode(', ', $list), | |
279 | )), | |
165aab59 CW |
280 | ts('Security Warning'), |
281 | \Psr\Log\LogLevel::WARNING, | |
282 | 'fa-lock' | |
098de400 TO |
283 | ); |
284 | } | |
285 | ||
286 | return $messages; | |
287 | } | |
288 | ||
af5201d4 TO |
289 | /** |
290 | * Determine whether $url is a public, browsable listing for $dir | |
291 | * | |
77855840 TO |
292 | * @param string $dir |
293 | * Local dir path. | |
294 | * @param string $url | |
295 | * Public URL. | |
af5201d4 TO |
296 | * @return bool |
297 | */ | |
298 | public function isBrowsable($dir, $url) { | |
1305c22b | 299 | if (empty($dir) || empty($url) || !is_dir($dir)) { |
a8488826 TO |
300 | return FALSE; |
301 | } | |
302 | ||
af5201d4 TO |
303 | $result = FALSE; |
304 | $file = 'delete-this-' . CRM_Utils_String::createRandom(10, CRM_Utils_String::ALPHANUMERIC); | |
305 | ||
a8488826 | 306 | // this could be a new system with no uploads (yet) -- so we'll make a file |
af5201d4 | 307 | file_put_contents("$dir/$file", "delete me"); |
21acec2c | 308 | $content = @file_get_contents("$url"); |
af5201d4 TO |
309 | if (stristr($content, $file)) { |
310 | $result = TRUE; | |
311 | } | |
312 | unlink("$dir/$file"); | |
313 | ||
314 | return $result; | |
315 | } | |
316 | ||
a738c74c TO |
317 | /** |
318 | * Determine whether $url is a public version of $dir in which files | |
319 | * are remotely accessible. | |
320 | * | |
77855840 TO |
321 | * @param string $dir |
322 | * Local dir path. | |
323 | * @param string $url | |
324 | * Public URL. | |
a738c74c TO |
325 | * @return bool |
326 | */ | |
327 | public function isDirAccessible($dir, $url) { | |
328 | $dir = rtrim($dir, '/'); | |
329 | $url = rtrim($url, '/'); | |
330 | if (empty($dir) || empty($url) || !is_dir($dir)) { | |
331 | return FALSE; | |
332 | } | |
333 | ||
334 | $result = FALSE; | |
335 | $file = 'delete-this-' . CRM_Utils_String::createRandom(10, CRM_Utils_String::ALPHANUMERIC); | |
336 | ||
337 | // this could be a new system with no uploads (yet) -- so we'll make a file | |
338 | file_put_contents("$dir/$file", "delete me"); | |
339 | ||
340 | $headers = @get_headers("$url/$file"); | |
341 | if (stripos($headers[0], '200')) { | |
342 | $content = @file_get_contents("$url/$file"); | |
343 | if (preg_match('/delete me/', $content)) { | |
344 | $result = TRUE; | |
345 | } | |
346 | } | |
347 | ||
348 | unlink("$dir/$file"); | |
349 | ||
350 | return $result; | |
351 | } | |
352 | ||
5bc392e6 EM |
353 | /** |
354 | * @param $topic | |
355 | * | |
356 | * @return string | |
357 | */ | |
7d342759 TO |
358 | public function createDocUrl($topic) { |
359 | return CRM_Utils_System::getWikiBaseURL() . $topic; | |
360 | } | |
a738c74c TO |
361 | |
362 | /** | |
363 | * Make a guess about the URL that corresponds to $targetDir. | |
364 | * | |
77855840 TO |
365 | * @param string $targetDir |
366 | * Local path to a directory. | |
a6c01b45 CW |
367 | * @return string |
368 | * a guessed URL for $realDir | |
a738c74c TO |
369 | */ |
370 | public function guessUrl($targetDir) { | |
371 | $filePathMarker = $this->getFilePathMarker(); | |
372 | $config = CRM_Core_Config::singleton(); | |
373 | ||
374 | list ($heuristicBaseUrl, $ignore) = explode($filePathMarker, $config->imageUploadURL); | |
b35868f5 | 375 | list ($ignore, $heuristicSuffix) = explode($filePathMarker, str_replace('\\', '/', $targetDir)); |
a738c74c TO |
376 | return $heuristicBaseUrl . $filePathMarker . $heuristicSuffix; |
377 | } | |
96025800 | 378 | |
349b394e | 379 | } |