3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Utils_Check_Security
{
38 * CMS have a different pattern to their default file path and URL.
40 * @TODO This function might be better shared in CRM_Utils_Check
41 * class, but that class doesn't yet exist.
43 public function getFilePathMarker() {
44 $config = CRM_Core_Config
::singleton();
45 switch ($config->userFramework
) {
55 * Run some sanity checks.
57 * @return array<CRM_Utils_Check_Message>
59 public function checkAll() {
60 $messages = array_merge(
61 $this->checkLogFileIsNotAccessible(),
62 $this->checkUploadsAreNotAccessible(),
63 $this->checkDirectoriesAreNotBrowseable()
69 * Check if our logfile is directly accessible.
71 * Per CiviCRM default the logfile sits in a folder which is
72 * web-accessible, and is protected by a default .htaccess
73 * configuration. If server config causes the .htaccess not to
74 * function as intended, there may be information disclosure.
76 * The debug log may be jam-packed with sensitive data, we don't
79 * Being able to be retrieved directly doesn't mean the logfile
80 * is browseable or visible to search engines; it means it can be
87 public function checkLogFileIsNotAccessible() {
90 $config = CRM_Core_Config
::singleton();
92 $log = CRM_Core_Error
::createDebugLogger();
93 $log_filename = str_replace('\\', '/', $log->_filename
);
95 $filePathMarker = $this->getFilePathMarker();
97 // Hazard a guess at the URL of the logfile, based on common
99 if ($upload_url = explode($filePathMarker, $config->imageUploadURL
)) {
100 $url[] = $upload_url[0];
101 if ($log_path = explode($filePathMarker, $log_filename)) {
102 $url[] = $log_path[1];
103 $log_url = implode($filePathMarker, $url);
104 $headers = @get_headers
($log_url);
105 if (stripos($headers[0], '200')) {
106 $docs_url = $this->createDocUrl('checkLogFileIsNotAccessible');
107 $msg = 'The <a href="%1">CiviCRM debug log</a> should not be downloadable.'
109 '<a href="%2">Read more about this warning</a>';
110 $messages[] = new CRM_Utils_Check_Message(
111 'checkLogFileIsNotAccessible',
112 ts($msg, array(1 => $log_url, 2 => $docs_url)),
113 ts('Security Warning')
123 * Check if our uploads directory has accessible files.
125 * We'll test a handful of files randomly. Hazard a guess at the URL
126 * of the uploads dir, based on common CiviCRM layouts. Try and
127 * request the files, and if any are successfully retrieved, warn.
129 * Being retrievable doesn't mean the files are browseable or visible
130 * to search engines; it only means they can be requested directly.
136 * @TODO: Test with WordPress, Joomla.
138 public function checkUploadsAreNotAccessible() {
141 $config = CRM_Core_Config
::singleton();
142 $privateDirs = array(
144 $config->customFileUploadDir
,
147 foreach ($privateDirs as $privateDir) {
148 $heuristicUrl = $this->guessUrl($privateDir);
149 if ($this->isDirAccessible($privateDir, $heuristicUrl)) {
150 $messages[] = new CRM_Utils_Check_Message(
151 'checkUploadsAreNotAccessible',
152 ts('Files in the data directory (<a href="%3">%2</a>) should not be downloadable.'
154 . '<a href="%1">Read more about this warning</a>',
156 1 => $this->createDocUrl('checkUploadsAreNotAccessible'),
160 ts('Security Warning')
169 * Check if our uploads or ConfigAndLog directories have browseable
172 * Retrieve a listing of files from the local filesystem, and the
173 * corresponding path via HTTP. Then check and see if the local
174 * files are represented in the HTTP result; if so then warn. This
175 * MAY trigger false positives (if you have files named 'a', 'e'
176 * we'll probably match that).
182 * @TODO: Test with WordPress, Joomla.
184 public function checkDirectoriesAreNotBrowseable() {
186 $config = CRM_Core_Config
::singleton();
188 $config->imageUploadDir
=> $config->imageUploadURL
,
191 // Setup index.html files to prevent browsing
192 foreach ($publicDirs as $publicDir => $publicUrl) {
193 CRM_Utils_File
::restrictBrowsing($publicDir);
196 // Test that $publicDir is not browsable
197 foreach ($publicDirs as $publicDir => $publicUrl) {
198 if ($this->isBrowsable($publicDir, $publicUrl)) {
199 $msg = 'Directory <a href="%1">%2</a> should not be browseable via the web.'
201 '<a href="%3">Read more about this warning</a>';
202 $docs_url = $this->createDocUrl('checkDirectoriesAreNotBrowseable');
203 $messages[] = new CRM_Utils_Check_Message(
204 'checkDirectoriesAreNotBrowseable',
205 ts($msg, array(1 => $publicDir, 2 => $publicDir, 3 => $docs_url)),
206 ts('Security Warning')
215 * Determine whether $url is a public, browsable listing for $dir
223 public function isBrowsable($dir, $url) {
224 if (empty($dir) ||
empty($url) ||
!is_dir($dir)) {
229 $file = 'delete-this-' . CRM_Utils_String
::createRandom(10, CRM_Utils_String
::ALPHANUMERIC
);
231 // this could be a new system with no uploads (yet) -- so we'll make a file
232 file_put_contents("$dir/$file", "delete me");
233 $content = @file_get_contents
("$url");
234 if (stristr($content, $file)) {
237 unlink("$dir/$file");
243 * Determine whether $url is a public version of $dir in which files
244 * are remotely accessible.
252 public function isDirAccessible($dir, $url) {
253 $dir = rtrim($dir, '/');
254 $url = rtrim($url, '/');
255 if (empty($dir) ||
empty($url) ||
!is_dir($dir)) {
260 $file = 'delete-this-' . CRM_Utils_String
::createRandom(10, CRM_Utils_String
::ALPHANUMERIC
);
262 // this could be a new system with no uploads (yet) -- so we'll make a file
263 file_put_contents("$dir/$file", "delete me");
265 $headers = @get_headers
("$url/$file");
266 if (stripos($headers[0], '200')) {
267 $content = @file_get_contents
("$url/$file");
268 if (preg_match('/delete me/', $content)) {
273 unlink("$dir/$file");
283 public function createDocUrl($topic) {
284 return CRM_Utils_System
::getWikiBaseURL() . $topic;
288 * Make a guess about the URL that corresponds to $targetDir.
290 * @param string $targetDir
291 * Local path to a directory.
293 * a guessed URL for $realDir
295 public function guessUrl($targetDir) {
296 $filePathMarker = $this->getFilePathMarker();
297 $config = CRM_Core_Config
::singleton();
299 list ($heuristicBaseUrl, $ignore) = explode($filePathMarker, $config->imageUploadURL
);
300 list ($ignore, $heuristicSuffix) = explode($filePathMarker, str_replace('\\', '/', $targetDir));
301 return $heuristicBaseUrl . $filePathMarker . $heuristicSuffix;