Commit | Line | Data |
---|---|---|
349b394e CB |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
349b394e | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
349b394e CB |
32 | * $Id: $ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Check_Security { | |
36 | ||
5c58b447 CB |
37 | /** |
38 | * CMS have a different pattern to their default file path and URL. | |
39 | * | |
40 | * @TODO This function might be better shared in CRM_Utils_Check | |
41 | * class, but that class doesn't yet exist. | |
42 | */ | |
a996bf82 | 43 | public function getFilePathMarker() { |
5c58b447 CB |
44 | $config = CRM_Core_Config::singleton(); |
45 | switch ($config->userFramework) { | |
46 | case 'Joomla': | |
47 | return '/media/'; | |
e7292422 | 48 | |
5c58b447 CB |
49 | default: |
50 | return '/files/'; | |
51 | } | |
52 | } | |
53 | ||
349b394e CB |
54 | /** |
55 | * Run some sanity checks. | |
56 | * | |
1248c859 | 57 | * @return array<CRM_Utils_Check_Message> |
349b394e | 58 | */ |
9979ff93 | 59 | public function checkAll() { |
e7d3e318 | 60 | $messages = array_merge( |
098de400 | 61 | $this->checkCxnOverrides(), |
1248c859 TO |
62 | $this->checkLogFileIsNotAccessible(), |
63 | $this->checkUploadsAreNotAccessible(), | |
44d3eb82 NG |
64 | $this->checkDirectoriesAreNotBrowseable(), |
65 | $this->checkFilesAreNotPresent() | |
e7d3e318 TO |
66 | ); |
67 | return $messages; | |
349b394e CB |
68 | } |
69 | ||
70 | /** | |
71 | * Check if our logfile is directly accessible. | |
72 | * | |
73 | * Per CiviCRM default the logfile sits in a folder which is | |
74 | * web-accessible, and is protected by a default .htaccess | |
75 | * configuration. If server config causes the .htaccess not to | |
76 | * function as intended, there may be information disclosure. | |
77 | * | |
78 | * The debug log may be jam-packed with sensitive data, we don't | |
79 | * want that. | |
80 | * | |
81 | * Being able to be retrieved directly doesn't mean the logfile | |
82 | * is browseable or visible to search engines; it means it can be | |
83 | * requested directly. | |
84 | * | |
a6c01b45 | 85 | * @return array |
16b10e64 | 86 | * Array of messages |
349b394e CB |
87 | * @see CRM-14091 |
88 | */ | |
23d89616 | 89 | public function checkLogFileIsNotAccessible() { |
e7d3e318 TO |
90 | $messages = array(); |
91 | ||
349b394e CB |
92 | $config = CRM_Core_Config::singleton(); |
93 | ||
94 | $log = CRM_Core_Error::createDebugLogger(); | |
b35868f5 | 95 | $log_filename = str_replace('\\', '/', $log->_filename); |
349b394e | 96 | |
a996bf82 | 97 | $filePathMarker = $this->getFilePathMarker(); |
5c58b447 | 98 | |
349b394e CB |
99 | // Hazard a guess at the URL of the logfile, based on common |
100 | // CiviCRM layouts. | |
5c58b447 CB |
101 | if ($upload_url = explode($filePathMarker, $config->imageUploadURL)) { |
102 | $url[] = $upload_url[0]; | |
103 | if ($log_path = explode($filePathMarker, $log_filename)) { | |
4de448d7 | 104 | // CRM-17149: check if debug log path includes $filePathMarker |
7332c9bd | 105 | if (count($log_path) > 1) { |
4de448d7 NG |
106 | $url[] = $log_path[1]; |
107 | $log_url = implode($filePathMarker, $url); | |
108 | $headers = @get_headers($log_url); | |
109 | if (stripos($headers[0], '200')) { | |
110 | $docs_url = $this->createDocUrl('checkLogFileIsNotAccessible'); | |
111 | $msg = 'The <a href="%1">CiviCRM debug log</a> should not be downloadable.' | |
112 | . '<br />' . | |
113 | '<a href="%2">Read more about this warning</a>'; | |
114 | $messages[] = new CRM_Utils_Check_Message( | |
115 | 'checkLogFileIsNotAccessible', | |
116 | ts($msg, array(1 => $log_url, 2 => $docs_url)), | |
117 | ts('Security Warning') | |
118 | ); | |
119 | } | |
349b394e | 120 | } |
5c58b447 | 121 | } |
349b394e | 122 | } |
e7d3e318 TO |
123 | |
124 | return $messages; | |
349b394e CB |
125 | } |
126 | ||
127 | /** | |
128 | * Check if our uploads directory has accessible files. | |
129 | * | |
130 | * We'll test a handful of files randomly. Hazard a guess at the URL | |
131 | * of the uploads dir, based on common CiviCRM layouts. Try and | |
132 | * request the files, and if any are successfully retrieved, warn. | |
133 | * | |
134 | * Being retrievable doesn't mean the files are browseable or visible | |
135 | * to search engines; it only means they can be requested directly. | |
136 | * | |
a6c01b45 | 137 | * @return array |
16b10e64 | 138 | * Array of messages |
349b394e | 139 | * @see CRM-14091 |
5c58b447 CB |
140 | * |
141 | * @TODO: Test with WordPress, Joomla. | |
349b394e | 142 | */ |
23d89616 | 143 | public function checkUploadsAreNotAccessible() { |
e7d3e318 TO |
144 | $messages = array(); |
145 | ||
349b394e | 146 | $config = CRM_Core_Config::singleton(); |
a738c74c TO |
147 | $privateDirs = array( |
148 | $config->uploadDir, | |
149 | $config->customFileUploadDir, | |
150 | ); | |
5c58b447 | 151 | |
a738c74c TO |
152 | foreach ($privateDirs as $privateDir) { |
153 | $heuristicUrl = $this->guessUrl($privateDir); | |
154 | if ($this->isDirAccessible($privateDir, $heuristicUrl)) { | |
155 | $messages[] = new CRM_Utils_Check_Message( | |
156 | 'checkUploadsAreNotAccessible', | |
157 | ts('Files in the data directory (<a href="%3">%2</a>) should not be downloadable.' | |
353ffa53 TO |
158 | . '<br />' |
159 | . '<a href="%1">Read more about this warning</a>', | |
a738c74c TO |
160 | array( |
161 | 1 => $this->createDocUrl('checkUploadsAreNotAccessible'), | |
162 | 2 => $privateDir, | |
163 | 3 => $heuristicUrl, | |
164 | )), | |
165 | ts('Security Warning') | |
166 | ); | |
5c58b447 | 167 | } |
349b394e | 168 | } |
e7d3e318 TO |
169 | |
170 | return $messages; | |
349b394e CB |
171 | } |
172 | ||
173 | /** | |
174 | * Check if our uploads or ConfigAndLog directories have browseable | |
175 | * listings. | |
176 | * | |
177 | * Retrieve a listing of files from the local filesystem, and the | |
178 | * corresponding path via HTTP. Then check and see if the local | |
179 | * files are represented in the HTTP result; if so then warn. This | |
180 | * MAY trigger false positives (if you have files named 'a', 'e' | |
181 | * we'll probably match that). | |
182 | * | |
a6c01b45 | 183 | * @return array |
16b10e64 | 184 | * Array of messages |
349b394e | 185 | * @see CRM-14091 |
5c58b447 CB |
186 | * |
187 | * @TODO: Test with WordPress, Joomla. | |
349b394e | 188 | */ |
23d89616 | 189 | public function checkDirectoriesAreNotBrowseable() { |
e7d3e318 | 190 | $messages = array(); |
349b394e | 191 | $config = CRM_Core_Config::singleton(); |
af5201d4 TO |
192 | $publicDirs = array( |
193 | $config->imageUploadDir => $config->imageUploadURL, | |
5c58b447 | 194 | ); |
af5201d4 TO |
195 | |
196 | // Setup index.html files to prevent browsing | |
197 | foreach ($publicDirs as $publicDir => $publicUrl) { | |
198 | CRM_Utils_File::restrictBrowsing($publicDir); | |
199 | } | |
200 | ||
201 | // Test that $publicDir is not browsable | |
202 | foreach ($publicDirs as $publicDir => $publicUrl) { | |
203 | if ($this->isBrowsable($publicDir, $publicUrl)) { | |
204 | $msg = 'Directory <a href="%1">%2</a> should not be browseable via the web.' | |
205 | . '<br />' . | |
206 | '<a href="%3">Read more about this warning</a>'; | |
207 | $docs_url = $this->createDocUrl('checkDirectoriesAreNotBrowseable'); | |
a2600a6d TO |
208 | $messages[] = new CRM_Utils_Check_Message( |
209 | 'checkDirectoriesAreNotBrowseable', | |
1248c859 TO |
210 | ts($msg, array(1 => $publicDir, 2 => $publicDir, 3 => $docs_url)), |
211 | ts('Security Warning') | |
a2600a6d | 212 | ); |
5c58b447 | 213 | } |
349b394e | 214 | } |
e7d3e318 TO |
215 | |
216 | return $messages; | |
349b394e CB |
217 | } |
218 | ||
44d3eb82 NG |
219 | |
220 | /** | |
47b04697 TO |
221 | * Check that some files are not present. |
222 | * | |
223 | * These files have generally been deleted but Civi source tree but could be | |
224 | * left online if one does a faulty upgrade. | |
44d3eb82 NG |
225 | * |
226 | * @return array of messages | |
227 | */ | |
228 | public function checkFilesAreNotPresent() { | |
229 | global $civicrm_root; | |
230 | ||
231 | $messages = array(); | |
232 | $files = array( | |
47b04697 TO |
233 | "{$civicrm_root}/packages/dompdf/dompdf.php", // CRM-16005, upgraded from Civi <= 4.5.6 |
234 | "{$civicrm_root}/packages/vendor/dompdf/dompdf/dompdf.php", // CRM-16005, Civi >= 4.5.7 | |
235 | "{$civicrm_root}/vendor/dompdf/dompdf/dompdf.php", // CRM-16005, Civi >= 4.6.0 | |
44d3eb82 NG |
236 | ); |
237 | foreach ($files as $file) { | |
238 | if (file_exists($file)) { | |
239 | $messages[] = new CRM_Utils_Check_Message( | |
240 | 'checkFilesAreNotPresent', | |
241 | ts('File \'%1\' presents a security risk and should be deleted.', array(1 => $file)), | |
242 | ts('Security Warning') | |
243 | ); | |
244 | } | |
245 | } | |
246 | return $messages; | |
247 | } | |
248 | ||
098de400 TO |
249 | /** |
250 | * Check that the sysadmin has not modified the Cxn | |
251 | * security setup. | |
252 | */ | |
253 | public function checkCxnOverrides() { | |
254 | $list = array(); | |
255 | if (defined('CIVICRM_CXN_CA') && CIVICRM_CXN_CA !== 'CiviRootCA') { | |
256 | $list[] = 'CIVICRM_CXN_CA'; | |
257 | } | |
098de400 TO |
258 | if (defined('CIVICRM_CXN_APPS_URL') && CIVICRM_CXN_APPS_URL !== \Civi\Cxn\Rpc\Constants::OFFICIAL_APPMETAS_URL) { |
259 | $list[] = 'CIVICRM_CXN_APPS_URL'; | |
260 | } | |
261 | ||
262 | $messages = array(); | |
263 | ||
264 | if (!empty($list)) { | |
265 | $messages[] = new CRM_Utils_Check_Message( | |
266 | 'checkCxnOverrides', | |
267 | ts('The system administrator has disabled security settings (%1). Connections to remote applications are insecure.', array( | |
268 | 1 => implode(', ', $list), | |
269 | )), | |
270 | ts('Security Warning') | |
271 | ); | |
272 | } | |
273 | ||
274 | return $messages; | |
275 | } | |
276 | ||
af5201d4 TO |
277 | /** |
278 | * Determine whether $url is a public, browsable listing for $dir | |
279 | * | |
77855840 TO |
280 | * @param string $dir |
281 | * Local dir path. | |
282 | * @param string $url | |
283 | * Public URL. | |
af5201d4 TO |
284 | * @return bool |
285 | */ | |
286 | public function isBrowsable($dir, $url) { | |
1305c22b | 287 | if (empty($dir) || empty($url) || !is_dir($dir)) { |
a8488826 TO |
288 | return FALSE; |
289 | } | |
290 | ||
af5201d4 TO |
291 | $result = FALSE; |
292 | $file = 'delete-this-' . CRM_Utils_String::createRandom(10, CRM_Utils_String::ALPHANUMERIC); | |
293 | ||
a8488826 | 294 | // this could be a new system with no uploads (yet) -- so we'll make a file |
af5201d4 | 295 | file_put_contents("$dir/$file", "delete me"); |
21acec2c | 296 | $content = @file_get_contents("$url"); |
af5201d4 TO |
297 | if (stristr($content, $file)) { |
298 | $result = TRUE; | |
299 | } | |
300 | unlink("$dir/$file"); | |
301 | ||
302 | return $result; | |
303 | } | |
304 | ||
a738c74c TO |
305 | /** |
306 | * Determine whether $url is a public version of $dir in which files | |
307 | * are remotely accessible. | |
308 | * | |
77855840 TO |
309 | * @param string $dir |
310 | * Local dir path. | |
311 | * @param string $url | |
312 | * Public URL. | |
a738c74c TO |
313 | * @return bool |
314 | */ | |
315 | public function isDirAccessible($dir, $url) { | |
316 | $dir = rtrim($dir, '/'); | |
317 | $url = rtrim($url, '/'); | |
318 | if (empty($dir) || empty($url) || !is_dir($dir)) { | |
319 | return FALSE; | |
320 | } | |
321 | ||
322 | $result = FALSE; | |
323 | $file = 'delete-this-' . CRM_Utils_String::createRandom(10, CRM_Utils_String::ALPHANUMERIC); | |
324 | ||
325 | // this could be a new system with no uploads (yet) -- so we'll make a file | |
326 | file_put_contents("$dir/$file", "delete me"); | |
327 | ||
328 | $headers = @get_headers("$url/$file"); | |
329 | if (stripos($headers[0], '200')) { | |
330 | $content = @file_get_contents("$url/$file"); | |
331 | if (preg_match('/delete me/', $content)) { | |
332 | $result = TRUE; | |
333 | } | |
334 | } | |
335 | ||
336 | unlink("$dir/$file"); | |
337 | ||
338 | return $result; | |
339 | } | |
340 | ||
5bc392e6 EM |
341 | /** |
342 | * @param $topic | |
343 | * | |
344 | * @return string | |
345 | */ | |
7d342759 TO |
346 | public function createDocUrl($topic) { |
347 | return CRM_Utils_System::getWikiBaseURL() . $topic; | |
348 | } | |
a738c74c TO |
349 | |
350 | /** | |
351 | * Make a guess about the URL that corresponds to $targetDir. | |
352 | * | |
77855840 TO |
353 | * @param string $targetDir |
354 | * Local path to a directory. | |
a6c01b45 CW |
355 | * @return string |
356 | * a guessed URL for $realDir | |
a738c74c TO |
357 | */ |
358 | public function guessUrl($targetDir) { | |
359 | $filePathMarker = $this->getFilePathMarker(); | |
360 | $config = CRM_Core_Config::singleton(); | |
361 | ||
362 | list ($heuristicBaseUrl, $ignore) = explode($filePathMarker, $config->imageUploadURL); | |
b35868f5 | 363 | list ($ignore, $heuristicSuffix) = explode($filePathMarker, str_replace('\\', '/', $targetDir)); |
a738c74c TO |
364 | return $heuristicBaseUrl . $filePathMarker . $heuristicSuffix; |
365 | } | |
96025800 | 366 | |
349b394e | 367 | } |